PrachiY commited on
Commit
dd4bb32
·
verified ·
1 Parent(s): 85a4b1a

Upload app.py with huggingface_hub

Browse files
Files changed (1) hide show
  1. app.py +6 -43
app.py CHANGED
@@ -1,53 +1,16 @@
1
- import torch
2
- import torch.nn.functional as F
3
- from torchvision import models, transforms
4
- from PIL import Image
5
- import gradio as gr
6
- from huggingface_hub import hf_hub_download
7
-
8
- # Define class labels
9
- clothing1m_classes = [
10
- "T-shirt", "Shirt", "Knitwear", "Chiffon", "Sweater", "Hoodie", "Windbreaker",
11
- "Jacket", "Down Coat", "Suits", "Shawl", "Dress", "Vest", "Underwear", "Shorts",
12
- "Trousers", "Jeans", "Leather Shoes", "Casual Shoes", "Sport Shoes", "Sandals"
13
- ]
14
-
15
- # Set device
16
- device = torch.device("cuda" if torch.cuda.is_available() else "cpu")
17
 
18
- # Load model
19
- model = models.resnet50(weights=None)
20
- model.fc = torch.nn.Linear(model.fc.in_features, 21)
21
- model_path = hf_hub_download(repo_id="PrachiY/image-classification-model", filename="model.pth")
22
- model.load_state_dict(torch.load(model_path, map_location=device))
23
- model.to(device)
24
- model.eval()
25
-
26
- # Define preprocessing
27
- transform = transforms.Compose([
28
- transforms.Resize((224, 224)),
29
- transforms.ToTensor(),
30
- transforms.Normalize(mean=[0.485, 0.456, 0.406],
31
- std=[0.229, 0.224, 0.225]),
32
- ])
33
 
34
- def classify_image(image):
35
- image = transform(image).unsqueeze(0).to(device)
36
- with torch.no_grad():
37
- output = model(image)
38
- probs = F.softmax(output, dim=1)
39
- pred_idx = probs.argmax(dim=1).item()
40
- pred_label = clothing1m_classes[pred_idx]
41
- return f"Predicted Class: {pred_label}"
42
 
43
- # Gradio Interface
44
  interface = gr.Interface(
45
  fn=classify_image,
46
  inputs=gr.Image(type="pil"),
47
- outputs="text",
 
48
  title="Clothing1M Classifier",
49
- description="Upload a clothing image and get a predicted label from 21 Clothing1M categories.",
50
- examples=[["example.jpg"]] # Optional
51
  )
52
 
53
  if __name__ == "__main__":
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
 
2
+ import gradio as gr
3
+ from classify import classify_image
 
 
 
 
 
 
 
 
 
 
 
 
 
4
 
5
+ examples = ["example.jpg"]
 
 
 
 
 
 
 
6
 
 
7
  interface = gr.Interface(
8
  fn=classify_image,
9
  inputs=gr.Image(type="pil"),
10
+ outputs=gr.Label(),
11
+ examples=examples,
12
  title="Clothing1M Classifier",
13
+ description="Upload a clothing image to classify it into 14 categories."
 
14
  )
15
 
16
  if __name__ == "__main__":