Human And Horse Prediction Using CNN

栏目: IT技术 · 发布时间: 5年前

Human And Horse Prediction Using CNN

Building a Convolutional neural network model to predict human and horse.

Human And Horse Prediction Using CNN

Aug 2 ·5min read

Human And Horse Prediction Using CNN

Photo by Brandon Hoogenboom on Unsplash

There are many transfer learning methods to solve classification problems with respect to the accuracy, i.e 1.VGG (e.g. VGG16 or VGG19) 2.GoogLeNet (e.g. InceptionV3).3.Residual Network (e.g. ResNet50).These are some pre-trained models, but here we are going to build an end to end model as per the convolutional neural networks architecture.

Convolutional Neural Network

There are many definitions to describe CNN but in simple terms, convolutional refers to the mathematical combination of two functions to produce a third function. It merges two sets of information. in the case of a CNN is used to analyze visual imaginary. They are also known as shift invariant or space invariant artificial neural networks, based on their shared-weights architecture and translation invariance characteristics.

Human And Horse Prediction Using CNN

Source

Layers in CNN

There are three layers in CNN, convolutional layer , pooling layer , and fully connected layer . Each of these layers has different parameters that can be optimized and performs a different task on the input data.

Human And Horse Prediction Using CNN

Source

Pooling Layer

The pooling layer is a building block on CNN. its function progressively reduces the spatial size of the presentation to reduce the number of parameters and computation in network. The pooling layer operates on each feature map independently. max-pooling is the most common approach used in the convolutional neural networks.

Dataset

We have data for training:

500 horse images and 527 humans (male & Female) images

For Validation:

122 Horse Images and 123 Human(male & Female) images

Dataset link

Implementation

Importing necessary libraries and packages.

import keras
from keras.preprocessing.image import ImageDataGenerator
import matplotlib.pyplot as plt

Data Load

train_data = "train/"
validation_data = "validation/"

Here we have train and validation datasets, we will use train data for training and validation data to prevent overfitting problem. also here we have a small size of the dataset as we before building the CNN model the data should have huge images to make the most accurate model. so we will generate more images using image augmentation.

Data Preprocessing

#For tarining
train_data_gen = 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')

Generating more training data for model training using image data generator parameters like rotation and width, height, zoom range.

train_data1 = train_data_gen.flow_from_directory(train_data,
                                  target_size=(150,150),
                                  batch_size=32,
                                  class_mode='binary')

Let’s Check the image classes

train_data1.class_indices

The classes are divided as {‘horses’: 0, ‘humans’: 1}.

Now Generating for validation.

validation_data_gen = ImageDataGenerator(rescale=1./255)
validation_data1 = validation_data_gen.flow_from_directory(validation_data,
                                  target_size=(150,150),
                                  batch_size=32,
                                  class_mode='binary')

Now plotting the generated images.

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

Let’s plots some training image datasets.

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

Human And Horse Prediction Using CNN

Augmented Image

Building CNN

We are using Keras here so it makes code much simpler form because it uses TensorFlow API. so let’s start to build a model step by step.

cnn_model = keras.models.Sequential([
keras.layers.Conv2D(filters=32, kernel_size=3, input_shape=[150,150,3]),
keras.layers.MaxPooling2D(pool_size=(2,3)),
keras.layers.Conv2D(filters=64, kernel_size=3),
keras.layers.MaxPooling2D(pool_size=(2,2)),
keras.layers.Conv2D(filters=128, kernel_size=3),
keras.layers.MaxPooling2D(pool_size=(2,2)),
keras.layers.Conv2D(filters=256, kernel_size=3),
keras.layers.MaxPooling2D(pool_size=(2,2)),

keras.layers.Dropout(0.5),
keras.layers.Flatten(), #after this we will go for neural network building
keras.layers.Dense(units=128, activation='relu'), #inputlayers
keras.layers.Dropout(0.1),
keras.layers.Dense(units=256, activation='relu'), #Hidden layer
keras.layers.Dropout(0.25),
keras.layers.Dense(units=2, activation='softmax') #output layer with 2 neurons

])

Basically we took 128 for input and 256 neurons for hidden, and we used relu and softmax activation functions, with respect to classes we used softmax activation function. Now let’s compile the model.

from keras.optimizers import Adam
from keras.callbacks import ModelCheckpoint

Let’s set the optimizer and loss function now.

cnn_model.compile(optimizer=Adam(lr=0.0001), loss='sparse_categorical_crossentropy', metrics=['accuracy'])

Before going to build a model we can set a path to save our model, so in my case, I have set my local systems path.

model_path='human_horse_predict.h5'checkpoint = ModelCheckpoint(model_path, monitor='val_accuracy', verbose=1, save_best_only=True, mode='max')
callbacks_list = [checkpoint]

Model Train

history = cnn_model.fit(train_data1,
                       epochs=100,
                       verbose=1,
                       validation_data=validation_data1,
                       callbacks=callbacks_list)

I have trained this model by using Cuda and it took less time for training, better you can train using Google COLAB.

Now the model is trained, let’s see the summary of the model.

#Summarize history for accuracy
plt.plot(history.history['accuracy'])
plt.plot(history.history['val_accuracy'])
plt.title('Model Loss')
plt.ylabel('accuracy')
plt.xlabel('epoch')
plt.legend(['train', 'test'], loc='upper left')
plt.show()

Let’s look at our model accuracy on train and test data.

Human And Horse Prediction Using CNN

Plot

You can see how our model is overfitted as per epoch. Let’s test the model.

Model Test

model1 = keras.models.load_model(model_path)

Give the saved model path.

Let’s take some human and horse images and assing in some variables.

##Horse images

h1 = 'train/horses/horse01-1.png'
h2 = 'train/horses/horse01-2.png'
h3 = 'train/horses/horse01-5.png'

#human images
hu1 = 'train/humans/human01-02.png'
hu2 = 'train/humans/human01-05.png'
hu3 = 'train/humans/human01-08.png

Creating one function which will prepress our image in an array format.

import numpy as np
from keras.preprocessing import image

Defining function.

def pred_human_horse(model, horse_or_human):
test_image = image.load_img(horse_or_human, target_size = (155,155))
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)

if pred==0:
print("Horse")
else:
print("Human")

Prediction

For using three images we are going to predict.

for horse_or_human in [h1,h2,h3]:
pred_human_horse(model1, horse_or_human)
Horse
Horse
Horse

So, we got a good prediction here.


以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持 码农网

查看所有标签

猜你喜欢:

本站部分资源来源于网络,本站转载出于传递更多信息之目的,版权归原作者或者来源机构所有,如转载稿涉及版权问题,请联系我们

编程之美

编程之美

《编程之美》小组 编 / 电子工业出版社 / 2008-3 / 40.00元

这本书收集了约60道算法和程序设计题目,这些题目大部分在近年的笔试、面试中出现过,或者是被微软员工热烈讨论过。作者试图从书中各种有趣的问题出发,引导读者发现问题,分析问题,解决问题,寻找更优的解法。本书的内容分为下面几个部分: (1)游戏之乐:从游戏和其他有趣问题出发,化繁为简,分析总结。 (2)数字之魅:编程的过程实际上就是和数字及字符打交道的过程。这一部分收集了一些好玩的对数字进行......一起来看看 《编程之美》 这本书的介绍吧!

图片转BASE64编码
图片转BASE64编码

在线图片转Base64编码工具

Markdown 在线编辑器
Markdown 在线编辑器

Markdown 在线编辑器

HSV CMYK 转换工具
HSV CMYK 转换工具

HSV CMYK互换工具