In this tutorial, we build a vehicle classification model using VGG16 for feature extraction and XGBoost for classification! 🚗🚛🏍️
It will based on Tensorflow and Keras
🔍 What You’ll Learn 🔍:
🖼️ Part 1: We kick off by preparing our dataset, which consists of thousands of vehicle images across five categories. We demonstrate how to load and organize the training and validation data efficiently.
🧠 Part 2: With our data in order, we delve into the feature extraction process using VGG16, a pre-trained convolutional neural network. We explain how to load the model, freeze its layers, and extract essential features from our images. These features will serve as the foundation for our classification model.
🚀 Part 3: The heart of our classification system lies in XGBoost, a powerful gradient boosting algorithm. We walk you through the training process, from loading the extracted features to fitting our model to the data. By the end of this part, you’ll have a finely-tuned XGBoost classifier ready for predictions.
🔮 Part 4: The moment of truth arrives as we put our classifier to the test. We load a test image, pass it through the VGG16 model to extract features, and then use our trained XGBoost model to predict the vehicle’s category. You’ll witness the prediction live on screen as we map the result back to a human-readable label.
Check out our tutorial here : https://youtu.be/taJOpKa63RU
Link for the full code : https://ko-fi.com/s/9bc3ded198
You can download the dataset here : https://www.kaggle.com/datasets/mrtontrnok/5-vehichles-for-multicategory-classification
Code for the XGBoost and VGG16 Object Classification :
Part 1 : vehicle images Image Preprocessing and Dataset Preparation :
Dataset name : 5 vehichles for multicategory classification : https://www.kaggle.com/datasets/mrtontrnok/5-vehichles-for-multicategory-classification
This script processes a dataset of vehicle images for a multi-category classification task.
It loads images from a structured directory, resizes them, and converts them into numpy arrays.
The processed images and their corresponding labels are then saved as `.npy` files for efficient access during model training.
Steps:
1. Load training and validation images from specified directories.
2. Resize images to 256×256 pixels.
3. Store images and their labels in numpy arrays.
4. Save the preprocessed data for future use in training and validation.
import numpy as np # For handling numerical data import os # For interacting with the operating system import cv2 # OpenCV for image processing import glob # For working with file paths # List all directories and files inside the dataset folder print(os.listdir("E:/Data-sets/5 vehichles for classification/")) # Define image size for resizing SIZE = 256 # Initialize lists to store training images and their labels train_images = [] train_labels = [] # Load training data for directory_path in glob.glob("E:/Data-sets/5 vehichles for classification/train/*"): label = directory_path.split("\\")[-1] # Extract category label from folder name # Iterate through each image file in the directory for img_path in glob.glob(os.path.join(directory_path, "*.png")): img = cv2.imread(img_path, cv2.IMREAD_COLOR) # Read image in color mode img = cv2.resize(img, (SIZE, SIZE)) # Resize image to 256x256 pixels train_images.append(img) # Append image to training data list train_labels.append(label) # Append corresponding label # Convert lists to numpy arrays for easier processing train_imagesNP = np.array(train_images) train_labelsNP = np.array(train_labels) # Print dataset statistics print(train_labelsNP) # Print all training labels print(train_imagesNP.shape) # Shape of training image data print(train_labelsNP.shape) # Shape of training label data # Initialize lists to store validation images and their labels validation_images = [] validation_labels = [] # Load validation data for directory_path in glob.glob("E:/Data-sets/5 vehichles for classification/validation/*"): label = directory_path.split("\\")[-1] # Extract category label from folder name print(label) # Print label for debugging for img_path in glob.glob(os.path.join(directory_path, "*.png")): print(img_path) # Print image path for debugging img = cv2.imread(img_path, cv2.IMREAD_COLOR) # Read image in color mode img = cv2.resize(img, (SIZE, SIZE)) # Resize image to 256x256 pixels validation_images.append(img) # Append image to validation data list validation_labels.append(label) # Append corresponding label # Convert lists to numpy arrays for easier processing validation_imagesNP = np.array(validation_images) validation_labelsNP = np.array(validation_labels) # Print validation dataset statistics print(validation_labelsNP) # Print all validation labels print(validation_imagesNP.shape) # Shape of validation image data print(validation_labelsNP.shape) # Shape of validation label data # Save the processed data as numpy arrays for future use print("Saving the data...") np.save("e:/temp/5-vehicles-train-images.npy", train_imagesNP) # Save training images np.save("e:/temp/5-vehicles-train-labels.npy", train_labelsNP) # Save training labels np.save("e:/temp/5-vehicles-validate-images.npy", validation_imagesNP) # Save validation images np.save("e:/temp/5-vehicles-validate-labels.npy", validation_labelsNP) # Save validation labels print("Data saving completed successfully.")
Link for the full code : https://ko-fi.com/s/9bc3ded198
Part 2 : Vehicle Image Dataset Preprocessing, Encoding, and Feature Extraction :
This script processes a dataset of vehicle images for a multi-category classification task.
It loads images from a structured directory, resizes them, and converts them into numpy arrays.
The processed images and their corresponding labels are then saved as `.npy` files for efficient access during model training.
Additionally, it loads the saved data, encodes labels, extracts features using the VGG16 model, and saves them for further use.
Steps:
1. Load training and validation images from specified directories.
2. Resize images to 256×256 pixels.
3. Store images and their labels in numpy arrays.
4. Save the preprocessed data for future use in training and validation.
5. Load the saved dataset.
6. Encode labels using LabelEncoder.
7. Normalize image pixel values.
8. Extract features using the VGG16 model.
9. Save extracted features and the VGG16 model.
import numpy as np # For handling numerical data import os # For interacting with the operating system import cv2 # OpenCV for image processing import glob # For working with file paths from sklearn import preprocessing # For encoding labels from keras.applications.vgg16 import VGG16 # Pre-trained model for feature extraction # List all directories and files inside the dataset folder print(os.listdir("E:/Data-sets/5 vehichles for classification/")) # Define image size for resizing SIZE = 256 # Initialize lists to store training images and their labels train_images = [] train_labels = [] # Load training data for directory_path in glob.glob("E:/Data-sets/5 vehichles for classification/train/*"): label = directory_path.split("\\")[-1] # Extract category label from folder name # Iterate through each image file in the directory for img_path in glob.glob(os.path.join(directory_path, "*.png")): img = cv2.imread(img_path, cv2.IMREAD_COLOR) # Read image in color mode img = cv2.resize(img, (SIZE, SIZE)) # Resize image to 256x256 pixels train_images.append(img) # Append image to training data list train_labels.append(label) # Append corresponding label # Convert lists to numpy arrays for easier processing train_imagesNP = np.array(train_images) train_labelsNP = np.array(train_labels) # Print dataset statistics print(train_labelsNP) # Print all training labels print(train_imagesNP.shape) # Shape of training image data print(train_labelsNP.shape) # Shape of training label data # Initialize lists to store validation images and their labels validation_images = [] validation_labels = [] # Load validation data for directory_path in glob.glob("E:/Data-sets/5 vehichles for classification/validation/*"): label = directory_path.split("\\")[-1] # Extract category label from folder name print(label) # Print label for debugging for img_path in glob.glob(os.path.join(directory_path, "*.png")): print(img_path) # Print image path for debugging img = cv2.imread(img_path, cv2.IMREAD_COLOR) # Read image in color mode img = cv2.resize(img, (SIZE, SIZE)) # Resize image to 256x256 pixels validation_images.append(img) # Append image to validation data list validation_labels.append(label) # Append corresponding label # Convert lists to numpy arrays for easier processing validation_imagesNP = np.array(validation_images) validation_labelsNP = np.array(validation_labels) # Print validation dataset statistics print(validation_labelsNP) # Print all validation labels print(validation_imagesNP.shape) # Shape of validation image data print(validation_labelsNP.shape) # Shape of validation label data # Save the processed data as numpy arrays for future use print("Saving the data...") np.save("e:/temp/5-vehicles-train-images.npy", train_imagesNP) # Save training images np.save("e:/temp/5-vehicles-train-labels.npy", train_labelsNP) # Save training labels np.save("e:/temp/5-vehicles-validate-images.npy", validation_imagesNP) # Save validation images np.save("e:/temp/5-vehicles-validate-labels.npy", validation_labelsNP) # Save validation labels print("Data saving completed successfully.") # Load the saved dataset print("Loading the saved data...") x_train = np.load("e:/temp/5-vehicles-train-images.npy") y_train = np.load("e:/temp/5-vehicles-train-labels.npy") x_test = np.load("e:/temp/5-vehicles-validate-images.npy") y_test = np.load("e:/temp/5-vehicles-validate-labels.npy") print("Data loaded successfully.") # Encode labels from text to integers le = preprocessing.LabelEncoder() y_train = le.fit_transform(y_train) y_test = le.fit_transform(y_test) # Get unique categories original_labels = le.classes_ np.save("e:/temp/5-vehicles-categories.npy", original_labels) # Save category labels print("Unique categories saved.") # Normalize image pixel values x_train = x_train / 255.0 x_test = x_test / 255.0 # Load pre-trained VGG16 model without fully connected layers vgg_model = VGG16(weights='imagenet', include_top=False, input_shape=(SIZE, SIZE, 3)) # Freeze layers to prevent training for layer in vgg_model.layers: layer.trainable = False print(vgg_model.summary()) # Extract features from images using VGG16 feature_extractor = vgg_model.predict(x_train) features = feature_extractor.reshape(feature_extractor.shape[0], -1) # Flatten features # Save extracted features and labels np.save("e:/temp/5-vehicles-features.npy", features) np.save("e:/temp/5-vehicles-y_train.npy", y_train) np.save("e:/temp/5-vehicles-x_test.npy", x_test) np.save("e:/temp/5-vehicles-y_test.npy", y_test) print("Feature extraction and saving completed.") # Save the VGG16 model vgg_model.save("e:/temp/5-vehicles-VGG16-model.h5") print("Model saved successfully.")
Link for the full code : https://ko-fi.com/s/9bc3ded198
Part3 : Train an XGBoost Model Using Extracted Features from VGG16 :
This script loads pre-extracted features from a VGG16 model and their corresponding labels.
It then trains an XGBoost classifier on these features and saves the trained model for future use.
Steps:
1. Load precomputed VGG16 features and encoded labels.
2. Initialize an XGBoost classifier.
3. Train the model using the features and labels.
4. Save the trained model to disk.
import numpy as np # Import NumPy for handling numerical data # Load the extracted features and labels print("Start loading the data") features = np.load("e:/temp/5-vehicales-features.npy") # Load extracted image features y_train = np.load("e:/temp//5-vehicales-y_train.npy") # Load corresponding encoded labels print("Finish loading the data") # Print the shape of the loaded data to verify dimensions print(features.shape) # Print feature array shape (number of samples, number of features) print(y_train.shape) # Print label array shape (number of samples,) # Import XGBoost for training the classifier import xgboost as xgb # Initialize the XGBoost classifier model = xgb.XGBClassifier() # Train the XGBoost model using the pre-extracted features and labels print("Training the XGBoost model with VGG16 features...") model.fit(features, y_train) print("Finished training the XGBoost model.") # Save the trained model to disk model.save_model("e:/temp/5-vehicles-XGboost.h5") print("XGBoost model saved successfully.")
Link for the full code : https://ko-fi.com/s/9bc3ded198
Part4 : Load and Test an XGBoost Model for Vehicle Classification :
This script loads a pre-trained XGBoost model and a VGG16 model to classify a random test image.
It extracts features from the test image using the VGG16 model and then predicts the vehicle category
using the trained XGBoost model.
Steps:
1. Load the test data (images and labels).
2. Load the pre-trained XGBoost and VGG16 models.
3. Select a random test image and preprocess it.
4. Extract features from the image using VGG16.
5. Predict the vehicle category using XGBoost.
6. Display the predicted category on the image.
import numpy as np # Import NumPy for handling numerical data # Load the test dataset print("Loading test data...") x_test = np.load("e:/temp//5-vehicales-x_test.npy") # Load test images y_test = np.load("e:/temp//5-vehicales-y_test.npy") # Load test labels print("Test data loaded successfully.") # Print the shape of the test data print(x_test.shape) # Shape of test images print(y_test.shape) # Shape of test labels # Import XGBoost and load the trained model import xgboost as xgb model = xgb.Booster() model.load_model("e:/temp/5-vehicles-XGboost.h5") # Load pre-trained XGBoost model print("XGBoost model loaded successfully.") # Import TensorFlow and load the pre-trained VGG16 model import tensorflow as tf model_file = "e:/temp/5-vehicales-Vgg_model.h5" vgg_model = tf.keras.models.load_model(model_file) # Load pre-trained VGG16 model # Import OpenCV for image processing import cv2 # Select a random image from the test dataset n = np.random.randint(0, x_test.shape[0]) # Generate a random index print("Random image index:", n) img = x_test[n] # Select the image print("Image shape:", img.shape) # Preprocess the image for VGG16 feature extraction input_img = np.expand_dims(img, axis=0) # Add batch dimension (1, width, height, channels) print("Preprocessed image shape:", input_img.shape) # Extract features from the test image using VGG16 input_img_feature = vgg_model.predict(input_img) # Reshape extracted features to match XGBoost input format input_img_features = input_img_feature.reshape(input_img_feature.shape[0], -1) # Convert features into an XGBoost DMatrix for prediction DMinput = xgb.DMatrix(input_img_features) # Make a prediction using the XGBoost model prediction = model.predict(DMinput) print("Raw prediction output:", prediction) # Get the predicted class index result = prediction[0] finalResult = np.argmax(result) print("Predicted class index:", finalResult) # Load original category labels originalLabels = np.load("e:/temp/5-vehicales-categories.npy") text = originalLabels[finalResult] # Get the class name print("Predicted category:", text) # Display the image with the predicted label font = cv2.FONT_HERSHEY_COMPLEX cv2.putText(img, text, (0, 20), font, 0.5, (0, 255, 255), 1) # Overlay text on image cv2.imshow("Predicted Image", img) # Show image cv2.waitKey(0) # Wait for user input cv2.destroyAllWindows() # Close image window
Link for the full code : https://ko-fi.com/s/9bc3ded198
Connect :
☕ Buy me a coffee — https://ko-fi.com/eranfeit
🖥️ Email : feitgemel@gmail.com
🤝 Fiverr : https://www.fiverr.com/s/mB3Pbb
Enjoy,
Eran