Classifying Images of Clothing Using TensorFlow
Train a Deep Learning model to classify images of clothing using Convolutional Neural Networks in TensorFlow.
Import the Fashion MNIST dataset
the MNIST fasion dataset conatins 70,000 grayscale image of 10 classes. Which reprsent individual clothing items with 28*28 pixels of resolution. Each value is in the range [0,255] which defines the color and intensity of each pixel. We'll be using 60,000 fir training and 10,000 for testing in order to classify images
Downloading data from https://storage.googleapis.com/tensorflow/tf-keras-datasets/train-labels-idx1-ubyte.gz
32768/29515 [=================================] - 0s 0us/step
40960/29515 [=========================================] - 0s 0us/step
Downloading data from https://storage.googleapis.com/tensorflow/tf-keras-datasets/train-images-idx3-ubyte.gz
26427392/26421880 [==============================] - 0s 0us/step
26435584/26421880 [==============================] - 0s 0us/step
Downloading data from https://storage.googleapis.com/tensorflow/tf-keras-datasets/t10k-labels-idx1-ubyte.gz
16384/5148 [===============================================================================================] - 0s 0us/step
Downloading data from https://storage.googleapis.com/tensorflow/tf-keras-datasets/t10k-images-idx3-ubyte.gz
4423680/4422102 [==============================] - 0s 0us/step
4431872/4422102 [==============================] - 0s 0us/step
The labels are an array of integers, ranging from 0 to 9. These correspond to the class of clothing the image represents:
Explore the data
(60000, 28, 28)
(60000,)
(10000, 28, 28)
(10000,)
Preprocess the data
train_images: (60000, 28, 28, 1)
test_images: (10000, 28, 28, 1)
First Label Before One-Hot Encoding: [0. 0. 0. 0. 0. 0. 0. 0. 0. 1.]
Building the model
For this project, we are going to use a typical CNN architecture represented in the image below.
As we have in the image, we will include a convolutional and a pooling layers, then another convolutional and pooling layers. Then, we are going to add a flatten layer to transform our 2d-array image in a 1d-array and add some dense layers. We can add some dropout layers to reduce overfitting. For the last layer, we add a dense layer with the number of classes from our problem (10) and a softmax activation, which creates the probability distribution for each class.
2022-08-07 19:15:23.876038: E tensorflow/stream_executor/cuda/cuda_driver.cc:271] failed call to cuInit: UNKNOWN ERROR (34)
2022-08-07 19:15:23.876091: I tensorflow/stream_executor/cuda/cuda_diagnostics.cc:156] kernel driver does not appear to be running on this host (p-db20e7c1-e3b3-4d56-a059-17e8ce061c91): /proc/driver/nvidia/version does not exist
2022-08-07 19:15:23.876473: I tensorflow/core/platform/cpu_feature_guard.cc:151] This TensorFlow binary is optimized with oneAPI Deep Neural Network Library (oneDNN) to use the following CPU instructions in performance-critical operations: AVX2 AVX512F FMA
To enable them in other operations, rebuild TensorFlow with the appropriate compiler flags.
Compile the model
Train the model
2022-08-07 19:15:24.062332: W tensorflow/core/framework/cpu_allocator_impl.cc:82] Allocation of 131712000 exceeds 10% of free system memory.
Epoch 1/10
840/840 [==============================] - ETA: 0s - loss: 0.5641 - accuracy: 0.80102022-08-07 19:17:00.505548: W tensorflow/core/framework/cpu_allocator_impl.cc:82] Allocation of 56448000 exceeds 10% of free system memory.
840/840 [==============================] - 105s 125ms/step - loss: 0.5641 - accuracy: 0.8010 - val_loss: 0.3295 - val_accuracy: 0.8796
Epoch 2/10
840/840 [==============================] - 100s 119ms/step - loss: 0.3399 - accuracy: 0.8796 - val_loss: 0.2788 - val_accuracy: 0.8972
Epoch 3/10
840/840 [==============================] - 110s 131ms/step - loss: 0.2895 - accuracy: 0.8966 - val_loss: 0.2656 - val_accuracy: 0.9015
Epoch 4/10
840/840 [==============================] - 101s 120ms/step - loss: 0.2510 - accuracy: 0.9094 - val_loss: 0.2421 - val_accuracy: 0.9112
Epoch 5/10
840/840 [==============================] - 100s 119ms/step - loss: 0.2260 - accuracy: 0.9173 - val_loss: 0.2284 - val_accuracy: 0.9174
Epoch 6/10
840/840 [==============================] - 109s 130ms/step - loss: 0.2044 - accuracy: 0.9252 - val_loss: 0.2263 - val_accuracy: 0.9204
Epoch 7/10
840/840 [==============================] - 125s 149ms/step - loss: 0.1799 - accuracy: 0.9350 - val_loss: 0.2286 - val_accuracy: 0.9174
Epoch 8/10
840/840 [==============================] - 128s 152ms/step - loss: 0.1644 - accuracy: 0.9394 - val_loss: 0.2370 - val_accuracy: 0.9202
Epoch 9/10
840/840 [==============================] - 117s 140ms/step - loss: 0.1499 - accuracy: 0.9457 - val_loss: 0.2398 - val_accuracy: 0.9224
Epoch 10/10
840/840 [==============================] - 109s 129ms/step - loss: 0.1360 - accuracy: 0.9490 - val_loss: 0.2485 - val_accuracy: 0.9196
This model reaches an accuracy of about 0.95 (or 95%) on the training data.
Evaluating the model
2022-08-07 19:33:47.804032: W tensorflow/core/framework/cpu_allocator_impl.cc:82] Allocation of 31360000 exceeds 10% of free system memory.
313/313 - 5s - loss: 0.2738 - accuracy: 0.9123 - 5s/epoch - 16ms/step
Test accuracy: 0.9122999906539917
It turns out that the accuracy on the test dataset is a little less than the accuracy on the training dataset. however this still represents a good result.
Make predictions
2022-08-07 19:33:52.846802: W tensorflow/core/framework/cpu_allocator_impl.cc:82] Allocation of 31360000 exceeds 10% of free system memory.
When the model predicts right, the text will be displayed in blue, if the prediction is wrong, it will be displayed in red. Also, it will be displayed the calculated probability for the predicted class.
Results
T-shirt/top
886
0
Trouser
0
990
Pullover
18
1
Dress
24
7
Coat
1
0
Sandal
0
0
Shirt
131
0
Sneaker
0
0
Bag
5
0
Ankle boot
0
0
All
1065
998
precision recall f1-score support
T-shirt/top 0.83 0.89 0.86 1000
Trouser 0.99 0.99 0.99 1000
Pullover 0.92 0.80 0.86 1000
Dress 0.94 0.90 0.92 1000
Coat 0.83 0.89 0.86 1000
Sandal 0.98 0.99 0.98 1000
Shirt 0.73 0.76 0.74 1000
Sneaker 0.96 0.97 0.96 1000
Bag 0.99 0.98 0.98 1000
Ankle boot 0.98 0.96 0.97 1000
accuracy 0.91 10000
macro avg 0.91 0.91 0.91 10000
weighted avg 0.91 0.91 0.91 10000
Conclusion
In this project, it was presented how to train a Convolutional Neural Network to classify images of clothing from the Fashion MNIST dataset using TensorFlow and Keras. Using this model, we got an overall accuracy of 91,22% in our test dataset, which is a good result. However, specifically for our Shirt class we got an accuracy of only 73%. We could try to improve the accuracy of this class using some data augmentation techniques. Furthermore, in case you want to get a model with higher accuracy, you could try changing some hyperparameters or using different network architectures.