Testing of Image Recognition Model in PyTorch
In the last section, we implemented a neural network or created a model which classified the handwritten digits. Now, we test our model by grabbing an image from the web. We used the following image:
When you paste this link on your browser, you will see the image of number five as:
After seeing this, we will realize that it is the number 5. Now, we will try to get our network to predict it.
We have the following steps to make predictions of the number image:
In the first step, we will perform a GET request to retrieve the image data. To make a GET request, we will need to import request as:
Now, we set a variable URL and assign the link as a string.
In the next step, we set a variable response whose value will get from the get() method of request. The get() method will consist of two arguments i.e., URL and stream and stream will be equals to true.
We will use the raw content of our response to obtain the image. For this, we first have to import Image from PIL (Python Image Library) as.
We use the open() method of image and pass the raw content of response as an argument. The value which will be returned from this method will assign to a variable named img as:
Now, we plot the image to make sure that everything is working well.
When we run it, it will generate an error because of PIL. We must have to install pillow first to run this code. We must have to run the conda install -c anaconda pillow command on anaconda command prompt to install pillow.
When you run the code, it will give the expected output.
We need to ensure that the image corresponds to what the neural network is trained to learn. Our image is of 1000*1000 pixels, so we need to make it into 28*28 grayscale image like ones in the training data. In our trained image dataset, images have a black background and white foreground, and in the above image there is white background and black foreground. Now, our first task is to preprocessing this image.
We will use the invert () method of PIL.ImageOps and pass our image as an argument. This method will invert the color of our image.
This image is in the RGB format with three channels of pixel intensity values, and this will problematic for numerous reasons. For this purpose, we must have to convert this image to be a binary black and white image, and we will convert this image as:
We will transform this image in the same way as we have transformed all of our other training images. We have to transform the image in 28*28 pixels, so we have to add an argument resize in our transformed chain composition as:
Now, our images are in the form of tensor, so we have to change it into numpy array. Before plotting our image, we have to import PIL.ImageOps and then plot the image as:
Now, we will feed this image into our neural network to make predictions. We will do it in the same way as we have done with MNIST.
It will give us the expected prediction as:
In the next step, we wrap our validation loader. It will create an object which allows us to go through the alterable validation loader one element at a time. We access it one element at a time by calling next on our dataiter. The next () function will grab the first batch of our validate data, and that validate data will be split into images and labels as:
To make a prediction, we have to reshape the images as we have done before and we will require the output of all the images and prediction as well.
Now, we will plot the images in the batch along with their corresponding labels. it will be done with the help of figure function of plt and set fig size is equal to the tuple of integers 25*4, which will specify the width and height of the figure.
Now, we plot 20 MNIST images from our batch. We use add_subplot() method to add a subplot to the current figure and pass 2, 10, and idx as arguments of the function. Here two is no of rows, ten is no of columns, and idx is index.
Now, we will display our images with the help of im_show() function and give a title for each image plot as:
Finally call plt.show() and it will give us the expected result.
It predicts all of them correctly except one, and that is to be expected from our Deep Neural Network because realistically image classification is best done with a convolutional neural network.