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:
- Binary Classification
- 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:
- Con2d
- Maxpooling2d
- Flatten
- Dense
Whereas A Complex And More Powerful CNN Model Consists Of Few More Layers, With All 4 Above:
- BatchNorm
- 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:
- https://medium.com/analytics-vidhya/multi-class-classification-using-cnn-For-custom-dataset-7759865bd19?source=friends_link&sk=e54faf08bbf2e3c73aabbccdc5d7480c
- References:
Https://Medium.Com/Analytics-Vidhya/Multi-Class-Classification-Using-Cnn-For-Custom-Dataset-7759865bd19?Source=Friends_link&Sk=E54faf08bbf2e3c73aabbccdc5d7480c