Food Freshness Detector
A ResNet18 image classifier trained with FastAI that predicts the freshness of fruits and vegetables.
Model source
- Kind
- Other
- File size
- 44.7 MB
- Version
- vRevision 7488ca2
- Updated
- Oct 22, 2026
- Downloads
- 160
- Source
- Hugging Face
Source description
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 |
Sources
1 source
Sources
1 sourceVerified Oct 22
Model artifacts
1 artifact
Model artifacts
1 artifactSource excerpts
3 excerpts
Source excerpts
3 excerpts--- language: en license: mit tags: - image-classification - food - freshness - fastai - pytorch - resnet library_name: fastai --- # Food Freshness Detector A ResNet18 image classifier trained with FastAI that predicts the freshness of fruits and vegetables. **Classes:** Fresh · Slightly Spoiled · Rotten ## Usage ```python 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 |
Source context: 160 downloads · 0 likes · Pipeline image-classification · Library fastai · Repo nathansekar/food-freshness-detector
Source context: 3909 downloads · 0 likes · Pipeline image-classification · Library fastai · Repo nathansekar/food-freshness-detector
