A small, fast image classifier that tells whether a food photo shows pizza, steak or sushi. It uses transfer learning: torchvision's EfficientNet-B2, pretrained on ImageNet, is kept frozen as a feature extractor and only a new 3-class head is trained, on 450 Food-101 photos.
| Architecture | torchvision efficientnet_b2 (backbone initialised from EfficientNet_B2_Weights.IMAGENET1K_V1), classifier replaced by Dropout(0.3) -> Linear(1408, 3) |
| Parameters | 7,705,221 total, of which 4,227 (the head) were trained |
| Input | an RGB image of any size |
| Preprocessing | model.get_transform(): resize to 288 (bicubic), center-crop 288x288, ImageNet mean/std |
| Output | softmax probabilities for all 3 classes: {"pizza": p, "steak": p, "sushi": p} |
| Files | model.safetensors (weights), config.json (class_names, dropout, pretrained), model.py (architecture + load() + Predictor), handler.py + requirements.txt (Inference Endpoint), metrics.json, assets/ (experiment graphs) |
config.json records pretrained: true because the backbone was initialised from ImageNet
weights before training. model.load() always rebuilds the network with pretrained=False
and then loads model.safetensors, so inference never downloads the torchvision weights.
Python
from huggingface_hub import hf_hub_download, snapshot_download
import sys
path = snapshot_download("shalev396/foodvision-mini")
sys.path.insert(0, path)
import model
predictor = model.load(path, device="cpu") # or "cuda"
image = hf_hub_download("shalev396/foodvision-mini", "examples/2582289.jpg", repo_type="space")
print(predictor.predict(image)) # a PIL image works too
# {'pizza': 0.964, 'steak': 0.017, 'sushi': 0.019}
Requirements (requirements.txt): torch, torchvision, pillow, huggingface_hub, safetensors.
Space API (free): the Space exposes
/predict (image in, [label, seconds, device] out). curl and @gradio/client examples are in its README.
Inference Endpoint: deploy this repo from its page (Deploy -> Inference Endpoints, CPU or GPU).
handler.py loads the model once (on cuda when the endpoint has a GPU) and accepts an image sent as
raw bytes (Content-Type: image/jpeg) or as JSON {"inputs": "<base64 image>"}. It returns
{"pizza": p, "steak": p, "sushi": p}.
curl $ENDPOINT_URL -H "Authorization: Bearer $HF_TOKEN" -H "Content-Type: image/jpeg" --data-binary @pizza.jpg
pizza_steak_sushi_20_percent subset of Food-101,
prepared for the PyTorch Deep Learning course
(zip).
It holds 450 train images (pizza 154, steak 146, sushi 150) and 150 test images (46 / 58 / 46).Adam(lr=1e-3), batch size 32, 10 epochs, seed 42.
Train and test images get the same preprocessing (no data augmentation). BatchNorm layers stay in
train mode, so their running statistics adapt to the food photos. The last epoch is kept (no early stopping)..pth state_dict was mapped 1:1 into FoodVisionNet and saved as safetensors.
On the 3 example images, the converted model's probabilities match the original's exactly (max |diff| 0.0).
Training time was not recorded.Full code: training/ · Colab. The notebook retrains the recipe, evaluates the new run next to this checkpoint, and exports whichever is better (the deployed one wins ties).
Every variant evaluated on the same 150 test images (from metrics.json -> comparison). The deployed
one is in bold. accuracy_<class> is the share of that class's photos classified correctly.
| variant | accuracy | f1_macro | accuracy_pizza | accuracy_steak | accuracy_sushi |
|---|---|---|---|---|---|
| original bootcamp checkpoint (deployed) | 0.960 | 0.961 | 0.957 | 0.948 | 0.978 |
| retrain with training/ on cpu, 2026-09-25 | 0.940 | 0.941 | 0.978 | 0.983 | 0.848 |
Training curves of the retrain (the original run's curves were not recorded). The loss is still falling at epoch 10. Test accuracy touches 0.960 at epoch 5 and ends at 0.940. With 150 test images, one epoch's swing is 1-3 images, and the recipe does not pick epochs on the test split.
| metric (test) | value |
|---|---|
| accuracy | 0.9600 |
| f1_macro | 0.9606 |
| accuracy_pizza | 0.9565 |
| accuracy_steak | 0.9483 |
| accuracy_sushi | 0.9783 |
The deployed checkpoint on the 150 test images, evaluated locally on CPU with this repo's training/src code.
144 of 150 test images are correct. 3 of the 6 errors are steak photos predicted as sushi. The bootcamp notebook printed 96.25% for this model. That figure is a mean of per-batch accuracies (the last batch holds only 22 images). The per-image accuracy on the same 150 images is 96.0%, and that is what is reported here.