Skip to content

ACM40960/project-Chenxi-Li

Repository files navigation

Black Bear vs Newfoundland Dog Image Recognition

Python Jupyter License Machine Learning

DenseNet121 Logo

Introduction

This project aims to classify similar images of black bears and Newfoundland dogs using a deep learning model built on DenseNet121 with customer layers. The project involves data loading, data augmentation, model training, evaluation, and visualization of results.

Dataset

The dataset used in this project consists of 215 images of black bears and 371 images of Newfoundland dogs. The images are collected from public image databases, including different angles, lighting conditions, and backgrounds. Stratified sampling is used to keep that the proportion of labels in each subsets is consistent with that in original data.

  • Data structure
    • data/Black bear
    • data/Newfoundland

Dataset is split as follows:

Dataset proportion numbers
Train 0.64 374
Test 0.16 94
Validation 0.20 118

Methodology

In this project, we utilize a transfer learning model based on DenseNet121 for the classification of images into two classes: Black Bear and Newfoundland. The core algorithm leverages the pre-trained DenseNet121 architecture as a feature extractor, combined with additional layers to enhance the model's performance. Some customer layers and EarlyStopping Callback function are added to it to improve the generalization ability of the model and prevent overfitting.

Model Architecture

Model Structure

  • All layers in the DenseNet121 model are frozen to take advantage of the features extracted from the pre-trained model without updating their weights. Custom layers are added on top of the base model for this specific classification task.

  • Additional layers are added on top of DenseNet121:

    • BatchNormalization Layer: Normalizes the output to stabilize and accelerate training.

    • Dropout Layer: Prevents overfitting by randomly setting a fraction of input units to zero.

    • Flatten Layer: Converts the feature maps into a one-dimensional vector.

    • Dense Layer (128 units): Fully connected layer with ReLU activation for further processing.

    • Dropout Layer: Additional dropout for regularization.

    • Output Dense Layer (2 units): Fully connected layer with softmax activation to output the probability distribution for the two classes.

Model Traning

  • Data Loading and Spliting

Images of black bears and Newfoundland dogs are loaded and split into training, validation, and test sets. Stratified sampling is used to keep that the proportion of labels in each subsets is consistent with that in original data.

# Split data 
train_df, test_df = train_test_split(df, test_size=0.2, stratify=df['label'], random_state=42)
train_df, val_df = train_test_split(train_df, test_size=0.2,
stratify=train_df['label'], random_state=42)
  • Creating Data Generators

Use ImageDataGenerator to create data generators for training, validation, and test sets, and images are scaled and normalized.

# Create data generators
train_generator, val_generator, test_generator = create_generators(train_df, val_df, test_df)
  • Model Construction

Construct a custom sequential model based on the pre-trained DenseNet121 model. DenseNet121 is used as the base model, and additional BatchNormalization, Dropout, and Dense layers are added to enhance generalization and prevent overfitting.

from tensorflow.keras.applications import DenseNet121
from tensorflow.keras.layers import Dense, Dropout, BatchNormalization, Flatten
from tensorflow.keras.models import Sequential

# Load the pre-trained DenseNet121 model without the top layer
base_model = DenseNet121(weights='imagenet', include_top=False, input_shape=(224, 224, 3))
base_model.trainable = False  # Freeze the base model

# Create a custom model on top of the DenseNet121 base
model = Sequential([
    base_model,
    BatchNormalization(),
    Dropout(0.25),
    Flatten(),
    Dense(128, activation='relu'),
    Dropout(0.25),
    Dense(2, activation='softmax')  
])

model.compile(optimizer='adam', loss='categorical_crossentropy', metrics=['accuracy'])
  • Model Training

Train the model using the training and validation data generators, and employ an EarlyStopping callback to prevent overfitting.

from tensorflow.keras.callbacks import EarlyStopping

early_stopping = EarlyStopping(monitor='val_loss', patience=10, restore_best_weights=True)
history = model.fit(train_generator, validation_data=val_generator, epochs=15,
callbacks=[early_stopping])

Model Evaluation

The model's performance is evaluated on the validation set, with metrics including loss and accuracy.

validation_loss, validation_accuracy = model.evaluate(val_generator)

Visualization:

In this project, several visualizations are used to analyze the training process. Below are the performances of the visualizations included in this project:

Training History: Accuracy and loss plots for training and validation sets.

The training and validation accuracy increase steadily and plateau around epoch 8, reaching nearly 100%. The training and validation loss decrease significantly and stabilize after epoch 6, indicating a well-fitting model with minimal overfitting. Model Structure

Confusion Matrix: Heatmap to show the confusion matrix.

Model Structure

ROC Curve: ROC curves and AUC scores for each class.

The ROC curve demonstrates the model’s ability to distinguish between classes, with an area under the curve (AUC) of 1.00, indicating perfect performance.

Model Structure Model Structure

Classification Report

The model achieved high performance metrics with targets of 0.98 for black bears and 0.99 for Newfoundland dogs.The overall accuracy of the model is 0.98, demonstrating its effectiveness in distinguishing between the two classes.

precision recall f1-score support
black bear 0.98 0.98 0.98 43
newfoundland 0.99 0.99 0.99 75
accuracy 0.98 118
macro avg 0.98 0.98 0.98 118
weighted avg 0.98 0.98 0.98 118

Visualizing Predictions on Unseen Data:

A function is provided to preprocess and recognize images from the unseen dataset, displaying the predictions.

Model Structure

Usage

Clone the repository:

git clone https://github.com/ACM40960/project-Chenxi-Li.git
cd project-Chenxi-Li

Install dependencies:

pip install tensorflow pandas numpy matplotlib scikit-learn seaborn colorama

Run the code: Open the Final Project(pure code).ipynb file in Jupyter Notebook and run all cells in order.

Contributing

Contributions are welcome! If you have any suggestions for improvements or find any issues, please submit an issue or a pull request.

License

This project is licensed under the MIT License.

References

Datasets: https://www.kaggle.com/datasets/hoturam/bear-dataset https://www.kaggle.com/datasets/imbikramsaha/dog-breeds

Muhammad Abdullah. (2024). Hotdog or Not: The Ultimate Classifier.

About

project-ChesleyLee created by GitHub Classroom

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published