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:
- What is Convolutional Neural Network?
- What is Google Colab?
- Import Libraries
- Load Data
- Show Image from Numbers
- Change Dimension / Feature Scaling
- Build First Convolutional Neural Network
- Train Model
- Test & Evaluate Model
- Confusion Matrix
- Classification Report
- Save Mode
- Build 2 Complex CNN
Software For This Project
- TensorFlow
- Keras
- NumPy
- Flask
- 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
https://colab.research.google.com/drive/1zTI5Ete5C1QdVvMx5IAquUoXgZhHVf5a
# Project: Horse or Human Classification using CNN
We have data for training:
500 horse images
527 human(male & female) images
For validation:
122 horse images
123 human(male & 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
## Load Libraries
"""
import matplotlib.pyplot as plt
import keras
from keras.preprocessing.image import ImageDataGenerator
from keras.optimizers import Adam
from keras.callbacks import ModelCheckpoint
##
from keras.layers.normalization import BatchNormalization
from keras.regularizers import l2
keras.__version__
"""## Load Data"""
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
cnn_model.compile(optimizer = Adam(lr=0.0001), loss='sparse_categorical_crossentropy', metrics=['accuracy'])
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
"""
# Load Liraries
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
model1 = keras.models.load_model(model_path1)
model2 = keras.models.load_model(model_path2)
model3 = keras.models.load_model(model_path3)
"""#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)
Model Deployment Code on AWS
app.py
#Import necessary libraries
from flask import Flask, render_template, request
import numpy as np
import os
from keras.preprocessing.image import load_img
from keras.preprocessing.image import img_to_array
from keras.models import load_model
#load model
model =load_model("horse_human_cnn_model_v2_1.h5")
print('@@ Model loaded')
def pred_human_horse(horse_or_human):
test_image = load_img(horse_or_human, target_size = (150, 150)) # load image
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
# Create flask instance
app = Flask(__name__)
# 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_path = os.path.join('static/user uploaded', 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__":
app.run(threaded=False,)
# #Fo AWS cloud
# if __name__ == "__main__":
# app.run(host='0.0.0.0.0', post='8080',threaded=False,)
Frontend Code (HTML) Files
index.html
<html>
<head>
<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/4.0.0/css/bootstrap.min.css" integrity="sha384-Gn5384xqQ1aoWXA+058RXPxPg6fy4IWvTNh0E263XmFcJlSAwiGgFAW/dAiS6JXm" crossorigin="anonymous">
<title>Predict Human or Horse ML App</title>
</head>
<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">
Upload Image <input type="file" name="image">
</span>
<span class="text-right">
<input type="submit" value="Predict" class="btn btn-primary">
</span>
</form>
</body>
</html>
predict.html
<html>
<head>
<!-- Required meta tags -->
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1, shrink-to-fit=no">
<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/4.0.0/css/bootstrap.min.css" integrity="sha384-Gn5384xqQ1aoWXA+058RXPxPg6fy4IWvTNh0E263XmFcJlSAwiGgFAW/dAiS6JXm" crossorigin="anonymous">
<title>Predict Human or Horse ML App</title>
</head>
<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">
<h1>It's <span class="badge badge-secondary">{{pred_output}}</span></h1>
</div>
</div>
</div>
</body>
</html>
Hello Sir it was great experience of learning from you.
Thanks for making such great tutorials.
I need your help in final step, My program is not running it gives me fowling massages
please if you are free read it and give me your guidance
runfile(‘C:/Users/rjzam/.spyder-py3/Horse Or Human/app.py’, wdir=’C:/Users/rjzam/.spyder-py3/Horse Or Human’)
Using TensorFlow backend.
C:\Users\rjzam\.conda\envs\tensorflow\lib\site-packages\tensorflow\python\framework\dtypes.py:516: FutureWarning: Passing (type, 1) or ‘1type’ as a synonym of type is deprecated; in a future version of numpy, it will be understood as (type, (1,)) / ‘(1,)type’.
_np_qint8 = np.dtype([(“qint8”, np.int8, 1)])
C:\Users\rjzam\.conda\envs\tensorflow\lib\site-packages\tensorflow\python\framework\dtypes.py:517: FutureWarning: Passing (type, 1) or ‘1type’ as a synonym of type is deprecated; in a future version of numpy, it will be understood as (type, (1,)) / ‘(1,)type’.
_np_quint8 = np.dtype([(“quint8”, np.uint8, 1)])
C:\Users\rjzam\.conda\envs\tensorflow\lib\site-packages\tensorflow\python\framework\dtypes.py:518: FutureWarning: Passing (type, 1) or ‘1type’ as a synonym of type is deprecated; in a future version of numpy, it will be understood as (type, (1,)) / ‘(1,)type’.
_np_qint16 = np.dtype([(“qint16”, np.int16, 1)])
C:\Users\rjzam\.conda\envs\tensorflow\lib\site-packages\tensorflow\python\framework\dtypes.py:519: FutureWarning: Passing (type, 1) or ‘1type’ as a synonym of type is deprecated; in a future version of numpy, it will be understood as (type, (1,)) / ‘(1,)type’.
_np_quint16 = np.dtype([(“quint16”, np.uint16, 1)])
C:\Users\rjzam\.conda\envs\tensorflow\lib\site-packages\tensorflow\python\framework\dtypes.py:520: FutureWarning: Passing (type, 1) or ‘1type’ as a synonym of type is deprecated; in a future version of numpy, it will be understood as (type, (1,)) / ‘(1,)type’.
_np_qint32 = np.dtype([(“qint32”, np.int32, 1)])
C:\Users\rjzam\.conda\envs\tensorflow\lib\site-packages\tensorflow\python\framework\dtypes.py:525: FutureWarning: Passing (type, 1) or ‘1type’ as a synonym of type is deprecated; in a future version of numpy, it will be understood as (type, (1,)) / ‘(1,)type’.
np_resource = np.dtype([(“resource”, np.ubyte, 1)])
C:\Users\rjzam\.conda\envs\tensorflow\lib\site-packages\tensorboard\compat\tensorflow_stub\dtypes.py:541: FutureWarning: Passing (type, 1) or ‘1type’ as a synonym of type is deprecated; in a future version of numpy, it will be understood as (type, (1,)) / ‘(1,)type’.
_np_qint8 = np.dtype([(“qint8”, np.int8, 1)])
C:\Users\rjzam\.conda\envs\tensorflow\lib\site-packages\tensorboard\compat\tensorflow_stub\dtypes.py:542: FutureWarning: Passing (type, 1) or ‘1type’ as a synonym of type is deprecated; in a future version of numpy, it will be understood as (type, (1,)) / ‘(1,)type’.
_np_quint8 = np.dtype([(“quint8”, np.uint8, 1)])
C:\Users\rjzam\.conda\envs\tensorflow\lib\site-packages\tensorboard\compat\tensorflow_stub\dtypes.py:543: FutureWarning: Passing (type, 1) or ‘1type’ as a synonym of type is deprecated; in a future version of numpy, it will be understood as (type, (1,)) / ‘(1,)type’.
_np_qint16 = np.dtype([(“qint16”, np.int16, 1)])
C:\Users\rjzam\.conda\envs\tensorflow\lib\site-packages\tensorboard\compat\tensorflow_stub\dtypes.py:544: FutureWarning: Passing (type, 1) or ‘1type’ as a synonym of type is deprecated; in a future version of numpy, it will be understood as (type, (1,)) / ‘(1,)type’.
_np_quint16 = np.dtype([(“quint16”, np.uint16, 1)])
C:\Users\rjzam\.conda\envs\tensorflow\lib\site-packages\tensorboard\compat\tensorflow_stub\dtypes.py:545: FutureWarning: Passing (type, 1) or ‘1type’ as a synonym of type is deprecated; in a future version of numpy, it will be understood as (type, (1,)) / ‘(1,)type’.
_np_qint32 = np.dtype([(“qint32”, np.int32, 1)])
C:\Users\rjzam\.conda\envs\tensorflow\lib\site-packages\tensorboard\compat\tensorflow_stub\dtypes.py:550: FutureWarning: Passing (type, 1) or ‘1type’ as a synonym of type is deprecated; in a future version of numpy, it will be understood as (type, (1,)) / ‘(1,)type’.
np_resource = np.dtype([(“resource”, np.ubyte, 1)])
Traceback (most recent call last):
File “C:\Users\rjzam\.spyder-py3\Horse Or Human\app.py”, line 13, in
model =keras.models.load_model(“human_or_horse_prediction.h5”)
File “C:\Users\rjzam\.conda\envs\tensorflow\lib\site-packages\keras\engine\saving.py”, line 492, in load_wrapper
return load_function(*args, **kwargs)
File “C:\Users\rjzam\.conda\envs\tensorflow\lib\site-packages\keras\engine\saving.py”, line 584, in load_model
model = _deserialize_model(h5dict, custom_objects, compile)
File “C:\Users\rjzam\.conda\envs\tensorflow\lib\site-packages\keras\engine\saving.py”, line 274, in _deserialize_model
model = model_from_config(model_config, custom_objects=custom_objects)
File “C:\Users\rjzam\.conda\envs\tensorflow\lib\site-packages\keras\engine\saving.py”, line 627, in model_from_config
return deserialize(config, custom_objects=custom_objects)
File “C:\Users\rjzam\.conda\envs\tensorflow\lib\site-packages\keras\layers\__init__.py”, line 168, in deserialize
printable_module_name=’layer’)
File “C:\Users\rjzam\.conda\envs\tensorflow\lib\site-packages\keras\utils\generic_utils.py”, line 147, in deserialize_keras_object
list(custom_objects.items())))
File “C:\Users\rjzam\.conda\envs\tensorflow\lib\site-packages\keras\engine\sequential.py”, line 301, in from_config
custom_objects=custom_objects)
File “C:\Users\rjzam\.conda\envs\tensorflow\lib\site-packages\keras\layers\__init__.py”, line 168, in deserialize
printable_module_name=’layer’)
File “C:\Users\rjzam\.conda\envs\tensorflow\lib\site-packages\keras\utils\generic_utils.py”, line 149, in deserialize_keras_object
return cls.from_config(config[‘config’])
File “C:\Users\rjzam\.conda\envs\tensorflow\lib\site-packages\keras\engine\base_layer.py”, line 1179, in from_config
return cls(**config)
File “C:\Users\rjzam\.conda\envs\tensorflow\lib\site-packages\keras\legacy\interfaces.py”, line 91, in wrapper
return func(*args, **kwargs)
TypeError: __init__() got an unexpected keyword argument ‘ragged’
I am not able to understand it Please help
Regards
RJ Zambare.
Try using virtual environment i have same problem it resolved
How we can check this model with confusion matrix. Please help me