Prediction of Aliens Vs. Predators

In this project, we will have some images of aliens and predators and by them, we will train a Convolutional Neural Network using Keras to predict if the image is an alien or predator.

The Data contains the following folders: 

  • train: 247 aliens and 247 predators
  • validation: 100 aliens and 100 predators

.

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)))

Adding a second convolutional layer to improve the accuracy

# Adding a second convolutional layer
classifier.add(Convolution2D(32, (3, 3), activation = 'relu'))
classifier.add(MaxPooling2D(pool_size = (2, 2)))

Adding a third convolutional layer

classifier.add(Convolution2D(32, (3, 3), activation = 'relu'))
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('/kaggle/input/alien-vs-predator-images/alien_vs_predator_thumbnails/data/train', 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(
        '/kaggle/input/alien-vs-predator-images/alien_vs_predator_thumbnails/data/validation',
        target_size=(64, 64),
        batch_size=32,
        class_mode='binary')

fit the CNN to the training set and evaluate our test set

classifier.fit_generator(
        training_set,
        steps_per_epoch=694,
        epochs=20,
        validation_data=test_set,
        validation_steps=200)

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

Also Read:  Prediction of Cats Vs Dogs

Let’s visualise the result:

from matplotlib import pyplot as plt
import cv2
S = 64

directory = os.listdir("/kaggle/input/alien-vs-predator-images/alien_vs_predator_thumbnails/data/validation/alien")
print(directory[3])

imgAlien = cv2.imread("/kaggle/input/alien-vs-predator-images/alien_vs_predator_thumbnails/data/validation/alien/" + directory[3])
plt.imshow(imgAlien)

imgAlien = cv2.resize(imgAlien, (S,S))
imgAlien = imgAlien.reshape(1,S,S,3)

pred = classifier.predict(imgAlien)
print("Probability that it is a alien = ", "%.2f" % (1-pred))
from matplotlib import pyplot as plt
import cv2
S = 64

directory = os.listdir("/kaggle/input/alien-vs-predator-images/alien_vs_predator_thumbnails/data/validation/predator")
print(directory[20])

imgAlien = cv2.imread("/kaggle/input/alien-vs-predator-images/alien_vs_predator_thumbnails/data/validation/predator/" + directory[20])
plt.imshow(imgAlien)

imgAlien = cv2.resize(imgAlien, (S,S))
imgAlien = imgAlien.reshape(1,S,S,3)

pred = classifier.predict(imgAlien)
print("Probability that it is a alien = ", "%.2f" % (1-pred))

2922cookie-checkPrediction of Aliens Vs. Predators

Leave a Reply

Your email address will not be published. Required fields are marked *