Car model prediction using CNN
Ankush Arya
Data Scientist Partnering with Experts to Enhance Public Health Safety and Innovation!
This project is to apply my learnings in Convolutional Neural Networks in TensorFlow from Coursera.
Goal
Code CNN which can detect the car model, given an image of car
Data set
The data used in this project was downloaded from the Stanford site. It was initially uploaded by a team of 4 person.
I initially scrapped web using beautifulsoup4 and searched the web for images of the cars and the classes the image belonged. While the team at Stanford had created a dataset of images of cars in 2013, I decided to take this dataset and engineer this to my use.
The initial meta data file was created in MATLAB, and for my current project I had to read in python, the below code reads .MAT file.
This meta file is used to split the data into train and test. I further reduced the data to filter only BMW car models. The script copies the images in separate folders on the basis of the classes. This allows the whole model finish execution in more realistic time while working in a home setup for learning.
I have moved the car images to their respective folders named by the model names.
Next, I copied the car images to the Google Drive so as I can use the GPU on colab.research.google.com.
The below code ensures the images are accessible to the colab notebook
Model
The keras and Tensorflow for the CNN, I have restrricetd the images to have 13 classes and hence the output layer has 13 classes with softmax.
The model here is a sequential model with multiple layers.
import tensorflow as t
import keras_preprocessing
from keras_preprocessing import image
from keras_preprocessing.image import ImageDataGenerator
from PIL import Image
TRAINING_DIR = "/content/drive/MyDrive/carsVilla/cars_train"
training_datagen = 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')
VALIDATION_DIR = "/content/drive/MyDrive/carsVilla/cars_test"
validation_datagen = ImageDataGenerator(rescale = 1./255)
train_generator = training_datagen.flow_from_directory(
TRAINING_DIR,
target_size=(150,150),
class_mode='categorical',
batch_size=10
)
validation_generator = validation_datagen.flow_from_directory(
VALIDATION_DIR,
target_size=(150,150),
class_mode='categorical',
batch_size=10
)
model = tf.keras.models.Sequential([
# Note the input shape is the desired size of the image 150x150 with 3 bytes color
# This is the first convolution
tf.keras.layers.Conv2D(64, (3,3), activation='relu', input_shape=(150, 150, 3)),
tf.keras.layers.MaxPooling2D(2, 2),
# The second convolution
tf.keras.layers.Conv2D(64, (3,3), activation='relu'),
tf.keras.layers.MaxPooling2D(2,2),
# The third convolution
tf.keras.layers.Conv2D(128, (3,3), activation='relu'),
tf.keras.layers.MaxPooling2D(2,2),
# The fourth convolution
tf.keras.layers.Conv2D(128, (3,3), activation='relu'),
tf.keras.layers.MaxPooling2D(2,2),
# Flatten the results to feed into a DNN
tf.keras.layers.Flatten(),
tf.keras.layers.Dropout(0.5),
# 512 neuron hidden layer
tf.keras.layers.Dense(512, activation='relu'),
tf.keras.layers.Dense(13, activation='softmax')
])
model.summary()
model.compile(loss = 'categorical_crossentropy', optimizer='rmsprop', metrics=['accuracy'])
history = model.fit(train_generator, epochs=500, steps_per_epoch=None, validation_data = validation_generator, verbose = 1, validation_steps=None)
The output of 500 Epochs is as below, as shown the validation accuracy is way below than the training
领英推荐
Found 396 images belonging to 13 classes
Found 135 images belonging to 13 classes.
Model: "sequential_1"
_________________________________________________________________
Layer (type) Output Shape Param #
=================================================================
conv2d_4 (Conv2D) (None, 148, 148, 64) 1792
max_pooling2d_4 (MaxPooling (None, 74, 74, 64) 0
2D)
conv2d_5 (Conv2D) (None, 72, 72, 64) 36928
max_pooling2d_5 (MaxPooling (None, 36, 36, 64) 0
2D)
conv2d_6 (Conv2D) (None, 34, 34, 128) 73856
max_pooling2d_6 (MaxPooling (None, 17, 17, 128) 0
2D)
conv2d_7 (Conv2D) (None, 15, 15, 128) 147584
max_pooling2d_7 (MaxPooling (None, 7, 7, 128) 0
2D)
flatten_1 (Flatten) (None, 6272) 0
dropout_1 (Dropout) (None, 6272) 0
dense_2 (Dense) (None, 512) 3211776
dense_3 (Dense) (None, 13) 6669
=================================================================
Total params: 3,478,605
Trainable params: 3,478,605
Non-trainable params: 0.
The plots of the initial run are as below. These show that validation accuracy just fell off after approx. 30 Epochs.
Further the training after 300 epochs did not add more information to the model
The complete code notebook is as below, send me your email address in message if you want to work this further, I will be happy to share the note book.
Citation
1.??????3D Object Representations for Fine-Grained Categorization
Jonathan Krause, Michael Stark, Jia Deng, Li Fei-Fei???????
4th IEEE Workshop on 3D Representation and Recognition, at ICCV 2013?(3dRR-13). Sydney, Australia. Dec. 8, 2013.
2.????How to load Matlab .mat files in Python
3.????Plant Disease Detection model using a?Convolutional Neural Network
Importing the necessary packages