Classifying Clothes with Scikit-learn and TensorFlow using F-MNIST.

Roberto Merlos
CUNY CSI MTH513
Published in
8 min readApr 16, 2019

Overview

In a world where machine learning is becoming the next real thing, we humans who desire some help from machines to tell us which clothes look better on us and to make sure that the machine knows that we are not putting a shirt in our heads instead of a hat, that machine must be able to identify and classify each piece of cloth according to its real value.

Classification is a really important task in the world of machine learning and data science and we are going to try to make a machine tell us either if we are wearing a 👕 in our 🧠 (←head … there is not a head emoji 😔) — anyways, let’s go back to happy mood … 🙂 …. So, what I was saying is that we are going to try to make a machine tell us either if we are wearing a shirt in our head or not. 🤓

Let’s get into it!

F-MNIST

You might be wondering what F-MNIST is and those who don’t know what it is might be taking an educated guess and thinking we are talking about some kind of UFO or something up there related to passionate stories that make us get goosebumps. Anyways, F-MNIST is nothing like that.

In the F-MINST Github page where the original data set is found we can find what F-MINST really is. I will quote it here so you don't have to search it up 😇

Fashion-MNIST is a dataset of Zalando's article images—consisting of a training set of 60,000 examples and a test set of 10,000 examples. Each example is a 28x28 grayscale image, associated with a label from 10 classes. We intend Fashion-MNIST to serve as a direct drop-in replacement for the original MNIST dataset for benchmarking machine learning algorithms. It shares the same image size and structure of training and testing splits.

Here’s an example how the data looks (each class takes three-rows):

Fashion-MNIST

Now I think it would be a super amazing if you visited Zalando Research’s Github by clicking the link below the picture or by clicking here and you will learn more about it.

I also could not hold myself from getting this really cool animation from their Github … Trust me, it’s not your mouse or trackpad or whatever you might be using to control your computer doing all that movement, it’s a simple a gif. 😀

Fashion-MNIST

Now that you know a little bit more about F-MNIST, let’s for real get to coding!

Just before we start doing some futuristic related stuff 🤖 I want to mention that the dataset has a label for each cloth described here in ascending order:

Coding 👨‍💻👩‍💻

In the title I mentioned about Scikit-learn and TensorFlow. We will use both approaches and see what we can do to make our machine be more accurate. Remember: we don’t want to end up with a shirt in our heads. Sadly, there is no hats in the data set but we have sneakers and the same reasoning applies: we don’t want to end up with a 👟 in our head.

It’s important to mention that for both approaches we will be using Python 🐍 Let’s finally get into it!

A) Using Scikit-learn aka sklearn.

First off, let’s create a python file using your favorite editor such as Spyder or PyCharm. Let’s start by importing some libraries to start with.

Seaborn is for plotting purposes, pandas is imported for CSV file I/O, prettytable to display pretty tables (excuse the redundancy🤦‍♂️), and the stuff from sklearn is what we care the most about and what we need for this machine to start looking like a 🤖 (not playing)

Now, it’s time to load the test and training data using pandas. The CSV files originally downloaded from Kaggle can be found here.

The CSV files contain all the pixels for each picture

Now let’s get our models ready by creating a dictionary containing each model and let’s create a list that will hold each score initially having a score of 0. Also let’s get the pretty table ready and let’s give it a title and let’s define its columns. In the next section we will see which one works best! 😀

We will test in this order: Linear models, ensemble models, a tree model, a neighbor model, and finally we give it a try to support vector machines (SVMs)

If you are confused of what is going on up there, I don’t blame you. If your face look like any of the following 😳 🤨 🤯 🥺 please now that I simply defined a dictionary called models and each line that looks like models["blahblah"] = blehbleh is simply an addition to the models dictionary using key blahblah and value blehbleh which is a model. 😃

Now let’s go through all models using a loop and let’s see who beats who. 💪

First we do a cross validation in the training data and see which score will be the highest so we can use that model to compare it with the test data.

Cross-validation of the training data

We don’t want to check every single score that we get. So, we only check a score if it’s greater than the maximum score in our scores list. If it is, then we proceed and make a prediction accordingly with the test data excluding the Id, generating a results CSV file which results in some predictions.

In the end of the loop we add a new row to our pretty table with the name of our model and its score

After a long long wait ⏱ (if you don’t have GPU capabilities) we get the following results with our amazing pretty table:

