HOME ABOUT ME BLOGS CONTACT YOUTUBE Login Register

CNN Based Object Detection

Jan. 1, 2019, 7:19 p.m.

The implementation of the CNN in the proposed model comprised of the following set of steps. • Preparation of the Dataset • Development of the CNN model • Training and Evaluation of the Built CNN model on the Dataset prepared • Deploying and Testing the CNN model on the Raspberry Pi

Preparing Dataset: Most important part of CNN based object detection model is dataset. Dataset helps the CNN model to to learn the object to identify an unseen object. Our dataset is comprised od five different image classes. Each image class consist of 1000 images. Total 5000 images in the dataset. Each image of the above dataset is the image taken by a logitech C270 camera in the resolution of the 640x480. These images are then resized to 48x48 to feed to the CNN model during training and evaluation. Fig. 1: A Five Class Image Dataset 2) Development of the CNN model: After developing the dataset, next step in implementation of the CNN in our proposed architecture was to develop a CNN model to be trained to detect the object. Taking into consideration the computational capacity of the raspberry pi, we developed a three layered CNN model with one dense and a classifier layer using the python Keras development environment as shown in the fig.3 below.

Nature

The dataset developed in A.1 is then fed to the CNN model for training and Evaluation of the model. Dataset is divided into training and evaluation data in the ratio of 7/3, 70% training data and 30% evaluation data set. The model developed above is trained and evaluated for 100 epochs on the data set prepared above. As the model is trained for large number of time the model produces less error in predicting the object and its accuracy increases. Variation of loss and accuracy with respect to the epochs are shown in the result section below.

Nature

3) Taining and Evaluation: The model is trained with all available data set for 100 number of epochs the trained model is then save in a binary format for the purpose of deploying it into the raspberry pi. Raspberry pi has less computational capability so it can not be used for simultaneous training and testing and exploiting its learning for the use case. Therefore it is best practice to develop a CNN model on a high computational capacity system update the dataset to be fed into the model for getting trained for some new input data and later model deployed to the small computational capability devices can be updated for any change in the data set.

Simple Python code for building dataset for training a CNN model.

                        #DATASET BUILD


import cv2
import matplotlib.pyplot as plt
%matplotlib tk
import numpy as np
import os
import time

cap = cv2.VideoCapture(0)
DIR = r'C:\Users\ayama\Desktop\PROJECT\5. Keras\DATASET1\set2'

while True:
    i= 0
    x = input("Enter the image set ")
    while i<1000:
        ret, frame = cap.read()
        gray= cv2.cvtColor(frame, cv2.COLOR_BGR2GRAY)
        cv2.imwrite(os.path.join(DIR, "eric"+str('.')+str(x)+str('.')+str(i)+"."+"jpg"), frame)
        i=i+1
        cv2.imshow('Window', frame)
    if x==3:
        cap.release()
        cv2.destroyAllWindows()
        break
                    

Code For Building a CNN Model and Training it with the above dataset.

                        # Building and Training CNN Model

import matplotlib
matplotlib.use("Agg")
 
# import the necessary packages
from keras.models import Sequential
from keras.layers.normalization import BatchNormalization
from keras.layers.convolutional import Conv2D
from keras.layers.convolutional import MaxPooling2D
from keras.layers.core import Activation
from keras.layers.core import Flatten
from keras.layers.core import Dropout
from keras.layers.core import Dense
from keras import backend as K
from keras.preprocessing.image import ImageDataGenerator
from keras.optimizers import Adam
from keras.preprocessing.image import img_to_array
from sklearn.preprocessing import LabelBinarizer
from sklearn.model_selection import train_test_split

import matplotlib.pyplot as plt
from imutils import paths
import numpy as np
import argparse
import random
import pickle
import cv2
import os


EPOCHS = 5
INIT_LR = 1e-3
BS = 32
IMAGE_DIMS = (48,48,3)


data = np.load('data1.npy')
labels = np.load('label1.npy')


lb = LabelBinarizer()
labels = lb.fit_transform(labels)

(trainX, testX, trainY, testY) = train_test_split(data,labels, test_size=0.2, random_state=42)

aug = ImageDataGenerator(rotation_range=25, width_shift_range=0.1,
	height_shift_range=0.1, shear_range=0.2, zoom_range=0.2,
	horizontal_flip=True, fill_mode="nearest")

