Convolution Neural Networks :Guide for your first CNN Project using Pytorch :)

Contains reusable codes you can use for any CNN project using Pytorch

Kritika Banerjee
4 min readOct 13, 2022

Writing your first CNN project could be difficult. Here is a detailed report of a project I just made.

What is in it for you?

See the basic steps for making any CNN classification project remains same. I will jot down all the important steps that you need to follow to make any CNN project. Only some extra steps needed to be there for handling image sizes, only that changes from project to project. These are all reusable code snippets, you can copy paste and use them in your projects as you see needful .I will be more than happy to help. I will be attaching link of my GitHub repo for reference as well.

I will make another blog to share Transfer Learning approach. For that also most of the steps remains same But for now , Lets make a CNN network from Scratch using Pytorch:)

P.s- These below screenshots are from one of my image classification projects where system classifies image as per label like boots, trouser etc. I will be attaching full notebook link in the end as well .

Step1.Import all important libraries

Step2.Declare a Config class which will contain all the variables that you need for your project like epochs, learning rate , Data etc.

Step3.Retrive Train data and test data from your data directory

Step4.Check the label map. What are classes in which we need to classify our data. Do some manipulations with Label map if need be.

Like here we need

The above was the given label map and we need to change it with Number first and value later hence the below manipulation.

Step5.View a sample image from your train dataset based on data, index and label map(used to print label associated with it)

Step6.Create a data Loader for your train and test dataset. If we have a large dataset then we create data loaders which helps to pass data in batches and not the whole data in one go.

Step7.Define your Convolution Neural Network model

Call your model by passing number of inputs and outputs. Number of inputs is 1, and number of output is 10( like 0000000001)

Step8.Count the number of trainable , Non trainable parameters in your model.

Step9.Model Training- Define your loss function(Criterion), optimizer and start training.

Step10.Save your model.

Step11. Load that model again.

Step12.Evaluate your model.

Step13.Model Prediction

Here is a hyperlink for my github notebook. Please feel free to go through it and let me know in comments if this was helpful.

--

--