Food Freshness Detector

A ResNet18 image classifier trained with FastAI that predicts the freshness of fruits and vegetables.

Classes: Fresh · Slightly Spoiled · Rotten

Usage

import json
import torch
from fastai.vision.all import create_cnn_model, resnet18
from torchvision import transforms
from PIL import Image
from huggingface_hub import hf_hub_download

# Download weights
weights_path = hf_hub_download("nathansekar/food-freshness-detector", "model_weights.pth")
config_path  = hf_hub_download("nathansekar/food-freshness-detector", "config.json")
vocab_path   = hf_hub_download("nathansekar/food-freshness-detector", "vocab.json")

with open(config_path) as f:
    config = json.load(f)
with open(vocab_path) as f:
    vocab = json.load(f)

model = create_cnn_model(resnet18, config["n_classes"])
model.load_state_dict(torch.load(weights_path, map_location="cpu"))
model.eval()

preprocess = transforms.Compose([
    transforms.Resize(256),
    transforms.CenterCrop(224),
    transforms.ToTensor(),
    transforms.Normalize(mean=[0.485, 0.456, 0.406], std=[0.229, 0.224, 0.225]),
])

image = Image.open("your_food_image.jpg").convert("RGB")
tensor = preprocess(image).unsqueeze(0)

with torch.no_grad():
    probs = torch.softmax(model(tensor), dim=1)[0]

pred_idx = probs.argmax().item()
print(f"Prediction: {vocab[pred_idx]} ({probs[pred_idx]:.1%})")

Model Details

Architecture ResNet18 (transfer learning)
Framework FastAI 2.x / PyTorch
Input RGB image → 224×224
Output 3-class softmax
Training data Synthetic fruit/vegetable images

Files

File Description
model_weights.pth Trained ResNet18 state dict
config.json Architecture config (arch, n_classes, img_size)
vocab.json Class label list
Downloads last month
3,913
Inference Providers NEW
This model isn't deployed by any Inference Provider. 🙋 Ask for provider support