class SmallerVGGNet:
	@staticmethod
	def build(width, height, depth, classes):
		# initialize the model along with the input shape to be
		# "channels last" and the channels dimension itself
		model = Sequential()
		inputShape = (height, width, depth)
		chanDim = -1
 
		# if we are using "channels first", update the input shape
		# and channels dimension
		if K.image_data_format() == "channels_first":
			inputShape = (depth, height, width)
			chanDim = 1
		# CONV => RELU => POOL
		model.add(Conv2D(32, (3, 3), padding="same",
			input_shape=inputShape))
		model.add(Activation("relu"))
		model.add(BatchNormalization(axis=chanDim))
		model.add(MaxPooling2D(pool_size=(3, 3)))
		model.add(Dropout(0.25))
		# (CONV => RELU) * 2 => POOL
		model.add(Conv2D(64, (3, 3), padding="same"))
		model.add(Activation("relu"))
		model.add(BatchNormalization(axis=chanDim))
		model.add(Conv2D(64, (3, 3), padding="same"))
		model.add(Activation("relu"))
		model.add(BatchNormalization(axis=chanDim))
		model.add(MaxPooling2D(pool_size=(2, 2)))
		model.add(Dropout(0.25))
 		# (CONV => RELU) * 2 => POOL
		model.add(Conv2D(128, (3, 3), padding="same"))
		model.add(Activation("relu"))
		model.add(BatchNormalization(axis=chanDim))
		model.add(Conv2D(128, (3, 3), padding="same"))
		model.add(Activation("relu"))
		model.add(BatchNormalization(axis=chanDim))
		model.add(MaxPooling2D(pool_size=(2, 2)))
		model.add(Dropout(0.25))
		# first (and only) set of FC => RELU layers
		model.add(Flatten())
		model.add(Dense(1024))
		model.add(Activation("relu"))
		model.add(BatchNormalization())
		model.add(Dropout(0.5))
 
		# softmax classifier
		model.add(Dense(classes))
		model.add(Activation("softmax"))
 
		# return the constructed network architecture
		return model
    

print("[INFO] compiling model...")
model = SmallerVGGNet.build(width=IMAGE_DIMS[1], height=IMAGE_DIMS[0],
	depth=IMAGE_DIMS[2], classes=len(lb.classes_))
opt = Adam(lr=INIT_LR, decay=INIT_LR / EPOCHS)
model.compile(loss="categorical_crossentropy", optimizer=opt,
	metrics=["accuracy"])
 
# train the network
print("[INFO] training network...")
H = model.fit_generator(
	aug.flow(trainX, trainY, batch_size=BS),
	validation_data=(testX, testY),
	steps_per_epoch=len(trainX) // BS,
	epochs=EPOCHS, verbose=1)


ap = argparse.ArgumentParser()
ap.add_argument("-d", "--dataset", required=True,
	help="path to input dataset (i.e., directory of images)")
ap.add_argument("-m", "--model", required=True,
	help="path to output model")
ap.add_argument("-l", "--labelbin", required=True,
	help="path to output label binarizer")
ap.add_argument("-p", "--plot", type=str, default="plot.png",
	help="path to output accuracy/loss plot")
args = vars(ap.parse_args())


model.save("model")


# plot the training loss and accuracy
plt.style.use("ggplot")
plt.figure()
N = EPOCHS
plt.plot(np.arange(0, N), H.history["loss"], label="train_loss")
plt.plot(np.arange(0, N), H.history["val_loss"], label="val_loss")
plt.plot(np.arange(0, N), H.history["acc"], label="train_acc")
plt.plot(np.arange(0, N), H.history["val_acc"], label="val_acc")
plt.title("Training Loss and Accuracy")
plt.xlabel("Epoch #")
plt.ylabel("Loss/Accuracy")
plt.legend(loc="upper left")
plt.savefig("plot1")
                    

TECHNOLOGIES THAT'S GOING TO CHANGE THE WORLD!

New York

Embedded Systems

Dec. 30, 2018, 11:18 a.m.

New York

Internet of Things

Dec. 30, 2018, 11:19 a.m.

New York

ML in Computer Vision

Dec. 30, 2018, 11:20 a.m.

×

Tickets

Need help?


CONTACT

Want to convey anything? Drop a Message.

Mumbai, India
Phone: +91 9555551613
Email: ayamamit@gmail.com

.iframeclass{ position: absolute; top: 0; width: 100%; } .iframecontainer{ position: relative; width: 100%; height: auto; padding-top: 61%; }