Skip to content
Advertisement

How to build a CNN model for MNIST fashion and test it with a another set of image from web?

Importing the data and splitting it into 4 for test and train

JavaScript

[5]

This is actually wrong. It should be printed as a trouser which is denoted by 1 cause in mnist dataset 5 is sandal 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

I tried with different images, I am getting number 5(sandals) when I try with some boot or canvas shoes even. What seems to be the actual mistake here?

Advertisement

Answer

The main problem is very simple. Here I will give you a complete implementation of your program. Please note that I may change the model definition and image preprocessing step. Ok, let get started.

Fashion MNIST

Get the data – Do some preprocessing – Visualize a sample.

JavaScript

[BONUS]: Look, these are 28 and grayscale images. Now, for any reason, if we want to resize and/or want to make it RGB (3 channel), we can do that too. Check my other answer here.

Now, let’s visualize one of a sample from our preprocessed data.

JavaScript

enter image description here

Observe that, the main object right white and the background is black.

Model and Training

It’s better to use pretrained weight I think. However, here is a toy model to train.

JavaScript
JavaScript

Prediction

Let’s make some predictions on the web-searched samples. Before that, let’s first define a function that will do the necessary preprocessing.

JavaScript

Ok, I scrape some Fashion MNIST looking similar data, let’s open one of them.

JavaScript

enter image description here

All is good so far, except now we have a white background, which is not like our training sample on which our model is trained on. If I’m not wrong, all the samples of Fashion MNIST do have a black background. At this point, if we pass this sample to the model for prediction, it wouldn’t make accurate or close accurate predictions.

When we make an RGB sample to Grayscale, the white pixel remains white and the other colorful pixel gets black. For our case to handle this, we can use the bitwise_not operation on the grayscale image before passing it to the model for prediction. This bitwise_not simply makes 0 to 1 and vice-versa.

JavaScript

enter image description here

Now, pass it to the model for predicted probabilities.

JavaScript

Now we can get the predicted label and compare gt.

JavaScript
Advertisement