Machine Learning Project – Bird Species Identification

Bird Species Identification

Bird species identification is the process of using computers to recognize and classify different types of birds based on their appearance. By analyzing unique features and patterns in bird images, advanced algorithms can accurately determine the species of a bird. This technology has applications in studying bird populations, migration patterns, and habitat preferences. It helps us better understand and protect bird diversity, and it enables citizen science participation, birdwatching apps, and environmental research.

MobileNetV2 Model Overview

MobileNetV2 is a special type of neural network designed to quickly and accurately classify images. It uses clever techniques to balance accuracy and efficiency, making it ideal for devices with limited resources like phones or tablets. MobileNetV2 can adapt its size to fit different devices, and it’s good at recognizing important features in images. Overall, MobileNetV2 is a powerful tool for tasks like object recognition and can be used in various applications.

Techniques

Bird species identification using machine learning involves techniques such as CNNs, Transfer Learning, Data Augmentation, Feature Extraction, Ensemble Learning, One-Shot Learning, Deep Learning architectures, and Multi-modal Approaches. These techniques enable accurate classification by analyzing bird images, leveraging pre-trained models, increasing data diversity, extracting features, combining models, handling limited samples, utilizing advanced architectures, and incorporating multiple data sources.

Birds Species Dataset

The Birds Species dataset is a collection of high-resolution images of 25 bird species. While it focuses on a limited number of species, the dataset provides valuable training and evaluation data for machine learning models. Each image is labeled with the corresponding bird species, allowing for supervised learning approaches. The dataset offers diverse images capturing different poses, backgrounds, and lighting conditions. It serves as a valuable resource for researchers and enthusiasts interested in developing accurate bird species identification algorithms for these specific 25 species. The dataset contributes to fields such as ornithology, biodiversity studies, and conservation efforts.

Prerequisites for Bird Species Identification Using Machine Learning

A strong grasp of both the Python programming language and the OpenCV library is essential. Apart from this, you should have the following system requirements.

1. Python 3.7 and above
2. Google Colab

Download Machine Learning Bird Species Identification Project

Please download the source code of Machine Learning Bird Species Identification Project from the following link: Machine Learning Bird Species Identification Project Code.

Why Google Colab?

Google Colab is a convenient online platform for writing and running Python code. It provides access to high-performance computers in the cloud, eliminating the need for local installations. With ample memory and fast processors, Colab is well-suited for tasks involving large datasets and speedy machine learning model training. Additionally, it supports collaborative work, allowing easy sharing with others. Colab comes pre-loaded with popular libraries commonly used in machine learning, enabling users to start their projects promptly. For optimal performance, it is recommended to use Google Colab.

Let’s Implement It

First of all, change the Google colab runtime to GPU from the Runtime option available in the menu section and upload the zip file of the dataset in Google colab.

This line of code unzips the data.

!unzip bird.zip

Import all the libraries required for implementation.

import matplotlib.pyplot as plt
import pathlib, os, random
import numpy as np
import pandas as pd
import tensorflow as tf
from keras.layers import Dense, Flatten, Conv2D, MaxPooling2D, Activation, BatchNormalization, Dropout , GlobalAveragePooling2D
from keras.preprocessing.image import ImageDataGenerator
from keras import Sequential
import keras
from keras.callbacks import Callback, EarlyStopping,ModelCheckpoint

The code counts the number of different bird species in the training dataset. It looks at the folders in the specified training directory and counts how many unique folders (representing bird species) are present. The resulting count represents the number of distinct bird species in the dataset.

train="/content/train/"
no_birds_classes = os.listdir(train)
len(no_birds_classes)

The code retrieves the names of different bird species from the “train” directory and stores them in an array called “BirdClasses”. These names are sorted alphabetically for convenience.

data = pathlib.Path("/content/train")
BirdClasses = np.array(sorted([item.name for item in data.glob("*")]))
print(BirdClasses)

The output of this step

Bird Classes output

The code defines a function called “view_random_image” that displays a random image from a specified directory and class. It selects a random image file from the target class folder, reads and displays the image using matplotlib, and returns the image.

def view_random_image(target_dir, target_class):
  target_folder = target_dir + target_class
  random_image = random.sample(os.listdir(target_folder), 1)
  img = plt.imread(target_folder + "/" + random_image[0] )
  plt.imshow(img)
  plt.title(target_class)
  plt.axis("off")
  return img

The code displays a grid of 16 images randomly selected from the bird classes in the “train” directory. Each image is shown in a subplot, with the bird class name displayed as the subplot’s title.

fig, axes = plt.subplots(nrows=4, ncols=4, figsize=(10, 10),
                        subplot_kw={'xticks': [], 'yticks': []})

random_index = np.random.randint(0 , len(BirdClasses)-1 , 16)

for i, ax in enumerate(axes.flat):
    ax.imshow(view_random_image(train,BirdClasses[random_index[i]]))
    ax.set_title(BirdClasses[random_index[i]])

The output of this step

Bird Species Identification output

