A powerful AI application built using EfficientNetB2 that can detect the type of food in an image and determine whether the image contains food or not.
Powered by BentoML π± and PyTorch π₯
FoodGeniusAI is an AI-powered food classification system that instantly identifies dishes from images. Built with EfficientNetB2 and BentoML, it offers both food detection and detailed dish classification with 80% accuracy.
- π Instant Food Detection: Automatically distinguishes food from non-food images
- π 101 Food Categories: Recognizes a wide variety of dishes with 80% accuracy
- β‘ Fast Processing: Optimized for real-time classification
- π Production-Ready: Deployable with BentoML for scalable serving
- π± REST API Support: Easy integration with any application
FoodGeniusAI uses two EfficientNetB2 models for food classification:
- Purpose: Determines if image contains food
- Performance: 90% accuracy
- Input: 224x224 RGB images
- Output: Binary classification (food/non-food)
- Training: Food5K dataset (5,000 images)
- Purpose: Identifies specific food category
- Performance: 80% accuracy
- Input: 224x224 RGB images
- Output: 101 food categories
- Training: Food101 dataset (101,000 images)
- Image β Food/Non-Food Detection
- If food detected β Food Category Classification
- Return prediction score for each category
-
ML & Training
- π§ EfficientNetB2: Lightweight and efficient CNN for image classification
- π₯ PyTorch: Deep learning framework for model training
- π Jupyter: Interactive development and model experimentation
-
UI & Serving
- π¨ Gradio: Interactive web interface for model demo
- π± BentoML: ML model serving and deployment
- π³ Docker: Containerization for consistent deployments
-
Infrastructure
- β Kubernetes: Container orchestration at scale
- π Prometheus & Grafana: Real-time metrics and visualization
Try out FoodGeniusAI instantly on Hugging Face Spaces:
- Requirements
- Running the Service
- Using the Service
- Training and Prediction
- Jupyter Notebooks
- Deploying to Kubernetes
- Observability
- Python 3.11+
- CUDA-compatible GPU (optional, for faster processing)
- Docker (optional, for containerization)
Clone the repository and install the dependencies:
git clone https://github.com/daviaraujocc/FoodGeniusAI.git
cd FoodGeniusAI
pip install -r requirements/test.txtbentoml serve Access at http://127.0.0.1:3000 and interact with the service through the Swagger UI.
To run the service in a container, you can use the following commands:
For CPU:
bentoml build -f bentofile.yamlbentoml containerize foodgenius-service:latestdocker run -p 3000:3000 foodgenius-service:$(bentoml get foodgenius-service:latest | yq -r ".version")For GPU:
bentoml build -f bentofile.gpu.yamlbentoml containerize foodgenius-service-gpu:latest docker run --gpus all -p 3000:3000 foodgenius-service-gpu:$(bentoml get foodgenius-service-gpu:latest | yq -r ".version")Note that to run with GPU you will need to have nvidia-container-runtime setup.
You can also use the service as a Python API:
bentoml build -f bentofile.yamlthen start the service:
import bentoml
bento = bentoml.get('foodgenius-service:latest')
server = bentoml.HTTPServer(bento, port=3000)
server.start(blocking=True)You can use the BentoML service with HTTP requests. Here are some examples:
The following example shows how to send a request to the service to classify an image via cURL:
curl -X POST \
'http://0.0.0.0:3000/classify' \
-H "Content-Type: multipart/form-data" \
-F "img=@examples/images/pizza.jpg"To send requests via client using python library:
IMG_PATH = "examples/images/pizza.jpg"
if __name__ == "__main__":
import bentoml
client = bentoml.SyncHTTPClient("http://localhost:3000")
print("Predictions for image {}".format(IMG_PATH))
print(client.classify(img=IMG_PATH))
client.close()Before running the scripts/notebooks, it's recommended to create a new environment:
conda env create -f environment.yml
conda activate foodgeniusYou can train the models using the train.py script. Here are the steps:
-
Train the
food_or_nonfoodmodel:python train.py \ --model food_or_nonfood \ --epochs 5 \ --model_name pretrained_effnetb2_food_or_nonfood.pth \ --split_size 1 \ --batch_size 32 \ --learning_rate 0.001 \ --device cuda # or cpu
-
Train the
food101model:python train.py \ --model food101 \ --epochs 10 \ --model_name pretrained_effnetb2_food101.pth \ --split_size 0.2 \ --batch_size 32 \ --learning_rate 0.001 \ --device cuda # or cpu
Use device
cudaif you have a GPU compatible available.
Results for the training process including accuracy, loss will be saved in the results directory.
| Parameter | Default | Description |
|---|---|---|
epochs |
5 |
Number of epochs for training |
batch_size |
32 |
Batch size for training |
split_size |
0.2 |
Train-test split size |
device |
cuda |
Device for training (cuda or cpu) |
learning_rate |
0.001 |
Learning rate for training |
model_name |
model.pth |
Name of the trained model file |
results/
β
βββ food101/
| βββ model_name
| βββ model.pth
β βββ model_results.csv
β βββ model_results.png
βββ food_or_nonfood/
| βββ model_name
| βββ model.pth
β βββ model_results.csv
β βββ model_results.png
You can make predictions using the predict.py script. Here are the steps:
-
Predict using the
food_or_nonfoodmodel:python predict.py \ --model food_or_nonfood \ --image path/to/image.jpg \ --model_path models/pretrained_effnetb2_food_or_nonfood.pth \ --device cpu
-
Predict using the
food101model:python predict.py \ --model food101 \ --image path/to/image.jpg \ --model_path models/pretrained_effnetb2_food101.pth \ --device cpu
This repository includes several Jupyter Notebooks that demonstrate the training and prediction processes using EfficientNetB2.
-
Food or Non-Food Classification
- effnetb2_training_food_or_nonfood.ipynb
- Trains a model on the Food5K dataset to classify images as food or non-food.
-
Food101 Classification
- effnetb2_training_food101.ipynb
- Trains a model on the Food101 dataset to classify images into 101 types of food.
- EfficientNetB2 Prediction
- effnetb2_predict.ipynb
- Demonstrates how to use the trained EfficientNetB2 model to make predictions on new images.
To deploy the service to k8s, you can use the following commands:
bentoml build -f bentofile.yamlbentoml containerize foodgenius-service:latest --image-tag {your-username-repo}/foodgenius-service:latestdocker push {your-username-repo}/foodgenius-service:latestEdit the manifests/deployment.yaml file to include your image, then apply it to your Kubernetes cluster:
kubectl apply -f manifests/deployment.yamlBentoML provides built-in observability features, including Prometheus metrics. You can access these metrics at the /metrics endpoint.
To install monitoring stack on kubernetes, you can do the following steps:
chmod +x scripts/setup_monitoring.sh; ./scripts/setup_monitoring.shThis script will install prometheus + grafana stack on namespace monitoring.
Access grafana dashboard (default username/password is admin):
kubectl port-forward svc/grafana -n monitoring 3000:3000Davi Araujo (@daviaraujocc)
- Automate training/deploy via ArgoCD Workflows + GitOps

