Pedestrian_Detection using CNN¶
Credit: AITS Cainvas Community
Photo by Antonius Setiadi K on Dribbble
Downloading dataset¶
In [1]:
!wget -N "https://cainvas-static.s3.amazonaws.com/media/user_data/cainvas-admin/PedestrianDataset.zip"
Extracting the dataset and removing unnecessary zip file¶
In [2]:
!unzip -qo "PedestrianDataset.zip"
!rm "PedestrianDataset.zip"
Importing relevant libraries¶
In [3]:
import numpy as np
import pandas as pd
import cv2
import os
from xml.etree import ElementTree
from matplotlib import pyplot as plt
In [4]:
import tensorflow as tf
from sklearn.metrics import confusion_matrix
from tensorflow.keras import datasets, layers, models
keras = tf.keras
In [5]:
class_names = ['person','person-like']
class_names_label = {class_name:i for i, class_name in enumerate(class_names)}
n_classes = 2
size = (120,120)
In [ ]:
Defining a function to load the dataset¶
In [6]:
def load_data():
datasets = ['Pedestrian_Detection/Train/Train', 'Pedestrian_Detection/Test/Test', 'Pedestrian_Detection/Val/Val']
output = []
for dataset in datasets:
imags = []
labels = []
directoryA = dataset +"/Annotations"
directoryIMG = dataset +"/JPEGImages/"
file = os.listdir(directoryA)
img = os.listdir(directoryIMG)
file.sort()
img.sort()
i = 0
for xml in file:
xmlf = os.path.join(directoryA,xml)
dom = ElementTree.parse(xmlf)
vb = dom.findall('object')
label = vb[0].find('name').text
labels.append(class_names_label[label])
img_path = directoryIMG + img[i]
curr_img = cv2.imread(img_path)
curr_img = cv2.resize(curr_img, size)
imags.append(curr_img)
i +=1
imags = np.array(imags, dtype='float32')
imags = imags / 255
labels = np.array(labels, dtype='int32')
output.append((imags, labels))
return output
In [7]:
#import random, datetime, os, shutil, math
#shutil.rmtree("Pedestrian-Detection/Test/Test/Annotations/.ipynb_checkpoints")
#shutil.rmtree("Pedestrian-Detection/Val/Val/Annotations/.ipynb_checkpoints")
#shutil.rmtree("Pedestrian-Detection/Train/Train/Annotations/.ipynb_checkpoints")
In [8]:
(train_images, train_labels),(test_images, test_labels),(val_images, val_labels) = load_data()
Checking shapes¶
In [9]:
train_images.shape
Out[9]:
Reviewing image samples from a random directory¶
In [10]:
plt.figure(figsize=(20,20))
for n , i in enumerate(list(np.random.randint(0,len(train_images),36))) :
plt.subplot(6,6,n+1)
plt.imshow(train_images[i])
plt.title(class_names[train_labels[i]])
plt.axis('off')
Defining the model¶
In [11]:
model = models.Sequential()
model.add(layers.Conv2D(4, (5, 5), activation='relu', input_shape=(120, 120, 3)))
model.add(layers.MaxPooling2D((2, 2)))
model.add(layers.Conv2D(12, (2, 2), activation='relu'))
model.add(layers.MaxPooling2D((2, 2)))
model.add(layers.Flatten())
model.add(layers.Dense(16, activation='relu'))
model.add(layers.Dense(2))
model.summary()
#model.add(layers.Conv2D(64, (3, 3), activation='relu'))
Compiling the model¶
In [12]:
from numpy.random import seed
seed(1)
In [13]:
model.compile(optimizer='adam',
loss=tf.keras.losses.SparseCategoricalCrossentropy(from_logits=True),
metrics=['accuracy'])
Importing and using callbacks¶
In [14]:
from tensorflow.keras.callbacks import ModelCheckpoint, EarlyStopping
from tensorflow.keras.callbacks import ReduceLROnPlateau
es = EarlyStopping(monitor='accuracy', mode='max', verbose=1, patience=2)
filepath = "modelPedestrianDetection.h5"
ckpt = ModelCheckpoint(filepath, monitor='accuracy', verbose=1, save_best_only=True, mode='max')
rlp = ReduceLROnPlateau(monitor='accuracy', patience=2, verbose=1)
Using fit()¶
In [15]:
history = model.fit(train_images, train_labels, epochs=60,
validation_data=(test_images, test_labels))
Defining a function to show accuracy and loss graphs¶
In [16]:
def plot_accuracy_loss(history):
"""
Plot the accuracy and the loss during the training of the nn.
"""
fig = plt.figure(figsize=(10,5))
# Plot accuracy
plt.subplot(221)
plt.plot(history.history['accuracy'],'bo--', label = "acc")
plt.plot(history.history['val_accuracy'], 'ro--', label = "val_acc")
plt.title("train_acc vs val_acc")
plt.ylabel("accuracy")
plt.xlabel("epochs")
plt.legend()
# Plot loss function
plt.subplot(222)
plt.plot(history.history['loss'],'bo--', label = "loss")
plt.plot(history.history['val_loss'], 'ro--', label = "val_loss")
plt.title("train_loss vs val_loss")
plt.ylabel("loss")
plt.xlabel("epochs")
plt.legend()
plt.show()
In [17]:
plot_accuracy_loss(history)
Model Predictions¶
In [18]:
preds = model.predict(val_images)
In [19]:
plt.figure(figsize=(20,20))
for n , i in enumerate(list(np.random.randint(0,len(val_images),36))) :
plt.subplot(6,6,n+1)
plt.imshow(val_images[i])
plt.axis('off')
x =np.argmax(preds[i]) # takes the maximum of of the 6 probabilites.
plt.title((class_names[x]))
In [20]:
result = []
for i in range(len(preds)):
result.append(np.argmax(preds[i]))
Confusion Matrix¶
In [21]:
tn, fp, fn, tp = confusion_matrix(val_labels,result).ravel()
In [22]:
(tn, fp, fn, tp)
Out[22]:
DeepCC¶
In [ ]:
!deepCC modelPedestrianDetection.h5
In [ ]:
d