- Blog Categories
- Software Development Projects and Ideas
- 12 Computer Science Project Ideas
- 28 Beginner Software Projects
- Top 10 Engineering Project Ideas
- Top 10 Easy Final Year Projects
- Top 10 Mini Projects for Engineers
- 25 Best Django Project Ideas
- Top 20 MERN Stack Project Ideas
- Top 12 Real Time Projects
- Top 6 Major CSE Projects
- 12 Robotics Projects for All Levels
- Java Programming Concepts
- Abstract Class in Java and Methods
- Constructor Overloading in Java
- StringBuffer vs StringBuilder
- Java Identifiers: Syntax & Examples
- Types of Variables in Java Explained
- Composition in Java: Examples
- Append in Java: Implementation
- Loose Coupling vs Tight Coupling
- Integrity Constraints in DBMS
- Different Types of Operators Explained
- Career and Interview Preparation in IT
- Top 14 IT Courses for Jobs
- Top 20 Highest Paying Languages
- 23 Top CS Interview Q&A
- Best IT Jobs without Coding
- Software Engineer Salary in India
- 44 Agile Methodology Interview Q&A
- 10 Software Engineering Challenges
- Top 15 Tech's Daily Life Impact
- 10 Best Backends for React
- Cloud Computing Reference Models
- Web Development and Security
- Find Installed NPM Version
- Install Specific NPM Package Version
- Make API Calls in Angular
- Install Bootstrap in Angular
- Use Axios in React: Guide
- StrictMode in React: Usage
- 75 Cyber Security Research Topics
- Top 7 Languages for Ethical Hacking
- Top 20 Docker Commands
- Advantages of OOP
- Data Science Projects and Applications
- 42 Python Project Ideas for Beginners
- 13 Data Science Project Ideas
- 13 Data Structure Project Ideas
- 12 Real-World Python Applications
- Python Banking Project
- Data Science Course Eligibility
- Association Rule Mining Overview
- Cluster Analysis in Data Mining
- Classification in Data Mining
- KDD Process in Data Mining
- Data Structures and Algorithms
- Binary Tree Types Explained
- Binary Search Algorithm
- Sorting in Data Structure
- Binary Tree in Data Structure
- Binary Tree vs Binary Search Tree
- Recursion in Data Structure
- Data Structure Search Methods: Explained
- Binary Tree Interview Q&A
- Linear vs Binary Search
- Priority Queue Overview
- Python Programming and Tools
- Top 30 Python Pattern Programs
- List vs Tuple
- Python Free Online Course
- Method Overriding in Python
- Top 21 Python Developer Skills
- Reverse a Number in Python
- Switch Case Functions in Python
- Info Retrieval System Overview
- Reverse a Number in Python
- Real-World Python Applications
- Data Science Careers and Comparisons
- Data Analyst Salary in India
- Data Scientist Salary in India
- Free Excel Certification Course
- Actuary Salary in India
- Data Analyst Interview Guide
- Pandas Interview Guide
- Tableau Filters Explained
- Data Mining Techniques Overview
- Data Analytics Lifecycle Phases
- Data Science Vs Analytics Comparison
- Artificial Intelligence and Machine Learning Projects
- Exciting IoT Project Ideas
- 16 Exciting AI Project Ideas
- 45+ Interesting ML Project Ideas
- Exciting Deep Learning Projects
- 12 Intriguing Linear Regression Projects
- 13 Neural Network Projects
- 5 Exciting Image Processing Projects
- Top 8 Thrilling AWS Projects
- 12 Engaging AI Projects in Python
- NLP Projects for Beginners
- Concepts and Algorithms in AIML
- Basic CNN Architecture Explained
- 6 Types of Regression Models
- Data Preprocessing Steps
- Bagging vs Boosting in ML
- Multinomial Naive Bayes Overview
- Gini Index for Decision Trees
- Bayesian Network Example
- Bayes Theorem Guide
- Top 10 Dimensionality Reduction Techniques
- Neural Network Step-by-Step Guide
- Technical Guides and Comparisons
- Make a Chatbot in Python
- Compute Square Roots in Python
- Permutation vs Combination
- Image Segmentation Techniques
- Generative AI vs Traditional AI
- AI vs Human Intelligence
- Random Forest vs Decision Tree
- Neural Network Overview
- Perceptron Learning Algorithm
- Selection Sort Algorithm
- Career and Practical Applications in AIML
- AI Salary in India Overview
- Biological Neural Network Basics
- Top 10 AI Challenges
- Production System in AI
- Top 8 Raspberry Pi Alternatives
- Top 8 Open Source Projects
- 14 Raspberry Pi Project Ideas
- 15 MATLAB Project Ideas
- Top 10 Python NLP Libraries
- Naive Bayes Explained
- Digital Marketing Projects and Strategies
- 10 Best Digital Marketing Projects
- 17 Fun Social Media Projects
- Top 6 SEO Project Ideas
- Digital Marketing Case Studies
- Coca-Cola Marketing Strategy
- Nestle Marketing Strategy Analysis
- Zomato Marketing Strategy
- Monetize Instagram Guide
- Become a Successful Instagram Influencer
- 8 Best Lead Generation Techniques
- Digital Marketing Careers and Salaries
- Digital Marketing Salary in India
- Top 10 Highest Paying Marketing Jobs
- Highest Paying Digital Marketing Jobs
- SEO Salary in India
- Brand Manager Salary in India
- Content Writer Salary Guide
- Digital Marketing Executive Roles
- Career in Digital Marketing Guide
- Future of Digital Marketing
- MBA in Digital Marketing Overview
- Digital Marketing Techniques and Channels
- 9 Types of Digital Marketing Channels
- Top 10 Benefits of Marketing Branding
- 100 Best YouTube Channel Ideas
- YouTube Earnings in India
- 7 Reasons to Study Digital Marketing
- Top 10 Digital Marketing Objectives
- 10 Best Digital Marketing Blogs
- Top 5 Industries Using Digital Marketing
- Growth of Digital Marketing in India
- Top Career Options in Marketing
- Interview Preparation and Skills
- 73 Google Analytics Interview Q&A
- 56 Social Media Marketing Q&A
- 78 Google AdWords Interview Q&A
- Top 133 SEO Interview Q&A
- 27+ Digital Marketing Q&A
- Digital Marketing Free Course
- Top 9 Skills for PPC Analysts
- Movies with Successful Social Media Campaigns
- Marketing Communication Steps
- Top 10 Reasons to Be an Affiliate Marketer
- Career Options and Paths
- Top 25 Highest Paying Jobs India
- Top 25 Highest Paying Jobs World
- Top 10 Highest Paid Commerce Job
- Career Options After 12th Arts
- Top 7 Commerce Courses Without Maths
- Top 7 Career Options After PCB
- Best Career Options for Commerce
- Career Options After 12th CS
- Top 10 Career Options After 10th
- 8 Best Career Options After BA
- Projects and Academic Pursuits
- 17 Exciting Final Year Projects
- Top 12 Commerce Project Topics
- Top 13 BCA Project Ideas
- Career Options After 12th Science
- Top 15 CS Jobs in India
- 12 Best Career Options After M.Com
- 9 Best Career Options After B.Sc
- 7 Best Career Options After BCA
- 22 Best Career Options After MCA
- 16 Top Career Options After CE
- Courses and Certifications
- 10 Best Job-Oriented Courses
- Best Online Computer Courses
- Top 15 Trending Online Courses
- Top 19 High Salary Certificate Courses
- 21 Best Programming Courses for Jobs
- What is SGPA? Convert to CGPA
- GPA to Percentage Calculator
- Highest Salary Engineering Stream
- 15 Top Career Options After Engineering
- 6 Top Career Options After BBA
- Job Market and Interview Preparation
- Why Should You Be Hired: 5 Answers
- Top 10 Future Career Options
- Top 15 Highest Paid IT Jobs India
- 5 Common Guesstimate Interview Q&A
- Average CEO Salary: Top Paid CEOs
- Career Options in Political Science
- Top 15 Highest Paying Non-IT Jobs
- Cover Letter Examples for Jobs
- Top 5 Highest Paying Freelance Jobs
- Top 10 Highest Paying Companies India
- Career Options and Paths After MBA
- 20 Best Careers After B.Com
- Career Options After MBA Marketing
- Top 14 Careers After MBA In HR
- Top 10 Highest Paying HR Jobs India
- How to Become an Investment Banker
- Career Options After MBA - High Paying
- Scope of MBA in Operations Management
- Best MBA for Working Professionals India
- MBA After BA - Is It Right For You?
- Best Online MBA Courses India
- MBA Project Ideas and Topics
- 11 Exciting MBA HR Project Ideas
- Top 15 MBA Project Ideas
- 18 Exciting MBA Marketing Projects
- MBA Project Ideas: Consumer Behavior
- What is Brand Management?
- What is Holistic Marketing?
- What is Green Marketing?
- Intro to Organizational Behavior Model
- Tech Skills Every MBA Should Learn
- Most Demanding Short Term Courses MBA
- MBA Salary, Resume, and Skills
- MBA Salary in India
- HR Salary in India
- Investment Banker Salary India
- MBA Resume Samples
- Sample SOP for MBA
- Sample SOP for Internship
- 7 Ways MBA Helps Your Career
- Must-have Skills in Sales Career
- 8 Skills MBA Helps You Improve
- Top 20+ SAP FICO Interview Q&A
- MBA Specializations and Comparative Guides
- Why MBA After B.Tech? 5 Reasons
- How to Answer 'Why MBA After Engineering?'
- Why MBA in Finance
- MBA After BSc: 10 Reasons
- Which MBA Specialization to choose?
- Top 10 MBA Specializations
- MBA vs Masters: Which to Choose?
- Benefits of MBA After CA
- 5 Steps to Management Consultant
- 37 Must-Read HR Interview Q&A
- Fundamentals and Theories of Management
- What is Management? Objectives & Functions
- Nature and Scope of Management
- Decision Making in Management
- Management Process: Definition & Functions
- Importance of Management
- What are Motivation Theories?
- Tools of Financial Statement Analysis
- Negotiation Skills: Definition & Benefits
- Career Development in HRM
- Top 20 Must-Have HRM Policies
- Project and Supply Chain Management
- Top 20 Project Management Case Studies
- 10 Innovative Supply Chain Projects
- Latest Management Project Topics
- 10 Project Management Project Ideas
- 6 Types of Supply Chain Models
- Top 10 Advantages of SCM
- Top 10 Supply Chain Books
- What is Project Description?
- Top 10 Project Management Companies
- Best Project Management Courses Online
- Salaries and Career Paths in Management
- Project Manager Salary in India
- Average Product Manager Salary India
- Supply Chain Management Salary India
- Salary After BBA in India
- PGDM Salary in India
- Top 7 Career Options in Management
- CSPO Certification Cost
- Why Choose Product Management?
- Product Management in Pharma
- Product Design in Operations Management
- Industry-Specific Management and Case Studies
- Amazon Business Case Study
- Service Delivery Manager Job
- Product Management Examples
- Product Management in Automobiles
- Product Management in Banking
- Sample SOP for Business Management
- Video Game Design Components
- Top 5 Business Courses India
- Free Management Online Course
- SCM Interview Q&A
- Fundamentals and Types of Law
- Acceptance in Contract Law
- Offer in Contract Law
- 9 Types of Evidence
- Types of Law in India
- Introduction to Contract Law
- Negotiable Instrument Act
- Corporate Tax Basics
- Intellectual Property Law
- Workmen Compensation Explained
- Lawyer vs Advocate Difference
- Law Education and Courses
- LLM Subjects & Syllabus
- Corporate Law Subjects
- LLM Course Duration
- Top 10 Online LLM Courses
- Online LLM Degree
- Step-by-Step Guide to Studying Law
- Top 5 Law Books to Read
- Why Legal Studies?
- Pursuing a Career in Law
- How to Become Lawyer in India
- Career Options and Salaries in Law
- Career Options in Law India
- Corporate Lawyer Salary India
- How To Become a Corporate Lawyer
- Career in Law: Starting, Salary
- Career Opportunities: Corporate Law
- Business Lawyer: Role & Salary Info
- Average Lawyer Salary India
- Top Career Options for Lawyers
- Types of Lawyers in India
- Steps to Become SC Lawyer in India
- Tutorials
- C Tutorials
- Recursion in C: Fibonacci Series
- Checking String Palindromes in C
- Prime Number Program in C
- Implementing Square Root in C
- Matrix Multiplication in C
- Understanding Double Data Type
- Factorial of a Number in C
- Structure of a C Program
- Building a Calculator Program in C
- Compiling C Programs on Linux
- Java Tutorials
- Handling String Input in Java
- Determining Even and Odd Numbers
- Prime Number Checker
- Sorting a String
- User-Defined Exceptions
- Understanding the Thread Life Cycle
- Swapping Two Numbers
- Using Final Classes
- Area of a Triangle
- Skills
- Software Engineering
- JavaScript
- Data Structure
- React.js
- Core Java
- Node.js
- Blockchain
- SQL
- Full stack development
- Devops
- NFT
- BigData
- Cyber Security
- Cloud Computing
- Database Design with MySQL
- Cryptocurrency
- Python
- Digital Marketings
- Advertising
- Influencer Marketing
- Search Engine Optimization
- Performance Marketing
- Search Engine Marketing
- Email Marketing
- Content Marketing
- Social Media Marketing
- Display Advertising
- Marketing Analytics
- Web Analytics
- Affiliate Marketing
- MBA
- MBA in Finance
- MBA in HR
- MBA in Marketing
- MBA in Business Analytics
- MBA in Operations Management
- MBA in International Business
- MBA in Information Technology
- MBA in Healthcare Management
- MBA In General Management
- MBA in Agriculture
- MBA in Supply Chain Management
- MBA in Entrepreneurship
- MBA in Project Management
- Management Program
- Consumer Behaviour
- Supply Chain Management
- Financial Analytics
- Introduction to Fintech
- Introduction to HR Analytics
- Fundamentals of Communication
- Art of Effective Communication
- Introduction to Research Methodology
- Mastering Sales Technique
- Business Communication
- Fundamentals of Journalism
- Economics Masterclass
- Free Courses
Image Classification in CNN: Everything You Need to Know
Updated on 23 September, 2022
9.2K+ views
• 14 min read
Table of Contents
Introduction
While going through the Facebook feed, have you ever wondered how the people in a group photo are automatically labelled by Facebook’s software? Behind every interactive user interface of Facebook you see, there is a complex and strong algorithm that is used to recognize and label each picture that is uploaded by us on to the social media platform. With every picture of ours, we only aid in improving the algorithm’s efficiency. Yes, Image Classification is one of the most widely used algorithms where we see the application of Artificial Intelligence.
Top Machine Learning and AI Courses Online
In recent times, Convolutional Neural Networks (CNN) has become one of the strongest proponents of Deep Learning. One popular application of these Convolutional Networks is Image Classification. In this tutorial, we will go through the basics of Convolutional Neural Networks, see the various layers involved in building a CNN model and finally visualize an example of the Image Classification task.
Image Classification
Before we get into the details of Deep Learning and Convolutional Neural Networks, let us understand the basics of Image Classification. In general, Image Classification is defined as the task in which we give an image as the input to a model built using a specific algorithm that outputs the class or the probability of the class that the image belongs to. This process in which we label an image to a particular class is called Supervised Learning.
Trending Machine Learning Skills
Enrol for the Machine Learning Course from the World’s top Universities. Earn Masters, Executive PGP, or Advanced Certificate Programs to fast-track your career.
There is a huge difference between how we see an image and how the machine (computer) sees the same image. To us, we are able to visualize the image and characterize it based on colour and size. On the other hand, to the machine, all it gets to see are numbers. The numbers that are seen are called pixels.
Each pixel has a value between 0 and 255. Hence, with these numerical data, the machine requires some pre-processing steps in order to derive some specific patterns or features that distinguish one image from the other. Convolutional Neural Networks help us build algorithms that are capable of deriving the specific pattern from images.
What We See Vs What the Computer Sees
Source – Difference between Computer and Human Eye
Deep Learning for Image Classification
Now that we have understood what is Image Classification, let us now see how we can implement it using Artificial Intelligence. For this, we use the popular Deep Learning methods. Deep Learning is a subset of Artificial Intelligence that makes use of large image datasets to recognize and derive patterns from various images to differentiate between various classes present in the image dataset.
The major challenge that Deep Learning faces is that for a huge database, it takes a very long time and it has a high computational cost. However, the Convolutional Neural Networks, which is a type of Deep Learning algorithm addresses this problem well.
Convolutional Neural Networks
In Deep Learning, Convolutional Neural Networks are a class of Deep Neural Networks that are mostly used in visual imagery. They are a special architecture of the Artificial Neural Networks (ANN) which was proposed in 1998 by Yann LeCunn. The Convolutional Neural Networks consist of two parts.
The first part consists of the Convolutional layers and the Pooling layers in which the main feature extraction process takes place. In the second part, the Fully Connected and the Dense layers perform several non-linear transformations on the extracted features and act as the classifier part. Learn CNN for image classification.
Consider the above-shown image example of what the human and the machine sees. As we see, the computer sees an array of pixels. For example, if the image size if 500×500, then the size of the array will be 500x500x3. Here, 500 stands for each height and width, 3 stands for the RGB channel where each colour channel is represented by a separate array. The pixel intensity varies from 0 to 255.
Now for Image Classification, the computer will look for the features at the base level. According to us as humans, these base-level features of the cat are its ears, nose and whiskers. While for the computer, these base-level features are the curvatures and boundaries. In this way by using several different layers such as the Convolutional layers and the Pooling layers, the computer extracts the base level features from the images.
In the Convolutional Neural Network model, there are several types of layers such as the –
- Input Layer
- Convolutional Layer
- Pooling Layer
- Fully Connected Layer
- Output Layer
- Activation Functions
Let us go through each of the layers in brief before we get into its application in Image Classification.
Input Layer
From the name, we understand that this is the layer in which the input image will be fed into the CNN model. Depending upon our requirement, we can reshape the image to different sizes such as (28,28,3)
Convolutional Layer
Then comes the most important layer which consists of a filter (also known as a kernel) with a fixed size. The mathematical operation of Convolution is performed between the input image and the filter. This is the stage in which most of the base features such as sharp edges and curves are extracted from the image and hence this layer is also known as the feature extractor layer.
Pooling Layer
After performing the convolution operation, we perform the Pooling operation. This is also known as downsampling where the spatial volume of the image is reduced. For example, if we perform a Pooling operation with a stride of 2 on an image with dimensions 28×28, then the image size reduced to 14×14, it gets reduced to half of its original size.
Fully Connected Layer
The Fully Connected Layer (FC) is placed just before the final classification output of the CNN model. These layers are used to flatten the results before classifying. It involves several biases, weights and neurons. Attaching an FC layer before classification results in an N-dimensional vector where N is a number of classes out of which the model has to choose a class.
Output Layer
Finally, the Output Layer consists of the label which is mostly encoded by using the one-hot encoding method.
Activation Function
These Activation Functions are the core of any Convolutional Neural Network model. These functions are used to determine the output of a neural network. In short, it determines whether a particular neuron should be activated (“fired”) or not. These are usually non-linear functions that are performed on the input signals. This transformed output is then sent as an input to the next layer of neurons. There are several activation functions such as the Sigmoid, ReLU, Leaky ReLU, TanH and Softmax.
Basic CNN Architecture
Source: Basic CNN Architecture
As defined earlier the above-shown diagram is the basic architecture of a Convolutional Neural Network model. Now that we are ready with the basics of Image Classification and CNN, let us now dive into its application with a real-time problem. Learn more about basic CNN architecture.
Convolutional Neural Networks Implementation
Now that we have understood the basics of Image Classification and Convolutional Neural Networks, let us visualize its implementation in TensorFlow/Keras with Python coding. In this, we shall build a simple Convolutional Neural Network Model with a Basic LeNet Architecture, train the model on a training set & test set and finally obtain the accuracy of the model on the test set data.
Problem Set
In this article for building and training the Convolutional Neural Network Model, we shall be using the famous Fashion MNIST dataset. MNIST stands for Modified National Institute of Standards and Technology. Fashion-MNIST is a dataset of Zalando’s article images—consisting of a training set of 60,000 examples and a test set of 10,000 examples. Each example is a 28×28 grayscale image, associated with a label from 10 classes.
Each training and test example is assigned to one of the following labels:
0 – T-shirt/top
1 – Trouser
2 – Pullover
3 – Dress
4 – Coat
5 – Sandal
6 – Shirt
7 – Sneaker
8 – Bag
9 – Ankle Boots
Source: Fashion MNIST Dataset Images
Program Code
Step 1 – Importing the Libraries
The First step to building any Deep Learning model is to import the libraries that are necessary for the program. In our example, as we are using the TensorFlow framework, we shall import the Keras library and also other important libraries such as the number for calculation and the matplotlib for plotting the plots.
#TensorFlow – Importing the Libraries
import numpy as np
import matplotlib.pyplot as plt
%matplotlib inline
import tensorflow as tf
from tensorflow import Keras
Step 2 – Getting and Splitting the Dataset
Once we have imported the libraries, the next step is to download the dataset and split the Fashion MNIST dataset into the respective 60,000 training and 10,000 test data. Fortunately, keras provides us with a predefined function to import the Fashion MNIST dataset and we can split them in the next line using a simple line of code that is self-understood.
#TensorFlow – Getting and Splitting the Dataset
fashion_mnist = keras.datasets.fashion_mnist
(train_images_tf, train_labels_tf), (test_images_tf, test_labels_tf) = fashion_mnist.load_data()
Step 3 – Visualizing the Data
As the dataset is downloaded along with the images and their corresponding labels, to make it more clear to the user, it is always advised to view the data so that we can understand the type of data that we are dealing with the build the Convolutional Neural Network Model accordingly. Here, with this simple block of code given below, we shall visualize the first 3 images of the training dataset that is shuffled randomly.
#TensorFlow – Visualizing the Data
def imshowTensorFlow(img):
plt.imshow(img, cmap=’gray’)
print(“Label:”, img[0])
imshowTensorFlow(train_images_tf[0])
Label: 9 Label: 0 Label: 3
The above-given image and their labels can be verified with the labels which are given in the Fashion MNIST dataset details above. From this, we infer that our data image is a grayscale image with a height of 28 pixels and a width of 28 pixels.
Hence, the model can be built with an input size of (28,28,1), where 1 stands for the grayscale image.
Step 4 – Building the Model
As mentioned above, in this article we will be building a simple Convolutional Neural Network with the LeNet architecture. LeNet is a convolutional neural network structure proposed by Yann LeCun et al. in 1989. In general, LeNet refers to LeNet-5 and is a simple Convolutional Neural Network.
Source: The LeNet Architecture
From the above-given Architecture diagram of the LeNet CNN Model, we see that there are 5+2 layers. The first and second layers are a Convolutional layer followed by a Pooling layer. Again, the third and fourth layers consist of a Convolutional layer and a Pooling layer. As a result of these operations, the size of the input image from 28×28 reduces to 7×7.
The fifth layer of the LeNet Model is the Fully Connected Layer which flattens the previous layer’s output. Followed by two Dense layers, the final output layer of the CNN model consist of a Softmax activation function with 10 units. Softmax function predicts a class probability for each of the 10 classes of the Fashion MNIST dataset.
#TensorFlow – Building the Model
model = keras.Sequential([
keras.layers.Conv2D(input_shape=(28,28,1), filters=6, kernel_size=5, strides=1, padding=”same”, activation=tf.nn.relu),
keras.layers.AveragePooling2D(pool_size=2, strides=2),
keras.layers.Conv2D(16, kernel_size=5, strides=1, padding=”same”, activation=tf.nn.relu),
keras.layers.AveragePooling2D(pool_size=2, strides=2),
keras.layers.Flatten(),
keras.layers.Dense(120, activation=tf.nn.relu),
keras.layers.Dense(84, activation=tf.nn.relu),
keras.layers.Dense(10, activation=tf.nn.softmax)
])
Step 5 – Model Summary
Once the layers of the LeNet model are finalized, we can proceed to compile the model and view a summaried version of the CNN model designed.
#TensorFlow – Model Summary
model.compile(loss=keras.losses.categorical_crossentropy,
optimizer=’adam’,
metrics=[‘acc’])
model.summary()
In this, as the final output has more than 2 classes (10 classes), we use the categorical crossentropy as the loss function and the Adam Optimizer to our model built. The model summary is given below.
Step 6 – Training the Model
Finally, we come to the part where we begin the training process of the LeNet CNN model. Firstly, we reshape the training dataset and normalize it to smaller values by dividing with 255.0 to reduce the computational cost. Then the training labels are converted from an integer class vector to a binary class matrix. For example, label 3 is converted to [0, 0, 0, 1, 0, 0, 0, 0, 0]
#TensorFlow – Training the Model
train_images_tensorflow = (train_images_tf / 255.0).reshape(train_images_tf.shape[0], 28, 28, 1)
test_images_tensorflow = (test_images_tf / 255.0).reshape(test_images_tf.shape[0], 28, 28 ,1)
train_labels_tensorflow=keras.utils.to_categorical(train_labels_tf)
test_labels_tensorflow=keras.utils.to_categorical(test_labels_tf)
H = model.fit(train_images_tensorflow, train_labels_tensorflow, epochs=30, batch_size=32)
At the end of training after 30 epochs, we obtain the final training accuracy and loss as,
Epoch 30/30
1875/1875 [==============================] – 4s 2ms/step – loss: 0.0421 – acc: 0.9850
Training Accuracy: 98.294997215271 %
Training Loss: 0.04584110900759697
Step 7 – Predicting the Results
Finally, once we are done with our training process of the CNN model, we shall fit the same model on the test dataset and predict the accuracy of 10,000 test images.
#TensorFlow – Comparing the Results
predictions = model.predict(test_images_tensorflow)
correct = 0
for i, pred in enumerate(predictions):
if np.argmax(pred) == test_labels_tf[i]:
correct += 1
print(‘Test Accuracy of the model on the {} test images: {}% with TensorFlow’.format(test_images_tf.shape[0],100 * correct/test_images_tf.shape[0]))
Also Read: Machine Learning Project Ideas
The output that we get is,
Test Accuracy of the model on the 10000 test images: 90.67% with TensorFlow
With this, we come to an end to the program on building an Image Classification Model with Convolutional Neural Networks.
Popular AI and ML Blogs & Free Courses
Conclusion
Thus, in this tutorial on implementing Image Classification in CNN, we have understood the basic concepts behind Image Classification, Convolutional Neural Networks along with its implementation in Python programming language with TensorFlow framework.
If you’re interested to learn more about machine learning, check out IIIT-B & upGrad’s PG Diploma in Machine Learning & AI which is designed for working professionals and offers 450+ hours of rigorous training, 30+ case studies & assignments, IIIT-B Alumni status, 5+ practical hands-on capstone projects & job assistance with top firms.
Frequently Asked Questions (FAQs)
1. Which CNN model is considered to be the most optimum for image classification?
The best CNN model for image classification is the VGG-16, which stands for Very Deep Convolutional Networks for Large-Scale Image Recognition. VGG, which was designed as a deep CNN, outperforms baselines on a wide range of tasks and datasets outside of ImageNet. The model's distinguishing feature is that when it was being created, more attention was placed on incorporating excellent convolution layers rather than focusing on adding a large number of hyper parameters. It has a total of 16 layers, 5 blocks, and each block has a maximum pooling layer, making it a quite large network.
2. What are the disadvantages of using CNN models for image classification?
When it comes to image classification, CNN models are highly successful. However, there are several drawbacks to employing CNNs. If the picture to be identified is slanted or rotated, the CNN model has problems accurately identifying the image. When CNN visualizes the images, there are no internal representations of the components and their part-whole connections. Furthermore, if the CNN model to be employed includes numerous convolutional layers, the classification process will take a long time.
3. Why is the use of the CNN model preferred over the ANN for image data as input?
By combining filters or transformations, CNN can learn many layers of feature representations for every image provided as input. Overfitting is decreased since the number of parameters for the network to learn in CNN is substantially smaller than in multilayer neural networks. When using ANN, neural networks may learn a single feature representation of the image, but, in the case of complex images, ANN will fail to provide improved visualizations or classifications since it cannot learn pixel dependencies existing in the input images.
RELATED PROGRAMS