BOOM! Finally! … AND THE WINNER IS, OUR DEAR FRIEND FROM ENSEMBLE:GRADIENT BOOSTING CLASSIFIER ! *

DONE! 🎊 … a balloon doesn’t hurt so here we go → 🎈

Now we can be sure at least that our probability of wearing a 👟 in our head has decreased thanks to the GBC. 🙏

* The 89.2% reflected is based on the cross-validation. The actual score would be less than that. It would oscillate between the 80s.

B) Using TensorFlow … with Keras.

For this approach we will be using Keras which is a neural-network library written in Python.

Let’s start the same way we started before. Let’s import some important libraries!

If you followed the first part, you already know why we import pandas and the matplotlib library. After we import those libraries, we take some stuff from our neural network Keras. EarlyStopping will simply stop the training process when a monitored quantity stops improving. In this way we will have less risk of overfitting our data. The Sequential model is a linear stack of layers and we will stack some layers.📚 The core layers we will be using are Dense, Dropout, Flatten, and Reshape. Additionally, we will be using a 2D convolutional layer i.e. Conv2D and a 2D pooling layer i.e. MaxPooling2D. We will also test some optimizers and see which one works best.

Before we apply all of the above, let’s load our data. Again, the data can be found here. We will also define a list of class names and split our training data in two.

class_names and X_test are defined to make some predictions later on 🔮😀

Now let’s put all those layers together using the sequential model 😀📚

Before the core, convolutional, and pooling layers are added, we need to reshape our 1×784 input to 28×28

Now let’s let’s compile our model and let’s choose an optimizer.

Using Adadelta (feel free to use another one)

Now, it’s finally time to train our data. We normalize each pixel of the X_train data and we run it the fitting process for 7 epochs with 200 samples per gradient update (batch size) validating on 1% of the data and having patience = 2 which means that if after 2 epochs, a epoch has finished and there’s no improvement, the training process will be stopped. You might be wondering why 7 epochs and the answer to that is that such a number is arbitrary and that is what seemed to work best for me in the model we created.

batch size: 200, epochs: 7, validation split: 1%, patience = 2 (feel free to try different numbers)

Now that the model has been trained, we can generate a results CSV file which results in some prediction and is ready for some testing.

We store our predictions in y_pred applying the argmax function returning the indices of the maximum values along the axis. Those indices indicate the class in which each prediction was classified. E.g. 0 → T-shirt/top.

Submitting our results to Kaggle for example where the real Y is we get a score of …

… 89.4% !!! 🤯 We just beat the GBC!!!

Let’s see if we can improve it a little bit more. Let’s try something different. Let’s change the optimizer.

Using RMSprop and training the model for 5 epochs

Obviously we did that for a reason and you guessed right. The result improved!!! Now we get two of these → 🤯🤯

90.1% !!! 🤯 Does this — does this mean goodbye GBC? We’ll kindly put GBC aside. Let’s give a kind farewell to our dear friend GBC:

Sorry GBC 😕 but we have to be sure we are not wearing a 👟 in our head and our dear neural network Keras seems to have beaten you today but don’t lose faith 💪😃

… Anyways, excuse all of that. Now let’s do some predictions with X_test who we have not forgotten about !!!

Now we will ask our machine what images 6 – 11 in X_test are. Let’s see how well our machine performs and let’s plot those results.

Testing 6 images in X_test

Now it’s time to see what in the world we have created and what things our 🤖 🧠 (robotic mind) thinks about the different images.

Shirt 👔
Bag 👜
Coat 🧥
Sandal 👠
T-shirt/Top 👕
Dress 👗

Tadaaaaa! Those results seem impressive, don’t they? Our machine seems to be very sure about what it’s doing. Would your amazing human mind classify those clothes in the same category? I would 🖐

(Optional)

Even though everything is optional if you think about it, this is optional 2x. If you have decided to try this last part, welcome! Let’s just simply try something else. I promise this is the last thing. Let’s search for some images and let’s see what our machine thinks about them. I will test a sneaker and a pair of trousers. 👟👖

This results are not as impressive as the other ones. However, I would say our image conversion perhaps was not the best so I don’t blame our machine. There’re in fact humans who would misclassify those grayish looking images (I’m one of them 🤓). Therefore, there’s still more we can do to make our 🤖 even better! 😃

I hope you enjoyed this “conversation” about fashion and clothes and brains and robots and whatever else you think we talked about. It was a pleasure to do some ML with fashion!

Clap if needed 👏😇.

I wish you an amazing day — full of robots 🤖🤖🤖

--

--