This project demonstrates the process of fine-tuning the InceptionV3 deep learning model for food image classification using a custom dataset. The primary objective is to develop a model capable of accurately identifying various food ingredients from images. This capability is a fundamental functionality of the ZeroWasteCook application, which aims to help users reduce food waste by identifying ingredients they have and suggesting recipes. InceptionV3, a state-of-the-art convolutional neural network pre-trained on the massive ImageNet dataset, requires input images to be preprocessed to a standard size of 299x299 pixels.
The dataset used in this project is the "Food Classification Dataset" provided by Bjorn on Kaggle. It contains a collection of images featuring various food items. We will leverage this data to train our model to accurately classify different food ingredients.
Dataset Link: https://www.kaggle.com/datasets/bjoernjostein/food-classification
- Import Libraries π: Imports necessary libraries for PyTorch, data manipulation, and visualisation.
- Upload and Extract Dataset π: Handles the uploading and extraction of the dataset from a ZIP file.
- Load and Summarise Data π: Reads the dataset labels and image directory, and provides a summary of the data.
- Custom Dataset Class ποΈ: Defines a custom PyTorch
Datasetclass (IngredientsDataset) to handle loading and preprocessing of the food images and their labels. - Preprocessing/Transform Function β¨: Defines the image transformations required for the InceptionV3 model, including resizing, cropping, converting to tensor, and normalisation.
- Train/Validation Split π: Splits the dataset into training and validation sets.
- Create DataLoader π: Creates PyTorch
DataLoaderinstances for efficient batching and shuffling of the training and validation data. - Display Image Sample πΌοΈ: Visualises a batch of images from the training data.
- Assign Hyperparameters βοΈ: Sets the hyperparameters for the model training process, such as the number of classes, learning rate, and number of epochs.
- Create InceptionV3 Model Class π§ : Defines a custom PyTorch
nn.Moduleclass (InceptionV3Network) that loads the pre-trained InceptionV3 model and modifies the final layer for the specific number of food classes. - Train Model Function πͺ: Implements the training loop for the InceptionV3 model, including forward and backward passes, loss calculation, and optimizer updates. It also includes plotting of training losses.
- Evaluate Model Function β : Implements the evaluation of the trained model on the validation set, calculating overall accuracy and providing a classification report with per-class metrics.
- Train, Visualise, and Evaluate π: Executes the training and evaluation functions.
- Save Model πΎ: Saves the trained model's state dictionary to a file.
- Upload the
archive.zipdataset file when prompted. - Run the cells sequentially to preprocess the data, define and train the model, and evaluate its performance.
The project provides the overall accuracy of the fine-tuned InceptionV3 model on the validation set and a detailed classification report showing precision, recall, and f1-score for each food class.
- Data Augmentation: Explore techniques to enhance the dataset and improve model generalisation. π
- Hyperparameter Tuning: Experiment with different settings to optimise model performance. π§
We would like to acknowledge Bjorn for providing the "Food Classification Dataset" on Kaggle, which was essential for training and evaluating the model in this project.
