Spaces:
Build error
Build error
Create new file
Browse files
app.py
ADDED
@@ -0,0 +1,48 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
import timm
|
2 |
+
import torch
|
3 |
+
import torch.nn.functional as nnf
|
4 |
+
import gradio as gr
|
5 |
+
import numpy as np
|
6 |
+
import json
|
7 |
+
|
8 |
+
class GELU(torch.nn.Module):
|
9 |
+
def forward(self, input: torch.Tensor) -> torch.Tensor:
|
10 |
+
return torch.nn.functional.gelu(input)
|
11 |
+
torch.nn.modules.activation.GELU = GELU
|
12 |
+
|
13 |
+
model = torch.load("/home/user/app/run45.pkl",map_location=torch.device('cpu'))
|
14 |
+
|
15 |
+
with open('/home/user/app/val.json', 'r') as handle:
|
16 |
+
parsed = json.load(handle)
|
17 |
+
|
18 |
+
classes = []
|
19 |
+
for i in range(len(parsed["categories"])):
|
20 |
+
if parsed["categories"][i]['supercategory'] == 'Plants':
|
21 |
+
classes.append(parsed["categories"][i]['name'])
|
22 |
+
|
23 |
+
classes = set(classes)
|
24 |
+
|
25 |
+
classes = list(classes)
|
26 |
+
classes.sort()
|
27 |
+
|
28 |
+
labels = classes
|
29 |
+
|
30 |
+
def classify_image(inp):
|
31 |
+
print(inp.shape)
|
32 |
+
inp = inp.astype(np.uint8).reshape((-1, 3, 224, 224))
|
33 |
+
print(inp.shape)
|
34 |
+
inp = torch.from_numpy(inp).float()
|
35 |
+
#confidences = model(inp)
|
36 |
+
|
37 |
+
preds = nnf.softmax(model(inp).data[0], dim=0)
|
38 |
+
preds = [pred.cpu() for pred in preds]
|
39 |
+
preds = [pred.detach().numpy() for pred in preds]
|
40 |
+
|
41 |
+
#confidences_dict = {classes[i]: float(confidences.data[0][i]) for i in range(len(confidences.data[0]))}
|
42 |
+
confidences_dict = {classes[i]: float(preds[i]) for i in range(len(preds))}
|
43 |
+
|
44 |
+
return confidences_dict
|
45 |
+
|
46 |
+
gr.Interface(fn=classify_image,
|
47 |
+
inputs=gr.Image(shape=(224, 224)),
|
48 |
+
outputs=gr.Label(num_top_classes=3)).launch(debug = True)
|