Classification of Animals Using CNN Model

Classification of animals using CNN Model

In this model, we try to predict whether the animal is a dog or a cat.


#We will use the Keras library to create our model.

Keras: is an open - source library that provides a python interface for artificial neural networks.

from keras.models import Sequential

from keras.layers import Conv2D,Activation,MaxPooling2D,Dense,Flatten,Dropout

import numpy as np

#Let’s initialize a convolutional neural network using the sequential model of Keras.

classifier = Sequential()

# Adding Convolution Layer

classifier.add(Conv2D(32,(3,3),input_shape=(64,64,3)))

#Conv2D stands for a 2-dimensional convolutional layer.

Here, 32 is the number of filters needed. A filter is an array of numeric values. (3,3) is the size of the filter, which means 3 rows and 3 columns.

The input image is 64 64 3 in dimensions, that is, 64 height, 64 widths, and 3 refer to RGB values. Each of the numbers in this array (64,64,3) is given values from 0 to 255, which describes the pixel intensity at that point.

#Let’s pass the feature maps through an activation layer called ReLU. ReLU stands for the rectified linear unit. ReLU is an activation function.

classifier.add(Activation('relu'))

#Add Pooling Layer, which helps to reduce the dimensionality of each feature map and retains the essential information. This helps to decrease the computational complexity of our network.

classifier.add(MaxPooling2D(pool_size =(2,2)))

#Repeat The Process Of Creating The Layers

classifier.add(Conv2D(32,(3,3)))

classifier.add(Activation('relu'))

classifier.add(MaxPooling2D(pool_size =(2,2)))

classifier.add(Conv2D(32,(3,3)))

classifier.add(Activation('relu'))

classifier.add(MaxPooling2D(pool_size =(2,2)))

#Creating A Dropout Layer, to help get rid of overfitting problem.

classifier.add(Flatten())

classifier.add(Dense(64))

classifier.add(Activation('relu'))

classifier.add(Dropout(0.5))

classifier.add(Dense(1))

#Apply a Sigmoid Function, to convert the data into probabilities for each class.

classifier.add(Activation('sigmoid'))

#Let’s do a summary of our classifier model. This is how the model looks like.

classifier.summary()

#Lets Compile Our Model.

classifier.compile(optimizer ='rmsprop', loss ='binary_crossentropy', metrics =['accuracy'])

#Setting a parameters so that the machine will get trained with the images at different positions and details to improve the accuracy.

from keras.preprocessing.image import ImageDataGenerator

train_datagen = ImageDataGenerator(rescale =1./255, shear_range =0.2, zoom_range = 0.2,

horizontal_flip =True)

test_datagen = ImageDataGenerator(rescale = 1./255)

#Setting Train and Test directories

training_set = train_datagen.flow_from_directory('C:/Users/Lab/Project/train', target_size =

(64,64), batch_size = 32, class_mode = 'binary')

test_set = test_datagen.flow_from_directory('C:/Users/Lab/Project/test', target_size = (64,64),

batch_size = 32, class_mode = 'binary')

#Training the classifier.

from IPython.display import display

from PIL import Image

classifier.fit_generator(training_set, steps_per_epoch =625, epochs = 30, validation_data

=test_set, validation_steps = 5000)

#Saving The Trained Model.

classifier.save('catdog_cnn_model.h5')

# Loading The Model For Testing.

from keras.models import load_model

classifier = load_model('catdog_cnn_model.h5')


# Testing The Classifier.

import numpy as np

from keras.preprocessing import image

test_image =image.load_img('C:/Users/Lab/image.jpeg', target_size =(64,64))

test_image =image.img_to_array(test_image)

test_image =np.expand_dims(test_image, axis =0)

result = classifier.predict(test_image)

if result[0][0] >= 0.5:

prediction = 'dog'

else:

prediction = 'cat'

print(prediction)


The computer is now an expert in classification. It just predicted the result correctly.

Conclusion

We have successfully created an image classifier using deep learning with the Keras library of Python.

Our image classifier predicted the results with an accuracy of 83.7 percentage.

Comments

Popular posts from this blog

Our First Workshop Went Like...!

Convolutional Neural Networks