Testing of LeNet Model for CIFAR-10 Dataset
In the previous topic, we found that our LeNet Model with Convolutional Neural Network was able to do the classification of MNIST dataset images. MNIST dataset contains the number of images which are the grayscale images, but in CHIFAR-10 dataset the images are colored and of different things. So our biggest question is that will our LeNet model classify the images of CIFAR-10 dataset. We will copy the code of our previous topic, i.e., Testing of CNN and do the following changes in the image transforms, implementation, training, validation, and the testing section of the code:
Note: If you are new here, then you must have to get knowledge of our previous topic for understanding this efficiently.
Changes in the section of Image Transform:
In the Image Transform section we will do the following changes:
Step 1:
In this, we are working with CIFAR-10 dataset so our first step is to load CIFAR-10 dataset rather than MNIST dataset. We load CIFAR-10 dataset by doing changes in the training dataset as well as validation data set in the following way:
Step 2:
In the next step, we will do the changes in our transform statement. We know that the MNIST image are of size 28 by 28 pixels but the CIFAR10 images are of size 32 by 32 pixels. So we will do the changes in the transform.compose() method’s first argument as:
Now, if we plot our CIFAR-10 images then it will give us the following output:
Step 3:
In the CIFAR10 images, we know that the images are classify in the classes. For better understanding and visualization we specify each images with its class. So we declare a list of classes in which we specifies the classes in order after im_convert () method as:
Step 4:
The labels represent the ordered numerical representation of these classes so we will use each respective label to index through our classes list and the output will be appropriate class. We will change the set_title() method as:
It will give the following output:
Changes in Implementation, Training, and Validation section:
Our Lenet model was implemented for MNIST images. MNIST images are the grayscale image, but we have to implement our model for CIFAR-10 dataset, which contains colored images. So we have to do the following changes in our code:
Step 1:
Previously, we were working with one channel grayscale images, and now we work with three-channel color images which are passes into the neural network. So in the first convolutional layer, we set 3 rather than one as:
Step 2:
Now, we have to train a large number of parameters. After the convolution of a 5 by 5 kernel, the images becomes 28 by 28 and then with next pooling 14 by 14 performing another convolution with the same size kernel. The image once again gets smaller by 4 by 4 decrement and becomes a 10 by 10. And finally, with another max-pooling, the vector which will then be fed into the fully connected network will be a 5 by 5 by 50.
So we have to change our first fully connected layer in our initializer as:
Step 3:
Now, we also have to change the shape of our output. For this, we have to change our view statement in the forward function as:
Now, we find the total loss and validation loss as well as accuracy and validation accuracy and plot it then it will give us the following output:
Step 4:
Now, we will use it to predict images from the web to simply gain a visual perspective of the model accuracy. We will use the following image: https://3c1703fe8d.site.internapcdn.net/newman/gfx/news/hires/2018/2-dog.jpg
When we plot this image, it will be shown as:
Step 5:
In the next step, we will remove our invert and convert method because this time our image will be extremely converted to a bio level format, and our network was trained on color images. We will transform the image and plot the images as:
After the transformation, we obtain a more abstracted representation of the image. It reduces to a smaller 32 by 32 representation.
Step 6:
Now, we make prediction on this image so we will squeeze the image and find the prediction using class as:
Changes in Testing section:
The testing section will be same as before. The same process of CNN testing will be follow but in colorful images we will use classes to predict each validation image as:
It seems to predict most of the images accurately. Overall it is very good that our model is so much able to generalize itself to new data based on its trained parameters.
Complete code: