# Horse or Human Prediction using Convolutional Neural Network

In the Machine Learning/Data Science/Deep Learning / Computer Vison End to End Project in Python Tutorial in Hindi, we explained each and every step of Machine Learning Project / Data Science Project / Deep Learning Project in detail.

Project name: Horse or Human Prediction using Convolutional Neural Network

What we cover in this Project:

1. What is Convolutional Neural Network?
3. Import Libraries
5. Show Image from Numbers
6. Change Dimension / Feature Scaling
7. Build First Convolutional Neural Network
8. Train Model
9. Test & Evaluate Model
10. Confusion Matrix
11. Classification Report
12. Save Mode
13. Build 2 Complex CNN

• TensorFlow
• Keras
• NumPy
• Pillow
• Matplotlib

## Build, Train and Visualize CNN Model

# -*- coding: utf-8 -*-
"""human_or_horse_prediction.ipynb

Automatically generated by Colaboratory.

Original file is located at

# Project: Horse or Human Classification using CNN
We have data for training:

500 horse images
527 human(male &amp; female) images

For validation:

122 horse images
123 human(male &amp; female) images

## Problem Statement:

Classifie given image is horse or human(male/female)

### Solution:

To solve this problem we are going to use Deep Learning Algorithm that is CNN (Convolutional Neural Network)

## Dara Scource

Raw Data Scource: https://www.kaggle.com/sanikamal/horses-or-humans-dataset

"""

import matplotlib.pyplot as plt

import keras
from keras.preprocessing.image import ImageDataGenerator
from keras.callbacks import ModelCheckpoint
##
from keras.layers.normalization import BatchNormalization
from keras.regularizers import l2

keras.__version__

train_data_path = "/content/drive/My Drive/My ML Project /DL Project/CNN/horse-or-human/dataset/train"
validation_data_path = "/content/drive/My Drive/My ML Project /DL Project/CNN/horse-or-human/dataset/validation"

"""# Data Preprocessing"""

training_datagen = ImageDataGenerator(rescale=1./255,
rotation_range=40,
width_shift_range=0.2,
height_shift_range=0.2,
shear_range=0.2,
zoom_range=0.2,
horizontal_flip=True,
fill_mode='nearest')

training_data =  training_datagen.flow_from_directory(train_data_path,
target_size=(150, 150),
batch_size=32,
class_mode='binary')

training_data.class_indices

valid_datagen = ImageDataGenerator(rescale=1./255)

valid_data =  valid_datagen.flow_from_directory(validation_data_path,
target_size=(150, 150),
batch_size=32,
class_mode='binary')

def plotImages(images_arr):
fig, axes = plt.subplots(1, 5, figsize=(20, 20))
axes = axes.flatten()
for img, ax in zip(images_arr, axes):
ax.imshow(img)
plt.tight_layout()
plt.show()

images = [training_data[0][0][0] for i in range(5)]
plotImages(images)

"""#Building cnn model"""

#Building cnn model
cnn_model = keras.models.Sequential([
keras.layers.Conv2D(filters=32, kernel_size=7, input_shape=[150, 150, 3],kernel_regularizer=l2(l=0.01)),
BatchNormalization(),
keras.layers.MaxPooling2D(pool_size=(2,2)),

keras.layers.Conv2D(filters=64, kernel_size=5),
BatchNormalization(),
keras.layers.MaxPooling2D(pool_size=(2,2)),

keras.layers.Conv2D(filters=128, kernel_size=3),
BatchNormalization(),
keras.layers.MaxPooling2D(pool_size=(2,2)),

keras.layers.Conv2D(filters=256, kernel_size=3),
BatchNormalization(),
keras.layers.MaxPooling2D(pool_size=(2,2)),

keras.layers.Flatten(), # neural network beulding
keras.layers.Dense(units=128, activation='relu'), # input layers
BatchNormalization(),
keras.layers.Dropout(0.5),
keras.layers.Dense(units=256, activation='relu'),
BatchNormalization(),
keras.layers.Dropout(0.5),
keras.layers.Dense(units=2, activation='softmax') # output layer
])

# compile cnn model

model_path = '/content/drive/My Drive/My ML Project /DL Project/CNN/horse-or-human/horse_or_human_predictor.h5'
checkpoint = ModelCheckpoint(model_path, monitor='val_accuracy', verbose=1, save_best_only=True, mode='max')
callbacks_list = [checkpoint]

# train cnn model
history = cnn_model.fit(training_data,
epochs=100,
verbose=1,
validation_data= valid_data,
callbacks=callbacks_list)

# summarize history for accuracy
plt.plot(history.history['accuracy'])
plt.plot(history.history['val_accuracy'])
plt.title('model accuracy')
plt.ylabel('accuracy')
plt.xlabel('epoch')
plt.legend(['train', 'test'], loc='upper left')
plt.show()
# summarize history for loss
plt.plot(history.history['loss'])
plt.plot(history.history['val_loss'])
plt.title('model loss')
plt.ylabel('loss')
plt.xlabel('epoch')
plt.legend(['train', 'test'], loc='upper left')
plt.show()

## Predict Horse or Human

"""
# Predict Horse or Human

Here we are loading train CNN model to predict Given input (image) is Horse of Human

# Import Libraries
"""

import numpy as np

import keras
from keras.preprocessing.image import ImageDataGenerator

"""# Import Model"""

model_path1 = '/content/drive/My Drive/My ML Project /DL Project/CNN/horse-or-human/horse_human_cnn_model_new.h5' #new model.ipynb
model_path2 = '/content/drive/My Drive/My ML Project /DL Project/CNN/horse-or-human/horse_human_cnn_model_v2.h5' #v2 solve overfitting Horse_or_human Classification using CNN.ipynb
model_path3 = '/content/drive/My Drive/My ML Project /DL Project/CNN/horse-or-human/horse_human_cnn_model_v2_1.h5' #v2 solve overfitting Horse_or_human Classification using CNN.ipynb

"""#Preprocessing"""

#horse image path
h1 = '/content/drive/My Drive/My ML Project /DL Project/CNN/horse-or-human/dataset/test_data/horse_test/horse1-204.png'
h2 = '/content/drive/My Drive/My ML Project /DL Project/CNN/horse-or-human/dataset/test_data/horse_test/horse2-069.png'
h3 = '/content/drive/My Drive/My ML Project /DL Project/CNN/horse-or-human/dataset/test_data/horse_test/horse3-070.png'
h4 = '/content/drive/My Drive/My ML Project /DL Project/CNN/horse-or-human/dataset/test_data/horse_test/horse4-439.png'
h5 = '/content/drive/My Drive/My ML Project /DL Project/CNN/horse-or-human/dataset/test_data/horse_test/horse5-203.png'
h6 = '/content/drive/My Drive/My ML Project /DL Project/CNN/horse-or-human/dataset/test_data/horse_test/horse6-161.png'
h7 = '/content/drive/My Drive/My ML Project /DL Project/CNN/horse-or-human/dataset/validation/horses/horse2-224.png'
h8 ="/content/drive/My Drive/My ML Project /DL Project/CNN/horse-or-human/dataset/validation/horses/horse5-123.png"

#human image path
hu1 = '/content/drive/My Drive/My ML Project /DL Project/CNN/horse-or-human/dataset/test_data/human_test/valhuman01-09.png'
hu2 = '/content/drive/My Drive/My ML Project /DL Project/CNN/horse-or-human/dataset/test_data/human_test/valhuman02-18.png'
hu3 = '/content/drive/My Drive/My ML Project /DL Project/CNN/horse-or-human/dataset/test_data/human_test/valhuman03-23.png'
hu4 = '/content/drive/My Drive/My ML Project /DL Project/CNN/horse-or-human/dataset/test_data/human_test/valhuman04-24.png'
hu5 = '/content/drive/My Drive/My ML Project /DL Project/CNN/horse-or-human/dataset/test_data/human_test/valhuman05-19.png'
hu6 = '/content/drive/My Drive/My ML Project /DL Project/CNN/horse-or-human/dataset/validation/humans/valhuman01-13.png'

import numpy as np
from keras.preprocessing import image

def pred_human_horse(model, horse_or_human):
test_image = image.load_img(horse_or_human, target_size = (150, 150))
test_image = image.img_to_array(test_image)/255
test_image = np.expand_dims(test_image, axis = 0)

result = model.predict(test_image).round(3)

pred = np.argmax(result)
print(result, "--->>>", pred)

if pred == 0:
print('Predicted>>> Horse')
else:
print('Predicted>>> Human')

"""## Predict Output"""

for horse_or_human in [h1,h2,h3,h4,h5,h6,h7,h8, hu1,hu2,hu3,hu4,hu5,hu6]:
pred_human_horse(model1, horse_or_human)

for horse_or_human in [h1,h2,h3,h4,h5,h6,h7,h8, hu1,hu2,hu3,hu4,hu5,hu6]:
pred_human_horse(model2, horse_or_human)

for horse_or_human in [h1,h2,h3,h4,h5,h6,h7,h8, hu1,hu2,hu3,hu4,hu5,hu6]:
pred_human_horse(model3, horse_or_human)

## app.py

#Import necessary libraries

import numpy as np
import os

from keras.preprocessing.image import img_to_array

def pred_human_horse(horse_or_human):
print("@@ Got Image for prediction")

test_image = img_to_array(test_image)/255 # convert image to np array and normalize
test_image = np.expand_dims(test_image, axis = 0) # change dimention 3D to 4D

result = model.predict(test_image).round(3) # predict class horse or human
print('@@ Raw result = ', result)

pred = np.argmax(result) # get the index of max value

if pred == 0:
return "Horse" # if index 0
else:
return "Human" # if index 1

#------------>>pred_human_horse<<--end

# render index.html page
@app.route("/", methods=['GET', 'POST'])
def home():
return render_template('index.html')

@app.route("/predict", methods = ['GET','POST'])
def predict():
if request.method == 'POST':
file = request.files['image'] # fet input
filename = file.filename
print("@@ Input posted = ", filename)

file.save(file_path)

print("@@ Predicting class......")
pred = pred_human_horse(horse_or_human=file_path)

return render_template('predict.html', pred_output = pred, user_image = file_path)

#Fo local system
if __name__ == "__main__":

# #Fo AWS cloud
# if __name__ == "__main__":

## index.html

<html>

<title>Predict Human or Horse ML App</title>
<body>

<!-- Show  Banner-->
<div>
<img src="static/images/horse or human banner.png" class="w3-border w3-padding" alt="Indian AI Production" style="width:100%">
</div>

<form action="/predict" method="post" enctype="multipart/form-data" style="margin-top: 50px; width: 60%; text-align: center; margin: auto;" onsubmit="showloading()">
<span class="text-left btn btn-default btn-file">
</span>

<span class="text-right">
<input type="submit" value="Predict" class="btn btn-primary">
</span>
</form>

</body>
</html>

## predict.html

<html>

<!-- Required meta tags -->
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1, shrink-to-fit=no">

<title>Predict Human or Horse ML App</title>
<body>

<!-- Show  Banner-->
<div>
<img src="static/images/horse or human banner.png" class="w3-border w3-padding" alt="Indian AI Production" style="width:100%">
</div>

<div class="container">
<div class="row">

<div class="col-sm">
<span class="border border-primary">
<img src="{{ user_image }}" alt="User Image" class="img-thumbnail">
</span>
</div>

<div class="col-sm">