...

🦕Dinosaur lower Classifications using Convolutional Neural Network

Tensorflow dinosaor

Introduction

Welcome to our comprehensive Dinosaur Image Classification Tutorial!

Let’s learn a simple way for Dinosaur lower Classifications 🦕

We’ll learn how use Convolutional Neural Network (CNN) to classify 5 dinosaur categories , based on 200 images :

  • Data Preparation: We’ll begin by downloading a curated dataset of dinosaur images, neatly categorized into five distinct classes. You’ll learn how to load and preprocess the data using Python, OpenCV, and Numpy, ensuring it’s perfectly ready for training.
  • CNN Architecture: Unravel the secrets of Convolutional Neural Networks (CNNs) as we dive into their structure and discuss the different layers—convolutional, pooling, and fully connected. Learn how these layers work together to extract meaningful features from images.
  • Model Training :  Using Tensorflow and Keras , we will define and train our custom CNN model. We’ll configure the loss function, optimizer, and evaluation metrics to achieve optimal performance during training.
  • Evaluation Metrics: We’ll evaluate our trained model using various metrics like accuracy and confusion matrix to measure its efficiency and robustness.
  • Predicting New Images: Finally , We put our pre-trained model to the test! We’ll showcase how to use the model to make predictions on fresh, unseen dinosaur images, and witness the magic of AI in action.

Check out our tutorial here : https://www.youtube.com/watch?v=ZhTGcw0C3Dk

You can find more tutorials, and join my newsletter here : https://eranfeit.net/

Link for the full code : https://ko-fi.com/s/a902042be9

Link to the dataset 5 Classes of Dinosaur s: https://www.kaggle.com/datasets/cmglonly/simple-dinosurus-dataset

Enjoy

Eran


Here is the code for the project :

Step 1 : Build The -Model

This section sets up the dataset by specifying the path, retrieving the list of categories (classes), and calculating the total number of classes. It also defines some parameters like batch size and target image size for image processing.

# Import necessary libraries for data processing import numpy as np import cv2 import os  # Define the path where the dataset is located path = "E:/Data-sets/Simple Dinosaur Dataset"  # Get the list of categories (classes) in the dataset directory and sort them categories = os.listdir(path) categories.sort() print(categories)  # Display the list of categories  # Calculate the number of categories (classes) in the dataset numOfClasses = len(categories) print("Number of categories:") print(numOfClasses)  # Define the batch size and target image size for training batchSize = 32 imageSize = (224, 224)

Link for the full code : https://ko-fi.com/s/a902042be9

This section sets up the data generators using Keras’s ImageDataGenerator. It applies basic augmentation techniques (rescaling and horizontal flipping), prepares the training and validation datasets, and checks the shapes of a batch of images and labels to ensure correctness.

# Prepare the data using Keras's ImageDataGenerator for data augmentation from keras.preprocessing.image import ImageDataGenerator  # Create an ImageDataGenerator for training with rescaling and augmentation datagen = ImageDataGenerator(     rescale=1.0/255,  # Normalize pixel values to the range [0, 1]     validation_split=0.2,  # Reserve 20% of the data for validation     horizontal_flip=True  # Apply random horizontal flip to augment data )  # Create the training dataset generator train_dataset = datagen.flow_from_directory(     batch_size=batchSize,     directory=path,     color_mode='rgb',  # Load images in RGB format     shuffle=True,  # Shuffle the data to ensure better training     target_size=imageSize,  # Resize images to 224x224     subset="training",  # Use this generator for training data     class_mode="categorical"  # Multi-class classification )  # Create the validation dataset generator validation_dataset = datagen.flow_from_directory(     batch_size=batchSize,     directory=path,     color_mode='rgb',  # Load images in RGB format     shuffle=True,  # Shuffle the data to ensure better validation performance     target_size=imageSize,  # Resize images to 224x224     subset="validation",  # Use this generator for validation data     class_mode="categorical"  # Multi-class classification )  # Load a batch of data to check the shapes batch_x, batch_y = next(train_dataset)  # Print the shapes of the first batch of images and labels print('Batch of images shape:', batch_x.shape) print('Batch of labels shape:', batch_y.shape)

