Jovian
⭐️
Sign In

DEEP LEARNING FOR MULTIPLE IMAGE CLASSIFICATION


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

The Data

TASK 1: Run the code below to download the dataset using Keras.

In [2]:
from keras.datasets import fashion_mnist

(x_train, y_train), (x_test, y_test) = fashion_mnist.load_data()
Using TensorFlow backend.

Visualizing the Data

TASK 2: Use matplotlib to view an image from the data set. It can be any image from the data set.

In [3]:
import matplotlib.pyplot as plt
In [4]:
x_train[1].shape
Out[4]:
(28, 28)
In [5]:
plt.imshow(x_train[3])
Out[5]:
<matplotlib.image.AxesImage at 0x1d4a4e45710>
Notebook Image

Preprocessing the Data

TASK 3: Normalize the X train and X test data by dividing by the max value of the image arrays.

In [6]:
x_test.max()
Out[6]:
255
In [7]:
x_test = x_test/255
In [8]:
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.

In [9]:
x_test.shape

Out[9]:
(10000, 28, 28)
In [10]:
x_test = x_test.reshape(10000,28,28,1)
In [11]:
x_train = x_train.reshape(60000,28,28,1)
In [12]:
x_train[0].shape
Out[12]:
(28, 28, 1)

TASK 5: Convert the y_train and y_test values to be one-hot encoded for categorical analysis by Keras.

In [13]:
from keras.utils.np_utils import to_categorical
In [14]:
y_cat_test = to_categorical(y_test)
In [15]:
y_cat_train = to_categorical(y_train)
In [16]:
y_cat_test
Out[16]:
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)

Building the Model

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']

In [17]:
from keras.models import Sequential
from keras.layers import Dense,Conv2D,MaxPool2D,Flatten
In [18]:
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.
In [21]:
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'))



In [22]:
models.compile(loss='categorical_crossentropy',
              optimizer='rmsprop',
              metrics=['accuracy'])
In [23]:
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 _________________________________________________________________

Training the Model

TASK 6: Train/Fit the model to the x_train set. Amount of epochs is up to you.

In [28]:
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

Evaluating the Model

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.

In [26]:
models.metrics_names
Out[26]:
['loss', 'acc']
In [27]:
from sklearn.metrics import classification_report
In [29]:
prediction = models.predict_classes(x_test)
In [30]:
prediction
Out[30]:
array([9, 2, 1, ..., 8, 1, 5], dtype=int64)
In [34]:
prediction.shape
Out[34]:
(10000,)
In [36]:
y_test.shape
Out[36]:
(10000,)
In [38]:
y_cat_test[0]
Out[38]:
array([0., 0., 0., 0., 0., 0., 0., 0., 0., 1.], dtype=float32)
In [39]:
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

Great Job!