Easy-to-use library for extending Sri Lankan food recognition models
Project description
๐ Sri Lankan Food Trainer
Easy-to-use Python library for extending the Sri Lankan Food Recognition model with your own vegetable classes โ no deep learning expertise required.
Built on a custom Prototypical Network trained on 8 Sri Lankan vegetableโcooking state combinations. Add a new class with as few as 20โ30 images, without retraining the full model from scratch.
Why This Library?
Traditional Sri Lankan cooking โ red curry, white curry, tempering โ transforms vegetables so dramatically that standard food recognition models fail to identify them. This library lets you extend a model specifically built for that challenge, and adapt it to new vegetables with minimal data.
| Approach | Images needed | Time |
|---|---|---|
| Train from scratch | 500โ1000+ per class | Days |
| This library | 20โ50 per class | 30โ60 minutes |
Installation
pip install srilankan-food-trainer
Requirements: Python 3.8+, PyTorch 2.0+, internet connection on first run (downloads pre-trained model from Hugging Face automatically)
GPU is recommended but not required โ CPU training works, just slower.
Quick Start
from srilankan_food_trainer import FoodModelExtender
# 1. Create extender โ downloads pre-trained model automatically
extender = FoodModelExtender(verbose=True)
# 2. Add your class
# ZIP filename becomes the class name:
# e.g. potato_tempered.zip โ class "potato_tempered"
extender.add_class("potato_tempered", "potato_tempered.zip", auto_extract=True)
# 3. Train (preserves original 8 classes + adds your new one)
extender.train(epochs=50)
# 4. Save
extender.save("extended_model.pth")
The saved model recognises all 9 classes โ original 8 + your new one.
Step-by-Step Guide
Step 1 โ Prepare your images
Collect 30โ50 images of your food class. Organise them in a folder, then zip it:
potato_tempered/ โ folder name = class name
โโโ image_001.jpg
โโโ image_002.jpg
โโโ image_003.jpg
โโโ ... (30โ50 images)
zip -r potato_tempered.zip potato_tempered/
Important: The ZIP filename becomes the class name. Use the format
vegetable_cookingstate(e.g.beetroot_white_curry.zip).
Step 2 โ Initialise the extender
from srilankan_food_trainer import FoodModelExtender
extender = FoodModelExtender(verbose=True)
# Output:
# ๐ Sri Lankan Food Model Extender initialized!
# Device: cuda
# Base model: ranasinghehashini/srilankan-food-recognition
Step 3 โ Add your class
extender.add_class(
class_name="potato_tempered",
images_path="potato_tempered.zip",
auto_extract=True
)
# Output:
# ๐ธ Adding new class: potato_tempered
# Found 37 images
# Split complete:
# Train: 25 images
# Val: 5 images
# Test: 7 images
The method automatically validates images, checks formats, and splits into train / validation / test sets (70% / 15% / 15%).
Step 4 โ Train
results = extender.train(epochs=50, save_checkpoints=True)
print(f"Best validation accuracy: {results['best_val_acc']:.2f}%")
print(f"Training time: {results['total_time']/60:.1f} minutes")
Expected training time: 30โ45 min with GPU, 60โ90 min on CPU.
Step 5 โ Save and use
extender.save("extended_model.pth")
Load it later with:
import torch
checkpoint = torch.load("extended_model.pth")
# checkpoint contains:
# - model_state_dict โ neural network weights
# - original_classes โ the 8 original class names
# - new_classes โ your added class names
# - training_history โ loss and accuracy per epoch
# - base_model โ source model reference
# - timestamp โ when trained
Image Requirements
| Requirement | Details |
|---|---|
| Minimum images | 20 per class |
| Recommended | 30โ50 per class |
| Format | JPG or PNG |
| Resolution | Minimum 224 ร 224 |
| Packaging | All images in one folder, zipped |
| ZIP naming | ZIP filename = class name |
Tips for good results:
- Use clear, well-lit photos
- Include different angles (top view, side view, close-up)
- Keep the subject consistent โ all images should be the same food type and cooking state
- Remove blurry or dark images before zipping
Naming Convention
Follow the same pattern as the original 8 classes โ vegetable_cookingstate:
potato_tempered โ
beetroot_white_curry โ
lentil_red_curry โ
"Potato Curry" โ spaces not allowed
potatocurry โ missing underscore separator
Pre-trained Model โ Original 8 Classes
The model this library extends was trained on:
| # | Class | Vegetable | Cooking state |
|---|---|---|---|
| 1 | carrot_raw |
Carrot | Raw |
| 2 | carrot_white_curry |
Carrot | White curry (coconut milk) |
| 3 | greenbeans_raw |
Green beans | Raw |
| 4 | greenbeans_tempered |
Green beans | Tempered (high-heat stir-fry) |
| 5 | greenbeans_white_curry |
Green beans | White curry (coconut milk) |
| 6 | pumpkin_raw |
Pumpkin | Raw |
| 7 | pumpkin_red_curry |
Pumpkin | Red curry (turmeric-based) |
| 8 | pumpkin_white_curry |
Pumpkin | White curry (coconut milk) |
Base model accuracy: 90.25% validation ยท 84.91% full test ยท 87.75% few-shot test
API Reference
FoodModelExtender(model_path=None, verbose=False)
| Parameter | Type | Default | Description |
|---|---|---|---|
model_path |
str or None |
None |
Path to local .pth checkpoint. If None, downloads pre-trained model automatically. |
verbose |
bool |
False |
Print detailed progress messages. |
.add_class(class_name, images_path, auto_extract=True)
Registers a new food class for training.
| Parameter | Type | Default | Description |
|---|---|---|---|
class_name |
str |
โ | Name for the new class. Should match ZIP filename. |
images_path |
str |
โ | Path to the ZIP file containing images. |
auto_extract |
bool |
True |
Automatically extract the ZIP file. |
Returns True on success, False on failure.
.train(epochs=50, save_checkpoints=True)
Fine-tunes the model to include the new class.
| Parameter | Type | Default | Description |
|---|---|---|---|
epochs |
int |
50 |
Training rounds. Try 75โ100 for low accuracy. |
save_checkpoints |
bool |
True |
Auto-save the best model during training. |
Returns a dict with best_val_acc, total_time, and training_history.
.save(output_path)
Saves the extended model as a PyTorch .pth checkpoint.
| Parameter | Type | Description |
|---|---|---|
output_path |
str |
Filename for the saved model (e.g. extended_model.pth). |
Understanding Your Results
| Validation accuracy | Meaning |
|---|---|
| 90%+ | Excellent โ very reliable |
| 85โ90% | Very good โ suitable for most applications |
| 80โ85% | Good โ consider adding more images |
| Below 80% | Needs improvement โ see troubleshooting |
Troubleshooting
"Not enough images" error You need at least 20 images. Add more images and re-zip.
"Model download failed" error Check your internet connection and re-run the cell. If it persists, restart your runtime.
"Out of memory" error Restart your runtime and try again. Keep image count under 100 per class.
Low accuracy (below 80%)
- Add more high-quality images (aim for 50)
- Increase epochs to 75 or 100
- Check that all images are the same food type and cooking state
- Remove blurry or poorly lit images
Wrong ZIP structure
โ
Correct: โ Wrong:
potato_tempered.zip potato_tempered.zip
โโโ potato_tempered/ โโโ img1.jpg โ images directly in ZIP
โโโ img1.jpg โโโ img2.jpg
โโโ img2.jpg
Google Colab Tutorial
A complete step-by-step tutorial is available โ no local setup needed:
๐ Open in Google Colab
The notebook covers:
- Installing the library
- Initialising the model extender
- Uploading your images (ZIP upload via Colab)
- Adding your class and validating images
- Training the extended model
- Saving and downloading the result
Expected total time: ~1 hour (including 30โ60 min training).
Related Resources
| Resource | Link |
|---|---|
| Pre-trained model | Hugging Face โ ranasinghehashini/srilankan-food-recognition |
| Training dataset | Kaggle โ Sri Lankan Food Recognition Dataset |
| Research | A Transformation-Aware Deep Learning Approach for Recognizing Cooked Vegetable Ingredients in Sri Lankan Cuisine, Ranasinghe G. H. C., 2026 |
Citation
@misc{ranasinghe2026foodshot,
author = {Ranasinghe, G. H. C.},
title = {A Transformation-Aware Deep Learning Approach for Recognizing
Cooked Vegetable Ingredients in Sri Lankan Cuisine},
year = {2026},
note = {BSc Hons in Computing, Coventry University / NIBM},
}
License
MIT License โ free to use, modify, and distribute with attribution.
Project details
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
Built Distribution
Filter files by name, interpreter, ABI, and platform.
If you're not sure about the file name format, learn more about wheel file names.
Copy a direct link to the current filters
File details
Details for the file srilankan_food_trainer-0.2.2.tar.gz.
File metadata
- Download URL: srilankan_food_trainer-0.2.2.tar.gz
- Upload date:
- Size: 16.3 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/6.2.0 CPython/3.13.9
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
90e50ce05904744d93323131826fa802748025096d55e5eb573cd402cbface80
|
|
| MD5 |
50a9cc7d83c253507026796d16b74a29
|
|
| BLAKE2b-256 |
9f6ce0c941d10a445499d746651b37321325de63e081c728368eb07cca061afa
|
File details
Details for the file srilankan_food_trainer-0.2.2-py3-none-any.whl.
File metadata
- Download URL: srilankan_food_trainer-0.2.2-py3-none-any.whl
- Upload date:
- Size: 14.0 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/6.2.0 CPython/3.13.9
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
5cd7882c7b01444202cac2a6d901e1a46857481eed8bc0f2ba2512be4a5b3a3a
|
|
| MD5 |
79b3482f205bfde007127d302e593c07
|
|
| BLAKE2b-256 |
42838214fd75b20227c5f6c723f9dd58861a92febd50fc66d7a5f9446b0208a0
|