The variables “train_data”, “test_data”, and “val_data” store the paths to the directories where the training, testing, and validation data are located, respectively.

train_data = "/content/train/"
test_data = "/content/test/"
val_data = "/content/valid/"

The line of code imports the MobileNetV2 model from the Keras library’s applications module.

from keras.applications.mobilenet_v2 import MobileNetV2

The code prepares the data for training, validation, and testing. It resizes the images to a specific size, normalizes the pixel values, and encodes the labels.

train_gen = ImageDataGenerator(rescale=1./255)
test_gen = ImageDataGenerator(rescale=1./255)
val_gen = ImageDataGenerator(rescale=1./255)

data_train = train_gen.flow_from_directory( train_data , target_size=(224,224) , batch_size=32 , class_mode = "categorical" ,shuffle=True )

data_val = val_gen.flow_from_directory( val_data , target_size=(224,224) , batch_size=32 , class_mode = "categorical" , shuffle=True )

data_test = test_gen.flow_from_directory( test_data , target_size=(224,224) , batch_size=32 , class_mode = "categorical" ,shuffle=False )

The output of this step

specific size outpt

The code creates a MobileNetV2 model that has been pre-trained on the ImageNet dataset. The model summary provides information about the layers and the number of parameters in each layer.

mobilenet = MobileNetV2( include_top=False , weights="imagenet" , input_shape=(224,224,3))
mobilenet.summary()

The code makes the MobileNetV2 model trainable and freezes all layers except for the last 20 layers for fine-tuning.

mobilenet.trainable=True
for layer in mobilenet.layers[:-20]:
  layer.trainable=False

The model consists of a MobileNetV2 base followed by some additional layers. It uses GlobalAveragePooling to reduce dimensions, BatchNormalization for normalization, and Dense layers for classification. The model is designed to classify images into 25 different bird species.

Model = Sequential([
    mobilenet,
    GlobalAveragePooling2D(),
    BatchNormalization(),
    Dense(256,activation='relu'),
    BatchNormalization(),
    Dense(25,activation='softmax')
])

Model.summary()

The model is prepared for training and evaluation with the Adam optimizer, categorical cross-entropy loss, and accuracy as the metric.

Model.compile( optimizer="adam", loss="categorical_crossentropy" , metrics=["accuracy"] )

The EarlyStopping callback monitors the validation accuracy during training. If the validation accuracy does not improve for ten consecutive epochs, the training process is stopped, and the best weights of the model are restored.

callbacks = [EarlyStopping(monitor='val_accuracy' , patience=10 , restore_best_weights=True)]

Train the model using the training data for 15 epochs with a batch size of 32. Evaluate the model’s performance on the validation data during training and use early stopping with patience of 10.

history = Model.fit(data_train,epochs=15 , batch_size=32 ,steps_per_epoch = len(data_train)
,callbacks=callbacks ,workers=10,use_multiprocessing=True, validation_data=data_val,validation_steps = len(data_val))

The output of this step

Bird Species output

Evaluate the trained model on the test data and print the test loss and accuracy.

results = Model.evaluate(data_test, verbose=0)
print("Test Loss: {:.5f}".format(results[0]))
print("Test Accuracy: {:.2f}%".format(results[1] * 100))

The output of this step

test data and print output

Make predictions on the test data using the trained model and obtain the predicted class labels.

pred = Model.predict(data_test)
pred = np.argmax(pred,axis=1)

Display the true label, predicted label, and the corresponding image from the test dataset at the specified index.

index =2
img , label = data_test[index]
label = data_test.labels[index]
print(f"True Label: {BirdClasses[label]}")
print(f"Predicted Label: {BirdClasses[pred[index]]}")  
plt.imshow(img[0])
plt.show()

Output

output Bird Species Identification

output Bird Species

Load an image from the specified path and preprocess it. Obtain the true label from the image path. Make predictions on the preprocessed image using the trained model. Display the image along with the true label and the predicted label.

from PIL import Image
from tensorflow.keras.preprocessing import image
import matplotlib.pyplot as plt
import numpy as np
image_path = "/content/test/BLACK-THROATED SPARROW/2.jpg"
image_width = 224
image_height = 224
img = image.load_img(image_path, target_size=(image_width, image_height))
img_array = image.img_to_array(img)
img_array = np.expand_dims(img_array, axis=0) 
true_label = 'BLACK-THROATED SPARROW'
img_array = img_array / 255.0 
predictions = Model.predict(img_array)
predicted_label = np.argmax(predictions, axis=1)
plt.imshow(img)
plt.axis("off")
plt.title(f"True Label: {true_label} \nPredicted Label: {BirdClasses[predicted_label[0]]}")
plt.show()

Output

Bird Species outp

Bird Species

Conclusion

In summary, bird species identification using machine learning has made significant progress. By using advanced models and techniques, we can accurately classify different bird species. Techniques such as data augmentation, ensemble learning, and one-shot learning have further improved the accuracy of identification systems. Moreover, by combining multiple data sources like images, these advancements contribute to our understanding and conservation of bird species, benefiting biodiversity conservation efforts.