This repository contains the implementation of a MobileNet model from scratch to classify images of cats and dogs using TensorFlow and Keras.
model.py
: Defines the MobileNet model architecture.train.py
: Handles training and evaluation of the model.test.py
: Loads an image, preprocesses it, and uses the trained model to make predictions.utils.py
: Contains utility functions such as image preprocessing.main.py
: The main script to run the training and testing.
-
Clone the repository:
git clone https://github.com/smhatefi/mobilenet_v1.git cd mobilenet_v1
-
Install the required packages:
Make sure you have Python and pip installed. Then, install the required Python packages:
pip install tensorflow keras matplotlib tensorflow-datasets
-
Download sample images for testing:
The
main.py
script downloads sample images for testing.
-
Train the model and test it with sample images:
Run the
main.py
script to train and evaluate the model:python main.py
This will:
- Train the MobileNet model on the Cats vs. Dogs dataset.
- Save the trained model to a file
mobilenet_model.h5
. - Evaluate the model on the test set and print the test accuracy.
The
main.py
script also includes a test phase that:- Loads the trained model.
- Preprocesses a sample image.
- Makes a prediction and displays the image with the predicted class label.
-
Test the model without training:
Download your desired image and save it in the main directory of project with the name
test.jpg
# For Example wget -q -O test.jpg https://www.alleycat.org/wp-content/uploads/2019/03/FELV-cat.jpg
Run the
test.py
script to test the model:python test.py
This will:
- Loads the trained model.
- Preprocesses the sample image.
- Makes a prediction and displays the image with the predicted class label.
In this case the model uses the pre-trained weights
mobilenet_model.h5
which i have provided in the repository.
utils.py
contains helper functions for preprocessing images:
- preprocess(image, label): Preprocesses the dataset images (resize and normalize).
- load_and_preprocess_image(image_path): Loads and preprocesses a single image for prediction.
The dataset used for training is the Cats vs. Dogs dataset from TensorFlow Datasets.
- The model architecture and training parameters can be adjusted in
model.py
andtrain.py
respectively. - The dataset used is the Cats vs. Dogs dataset available in TensorFlow Datasets. Ensure you have a stable internet connection to download the dataset.
This project is licensed under the MIT License. See the LICENSE
file for details.
- The MobileNet model architecture is inspired by the original MobileNet paper.
- To implement the architecture, this post on Kaggle has been used.