How to create a clothing classifier program using Deep Neural Network on Google Colab

--

(Fashion MNIST)

Input

The input for this data is 28 x 28 = 784 pixels, the neural network takes a vector as input thus these 28 by 28 images are converted to a one-dimensional array of 28 by 28, 784 pixels. The process that has occurred, when a 2d image is converted into a vector is flattening. The following is the code that is performed through a flattening input layer which converts the 2d images of 28 x 28 to a 1d array of 784 pixels. The hidden dense layer will contain 128 units/neurons; each of these neurons/nodes will take input from all 784(28x28) nodes in the previous layer. Relu(Rectified Linear Unit) is a mathematical function, that is added to the dense layer to allow it to solve more complex problems). The output layer then consists of 10 nodes, as each represents a different article/class of clothing. Each node will take input from the 128 nodes in the previous layer. Where each node weights the input according to learned parameters, and then outputs a value in the range, representing the probability that the image belongs to that class. The sum of all 10 node values is 1. Softmax is a function that provides probabilities for each possible output class.

The reason why there are many training data set images is for the reason that the classifier needs to be tested to see how reliable it is.

Trending AI Articles:

1. Machines Demonstrate Self-Awareness

2. Bursting the Jargon bubbles — Deep Learning

3. How Can We Improve the Quality of Our Data?

4. Machine Learning using Logistic Regression in Python with Code

For example, when studying for a math test, you are given equations and problems to learn how to do the questions, but when the test appears, there are no questions that are the same from the problems you practiced with previously. This is why there are a lot of images to train the data first to see how well it will perform with the test data. As just like with the math test, one has to understand how to do the question and not just memorize how it is done, which is similar to what the classifier should do. The classifier should not memorize the data, but instead, pinpoint and know what is precisely what by learning and training. The validation set is then used again when training is complete to measure the final accuracy of the model. For example, if the loss is being reduced during training, but accuracy deteriorates on the validation set, that is an indication that the model is memorizing the test set. The Training Set is the data used for training the neural network. The Test set is the data used for testing the final performance of our neural network.

In order to create this program, the following needs to be done within Google Colab, and to create a faster-paced program change the hardware accelerator within runtime in notebook settings to GPU

1. Install the library called Tensorflow datasets, which allows the usage of the Fashion MNIST dataset

2. Import the following libraries

3. Import the Fashion MNIST dataset. It will split the data into train and test

4. Then create a list to list a map to the numeric value to a human readable string

5. Preprocess that data

Using the normalized function, every pixel value is casted to the float type, and then the value is normalized to be in a range between 0–1 instead of 0–255

Build the model by applying the flattening layer, dense layer(hidden layer), and lastly the output layer.

6. Compile the model

The Loss function is an algorithm for measuring how far the model’s outputs are from the desired output. The goal of training is to measure the loss.

The Optimizer is an algorithm for adjusting the inner parameters of the model to minimize loss.

The Metrics is used to monitor the training and testing steps. The following example uses accuracy, the fraction of the images that are correctly classified.

Use the Adam optimizer, the loss function will need to be specified when doing classification

7. Train the model

First, we define the iteration behavior for the train dataset:

  • dataset.repeat() allows the data set to repeat forever
  • epochs parameter described below limits how long training should be performed
  • dataset.shuffle randomizes the order so our model cannot learn anything from the order of the examples.
  • dataset.batch tells model.fit to use batches of 32 images and labels when updating the model variables.

Training is performed by calling the `model.fit` method:

Feed the training data to the model using `train_dataset`, the model then learns to associate images and labels.

  • `epochs=5` parameter limits training to 5 full iterations of the training dataset, so a total of 5 * 60000 = 300000 examples.

8. Evaluate accuracy

Compare how the model performs on the test dataset, in order to fine the accuracy of the program, use all examples in the test dataset. The accuracy of the classifier ended up at 87%.

When a clothing article picture is given to the program, it ultimately needs to put it in one of the categories, which is where it performs probability distribution of which article of clothing is it most likely going to be. Thus when that number is given, it is going to be 0–1 as the output layer will calculate what that probability of the picture of clothing to be that specific label of the clothing piece.

The shirt is what has the highest probability, thus the program will match the shirt in the picture to the category of shirt

The following is the code for the clothing classifier(Fashion MNIST): https://colab.research.google.com/drive/1QZ2XFxLpy7sFCv-oAAEfcgxbnRhmAaI-

Key Takeaways

  • It is good to have a large number of test data to see if the program is memorizing the data rather than understanding it
  • It is vital to test the accuracy, as it allows the representation of if the program is classifying the images not or adequately
  • Steps and layers need to be first identified to understand what is occurring and how many layers will be needed to create a high accuracy rate

Thank you for reading my article, if you enjoyed the read, please clap and comment any feedback you have below. If you want to reach out to me, you can connect with me through LinkedIn.

Don’t forget to give us your 👏 !

--

--