🍨🍦 Lumia101/Food101-EfficientNet-B0

This model is a fine-tuned model of EfficientNet-B0, pre-trained on ImageNet-1K, for the Food 101 dataset.

πŸ” Model Information

  • Accuracy: 79.57%
  • Batch Size: 256
  • Optimizer: AdamW
  • Learning Rate
    • Feature Extractor: 1e-4
    • Classifier: 1e-3
  • LR Scheduler Type: CosineAnnealingLR
  • Epochs: 20
  • AMP: Native AMP

πŸ“ˆ Training Log

See TensorBoard.

πŸ’‘ How to use

This model is not compatible with timm or transformer because its Classifier layer differs from the standard EfficientNet-B0. You can choose one of two methods to use this model.

1: Use Colab

Paste the following code into Colab and run it. Runtime doesn't matter.

# Import libraries
import torch
import torch.nn as nn
import torchvision.models as models
from torchvision.transforms import v2
from safetensors.torch import load_file
from PIL import Image
import json
from huggingface_hub import hf_hub_download

# Download my files
hf_hub_download(
    repo_id="Lumia101/Food101-EfficientNet-B0",
    filename="config.json",
    local_dir='.'
)

hf_hub_download(
    repo_id="Lumia101/Food101-EfficientNet-B0",
    filename="model.safetensors",
    local_dir='.'
)

# Load classes
with open("config.json") as f:
    config = json.load(f)
id2label = config["id2label"]

# Make model
model = models.efficientnet_b0(weights=None)
model.classifier = nn.Sequential(
    nn.Dropout(p=0.2, inplace=True),
    nn.Linear(1280, 512),
    nn.SiLU(),
    nn.Dropout(0.2),
    nn.Linear(512, 101)
)

# Load weights
state_dict = load_file("model.safetensors")
model.load_state_dict(state_dict)
model.eval()

# Transform it
transform = v2.Compose([
    v2.Resize(160),
    v2.CenterCrop(128),
    v2.ToImage(),
    v2.ToDtype(torch.float32, scale=True),
    v2.Normalize(
        mean=(0.485, 0.456, 0.406),
        std=(0.229, 0.224, 0.225),
    ),
])

# Inference code
def predict(image_path, top_k=5):
    img = Image.open(image_path).convert("RGB")
    tensor = transform(img).unsqueeze(0)

    with torch.no_grad():
        output = model(tensor)
        probs = torch.softmax(output, dim=1)
        top = torch.topk(probs, top_k)

    return [(id2label[str(i.item())], round(p.item() * 100, 2))
            for i, p in zip(top.indices[0], top.values[0])]

# Classify it!
results = predict("your_own_picture.png")
for label, prob in results:
    print(f"{label}: {prob}%")

2: Use HF Space

You can use it more easily by accessing the My HuggingFace Space I created.

βš’οΈ Framework used

  • PyTorch
    • torch 2.10.0
    • torchvision 0.25.0
  • datasets 4.0.0
  • safetensors 0.7.0
  • tqdm 4.67.3
Downloads last month
28
Inference Providers NEW
This model isn't deployed by any Inference Provider. πŸ™‹ Ask for provider support

Model tree for Lumia101/Food101-EfficientNet-B0

Finetuned
(53)
this model

Dataset used to train Lumia101/Food101-EfficientNet-B0

Collection including Lumia101/Food101-EfficientNet-B0