raihanp commited on
Commit
b1ae1a1
·
verified ·
1 Parent(s): d6ef684

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +15 -10
app.py CHANGED
@@ -3,7 +3,13 @@ import torch
3
  import gradio as gr
4
  from torchvision.transforms import Compose, Normalize, ToTensor, Resize, CenterCrop
5
 
6
-
 
 
 
 
 
 
7
  inference = torch.load('fine_tune_resnet.pth', map_location=torch.device('cpu'))
8
  inference.eval()
9
 
@@ -14,18 +20,17 @@ def classifier(image):
14
  ToTensor(),
15
  Normalize(mean=[0.485, 0.456, 0.406], std=[0.229, 0.224, 0.225])
16
  ])
17
-
18
- if torch.cuda.is_available():
19
- inference.cpu()
20
 
21
  with torch.no_grad():
22
- output = inference(test_transform(Image.open('/content/1000-ml-plastic-water-bottle-500x500.webp')).unsqueeze(0))
23
- _, prediction = torch.max(output,1)
24
- confidence = round(torch.softmax(output,1).max().item(),4)*100
 
 
 
25
 
26
- return f'{label_dict[prediction.item()]} (Confidence: {confidence}%)'
27
 
28
- iface = gr.Interface(fc=classifier,
29
  inputs=gr.Image(type="pil"),
30
- outputs='text')
31
  iface.launch(share=True)
 
3
  import gradio as gr
4
  from torchvision.transforms import Compose, Normalize, ToTensor, Resize, CenterCrop
5
 
6
+ labels = {0: 'glass',
7
+ 1: 'metal',
8
+ 2: 'organic-waste',
9
+ 3: 'organice-waste',
10
+ 4: 'paper',
11
+ 5: 'plastic',
12
+ 6: 'textiles'}
13
  inference = torch.load('fine_tune_resnet.pth', map_location=torch.device('cpu'))
14
  inference.eval()
15
 
 
20
  ToTensor(),
21
  Normalize(mean=[0.485, 0.456, 0.406], std=[0.229, 0.224, 0.225])
22
  ])
 
 
 
23
 
24
  with torch.no_grad():
25
+ output = inference(test_transform(image).unsqueeze(0))
26
+ out = torch.softmax(output,1)
27
+
28
+ values,indices = torch.topk(out[0],k=7)
29
+
30
+ return {labels[i.item()]: v.item() for i, v in zip(indices,values)}
31
 
 
32
 
33
+ iface = gr.Interface(fn=classifier,
34
  inputs=gr.Image(type="pil"),
35
+ outputs='label')
36
  iface.launch(share=True)