Link for the full code : https://ko-fi.com/s/a902042be9

This section constructs a Convolutional Neural Network (CNN) using the Keras Sequential model. It includes three convolutional layers with different filter sizes, each followed by a max-pooling layer and some dropout for regularization. The model concludes with a fully connected layer and a softmax output layer for multi-class classification.

# Import necessary modules for creating a neural network model from keras.models import Sequential from keras.layers import Conv2D, MaxPooling2D, Dropout, Flatten, Dense  # Build a sequential model model = Sequential()  # Add a convolutional layer with 16 filters model.add(Conv2D(filters=16, kernel_size=3, activation='relu', padding='same', input_shape=(224, 224, 3))) model.add(MaxPooling2D(pool_size=2))  # Add a max-pooling layer to reduce spatial dimensions model.add(Dropout(0.5))  # Add dropout for regularization to avoid overfitting  # Add a convolutional layer with 32 filters model.add(Conv2D(filters=32, kernel_size=3, activation='relu', padding='same')) model.add(MaxPooling2D(pool_size=2))  # Add another max-pooling layer  # Add a convolutional layer with 64 filters model.add(Conv2D(filters=64, kernel_size=3, activation='relu', padding='same')) model.add(MaxPooling2D(pool_size=2))  # Add another max-pooling layer model.add(Dropout(0.5))  # Add dropout for further regularization  # Flatten the output to pass into the fully connected layers model.add(Flatten())  # Add a fully connected layer with 128 units model.add(Dense(128, activation='relu')) model.add(Dropout(0.5))  # Add dropout for regularization  # Output layer with 'numOfClasses' units (number of categories) using softmax activation model.add(Dense(numOfClasses, activation='softmax'))  # Print the model summary to see the architecture print(model.summary())

Link for the full code : https://ko-fi.com/s/a902042be9

This section compiles the model using the Adam optimizer and categorical crossentropy loss, then calculates the steps per epoch for training and validation. It defines a callback to save the best model based on validation accuracy and trains the model for 50 epochs.

# Compile the model using categorical crossentropy loss and the Adam optimizer model.compile(loss="categorical_crossentropy", optimizer="Adam", metrics=["accuracy"])  # Calculate the number of steps per epoch for training and validation stepsPerEpochs = np.ceil(train_dataset.samples / batchSize) validationSteps = np.ceil(validation_dataset.samples / batchSize)  # Import ModelCheckpoint to save the best model during training from keras.callbacks import ModelCheckpoint  # Define the file path to save the best model best_model_file = "e:/temp/dino.h5" # Create a ModelCheckpoint callback to save the model with the highest validation accuracy best_model = ModelCheckpoint(best_model_file, monitor='val_accuracy', verbose=1, save_best_only=True)  # Train the model and save the training history history = model.fit(     train_dataset,     steps_per_epoch=stepsPerEpochs,     epochs=50,     validation_data=validation_dataset,     validation_steps=validationSteps,     callbacks=[best_model]  # Use the ModelCheckpoint callback during training )

Link for the full code : https://ko-fi.com/s/a902042be9

This section visualizes the model’s performance during training by plotting the training and validation accuracy, as well as the training and validation loss over each epoch. It provides insights into how well the model is learning and if it is overfitting.

# Import the plotting library import matplotlib.pyplot as plt  # Extract the training and validation accuracy from the history acc = history.history['accuracy'] val_acc = history.history['val_accuracy'] loss = history.history['loss'] val_loss = history.history['val_loss']  # Define the range of epochs for plotting epochsForGraph = range(len(acc))  # Plot the training and validation accuracy plt.plot(epochsForGraph, acc, 'r', label="Train accuracy") plt.plot(epochsForGraph, val_acc, 'b', label="Validation accuracy") plt.xlabel('Epochs') plt.ylabel('Accuracy') plt.title("Train and Validation Accuracy") plt.legend(loc='lower right') plt.show()  # Plot the training and validation loss plt.plot(epochsForGraph, loss, 'r', label="Train loss") plt.plot(epochsForGraph, val_loss, 'b', label="Validation loss") plt.xlabel('Epochs') plt.ylabel('Loss') plt.title("Train and Validation Loss") plt.legend(loc='upper right') plt.show()

