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.
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 |
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.
-
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.
-
- 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])
The model's performance is evaluated on the validation set, with metrics including loss and accuracy.
validation_loss, validation_accuracy = model.evaluate(val_generator)
In this project, several visualizations are used to analyze the training process. Below are the performances of the visualizations included in this project:
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.
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.
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 |
A function is provided to preprocess and recognize images from the unseen dataset, displaying the predictions.
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.
Contributions are welcome! If you have any suggestions for improvements or find any issues, please submit an issue or a pull request.
This project is licensed under the MIT License.
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.