In Part 1 of this tutorial, towards the end, we saw if we take an average of all the pixels in the apple and banana image dataset we get an image of banana and apple which is not much like them as given below. The images look more like a ghost :)
At this point, you might argue that if the average images do not look like the object itself how are we going to classify using this or even if we classify will that classification be correct?
We need to remember that the classifier we are creating is a baseline binary classifier. Every other classifier we create after this method using neural net should perform better than this. This classifier will act as a benchmark. Now let’s see how are we going to create this classifier using these images.
Step 11: Let’s take an arbitrary apple and compare it to our ideal average apple.
Step 12: Now we will calculate RMSE (root mean square error) and MAE (mean absolute error) between the pixels of this image and the pixels of our average apple image.
As we can see we got two tensors of rank 0 which give us the MAE and RMSE values for apple.
Step 13: Now once we have our classifier ready we need to have metrics to measure the performance of this classifier. We will broadcasting technique of PyTorch to calculate this accuracy. Broadcasting helps PyTorch to perform operations on two different rank tensors by stretching the smaller rank tensor without giving any error. You can learn more about it here. We will prepare our validation dataset, just as we prepared our training dataset.
Our validation set is ready. We will just check the shape of the set.
Step 13: Now, in order to calculate accuracy metrics we need to write a function is_apple which will tell us if an image is close to apple or banana. For this purpose, we also need to define a distance function that will calculate how distant is an image from the average apple image. We will use this function for the calculation of MAE.
This is the same as we calculated the distance between an apple image and the mean apple image using the MAE metric. But in order to calculate a metric for overall accuracy, we will need to calculate the distance to the ideal apple for every image in the validation set.
Step 14: We won’t be using loops as we have broadcasting feature in PyTorch which is very fast and helpful.
Step 15: Next we will create is_apple function
Final Step : Now we can calculate the accuracy for each of the apples and bananas by taking the average of that function for all apples and bananas its inverse for all bananas:
We got an accuracy of 88.68 for our baseline binary image classifier is not bad.
I hope by now you would get a good idea about how an image is processed by the computer and how we can create a classifier using this method. There are a lot of things we can argue upon such as:
1). This method is more suitable for greyscale and very similar images dataset such as MNIST dataset. Using this method on a dataset that has the object to be identified at different places in the image might create a poor result.
But still, it's a very good approach to get a better understanding of how an image classifier works behind the scene.
You can find the entire code in my GitHub gist.
2). Cover photo source: https://miro.medium.com/max/640/1*s6hB0AC-dS0h5qZIuE4zkA.jpeg