Here, we will create a Convolutional Neural Network (CNN) model to classify car images into seven categories: Audi, Hyundai Creta, Mahindra Scorpio, Rolls Royce, Maruti Suzuki Swift, Tata Safari, and Toyota Innova.
Why Use CNN?
Image classification requires extracting meaningful features from images to identify patterns within the dataset. CNNs are ideal for this task as they utilize a hierarchical structure that progressively builds a network, narrowing down to a fully connected layer (dense layer) where all neurons are interconnected to process the final output.
CNNs are particularly effective for image-based tasks because they reduce the number of parameters while maintaining model quality. Images have high dimensionality, with each pixel serving as a feature. CNNs handle this by leveraging a sliding window (convolutional filter) smaller than the input matrix, effectively reducing dimensionality while preserving important spatial information. This makes CNNs a powerful choice for image classification problems.
Dataset Link : https://www.kaggle.com/code/kshitij192/car-images-classification-using-cnn
User of tensorflow.keras.layers
from tensorflow.keras.layers import Dense,Dropout,Convolution2D,MaxPooling2D,Flatten #action detectionimport tensorflow
This line imports specific layers and functionalities from TensorFlow’s Keras library for building a Convolutional Neural Network (CNN). Here’s what each term represents:
Dense
: A fully connected layer where every neuron is connected to every neuron in the next layer. Used for classification or regression tasks.Dropout
: A regularization technique that randomly drops a fraction of neurons during training to prevent overfitting.Convolution2D
: A 2D convolution layer used to apply convolutional filters to extract features from images (like edges or textures).MaxPooling2D
: A pooling layer used to downsample the feature maps by taking the maximum value in a specific window, reducing dimensionality while retaining key features.Flatten
: Flattens the multidimensional feature maps into a 1D vector to feed into the dense layers.
Summary:
These are essential building blocks for constructing a CNN, where convolutional and pooling layers extract features from images, and dense layers (with Dropout for regularization) handle the classification or prediction tasks.
from tensorflow.keras.preprocessing.image import ImageDataGenerator
IMAGE_SIZE = 128
train_datagen = ImageDataGenerator(
rescale=1./255,
rotation_range=10,
horizontal_flip=True
)
train_generator = train_datagen.flow_from_directory(
'/kaggle/input/cars-image-dataset/Cars Dataset/train',
target_size=(IMAGE_SIZE,IMAGE_SIZE),
class_mode="sparse",
)
This code is setting up the preprocessing and augmentation for image data to train a deep learning model. Here’s a breakdown:
ImageDataGenerator
: This is used to apply real-time data augmentation and preprocessing to the images as they are loaded.rescale=1./255
: This normalizes pixel values by scaling them to a range of 0 to 1, improving model training.rotation_range=10
: Randomly rotates images by up to 10 degrees to help the model generalize better.horizontal_flip=True
: Randomly flips images horizontally to increase diversity in the training data.
train_generator
: This creates a data generator that loads images from the specified directory ('/kaggle/input/cars-image-dataset/Cars Dataset/train'
) and applies the transformations.target_size=(IMAGE_SIZE, IMAGE_SIZE)
: Resizes all images to 128×128 pixels.class_mode="sparse"
: The labels are integers representing the class (e.g., different car types), which is suitable for multi-class classification with sparse labels.
In short:
This code prepares the training images, applying normalization and augmentation (rotation, flipping) to help the model generalize better, while also resizing the images to 128×128 pixels.