Prediction of Cats Vs Dogs

In this project, we will have some images of cats and dogs and by them, we will train a Convolutional Neural Network using Keras to predict if the image is a dog or cat.

We have 10000 images in total in the dataset which divided to 8000 images for the training set and 2000 images for the test set.

.

Building CNN

Let’s import the Keras libraries and packages.

from keras.models import Sequential
from keras.layers import Convolution2D
from keras.layers import MaxPooling2D
from keras.layers import Flatten
from keras.layers import Dense

In this stage, we will initialise the CNN.

classifier = Sequential()

Step 1: Create Convolutional Layer

classifier.add(Convolution2D(filters = 32, kernel_size=(3,3), data_format= "channels_last", input_shape=(64, 64, 3), activation="relu"))

Step 2: Create Pooling Layer

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

Step 3: Create Flattening

classifier.add(Flatten())

Step 4: Create Fully Connection

classifier.add(Dense(units = 128, activation = 'relu'))
classifier.add(Dense(units = 1, activation = 'sigmoid'))

Compiling CNN:

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

Fitting the CNN to the images:

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)

Create the training set:

training_set = train_datagen.flow_from_directory('~/DataSet GitHub/CNN/dataset/training_set', target_size=(64, 64), batch_size=32, class_mode='binary')

Create the test set for evaluating our model:

test_set = test_datagen.flow_from_directory(
        '~/DataSet GitHub/CNN/dataset/test_set',
        target_size=(64, 64),
        batch_size=32,
        class_mode='binary')

fit the CNN to the training set:

classifier.fit_generator(
        training_set,
        steps_per_epoch=8000,
        epochs=20,
        validation_data=test_set,
        validation_steps=2000)

As we can see the accuracy of the model for the training set is %97 and for the test set is %76. We can add the more Convolutional Layer to improve the accuracy of the model. In addition, more images will give us better accuracy.

Also Read:  Prediction of Churn For Bank Customers

Let’s visualise the result:

import numpy as np
from keras.preprocessing import image
test_image = image.load_img('~DataSet GitHub/CNN/dataset/test_set/cats/cat.4018.jpg', 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)
#training_set.class_indices
if result[0][0] == 1:
    prediction = 'dog'
    print('/ prediction = dog')
else:
    prediction = 'cat'
    print('prediction = cat')
import numpy as np
from keras.preprocessing import image
test_image = image.load_img('~/DataSet GitHub/CNN/dataset/test_set/dogs/dog.4046.jpg', 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)
#training_set.class_indices
if result[0][0] == 1:
    prediction = 'dog'
    print('prediction = dog')
else:
    prediction = 'cat'
    print('prediction = cat')
2781cookie-checkPrediction of Cats Vs Dogs

Leave a Reply

Your email address will not be published.