Original article was published on Becoming Human: Artificial Intelligence Magazine
In this blog, I’ll show how to build CNN model for image classification.
In this project, I have used MNIST dataset, which is the basic and simple dataset which helps the beginner to understand the theory in depth.
So let’s start….
The MNIST database of handwritten digits, available from this page, has a training set of 60,000 examples, and a test set of 10,000 examples. It is a subset of a larger set available from NIST. The digits have been size-normalized and centred in a fixed-size image.
So, now let’s jump into CODES!!
You can access codes for this project here.
Import necessary libraries.
#Import necessary libraries
import pandas as pd
import numpy as np
from tensorflow.keras.datasets import mnist
import matplotlib.pyplot as plt
from tensorflow.keras.utils import to_categorical
from tensorflow.keras.models import Sequential
from tensorflow.keras.layers import Dense, Conv2D, MaxPool2D,
from tensorflow.keras.callbacks import EarlyStopping
(x_train, y_train), (x_test, y_test) = mnist.load_data()
Check the shape of the training data
>>(60000, 28, 28)
This means there are 60000 images of size 28 X 28.
Assigning the first image as single_image and finding the shape of the same.
single_image = x_train
Now let’s check the shape of y_train
Top 4 Most Popular Ai Articles:
Applying to_categorically to y_train — It converts a class vector (integer) to binary class matrix
y_example = to_categorical(y_train)
Checking the shape of y_example
Apply to_categorical to y_test and y_train
y_cat_test = to_categorical(y_test,10)
y_cat_train = to_categorical(y_train,10)
Divide x_train and x_test by 255 in order to normalise the image
x_train = x_train/255
x_test = x_test/255
Now let’s again check the shape of x_train and x_test
>>(60000, 28, 28)
>>(10000, 28, 28)
Now we need to reshape x_train and x_test
x_train = x_train.reshape(60000, 28, 28, 1)
>>(60000, 28, 28, 1)
x_test = x_test.reshape(10000,28,28,1)
>>(10000, 28, 28, 1)
Now let’s build a Convolutional Neural Network Model.
model = Sequential()
# CONVOLUTIONAL LAYER
model.add(Conv2D(filters=32, kernel_size=(4,4),input_shape=(28, 28, 1), activation=’relu’,))
# POOLING LAYER
# FLATTEN IMAGES FROM 28 by 28 to 764 BEFORE FINAL LAYER
# 128 NEURONS IN DENSE HIDDEN LAYER
# LAST LAYER IS THE CLASSIFIER, THUS 10 POSSIBLE CLASSES
Let’s check the model
Now we’ll use EarlyStopping while fitting the model.
early_stop = EarlyStopping(monitor=’val_loss’,patience=2)
Evaluating our model
Now we are going to predict from the model
my_number = x_test
Great!! we are getting prediction as 1.
You can view my codes in my GitHub account, details are mentioned below.
So, that’s all about how to build a Convolutional Neural Network.
I hope you like this blog. Feel free to share your thoughts in the comment section and you can also connect with me.
Don’t forget to give us your 👏 !
Building a Convolutional Neural Network (CNN) Model for Image classification. was originally published in Becoming Human: Artificial Intelligence Magazine on Medium, where people are continuing the conversation by highlighting and responding to this story.