Convolutions for Fashion MNIST in Python
In this tutorial, let us find out how to use convolutions for fashion MNIST with Python. Fashion MNIST data set consists of Zalando’s article images of a training set of 60,000 examples and a test set of 10,000 examples. Often used for bench marking machine learning algorithms. Let us see how to classify images with a simple example.
Introduction:
For this we define a neural network consisting of convolution layers in addition to pooling layers. For instance we need to distinguish between a shirt and a shoe, let us see an example.
Fashion MNIST example:
Let us see the example step by step.
1. Importing necessary Python libraries.
First let us import the libraries required for this example. First let us import the libraries required for this example.
import tensorflow as tf import numpy as np from tensorflow import keras
2.The callback function.
Here we define a callback function to stop the training after particular metric. Therefore further epochs need not be executed.
class CustomCallbacks(tf.keras.callbacks.Callback): def on_epoch_end(self, epoch, logs={}): if(logs.get('acc')>0.998): print("\n 99% acc reached") self.model.stop_training = True
Here we see that if the accuracy reaches above 0.988 the training stops.
3. Training the data.
As we can see there is presence of pooling layers. The Convolution layers in a CNN summarize the presence of features in an input image.
The pooling layer is used to:
- Down sample the detection of features in feature maps.
- Calculate and implement average and maximum pooling in a CNN.
- Global pooling in a CNN.
We use maximum pooling in the below example, which reduces the matrix size depending on the pooling dimensions.
def preprocess_images(image_set): image_set = image_set.reshape(-1, 28, 28, 1) image_set = image_set / 255.0 return image_set mnist = tf.keras.datasets.mnist (training_images, training_labels), (test_images, test_labels) = mnist.load_data() training_images = preprocess_images(training_images) test_images = preprocess_images(test_images) model = tf.keras.models.Sequential([ tf.keras.layers.Conv2D(64, (3, 3), activation='relu', input_shape=(28,28,1)), tf.keras.layers.MaxPooling2D(2, 2), tf.keras.layers.Flatten(), tf.keras.layers.Dense(128, activation='relu'), tf.keras.layers.Dense(64, activation='relu'), tf.keras.layers.Dense(10, activation='softmax') ])
4. Compiling and testing the Data.
Finally, we then test the data and see if the accuracy is above a certain value, the training stops. In conclusion, we are improving the dataset by using convolutions. Below is the Python code for this:
model.compile( optimizer='adam', loss='sparse_categorical_crossentropy', metrics=['accuracy'] ) model.fit( training_images, training_labels, batch_size=64, epochs=20, callbacks=[CustomCallbacks()] )
Output:
Epoch 1/20 60000/60000 [==============================] - 82s 1ms/sample - loss: 0.1793 - acc: 0.9467 Epoch 2/20 60000/60000 [==============================] - 82s 1ms/sample - loss: 0.0551 - acc: 0.9827 Epoch 3/20 60000/60000 [==============================] - 76s 1ms/sample - loss: 0.0354 - acc: 0.9889 Epoch 4/20 60000/60000 [==============================] - 79s 1ms/sample - loss: 0.0248 - acc: 0.9920 Epoch 5/20 60000/60000 [==============================] - 78s 1ms/sample - loss: 0.0179 - acc: 0.9944 Epoch 6/20 60000/60000 [==============================] - 80s 1ms/sample - loss: 0.0144 - acc: 0.9953 Epoch 7/20 60000/60000 [==============================] - 79s 1ms/sample - loss: 0.0111 - acc: 0.9965 Epoch 8/20 60000/60000 [==============================] - 82s 1ms/sample - loss: 0.0071 - acc: 0.9976 Epoch 9/20 60000/60000 [==============================] - 84s 1ms/sample - loss: 0.0089 - acc: 0.9973 Epoch 10/20 59968/60000 [============================>.] - ETA: 0s - loss: 0.0047 - acc: 0.9986 99% acc reached 60000/60000 [==============================] - 87s 1ms/sample - loss: 0.0047 - acc: 0.9986
The Callback function is present. Therefore only 10 epochs are executed as the accuracy level is reached.
In conclusion, convolution improves the dataset.
Leave a Reply