How To Use CNN Models With Full Potential?

What is a CNN model?

A CNN model is a Deep Learning algorithm which is used to train classification models. however, The way to use CNN models depend on images/ videos (number of frames per second) are count in as each example.

A CNN model has two main types of classification problems:

  1. Binary Classification
  2. Multi-class classification

Binary Classification:

Binary Classification deals with 2 classes, i.e. only 2 types of images are specific as input in the model.

Example:

  • Cat and dog
  • Apple and Mango

In this classification mode the whole CNN model is pretty much similar and has no changes except the output layer. In the output layer we use only one neuron and sigmoid activation function, as it’s more reliable and deals with binary prediction. The sigmoid activation function predicts only one value which is between 0 and 1.

Multi-class Classification:

On the contrary in multi-class you have advantage to classify more than 2 types of images. So here you can have any number of classes ranging from 2 to N.

Example:

  • Cat, dog, human.
  • Any N number of fruits.
  • Any N number of cars.

Here as there are more than 2 image types, we use a different approach. Here the output layer consists of N i.e. total number of image types as number of neurons and an activation function named softmax to do the prediction.

How to use CNN model for either of mentioned above?

First thing is there is not much difference in both the types. so, The model will be set up as per the project needs. 

The basic CNN model contains 4 main layers:

  1. Con2d
  2. Maxpooling2d
  3. Flatten
  4. Dense

Whereas a Complex and more powerful CNN model consists of few more layers, with all 4 above:

  1. BatchNorm
  2. Dropout

Here are snaps of how to use them.

Basic CNN model:

As I mentioned above one 4 layers will do the work, condition your images dimensions, dataset size (number of images in each type) I will discuss that later during the data augmentation part. thus, For now this basic CNN can solve most classification problems. The layer formation depends on the dimensions of the image.

Example:

If the image is 100×100 you would need to reduce the dimensions to an optimal dimension of 8×8 or 7×7.

If the image is 500×500 then you may need to add more layers. so, You can print a model summary to check how much the dimensions are reduced to.

Complex CNN:

Here you can see I have added a couple of more layers and they combine with few other layers. Will go one by one for the reasons behind combining these layers:

however, Adding a Batch normalization layer makes CNN model faster and more stable through normalization of the input layer by dramatically reducing the number of training epochs required to train deep networks.

thus, Adding a Dropout Layer helps removal of ambiguous nodes which hold the same values. Dropout helps avoid overfitting as it removes most of the repetitive nodes that also give the CNN model the same values after each epoch.

After setting up the CNN model, it needs to get compiled.

Note: The loss variable here changes as per the CNN model type(binary or multi), so the changes need to be done accordingly.

So how does data get fed into a CNN model?

There is a part called Data Augmentation which deals with data preprocessing before it’s sent into the pipeline. The data augmentation is nothing but basic Graphical operations (like Rotation, Translation, Scaling, Zoom, …). All these operations happen while the model is in execution, and the main reason for using data augmentation is to avoid manual data preprocessing using some other libraries.

As I mentioned above the Data augmentation plays a huge role in projects which have smaller datasets (few hundreds in each image type). Its’ better to keep an optimal dataset size which is at least 200 images in each image type/class, with dimensions of 100 x 100.

Even this is considered smaller, but data augmentations help you out in creating more copies of each image at runtime and you no need to worry about where to store these copies, cause they are stored in RAM and after execution the copies created are destroyed.

Here’s how the block of data augmentation looks like.

As you can see the operations given here are basic image processing functions combined into a single library.

Conclusion:

The way to use CNN models are easy to understand and implement. Training your own model on custom datasets which need different modifications, can be done by ease if you know your model and dataset attributes well.

References: 

  1. https://medium.com/analytics-vidhya/multi-class-classification-using-cnn-for-custom-dataset-7759865bd19?source=friends_link&sk=e54faf08bbf2e3c73aabbccdc5d7480c

written by: Pushpraj Maraje

reviewed by: Kothakota Viswanadh

If you are Interested In Machine Learning You Can Check Machine Learning Internship Program
Also Check Other Technical And Non Technical Internship Programs

Leave a Comment

Your email address will not be published. Required fields are marked *