Car Model Prediction Using CNN

Naman Sharma
2 min readApr 9, 2024

What is CNN?

A Convolutional Neural Network (CNN) is a type of artificial neural network that is commonly used for image processing and analysis. It is designed to automatically learn and extract features from images, reducing the need for manual feature engineering. CNNs consist of layers of convolutional filters that scan over the input image, detecting patterns such as edges, shapes, and textures. These filters help to identify important features that can be used for image classification, object detection, and other tasks. CNNs can also be trained to recognize patterns in large datasets, making them useful for a variety of applications such as medical imaging, facial recognition, and self-driving cars. Overall, CNNs are a powerful tool for image analysis and have revolutionized the field of computer vision.

Let us start with our project

Data Preprocessing

ImageDataGenerator creates modified versions of the images in the training data by randomly rotating, shifting, shearing, zooming, and flipping them. These modified images are then used to train the model, which helps to improve its ability to generalize and reduces overfitting.

pip install tensorflow
pip install keras
import tensorflow as tf
from keras.preprocessing.image import ImageDataGenerator
train_datagen=ImageDataGenerator(rescale=1./255,
shear_range=0.2,
zoom_range=0.2,
horizontal_flip=True)
training_set=train_datagen.flow_from_directory(r"path of training dataset",target_size=(64,64),batch_size=32,class_mode='sparse')

test_datagen=ImageDataGenerator(rescale=1./255)
test_set=train_datagen.flow_from_directory(r"path of test dataset",target_size=(64,64),batch_size=32,class_mode='sparse')

In the above code we have used sparse as the class mode as here we have multiclass classification.

Building The CNN

cnn=tf.keras.models.Sequential() #Initializing CNN

cnn.add(tf.keras.layers.Conv2D(filters=32, kernel_size=3, activation='relu', input_shape=[64, 64, 3]))# Convolution

cnn.add(tf.keras.layers.MaxPooling2D(pool_size=2, strides=2))#Pooling

# Second Convolution
cnn.add(tf.keras.layers.Conv2D(filters=32, kernel_size=3, activation='relu', input_shape=[64, 64, 3]))
cnn.add(tf.keras.layers.MaxPooling2D(pool_size=2, strides=2))

#Flattening Layer
cnn.add(tf.keras.layers.Flatten())

#Full Connected Layer
cnn.add(tf.keras.layers.Dense(units=128,activation='relu'))

#output Layer
cnn.add(tf.keras.layers.Dense(units=1,activation='softmax'))

#Compiling The CNN

cnn.compile(optimizer='adam',loss='categorical_Crossentropy',metrics=['accuracy'])
#Training the cnn on the training Set and evaluating it on test set
cnn.fit(x=training_set, validation_data=test_set,epochs=25)

#Predicting The single output
import numpy as np
from keras.preprocessing import image
test_image = image.load_img(r'add the path of the image', target_size=(64, 64))
test_image = image.img_to_array(test_image)
test_image = np.expand_dims(test_image, axis=0)
result = cnn.predict(test_image)

When we were compiling the CNN we have used categorical cross-entropy loss because it is a popular choice for multi-class classification problems in machine learning.

Thank you for reading...

--

--

Naman Sharma

Hi there! I'm excited to be sharing my ideas with you on this platform. I'm a curious and creative individual with a passion for exploring new topics.