How to Build and Train a Car Image Classification Model Using Transfer Learning with VGG16
Share
Condition for Using VGG16 Transfer Learning Model for Car Image Classification
Description: This code demonstrates how to use VGG16 as a transfer learning model for classifying car images into different categories. It involves loading and preprocessing images, applying the VGG16 model with custom layers, and training the model on the dataset. Finally, the model's performance is evaluated using confusion matrix and other classification metrics.
Step-by-Step Process
Import Libraries: Import necessary libraries like NumPy, TensorFlow, and scikit-learn for data manipulation, model creation, and evaluation.
Load and Inspect Images: Load the car image dataset by reading the file names from the specified path using Python's os library.
Preprocess Data: Resize images to 224x224 pixels, convert them to RGB (if necessary), and convert into NumPy arrays.
Generate Labels: Generate unique integer labels for each car category.
Train-Test Split: Split the dataset into training and testing sets using scikit-learn’s train_test_split for model evaluation.
Use VGG16 Model: Load VGG16 with pre-trained weights, freeze its layers, and add custom dense layers for classification.
Train and Evaluate Model: Train the model, evaluate its performance with accuracy, F1 score, recall, precision, and confusion matrix.
Sample Source Code
# Import Necessary Libraries
import numpy as np
import os
import random
import matplotlib.pyplot as plt
import seaborn as sns
from PIL import Image
from sklearn.preprocessing import MinMaxScaler
from sklearn.model_selection import train_test_split
from tensorflow.keras.layers import Flatten, Dense
from tensorflow.keras.models import Model
import warnings
warnings.filterwarnings("ignore")
from sklearn.metrics import (classification_report, confusion_matrix, accuracy_score,
f1_score, recall_score, precision_score)
from tensorflow.keras.applications import VGG16
# Create a figure with subplots to display the images
plt.figure(figsize=(25, 20))
# Loop through the sample images and display them
for i, img_file in enumerate(sample_images):
img_path = os.path.join(paths, img_file)
img = Image.open(img_path)
# Generate X and Y variable
x = np.concatenate((audi_features, innova_features, Rolls_Royce_features,
Swift_features, Hyundai_Creta_features, Tata_Safari_features, Mahindra_Scorpio_features), axis=0)
y_audi = [0 for i in range(len(audi_features))]
y_innova = [1 for i in range(len(innova_features))]
y_RR = [2 for i in range(len(Rolls_Royce_features))]
y_swift = [3 for i in range(len(Swift_features))]
y_creta = [4 for i in range(len(Hyundai_Creta_features))]
y_safari = [5 for i in range(len(Tata_Safari_features))]
y_scorpio = [6 for i in range(len(Mahindra_Scorpio_features))]
y = np.concatenate((y_audi, y_innova, y_RR, y_swift, y_creta, y_safari, y_scorpio), axis=0)
# Data Split
x_train, x_test, y_train, y_test = train_test_split(x, y, test_size=0.2, random_state=42)