Views
No views yet
Food-or-Not-SigLIP2 is a vision-language encoder model fine-tuned from google/siglip2-base-patch16-224 for binary image classification. It is trained to distinguish between images of food and non-food objects using the SiglipForImageClassification architecture.
1Classification Report:
2 precision recall f1-score support
3
4 food 0.8902 0.8610 0.8753 4000
5 not-food 0.8654 0.8938 0.8794 4000
6
7 accuracy 0.8774 8000
8 macro avg 0.8778 0.8774 0.8773 8000
9weighted avg 0.8778 0.8774 0.8773 8000Class 0: "food"
Class 1: "not-food"pip install -q transformers torch pillow gradio1import gradio as gr
2from transformers import AutoImageProcessor, SiglipForImageClassification
3from PIL import Image
4import torch
5
6# Load model and processor
7model_name = "prithivMLmods/Food-or-Not-SigLIP2" # Replace with your model path if different
8model = SiglipForImageClassification.from_pretrained(model_name)
9processor = AutoImageProcessor.from_pretrained(model_name)
10
11# Label mapping
12id2label = {
13 "0": "food",
14 "1": "not-food"
15}
16
17def classify_food(image):
18 image = Image.fromarray(image).convert("RGB")
19 inputs = processor(images=image, return_tensors="pt")
20
21 with torch.no_grad():
22 outputs = model(**inputs)
23 logits = outputs.logits
24 probs = torch.nn.functional.softmax(logits, dim=1).squeeze().tolist()
25
26 prediction = {
27 id2label[str(i)]: round(probs[i], 3) for i in range(len(probs))
28 }
29
30 return prediction
31
32# Gradio Interface
33iface = gr.Interface(
34 fn=classify_food,
35 inputs=gr.Image(type="numpy"),
36 outputs=gr.Label(num_top_classes=2, label="Food Classification"),
37 title="Food-or-Not-SigLIP2",
38 description="Upload an image to detect if it contains food or not."
39)
40
41if __name__ == "__main__":
42 iface.launch()