Giter VIP home page Giter VIP logo

foodrecognitionml's Introduction

Thyme & Budget Recognition Service

This repository contains the source code for Thyme & Budget Recognition Service along with the instructions to train the model. Built with Python, TensorFlow, and the lightweight yet robust MobileNetV2 architecture, our model is designed to accurately classify food and non-food items.

Dataset

The model is trained on the Food-5K image dataset provided on Kaggle. The dataset consists of food and non-food images, divided into 3 sets - train, validation, and evaluation. Each set contains 2 categories - food and non_food, the training set contains 3000 images each while the other set consists of 1000 images.

Prerequisites

  • Python 3.7 or later
  • TensorFlow 2.x
  • pip

Local Setup

  1. Clone the repository: git clone https://github.com/xrando/FoodRecognitionML.git
  2. Navigate to the project directory: cd FoodRecognitionML
  3. Install the required Python packages: pip install -r requirements.txt

Training the Model

The model training script is Food_Recognition_model_training.py. This script uses TensorFlow and Keras to train a binary classification model. The model is based on the MobileNetV2 architecture, a lightweight deep learning model designed for mobile and embedded vision applications.

The script uses transfer learning, where the MobileNetV2 model, pre-trained on the ImageNet dataset, is used as a feature extractor. The output of this base model is then passed through a custom classification head, which consists of a global average pooling layer, a dense layer with 1024 units and ReLU activation, a dropout layer for regularization, and a final dense layer with sigmoid activation for binary classification.

The model is trained using the Adam optimizer with a learning rate schedule, binary cross-entropy loss, and accuracy as the metric.

The model has already been pre-trained and is available in the repository. However, if you wish to train the model again, run the model training script: python Food_Recognition_model_training.py

This will train the model and save it as food_classification_model.h5 in the project directory.

Evaluating the Model

After training, the model is evaluated on a test set. The script prints a classification report and confusion matrix, and plots the training and validation accuracy over epochs to visualize the model's learning process.

Model Performance and Tuning

The training process was carried out over 20 epochs. The model's performance improved over time, as evidenced by the decreasing loss and increasing accuracy on the training set as seen in Training.txt. The validation loss fluctuated, indicating that the model was learning to generalize to unseen data, but also that it was sensitive to the specific samples in the validation set. The highest validation accuracy achieved was 97.78% in the 15th epoch.

However, the model's performance on the validation set varied, with the accuracy ranging from 54.03% to 97.78%. After the final epoch, the model achieved a training accuracy of 99.29% and a validation accuracy of 94.56%. This indicates that the model was able to learn the training data well and also generalize to unseen data to a significant extent.

The model was then evaluated on the test set, achieving an accuracy of 92%. The precision and recall for the two classes were also high, indicating that the model was able to correctly identify the majority of samples in both classes. The confusion matrix further illustrates this, showing that the model made relatively few misclassifications.

The detailed performance metrics on the test set are as follows:

Metric Class 0 Class 1
Precision 0.99 0.91
Recall 0.90 0.99
F1-Score 0.94 0.95
Support 500 500
  • Accuracy: 0.95
  • Macro Average: 0.95
  • Weighted Average: 0.95

Confusion Matrix

The confusion matrix for the model on the test set is as follows (1 = food, 0 = non-food):

img.png

Plot for training and validation accuracy

alt text

Running the Application Locally

Start the Flask application: flask run --host=0.0.0.0

The application will be accessible at http://localhost:5000.

Testing the Application locally

Run the testing script: python Food_Recognition_testing.py

This will classify the images in the test_imgs directory as food or non-food.

Deploying to AWS EC2

  1. Create an EC2 instance (Ubuntu Server 20.04 LTS is recommended).
  2. Connect to the instance via SSH.
  3. Update the package lists for upgrades and new package installations: sudo apt update
  4. Install Python 3, pip, and git: sudo apt install python3 python3-pip git
  5. Clone the repository: git clone https://github.com/xrando/FoodRecognitionML.git
  6. Navigate to the project directory: cd FoodRecognitionML
  7. Install the required Python packages: pip3 install -r requirements.txt
  8. Start the Flask application: flask run --host=0.0.0.0

The application will be accessible at your EC2 instance's public IP address on port 5000.

Testing the Deployed Application

To test the deployed application, you can send a POST request to the deployed endpoint using the provided script after modifying the endpoint to that of the deployed server: python Send_Post_Request_To_Endpoint.py

This script sends an image to the /classify-image endpoint of the deployed application and prints the response.

Author

Benjamin Loh Choon How, 15/3/2024

foodrecognitionml's People

Contributors

xrando avatar

Watchers

 avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.