-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathapp.py
67 lines (56 loc) · 1.99 KB
/
app.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
import paddle
from paddle.vision import transforms
import gradio as gr
import numpy as np
class Place365cls:
def __init__(self, model_pth='./place365.pdparams'):
# init model
network = paddle.vision.models.resnet18(pretrained=False)
network.fc = paddle.nn.Linear(512, 365)
model = paddle.Model(network)
model.load(model_pth)
self.model = model
self.sfm=paddle.nn.Softmax()
# init transform
self.transform = transforms.Compose([
transforms.Resize(224),
transforms.CenterCrop((224, 224)),
transforms.Normalize(mean=[127.5], std=[127.5]),
transforms.ToTensor()
])
# inti category name
self.id2categories_name = dict()
with open('categories_places365.txt') as f:
lines = f.readlines()
for line in lines:
category_name, id = line.split(' ')
id = int(id)
category_name = category_name[3:]
self.id2categories_name[id] = category_name
def predict(self, img, top_n=5):
img = self.transform(img)
img = img.unsqueeze(0)
out = self.model.predict_batch(img)[0]
out = self.sfm(paddle.Tensor(out))
score, label = paddle.topk(out[0], k=5)
return {self.id2categories_name[int(c)]: float(s) for c, s in zip(label, score)}
"""
label = np.argsort(out)[-top_n:][::-1]
top_labels=dict()
for c in label:
c=int(c)
c_name=self.id2categories_name[c]
score=float(out[c])
top_labels[c_name]=score
return top_labels
"""
cls_model = Place365cls()
def predict(image):
result = cls_model.predict(image, top_n=5)
return result
if __name__ == "__main__":
interface = gr.Interface(fn=predict, inputs="image",
outputs=gr.Label(),
title="Place365 classification"
)
interface.launch()