CoolFace
Modelpublic

Lumia101/Food101-EfficientNet-B0

sourceHugging Facemitupdated 6mo agoView on Hugging Face
1likes46downloads
Model Card

๐Ÿจ๐Ÿฆ 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.

Python
# 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