NOTE: This Use Case is not purposed for resource constrained devices.
Pothole Detection¶
Credit: AITS Cainvas Community
Photo by Jack Last on Dribbble
As we are all aware that Tesla's quest for Self Driving Cars, and we are aware these cars are being developed not just for safety issues of car riders but also to make their journey more comfortable.So, in order to provide comfortable journey it should be able to detect potholes and avoid them.
Importing the Dataste¶
In [1]:
# This will load the dataset.You will see a folder called ALL in your workspace.
!wget -N "https://cainvas-static.s3.amazonaws.com/media/user_data/cainvas-admin/dataset.zip"
!unzip -qo dataset.zip
!rm dataset.zip
Importing the libraries¶
In [2]:
# import libraries
import numpy as np
import pandas as pd
import matplotlib.pyplot as plt
import cv2
from keras.layers import Dense, Flatten, AveragePooling2D, Dropout
from keras.models import Model
from keras.applications.vgg16 import VGG16
from keras.preprocessing import image
from keras.preprocessing.image import ImageDataGenerator
from keras.optimizers import Adam
Data Visulization¶
In [3]:
data_path = "My Dataset/"
In [4]:
# Check images
img = cv2.imread(data_path+"train"+'/'+"Pothole"+"/"+"131.pothole.jpg")
img = cv2.cvtColor(img, cv2.COLOR_BGR2RGB)
In [5]:
# pothole
plt.imshow(img)
Out[5]:
In [6]:
img.shape
Out[6]:
Dataset Generation¶
In [7]:
# Data agumentation on train and test
train_datagen = ImageDataGenerator(rescale = 1./255,
zoom_range = 0.2,
rotation_range=15,
horizontal_flip = True)
test_datagen = ImageDataGenerator(rescale = 1./255)
In [8]:
# create dataset train
training_set = train_datagen.flow_from_directory(data_path + 'train',
target_size = (224, 224),
batch_size = 16,
class_mode = 'categorical',
shuffle=True)
In [9]:
# Create test data set
test_set = test_datagen.flow_from_directory(data_path + 'test',
target_size = (224, 224),
batch_size = 16,
class_mode = 'categorical',
shuffle = False)
Model Architecture¶
In [10]:
# Model creation with changes
model = VGG16(input_shape=(224,224,3),include_top=False)
for layer in model.layers:
layer.trainable = False
newModel = model.output
newModel = AveragePooling2D()(newModel)
newModel = Flatten()(newModel)
newModel = Dense(128, activation="relu")(newModel)
newModel = Dropout(0.5)(newModel)
newModel = Dense(2, activation='softmax')(newModel)
model = Model(inputs=model.input, outputs=newModel)
In [11]:
model.summary()
Model Training¶
In [12]:
opt=Adam(learning_rate=0.0001)
model.compile(optimizer=opt, loss='categorical_crossentropy', metrics=['accuracy'])
In [13]:
history = model.fit(training_set,
validation_data=test_set,
epochs=10)
Tarining Curves¶
In [14]:
acc = history.history['accuracy']
val_acc = history.history['val_accuracy']
loss = history.history['loss']
val_loss = history.history['val_loss']
epochs=range(len(acc))
In [15]:
plt.plot(epochs,acc,label='Trainin_acc',color='blue')
plt.plot(epochs,val_acc,label='Validation_acc',color='red')
plt.legend()
plt.title("Training and Validation Accuracy")
Out[15]:
In [16]:
plt.plot(epochs,loss,label='Training_loss',color='blue')
plt.plot(epochs,val_loss,label='Validation_loss',color='red')
plt.legend()
plt.title("Training and Validation loss")
Out[16]:
Accessing Model Performance¶
In [17]:
class_dict = {0:'Plain',
1:'Pothole'}
In [18]:
# New Data for testing
file_path = 'My Dataset/test/Plain/2.jpg'
test_image = cv2.imread(file_path)
test_image = cv2.cvtColor(test_image, cv2.COLOR_BGR2RGB)
test_image = cv2.resize(test_image, (224,224),interpolation=cv2.INTER_CUBIC)
plt.imshow(test_image)
test_image = np.expand_dims(test_image,axis=0)
probs = model.predict(test_image)
pred_class = np.argmax(probs)
pred_class = class_dict[pred_class]
if (pred_class == 'Plain'):
print('Plain Road Detected')
else:
print('Pothole Detected')
In [19]:
# New Data for testing
file_path = 'My Dataset/test/Pothole/2.jpg'
test_image = cv2.imread(file_path)
test_image = cv2.cvtColor(test_image, cv2.COLOR_BGR2RGB)
test_image = cv2.resize(test_image, (224,224),interpolation=cv2.INTER_CUBIC)
plt.imshow(test_image)
test_image = np.expand_dims(test_image,axis=0)
probs = model.predict(test_image)
pred_class = np.argmax(probs)
pred_class = class_dict[pred_class]
if (pred_class == 'Plain'):
print('Plain Road Detected')
else:
print('Pothole Detected')
Saving the Model¶
In [20]:
model.save("pothole_detection.h5")
Compiling the Model with DeepC Compiler¶
In [21]:
!deepCC pothole_detection.h5