...

120 Dog Breeds, more than 10,000 Images: Deep Learning Tutorial for dogs classification

dog breed recognition

📽️ In our latest video tutorial, we will create a dog breed recognition model using the NasLarge pre-trained model 🚀 and a massive dataset featuring over 10,000 images of 120 unique dog breeds 📸.

What You’ll Learn:

🔹 Data Preparation: We’ll begin by downloading a dataset of of more than 20K Dogs images, neatly categorized into 120 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 and the NAS model : We will use the Nas Large model , and customize it to our own needs.

🔹 Model Training: Harness the power of Tensorflow and Keras to define and train our custom CNN model based on Nas Large model . We’ll configure the loss function, optimizer, and evaluation metrics to achieve optimal performance during training.

🔹 Predicting New Images: Watch as 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=vH1UVKwIhLo

Code for the full here : https://ko-fi.com/s/3db9285a46

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


Here is the code for Dogs Classification :

Step 1 — Prepare the data for deep learning dogs classification :

This code processes a dataset of dog breed images for training a machine learning model.

It starts by importing libraries for handling images and data, defining image parameters, and setting up file paths.

The script loads a CSV file containing dog breed labels, performs exploratory data analysis on these labels, and displays a sample image. It then iterates through all images, resizing each one to a consistent format and pairing it with its corresponding label.

Finally, the code saves the processed images and labels as NumPy arrays, ready for use in model training.

# This section imports the necessary libraries for the script, which include numpy, cv2 for image processing, and pandas for data handling. import numpy as np  # Import numpy for array handling import cv2          # Import OpenCV for image processing import pandas as pd # Import pandas for handling CSV files and dataframes  # Defines global constants for image size and full-size dimensions to be used when resizing and processing images. IMAGE_SIZE = (331, 331)       # Target size for resizing images IMAGE_FULL_SIZE = (331, 331, 3)  # Full-size dimension including color channels  # Sets paths to the image folder and the labels CSV file which contains image IDs and corresponding dog breed labels. trainMyImageFolder = "E:/Data-sets/Dog Breed Identification/train"  # Path to folder containing training images  # Loads the CSV file containing image IDs and dog breeds, then performs some basic data exploration to understand its contents. df = pd.read_csv('E:/Data-sets/Dog Breed Identification/labels.csv')  # Load CSV file containing image IDs and labels print("head of labels:") print("================") print(df.head())            # Print the first few rows of the DataFrame to check data format print(df.describe())        # Print summary statistics of the DataFrame  print("Group by labels: ") grouplables = df.groupby("breed")["id"].count()  # Group data by breed and count occurrences print(grouplables.head(10))  # Display the counts of the top 10 breeds  # Reads and displays a sample image using OpenCV to verify the image path and loading process. imgPath = "E:/Data-sets/Dog Breed Identification/train/00a366d4b4a9bbb6c8a63126697b7656.jpg"  # Path to a sample image img = cv2.imread(imgPath)  # Read image using OpenCV # cv2.imshow("img", img)    # Uncomment to display the image in a window # cv2.waitKey(0)            # Wait for any key press to close the window  # This section loops through the DataFrame to load each image and its label, resizing images and storing them in lists. allImages = []  # Initialize list to store image data allLabes = []   # Initialize list to store breed labels  import os       # Import os for handling file paths  # Loop through each row in the DataFrame, process each image and label for ix, (image_name, breed) in enumerate(df[['id', 'breed']].values):     img_dir = os.path.join(trainMyImageFolder, image_name + '.jpg')  # Construct full image path     print(img_dir)    # Print the image path for debugging      img = cv2.imread(img_dir)    # Read image from the directory     resized = cv2.resize(img, IMAGE_SIZE, interpolation=cv2.INTER_AREA)  # Resize image to the specified size     allImages.append(resized)    # Append resized image to the list     allLabes.append(breed)       # Append label to the list  print(len(allImages))  # Print total number of images loaded print(len(allLabes))   # Print total number of labels loaded   # This section saves the lists of images and labels as .npy files for future use in model training. print("Save the data:") np.save("e:/temp/allDogsImages.npy", allImages)  # Save all images as a numpy array np.save("e:/temp/allDogsLables.npy", allLabes)   # Save all labels as a numpy array print("Finished saving the data...")

Code for the full here : https://ko-fi.com/s/3db9285a46


Step 2 : Build the Dogs classification model based on NASNetLarge architecture

This code trains a deep learning model using a pre-trained NASNetLarge architecture to classify images of dogs into various breeds.

It loads and preprocesses images and labels, encodes labels, splits the data into training and testing sets, and builds a model by adding classification layers on top of NASNetLarge.

Finally, it compiles the model, defines callbacks for efficient training, and trains it while monitoring validation performance to save the best model.

# Import necessary libraries import numpy as np  # For numerical operations and array handling  # Set image parameters IMAGE_SIZE = (331, 331)  # Target image dimensions for resizing IMAGE_FULL_SIZE = (331, 331, 3)  # Full image size including color channels batchSize = 8  # Number of images per training batch  # Load preprocessed image and label data allImages = np.load("e:/temp/allDogsImages.npy")  # Load images as numpy array allLabes = np.load("e:/temp/allDogsLables.npy")   # Load labels as numpy array  # Display the shapes of loaded data to verify successful loading print(allImages.shape)  # Print shape of image array print(allLabes.shape)   # Print shape of labels array  # Convert labels from text to integer values for model processing print(allLabes)  # Print original labels to see text values  from sklearn.preprocessing import LabelEncoder  # For encoding text labels into integers le = LabelEncoder()  # Initialize the label encoder integerLables = le.fit_transform(allLabes)  # Fit encoder and transform text labels to integers print(integerLables)  # Display encoded integer labels  # Count the number of unique breeds (categories) numOfCategories = len(np.unique(integerLables))  # Count unique integer labels, e.g., 120 breeds print(numOfCategories)  # Print the total number of categories  # Convert integer labels to categorical format (one-hot encoding) from tensorflow.keras.utils import to_categorical  # For one-hot encoding allLablesForModel = to_categorical(integerLables, num_classes=numOfCategories)  # Convert to categorical print(allLablesForModel)  # Display the one-hot encoded labels  # Normalize the image pixel values from 0-255 to 0-1 allImagesForModel = allImages / 255.0  # Scale image values to [0, 1]  # Split the data into training and testing sets from sklearn.model_selection import train_test_split  # For splitting data  print("Before split train and test:")  # Split images and labels into training (70%) and testing (30%) sets X_train, X_test, y_train, y_test = train_test_split(     allImagesForModel, allLablesForModel, test_size=0.3, random_state=42)  # Print shapes of training and testing data arrays to verify split print("X_train, X_test, y_train, y_test -----> shapes:") print(X_train.shape)  # Shape of training images print(X_test.shape)   # Shape of testing images print(y_train.shape)  # Shape of training labels print(y_test.shape)   # Shape of testing labels  # Free up memory by deleting unused variables del allImages del allLabes del integerLables del allImagesForModel  # Build the model using a pre-trained NASNetLarge model  from tensorflow.keras.layers import Dense, Flatten  # Import layers for model building from tensorflow.keras.models import Model  # Import Model class from tensorflow.keras.applications.nasnet import NASNetLarge  # Import NASNetLarge  # Load NASNetLarge model with pre-trained weights, excluding top layers myModel = NASNetLarge(input_shape=IMAGE_FULL_SIZE, weights='imagenet', include_top=False)  # Freeze all layers of the NASNetLarge model to prevent re-training for layer in myModel.layers:     layer.trainable = False  # Set layer to non-trainable     print(layer.name)  # Print layer name (optional)  # Add a flattening layer to prepare features for the final dense layer plusFlattenLayer = Flatten()(myModel.output)  # Flatten the output from NASNetLarge  # Add a dense layer for final predictions with softmax activation for multi-class classification prediction = Dense(numOfCategories, activation='softmax')(plusFlattenLayer)  # Create a new model with NASNetLarge as the base and custom dense layer on top model = Model(inputs=myModel.input, outputs=prediction)  # Uncomment to print a summary of the model architecture # print(model.summary())  from tensorflow.keras.optimizers import Adam  # Import Adam optimizer  # Set learning rate and initialize optimizer lr = 1e-4  # Learning rate for optimizer opt = Adam(lr)  # Initialize Adam optimizer with defined learning rate  # Compile the model with categorical crossentropy loss and accuracy metric model.compile(     loss='categorical_crossentropy',  # Loss function for multi-class classification     optimizer=opt,  # Use Adam optimizer     metrics=['accuracy']  # Track accuracy during training )  # Calculate the number of steps per epoch and validation steps stepsPerEpoch = np.ceil(len(X_train) / batchSize)  # Number of steps in each training epoch validationSteps = np.ceil(len(X_test) / batchSize)  # Number of steps for validation  # Set up callbacks for early stopping and model checkpointing from keras.callbacks import ModelCheckpoint, ReduceLROnPlateau, EarlyStopping  best_model_file = "e:/temp/dogs.h5"  # Path to save the best model  callbacks = [     ModelCheckpoint(best_model_file, verbose=1, save_best_only=True),  # Save best model during training     ReduceLROnPlateau(monitor='val_loss', patience=3, factor=0.1, verbose=1, min_lr=1e-6),  # Reduce LR on plateau     EarlyStopping(monitor='val_accuracy', patience=7, verbose=1)  # Stop training if no improvement in val accuracy ]  # Train the model r = model.fit(     X_train, y_train,     validation_data=(X_test, y_test),     epochs=30,  # Number of training epochs     batch_size=batchSize,  # Batch size for training     steps_per_epoch=stepsPerEpoch,  # Steps per epoch based on training set size     validation_steps=validationSteps,  # Steps for validation based on testing set size     callbacks=[callbacks]  # Include defined callbacks )

Code for the full here : https://ko-fi.com/s/3db9285a46


Step 3 : Test the Deep Learning dogs classification model :

This code loads a pre-trained model to classify a single image of a dog by breed.

It preprocesses the image to match the model’s input requirements, performs a prediction, and displays the predicted breed name on the image. The image with the prediction is then displayed and saved locally.

These are our test images :

1*e6y YQT5hUWS4jKRwUYzyA

Wheaten Terrier

1*vcSaHgrXv3ZipYr7GHogGw

Irish Water Spaniel

# Import necessary libraries import tensorflow as tf  # TensorFlow for loading and using the trained model import os  # For file path management import numpy as np  # For numerical operations import cv2  # For image processing  # Load the pre-trained model from a file modelFile = "e:/temp/dogs.h5"  # Path to the saved model model = tf.keras.models.load_model(modelFile)  # Load the model  # Uncomment to print a summary of the model architecture # print(model.summary())  # Set input image size for the model inputShape = (331, 331)  # Image dimensions expected by the model  # Load labels to get the breed names allLabes = np.load("e:/temp/allDogsLables.npy")  # Load labels from file categories = np.unique(allLabes)  # Get unique breed names  # Function to preprocess the input image def prepareImage(img):     resized = cv2.resize(img, inputShape, interpolation=cv2.INTER_AREA)  # Resize image to inputShape     imgResult = np.expand_dims(resized, axis=0)  # Add batch dimension for model input     imgResult = imgResult / 255.0  # Normalize pixel values to [0, 1]     return imgResult  # Path to the test image testImagePath = "TensorFlowProjects/Dog Breed Identification - Pre-trained - Works !!/Irish_Water_Spaniel1.png" # Alternative test image paths (can uncomment and use one of these) # testImagePath = "TensorFlowProjects/Dog Breed Identification - Pre-trained - Works !!/soft-coated_wheaten_terrier.jpg"  # Load the test image img = cv2.imread(testImagePath)  # Read the image from file imageForModel = prepareImage(img)  # Prepare image for the model  # Predict the breed of the dog in the image resultArray = model.predict(imageForModel, verbose=1)  # Get prediction from the model answers = np.argmax(resultArray, axis=1)  # Find the class with the highest probability  # Get the breed name corresponding to the predicted class print(answers)  # Print the integer label of the predicted breed text = categories[answers[0]]  # Retrieve breed name from categories array print(text)  # Print the breed name  # Display the breed name on the image font = cv2.FONT_HERSHEY_COMPLEX  # Set font style cv2.putText(img, text, (0, 20), font, 0.5, (209, 19, 77), 2)  # Add text to the image  # Show the image with the breed name overlayed cv2.imshow('img', img)  # Display the image in a new window cv2.waitKey(0)  # Wait for a key press to close the window  # Save the annotated image cv2.imwrite("e:/temp/img1.jpg", img)  # Save the image with breed name cv2.destroyAllWindows()  # Close all OpenCV windows

Code for the full here : https://ko-fi.com/s/3db9285a46


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