How to classify everyday images using Deep Learning?

Image classification using feed-forward neural network in PyTorch with CIFAR-10 Data Set

Manish Kumar Shah
4 min readJun 22, 2020
CIFAR10 Data Set

We saw that with the Logistic Regression Model we were able to get accuracy somewhere around 38%(Approx.). If You haven’t gone through that post, You can find it here 👉 (Link)

Let’s get started🤖

  1. Our first step always is to import all the important libraries:

2. Can we work on the dataset, without downloading it!!🤦‍♂️No, let’s download it then. Well If there is any way we can do so... Do let me know???

3. Knowing the dataset & test_dataset:

Is there any way to have a look at each and every class of the dataset?

4. We will be requiring a validation set to validate our model Since this dataset contains an extra 10,000 test images but no validation images so, we can create the validation set from the dataset . We will then have three sets of CIFAR10 dataset i.e., train_ds , val_ds & test_ds .

Let’s set the size of the batch before we create the Data Loaders as
batch_size=128

5. Data Loaders: This is a very important part, It helps in shuffling the data in the batches, also num_workers=4 this is for specifying that if there are four cores then use it.

6. Let’s view a set of Data from train_loader: 👇

We can define a function to calculate the accuracy of each epoch sessions:

6. Model:🕴

After defining our model, We will need to enable GPU, Since our model is not a simple model anymore it contains 5 hidden feed-neural networks, which may require GPU to function. So, after enabling let's check if GPU got enabled or not. If torch.cuda.is_available() returns True then 👍.

We may also define a function to choose the device from CPU or GPU depending on the availability.

Defining a function to move the tensor(s) to a chosen device.

7. Now we can move all our data to the chosen device by defining a simple class:

Let’s Move or data’s to the selected device use the above defined class

8. Training the Model: (Finally 🤘)

But before we start the training of our model we need to define two more basic functions:

Let’s assign the size of the input our model will take and the output size our model will return-

input_size = 3*32*32
num_classes = 10

Are you not sure about the number of hidden layers or the size of the input or output????😳 Worry Not… Let’s have a look🤗

Let's evaluate our model for the very first time, And see how it performs before the training.

It’s very sad to see such a bad 'val_loss': 2.304230213165283, 'val_acc': 0.09589843451976776 values, Let's train Our Model😡

Let us see the performance of the model for 50 epochs and keep the learning rate as 0.01:-

Owo!!👶 We are able to get 47% accuracy from 9.5% accuracy. Well, then let’s Train our model for more 10 epochs and this time decreasing the learning rate by a factor of 10.

We see that our model is able to get almost 50%(Approx.) Accuracy. Great Isn’t it.

9. Let’s plot some graphs and try to visualize the rates of losses and accuracies. But before that, we need to define the plotting functions. So here we go…

10. Plotting:

11. Now, what??? We have defined our model, we have trained our model. So let’s quickly do some predictions with our trained model.
But, before that, as usual, we will define a function. Defining a function that may be used several times in the future is a good practice for every programmer that should be always followed.

12. Now Finally, It’s the time of Enlightening😇 ourselves. Go….

Owao prediction seems correct.Well let us check for 10 other predictions too..

13. Predictions

We can see that our Model has improved by some margin compared to logistic regression.

With this feed-forward neural network our cat is predicted as cat , ship as ship , frog as frog and automobile as automobile , Except some of them like airplane has become ship and there may be more. But since at 50%(Approx.) accuracy, our model is available to predict well to some extent.

14. Now Is there any way we can push our accuracy to more than 70%, Well have you heard of Convolution Neural Network. Yeah, this can be achieved with CNN. So this I will Cover in the next Blog.
Happy Learning🎓🎓🎓🎓🎩

15. CNN coming soon
Since you have made it till here then why not have a look at the entire notebook.
Notebook
Also I am trying to maintain a Repo of all the basics of Machine Learning with Scikit-Learn and Deep Learning with PyTorch
Repo of all the basic concept of Machine learning 👉 Click Me
Repo of all the basic concept of Deep learning with PyTorch 👉 Click Me

References:

--

--