Link for the full code : https://ko-fi.com/s/a902042be9


Step 2 Test The Model :

Let’s test it on theses 2 test images :

1*ZZkqVMQ4jij9lhvB mRJog

This section loads a pre-trained model from a specified file path and prepares the dataset for prediction by listing and sorting the categories. It also sets the batch size for running predictions.

# Import necessary libraries import tensorflow as tf import os from keras.utils import img_to_array, load_img import numpy as np import cv2  # Load the pre-trained model from a specified file path model = tf.keras.models.load_model("e:/temp/dino.h5") print(model.summary())  # Print the model architecture to see its structure  # Set the batch size for prediction batchSize = 32  # Get the list of categories from the dataset directory source_folder = "E:/Data-sets/Simple Dinosaur Dataset" categories = os.listdir(source_folder) categories.sort()  # Sort the categories for consistent order print(categories)  # Display the list of categories  # Calculate the number of classes based on the number of categories numOfClasses = len(categories) print("Number of categories:") print(numOfClasses)

Link for the full code : https://ko-fi.com/s/a902042be9

This function preprocesses an image by resizing it to the required input size, converting it to an array, adding a batch dimension, and normalizing the pixel values. This ensures that the image is properly formatted for model prediction.

# Define a function to prepare an image for the model def prepareImage(pathForImage):     image = load_img(pathForImage, target_size=(224, 224))  # Load the image and resize to 224x224     imgResult = img_to_array(image)  # Convert the image to a numpy array     imgResult = np.expand_dims(imgResult, axis=0)  # Add an extra dimension for batch size     imgResult = imgResult / 255.0  # Normalize the pixel values to the range [0, 1]     return imgResult

Link for the full code : https://ko-fi.com/s/a902042be9

This section loads a test image, preprocesses it using the prepareImage function, and runs the prediction using the pre-trained model. It then determines the predicted class and displays the corresponding label.

# Specify the path for the test image to predict testImagPath = "TensorFlowProjects/Dinosaurs-Classification/test_pterodacty.jpg" # testImagPath = "TensorFlowProjects/Dinosaurs-Classification/test2_Triceratops.jpg"  # Prepare the image for prediction using the previously defined function imageForModel = prepareImage(testImagPath)  # Run the prediction using the model resultArray = model.predict(imageForModel, batch_size=batchSize, verbose=1) # Get the index of the class with the highest prediction score answers = np.argmax(resultArray, axis=1)  # Get the predicted category label text = categories[answers[0]] print("Predicted: " + text)

Link for the full code : https://ko-fi.com/s/a902042be9

This section reads the original image using OpenCV, adds the predicted class label as an overlay text, and displays the annotated image. It then saves the annotated image and waits for a user action to close the window.

# Read the original test image using OpenCV img = cv2.imread(testImagPath) font = cv2.FONT_HERSHEY_COMPLEX  # Set the font for displaying text on the image  # Overlay the predicted text on the image cv2.putText(img, text, (0, 20), font, 1, (209, 19, 77), 2)  # Display the image with the prediction cv2.imshow('img', img)  # Save the annotated image to a specified path cv2.imwrite("e:/temp/1.png", img)  # Wait indefinitely until a key is pressed cv2.waitKey(0)  # Close all OpenCV windows cv2.destroyAllWindows()

Link for the full code : https://ko-fi.com/s/a902042be9

Connect :

☕ Buy me a coffee — https://ko-fi.com/eranfeit

🖥️ Email : feitgemel@gmail.com

🌐 https://eranfeit.net

🤝 Fiverr : https://www.fiverr.com/s/mB3Pbb

Enjoy,

Eran

error: Content is protected !!
Eran Feit