In this project we build an image classifier with Keras and Convolutional Neural Networks for the Fashion MNIST dataset. This data set includes 10 labels of different clothing types with 28 by 28 grayscale images. There is a training set of 60,000 images and 10,000 test images.
Label Description
0 T-shirt/top
1 Trouser
2 Pullover
3 Dress
4 Coat
5 Sandal
6 Shirt
7 Sneaker
8 Bag
9 Ankle boot
TASK 1: Run the code below to download the dataset using Keras.
from keras.datasets import fashion_mnist
(x_train, y_train), (x_test, y_test) = fashion_mnist.load_data()
Using TensorFlow backend.
TASK 2: Use matplotlib to view an image from the data set. It can be any image from the data set.
import matplotlib.pyplot as plt
x_train[1].shape
(28, 28)
plt.imshow(x_train[3])
<matplotlib.image.AxesImage at 0x1d4a4e45710>
TASK 3: Normalize the X train and X test data by dividing by the max value of the image arrays.
x_test.max()
255
x_test = x_test/255
x_train = x_train/255
Task 4: Reshape the X arrays to include a 4 dimension of the single channel. Similar to what we did for the numbers MNIST data set.
x_test.shape
(10000, 28, 28)
x_test = x_test.reshape(10000,28,28,1)
x_train = x_train.reshape(60000,28,28,1)
x_train[0].shape
(28, 28, 1)
TASK 5: Convert the y_train and y_test values to be one-hot encoded for categorical analysis by Keras.
from keras.utils.np_utils import to_categorical
y_cat_test = to_categorical(y_test)
y_cat_train = to_categorical(y_train)
y_cat_test
array([[0., 0., 0., ..., 0., 0., 1.],
[0., 0., 1., ..., 0., 0., 0.],
[0., 1., 0., ..., 0., 0., 0.],
...,
[0., 0., 0., ..., 0., 1., 0.],
[0., 1., 0., ..., 0., 0., 0.],
[0., 0., 0., ..., 0., 0., 0.]], dtype=float32)
TASK 5: Use Keras to create a model consisting of at least the following layers :
2D Convolutional Layer, filters=32 and kernel_size=(4,4)
Pooling Layer where pool_size = (2,2)
Flatten Layer
Dense Layer (128 Neurons, but feel free to play around with this value), RELU activation
Final Dense Layer of 10 Neurons with a softmax activation
Then compile the model with these parameters: loss='categorical_crossentropy', optimizer='rmsprop', metrics=['accuracy']
from keras.models import Sequential
from keras.layers import Dense,Conv2D,MaxPool2D,Flatten
models = Sequential()
WARNING:tensorflow:From c:\users\vsneh\appdata\local\programs\python\python36\lib\site-packages\keras\backend\tensorflow_backend.py:66: The name tf.get_default_graph is deprecated. Please use tf.compat.v1.get_default_graph instead.
models = Sequential()
# CONVOLUTIONAL LAYER
models.add(Conv2D(filters=32, kernel_size=(4,4),input_shape=(28, 28, 1), activation='relu',))
# POOLING LAYER
models.add(MaxPool2D(pool_size=(2, 2)))
# FLATTEN IMAGES FROM 28 by 28 to 764 BEFORE FINAL LAYER
models.add(Flatten())
# 128 NEURONS IN DENSE HIDDEN LAYER (YOU CAN CHANGE THIS NUMBER OF NEURONS)
models.add(Dense(128, activation='relu'))
# LAST LAYER IS THE CLASSIFIER, THUS 10 POSSIBLE CLASSES
models.add(Dense(10, activation='softmax'))
models.compile(loss='categorical_crossentropy',
optimizer='rmsprop',
metrics=['accuracy'])
models.summary()
Model: "sequential_3"
_________________________________________________________________
Layer (type) Output Shape Param #
=================================================================
conv2d_2 (Conv2D) (None, 25, 25, 32) 544
_________________________________________________________________
max_pooling2d_2 (MaxPooling2 (None, 12, 12, 32) 0
_________________________________________________________________
flatten_2 (Flatten) (None, 4608) 0
_________________________________________________________________
dense_3 (Dense) (None, 128) 589952
_________________________________________________________________
dense_4 (Dense) (None, 10) 1290
=================================================================
Total params: 591,786
Trainable params: 591,786
Non-trainable params: 0
_________________________________________________________________
TASK 6: Train/Fit the model to the x_train set. Amount of epochs is up to you.
models.fit(x_train,y_cat_train,epochs = 2)
Epoch 1/2
60000/60000 [==============================] - 42s 704us/step - loss: 0.2437 - acc: 0.9122
Epoch 2/2
60000/60000 [==============================] - 41s 688us/step - loss: 0.2185 - acc: 0.9228
TASK 7: Show the accuracy,precision,recall,f1-score the model achieved on the x_test data set. Keep in mind, there are quite a few ways to do this, but we recommend following the same procedure we showed in the MNIST lecture.
models.metrics_names
['loss', 'acc']
from sklearn.metrics import classification_report
prediction = models.predict_classes(x_test)
prediction
array([9, 2, 1, ..., 8, 1, 5], dtype=int64)
prediction.shape
(10000,)
y_test.shape
(10000,)
y_cat_test[0]
array([0., 0., 0., 0., 0., 0., 0., 0., 0., 1.], dtype=float32)
print(classification_report(y_test,prediction))
precision recall f1-score support
0 0.88 0.83 0.85 1000
1 0.98 0.98 0.98 1000
2 0.81 0.88 0.84 1000
3 0.88 0.94 0.91 1000
4 0.80 0.88 0.84 1000
5 0.98 0.97 0.98 1000
6 0.81 0.62 0.70 1000
7 0.95 0.98 0.96 1000
8 0.97 0.99 0.98 1000
9 0.97 0.96 0.97 1000
accuracy 0.90 10000
macro avg 0.90 0.90 0.90 10000
weighted avg 0.90 0.90 0.90 10000