This project demonstrates the use of various convolutional neural network (CNN) architectures for butterfly image classification. It includes model training, evaluation, and deployment using FastAPI.
The project performs the following tasks:
- Downloads the butterfly image classification dataset from Kaggle.
- Preprocesses the images for training.
- Trains multiple CNN architectures (Proprietary CNN, VGG16, ResNet50, and Baseline CNN).
- Evaluates model performance.
- Serves the trained models through a FastAPI application to predict the class of uploaded butterfly images.
Ensure you have the following dependencies installed:
tensorflow
fastapi
uvicorn
pandas
numpy
PillowInstall the required packages:
pip install tensorflow fastapi uvicorn pandas numpy PillowThe dataset used in this project is the Butterfly Image Classification dataset, which you can download from Kaggle. Ensure you have the Kaggle API set up to download the dataset.
-
Download the dataset:
- The dataset will be automatically downloaded when you run
main.py.
- The dataset will be automatically downloaded when you run
-
Train the models:
- Run
main.pyto perform data preprocessing and model training.
python main.py
The trained models will be saved as
proprietary_cnn.h5,vgg16_model.h5,resnet50_model.h5, andbaseline_cnn.h5. - Run
-
Start the FastAPI server:
- Run
inference.pyto start the FastAPI server.
python inference.py
The server will run and provide an endpoint for accessing the API.
- Run
-
Test the API: You can use tools like Postman or
curlto send POST requests to the API.Example
curlrequest:curl -X POST "http://127.0.0.1:8000/predict" -F "model_type=proprietary_cnn" -F "image=@path/to/your/image.jpg"
The response will return the predicted class for the uploaded image.
The main.py script performs the following:
- Downloads the Butterfly Image Classification dataset from Kaggle.
- Loads and preprocesses the images.
- Defines and trains multiple CNN architectures:
- Proprietary CNN
- VGG16
- ResNet50
- Baseline CNN
- Evaluates the models and prints the test accuracy.
- Saves the trained models to disk.
The inference.py script uses FastAPI to serve predictions based on the trained models. You can send data in the following format:
POST /predict
Form Data:
- model_type: "proprietary_cnn" (or "vgg16", "resnet50", "baseline_cnn")
- image: [upload your image file]
The response will provide the predicted class for the uploaded image.