Upload 3 files
Browse files- app.py +52 -0
- model.pth +3 -0
- requirements.txt +3 -0
app.py
ADDED
@@ -0,0 +1,52 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
from os import listdir
|
2 |
+
|
3 |
+
import gradio as gr
|
4 |
+
import torch
|
5 |
+
from torchvision import transforms
|
6 |
+
from torchvision.models import resnet34
|
7 |
+
|
8 |
+
|
9 |
+
def image_classifier(image):
|
10 |
+
# Готовим данные
|
11 |
+
inputs = transform(image)
|
12 |
+
inputs = inputs.unsqueeze(0)
|
13 |
+
|
14 |
+
# Пропускаем данные через модель
|
15 |
+
with torch.no_grad():
|
16 |
+
outputs = model(inputs)
|
17 |
+
|
18 |
+
# Ставим метки и вероятности
|
19 |
+
predictions = torch.nn.functional.softmax(outputs[0], dim=0)
|
20 |
+
predictions = predictions[:len(labels)]
|
21 |
+
return {labels[i]: p.item() for i, p in enumerate(predictions)}
|
22 |
+
|
23 |
+
|
24 |
+
# Загружаем модель
|
25 |
+
model = resnet34()
|
26 |
+
state = torch.load('model.pth', map_location=torch.device('cpu'))
|
27 |
+
model.load_state_dict(state)
|
28 |
+
model.eval()
|
29 |
+
|
30 |
+
# Преобразования над сырыми данными
|
31 |
+
transform = transforms.Compose([
|
32 |
+
transforms.Resize(256),
|
33 |
+
transforms.CenterCrop(224),
|
34 |
+
transforms.ToTensor(),
|
35 |
+
transforms.Normalize(mean=[0.485, 0.456, 0.406],
|
36 |
+
std=[0.229, 0.224, 0.225]),
|
37 |
+
])
|
38 |
+
|
39 |
+
# Названия классов
|
40 |
+
labels = [
|
41 |
+
'Pedro Pascal',
|
42 |
+
'Robert Downey Jr',
|
43 |
+
'Tom Holland',
|
44 |
+
]
|
45 |
+
|
46 |
+
# Добавляем виджет в интерфейс
|
47 |
+
img_widget = gr.Image(image_mode='RGB', type='pil')
|
48 |
+
app = gr.Interface(fn=image_classifier, inputs=img_widget, outputs='label',
|
49 |
+
flagging_mode='never')
|
50 |
+
|
51 |
+
# Запускаем приложение
|
52 |
+
app.launch()
|
model.pth
ADDED
@@ -0,0 +1,3 @@
|
|
|
|
|
|
|
|
|
1 |
+
version https://git-lfs.github.com/spec/v1
|
2 |
+
oid sha256:7acbc67a9dcc79c402995c89779769616312cfd21ba2572530681f0754ca71b3
|
3 |
+
size 87318722
|
requirements.txt
ADDED
@@ -0,0 +1,3 @@
|
|
|
|
|
|
|
|
|
1 |
+
--index-url https://download.pytorch.org/whl/cpu
|
2 |
+
torch
|
3 |
+
torchvision
|