Car model prediction using CNN
image from internet

Car model prediction using CNN

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.

  1. Convolution uses 64 with MaxPolling and activation function as ReLU
  2. Dense Layer is flattened
  3. 50% of nodes are dropped
  4. Softmax function is used for multiclass 13 models of the cars

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

No alt text provided for this image

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

How to load Matlab .mat files in Python | by Ashique Mahmood | Towards Data Science

3.????Plant Disease Detection model using a?Convolutional Neural Network

Plant AI — Plant Disease Detection using Convolutional Neural Network | by Oluwafemi Tairu | Towards Data Science

Importing the necessary packages


要查看或添加评论,请登录

Ankush Arya的更多文章

  • Computer Vision : A PyTorch Model Trained on the Stanford Cars Dataset

    Computer Vision : A PyTorch Model Trained on the Stanford Cars Dataset

    Introduction In the rapidly advancing field of computer vision, new breakthroughs constantly reshape the boundaries of…

  • Customer churn

    Customer churn

    Introduction Churn is the term for the natural and inevitable way that a business will lose customers. In this post, I…

    2 条评论
  • Understanding interactions between the predictors and response variable

    Understanding interactions between the predictors and response variable

    Introduction Before diving into modelling, I'll show you how to do some exploratory data analysis and document your…

  • Download any PDF easily with python

    Download any PDF easily with python

    Steps to download pdf files through python Import the requests module and pandas and datetime for data manipulation…

    2 条评论
  • Automating laptop configuration

    Automating laptop configuration

    I have been working for sometime now to find solution to automate installing software and setting softwares on my…

    1 条评论
  • Is India flattening the curve?

    Is India flattening the curve?

    I am trying to get deeper in the data and trying to understand growth of infected people in India. And try to look into…

    1 条评论
  • COVID19 growth viz-a-viz India

    COVID19 growth viz-a-viz India

    With a population of 1.3 billion and home to the worlds most densely populated cities namely Kolkata, Mumbai and…

    6 条评论
  • Quest for COVID19 Data

    Quest for COVID19 Data

    My quest for understanding the COVID data from the a Data Scientist perspective has taken me on a journey which is…

    7 条评论

社区洞察

其他会员也浏览了