ethz/food101
Viewer β’ Updated β’ 101k β’ 28.8k β’ 134
This model is a fine-tuned model of EfficientNet-B0, pre-trained on ImageNet-1K, for the Food 101 dataset.
See TensorBoard.
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.
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}%")
You can use it more easily by accessing the My HuggingFace Space I created.
Base model
google/efficientnet-b0