Human And Horse Prediction Using CNN
Building a Convolutional neural network model to predict human and horse.
Aug 2 ·5min read
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.
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.
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)
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.
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.
以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持 码农网
猜你喜欢:本站部分资源来源于网络,本站转载出于传递更多信息之目的,版权归原作者或者来源机构所有,如转载稿涉及版权问题,请联系我们。
Effective Java
Joshua Bloch / Addison-Wesley Professional / 2018-1-6 / USD 54.99
The Definitive Guide to Java Platform Best Practices—Updated for Java 9 Java has changed dramatically since the previous edition of Effective Java was published shortly after the release of Jav......一起来看看 《Effective Java》 这本书的介绍吧!