Image Classification Using CNN with Keras

A CNN model is being processed on the basis of the image classification process.

1. What is Convolutional Neural Network (CNN)?

The CNN model is a neural network used to expose the attributes of the image. The more CNN layers, the more attributes it reveals. The high attributes are among the features that increase image recognition success. There are some layers in the CNN structure, let's take a look at them.

A) Convolutional Layer

This layer infers attributes. Some filters are used for these inferences. The working structure of the filters is several matrix operations, that is, to multiply the main image matrix by another matrix that we have chosen. For example, if you want to use

Let×t put a 5×5 image matrix in a 3X3 filter. When we put the filter matrix above the image matrix, let's insert the overlapping cell values into the matrix product and write it to a new matrix. The matrix that occurs when we do this on the entire image is our attribute matrix.

Edge Detection Filter

Edge information is within attributes obtained from the image. It'll help us find high-frequency areas of the image.

Stride (Step Shift)

A matrix is a parameter that determines how much the filter is shifted and processed when performing calculations. Directly affects the output size.


After convolutional, it is added to destroy the size difference between the input matrix and the output matrix. This is provided with extra pixels to be added to the input matrix.

Filtering with Padding (Karnel filter matrix)


Reduces height and width information by keeping the number of channels (RGB or grey) of the image matrix constant. There is no attribute inference. There are different calculation algorithms such as maximum, average and L2-norm pooling. The operating system is when the image matrix in which a filter matrix navigates creates a new matrix by selecting a value based on the algorithm used.

Max Pooling Example

Output Matrix Discovery Formula

Output Matrix Size Calculation Formula

B) Flattening Layer

The task of this layer is to prepare the data to be sent to the Fully Connected Layer. The procedure is to convert the matrices from the convolutional pooling layer to a one-dimensional matrix.

C) Fully Connected Layer

It is the part where the process of learning with neural networks takes place.

2.Impeding with Keras

The data set we will use will be the CİFAR-10 data set that comes with keras.

Loading Data

from keras.datasets import cifar10
(x_train,y_train),(x_test,y_test) = cifar10.load_data()

Data Recognition and Display

# explore datadatanum=1000import matplotlib.pyplot as pltimg=plt.imshow(x_train[datanum])# See labelprint("Label:",y_train[datanum])

One Hot Encoding

One Hot Encoding is the process of naming categorical variables binary.

from keras.utils import to_categoricaly_train_ohe=to_categorical(y_train)y_test_ohe=to_categorical(y_test)# Y_trainprint(y_train_ohe) after ohe

Normalizing image pixels


Creating a Model by Creating a CNN Layer

from keras.models import Sequentialfrom keras.layers import Dense,Flatten,Conv2D,MaxPool2D# Model definedmodel=Sequential()#CNN and pooling layers are addedmodel.add(Conv2D(32,(5,5),activation='relu',input_shape=(32,32,3)model.add(MaxPool2D(pool_size=(2,2)))model.add(Conv2D(32,(5,5),activation='relu'))model.add()) Conv2D(32,(5,5),activation='relu'))model.add(MaxPool2D(pool_size=(2,2)#Flatten layer is addedmodel.add(Flatten())model.add(Dense(1000,activation='re (lu') )model.add(10,activation='softmax') )# 10 nerve nodes were reached because there were 10 tags.# Model compilemodel.compile(loss='categorical_crossentropy',optimizer='adam',metrics=['accuracy'])

Model Summary

Model Success Measurement and Visualization

Success Rate with 150 Epochs

Success and Loss Values

Model's Forecast Process

A module named ImageClassifiar() was created. The image was uploaded through internal storage and the model was estimated.

Registration of the Trained Model'model_1.h5')

Reuse of a Pre-Prepared Model

from keras.models import load_modelmodel=load_model('model_1.h5')


KARABAY A, 2020 . Image Classification Using CNN with Keras,

(Accessed March 27, 2020).

  Share this post

Comments (0)


Sign up for the email newsletter to be the first to know about my blog posts