Skip to content

Repository files navigation

Flower Classification with MobileNetV2

This repository contains an explanatory Jupyter notebook for building a flower classifier using transfer learning with MobileNetV2. The project uses the TensorFlow flower dataset and demonstrates how a pretrained convolutional neural network can be adapted for image classification with clear, step-by-step code and explanations.

Project Overview

This notebook walks through a complete computer vision workflow for classifying flower images into five categories: daisy, dandelion, rose, sunflower, and tulip. It shows how to move from dataset preparation to model training, evaluation, and refinement using transfer learning.

What This Project Demonstrates

  • Transfer learning with a pretrained MobileNetV2 model
  • Reproducible training setup using fixed seeds
  • Stratified train, validation, and test splits
  • Class distribution analysis and dataset inspection
  • Model training with feature extraction and fine-tuning
  • Evaluation using accuracy, loss, classification report, and confusion matrix
  • Practical use of callbacks such as checkpointing and early stopping

Technologies Used

  • Python
  • TensorFlow / Keras
  • NumPy
  • Matplotlib
  • Seaborn
  • scikit-learn
  • Jupyter Notebook

Project Files

Key Results

  • The base transfer-learning model achieved a test accuracy of 89.29%.
  • After fine-tuning selected layers with a lower learning rate, the model improved to 89.47% test accuracy.
  • The notebook includes a classification report and confusion matrix to analyze performance beyond overall accuracy.

Workflow

  1. Download the TensorFlow flower dataset
  2. Explore class names and image distribution
  3. Create stratified train, validation, and test splits
  4. Build and preprocess image batches for training
  5. Train the MobileNetV2 model using transfer learning
  6. Fine-tune deeper layers to improve performance
  7. Evaluate the final model and inspect predictions

28-Step Workflow Explained

The notebook follows a structured, educational workflow. The steps below reflect the full sequence used in the project:

  1. Step 0: Install the required Python packages if they are not already available.
  2. Step 1: Import the libraries needed for data handling, visualization, and model building.
  3. Step 2: Set the random seed and configure image size, batch size, and training epochs.
  4. Step 3: Download the TensorFlow flower dataset.
  5. Step 4: Discover the flower classes and collect the image file paths for each category.
  6. Step 5: Inspect the class distribution to understand whether the dataset is balanced.
  7. Step 6: Split the data into training, validation, and test sets using stratification.
  8. Step 7: Verify that the class proportions remain balanced across the splits.
  9. Step 8: Build a memory-efficient TensorFlow data pipeline for loading and batching images.
  10. Step 9: Inspect a batch of images before training to confirm the pipeline is working correctly.
  11. Step 10: Prepare the data flow so the model can train efficiently on batches of images.
  12. Step 11: Apply data augmentation techniques to improve generalization and reduce overfitting.
  13. Step 12: Visualize the augmented images to confirm that the transformations look reasonable.
  14. Step 13: Load the pretrained MobileNetV2 base model for transfer learning.
  15. Step 14: Freeze the pretrained layers and prepare the model for feature extraction training.
  16. Step 15: Inspect how many parameters are trainable versus frozen.
  17. Step 16: Compile the model with an optimizer, loss function, and accuracy metric.
  18. Step 17: Define training callbacks for checkpointing, early stopping, and learning rate reduction.
  19. Step 18: Train the model during the initial transfer-learning phase.
  20. Step 19: Plot training and validation accuracy/loss curves to analyze learning behavior.
  21. Step 20: Evaluate the trained model on the untouched test set.
  22. Step 21: Generate detailed test predictions for each image in the test set.
  23. Step 22: Create a classification report to measure precision, recall, and F1-score per class.
  24. Step 23: Build a confusion matrix to inspect where the model is making mistakes.
  25. Step 24: Visualize correct and incorrect predictions to understand model behavior qualitatively.
  26. Step 25: Create a function to predict the class of a single new image.
  27. Step 26: Unfreeze selected layers and continue training with a lower learning rate for fine-tuning.
  28. Step 28: Fine-tune the model and re-evaluate its performance on the test data.

Why This Project Is Valuable

This project is a strong example of applied deep learning and computer vision. It highlights how pretrained models can be used efficiently to solve image classification tasks while keeping the workflow understandable and practical.

How to Run

python -m venv venv
source venv/bin/activate      # Windows: venv\Scripts\activate
pip install tensorflow scikit-learn matplotlib seaborn pillow jupyter

Then open the notebook in Jupyter and run the cells in order.

What You Can Learn From This Project

  • How transfer learning works in practice
  • How to evaluate and compare model performance
  • How to improve results through fine-tuning
  • How to structure a notebook for clear experimentation and explanation

Contact

For questions or collaboration opportunities, feel free to connect through GitHub.

About

MobileNet — a Jupyter Notebook demonstrating how to fine-tune MobileNet on a custom dataset and prepare a model for mobile deployment.

Topics

Resources

Stars

0 stars

Watchers

0 watching

Forks

Releases

Packages

Contributors

Languages