Tags:
car
vehicle
Bus
Transport
Similar Use Cases: Pothole Detection App Vehicle Classification App Car Damage Detection App Tyre Prediction App
Similar Use Cases: Pothole Detection App Vehicle Classification App Car Damage Detection App Tyre Prediction App
In [1]:
!wget "https://cainvas-static.s3.amazonaws.com/media/user_data/cainvas-admin/car_bus.zip"
In [2]:
!unzip -qo "car_bus.zip"
Importing Libraries¶
In [3]:
from tensorflow.keras.models import Sequential
from tensorflow.keras.layers import Conv2D
from tensorflow.keras.layers import MaxPool2D
from tensorflow.keras.layers import Flatten
from tensorflow.keras.layers import Dense
from tensorflow.keras.layers import Dropout
from tensorflow.keras.preprocessing.image import ImageDataGenerator
import matplotlib.pyplot as plt
from matplotlib.image import imread
Image Preprocessing
In [4]:
train_datagen = ImageDataGenerator(rescale = 1/255, shear_range = 0.2, zoom_range = 0.2, horizontal_flip = True)
test_datagen = ImageDataGenerator(rescale = 1/255)
training_set = train_datagen.flow_from_directory('Training_set/Training_set', target_size = (200, 200), batch_size=16, class_mode='binary')
test_set = test_datagen.flow_from_directory('test/test', target_size=(200, 200), batch_size = 16, class_mode = 'binary')
Creating model and Layers
In [5]:
model = Sequential()
model.add(Conv2D(64, (3, 3), input_shape=(200, 200, 3), activation ='relu'))
model.add(MaxPool2D(pool_size=(2,2)))
model.add(Conv2D(32, (3, 3), activation ='relu'))
model.add(MaxPool2D(pool_size=(2,2)))
model.add(Conv2D(16, (3, 3), activation ='relu'))
model.add(MaxPool2D(pool_size=(2,2)))
model.add(Conv2D(10, (3, 3), activation ='relu'))
model.add(MaxPool2D(pool_size=(2,2)))
model.add(Flatten())
model.add(Dense(units = 64, activation = 'relu'))
model.add(Dropout(0.4))
model.add(Dense(units=1, activation = 'sigmoid'))
Compiling model and Train model
In [6]:
model.compile(optimizer = 'adam',loss = 'binary_crossentropy', metrics = ['accuracy'])
history = model.fit_generator(training_set, epochs = 20, validation_data=test_set)
Our model Summary
In [7]:
model.summary()
Model Evaluation
In [8]:
model.evaluate(test_set)
Out[8]:
In [9]:
from matplotlib.image import imread
for i in range(9):
plt.figure(i)
plt.subplot(330+1+i)
img = imread('test/test/electric car/electric__image-10'+str(i)+'.jpeg')
plt.imshow(img)
print(img.shape)
In [10]:
CATEGORIES = ["electric car", "electric bus"]
import cv2
image_array = cv2.imread('training_set/Training_set/electric bus/821c1f91.jpg', cv2.IMREAD_GRAYSCALE)
plt.imshow(image_array, cmap = 'gray')
plt.show()
In [11]:
new_array = cv2.resize(image_array, (90, 90))
plt.imshow(new_array, cmap='gray')
plt.show()
Prediction
In [12]:
import numpy as np
CATEGORIES = ["electric car", "electric bus"]
def prepare(num=1):
batch = None
for i in range(num):
batch = next(test_set)
img = batch[0]
label = batch[1]
return img[0], label
#model = tf.keras.models.load_model('car_bus_classification.m5')
test_img, _ = prepare(5)
prediction = model.predict(np.expand_dims(test_img, axis=0))
print(prediction)
print(CATEGORIES[int(prediction[0][0])])
Accuracy Graph
In [13]:
accuracy_train = history.history['accuracy']
accuracy_val = history.history['val_accuracy']
plt.plot(accuracy_train, 'r--', label = 'training accuracy', linewidth = 1)
plt.plot(accuracy_val, 'b--', label = 'validation accuracy', linewidth = 1)
plt.title('training accuracy and validation accuracy')
plt.xlabel('epochs')
plt.ylabel('accuracy')
plt.legend()
plt.grid()
plt.show()
Loss Graph
In [14]:
loss_train = history.history['loss']
loss_val = history.history['val_loss']
plt.plot(loss_train, 'r--', label = 'training loss')
plt.plot(loss_val, 'b--', label = 'validation loss')
plt.title('training loss and validation loss')
plt.xlabel('epochs')
plt.ylabel('loss')
plt.legend()
plt.grid()
plt.show()
In [18]:
model.save('car_bus_classification.h5')
In [19]:
!deepCC car_bus_classification.h5