List of Topics:
Location Research Breakthrough Possible @S-Logix pro@slogix.in

Office Address

Social List

How to Build and Train a Car Image Classification Model Using Transfer Learning with VGG16

Car Image Classification Model with VGG16

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

    path = "/home/soft12/Downloads/sample_dataset/Website/Dataset/Cars Dataset/train"

    files = os.listdir(path)

    def read_image(files,path):
    for i in files:
    img_files = os.listdir(path+'/'+i)
    yield img_files

    img_genrator = read_image(files, path)

    audi_files = next(img_genrator)
    innova_files = next(img_genrator)
    Rolls_Royce_files = next(img_genrator)
    Swift_files = next(img_genrator)
    Hyundai_Creta_files = next(img_genrator)
    Tata_Safari_files = next(img_genrator)
    Mahindra_Scorpio_files = next(img_genrator)

    def plot_images(images, paths, folder):
    paths = paths+'/'+folder
    # Sample_Images
    sample_images = random.sample(images, 5)

    # 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)

    # Plot each image
    plt.subplot(1, 5, i+1)
    plt.imshow(img)
    plt.axis('off')
    plt.title(f"Image {i+1}")

    # Show the plot
    plt.tight_layout()
    plt.show()

    # Show Images
    plot_images(audi_files, path, files[0])
    plot_images(innova_files, path, files[1])
    plot_images(Rolls_Royce_files, path, files[2])
    plot_images(Swift_files, path, files[3])
    plot_images(Hyundai_Creta_files, path, files[4])
    plot_images(Tata_Safari_files, path, files[5])
    plot_images(Mahindra_Scorpio_files, path, files[6])

    def image_process(images, path, folder):
    path = path+'/'+folder
    final_feature = []
    for i in images[:150]:
    image = Image.open(path+'/'+i)
    if image.mode != 'RGB':
    image = image.convert('RGB')
    resize_img = image.resize((224,224))
    img_array = np.array(resize_img)

    final_feature.append(img_array)

    return np.array(final_feature)

    # Get Features
    audi_features = image_process(audi_files, path, files[0])
    innova_features = image_process(innova_files, path, files[1])
    Rolls_Royce_features = image_process(Rolls_Royce_files, path, files[2])
    Swift_features = image_process(Swift_files, path, files[3])
    Hyundai_Creta_features = image_process(Hyundai_Creta_files, path, files[4])
    Tata_Safari_features = image_process(Tata_Safari_files, path, files[5])
    Mahindra_Scorpio_features = image_process(Mahindra_Scorpio_files, path, files[6])

    # 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)

Screenshots
  • Car Price Prediction Model Output Screenshot