prithivMLmods commited on
Commit
f38ab86
·
verified ·
1 Parent(s): 5c2777f

Create fashion_mnist_cloth.py

Browse files
Files changed (1) hide show
  1. fashion_mnist_cloth.py +42 -0
fashion_mnist_cloth.py ADDED
@@ -0,0 +1,42 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import gradio as gr
2
+ from transformers import AutoImageProcessor
3
+ from transformers import SiglipForImageClassification
4
+ from transformers.image_utils import load_image
5
+ from PIL import Image
6
+ import torch
7
+
8
+ # Load model and processor
9
+ model_name = "prithivMLmods/Fashion-Mnist-SigLIP2"
10
+ model = SiglipForImageClassification.from_pretrained(model_name)
11
+ processor = AutoImageProcessor.from_pretrained(model_name)
12
+
13
+ def fashion_mnist_classification(image):
14
+ """Predicts fashion category for an image."""
15
+ image = Image.fromarray(image).convert("RGB")
16
+ inputs = processor(images=image, return_tensors="pt")
17
+
18
+ with torch.no_grad():
19
+ outputs = model(**inputs)
20
+ logits = outputs.logits
21
+ probs = torch.nn.functional.softmax(logits, dim=1).squeeze().tolist()
22
+
23
+ labels = {
24
+ "0": "T-shirt / top", "1": "Trouser", "2": "Pullover", "3": "Dress", "4": "Coat",
25
+ "5": "Sandal", "6": "Shirt", "7": "Sneaker", "8": "Bag", "9": "Ankle boot"
26
+ }
27
+ predictions = {labels[str(i)]: round(probs[i], 3) for i in range(len(probs))}
28
+
29
+ return predictions
30
+
31
+ # Create Gradio interface
32
+ iface = gr.Interface(
33
+ fn=fashion_mnist_classification,
34
+ inputs=gr.Image(type="numpy"),
35
+ outputs=gr.Label(label="Prediction Scores"),
36
+ title="Fashion MNIST Classification Labels",
37
+ description="Upload an image to classify it into one of the 10 Fashion-MNIST categories."
38
+ )
39
+
40
+ # Launch the app
41
+ if __name__ == "__main__":
42
+ iface.launch()