Spaces:
Runtime error
Runtime error
Weicheng HE
commited on
Commit
·
4af5909
1
Parent(s):
3d879f2
add app&examples
Browse files
1.png
ADDED
|
2.png
ADDED
|
3.png
ADDED
|
app.py
CHANGED
|
@@ -1,7 +1,36 @@
|
|
| 1 |
import gradio as gr
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 2 |
|
| 3 |
-
def
|
| 4 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 5 |
|
| 6 |
-
|
| 7 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
import gradio as gr
|
| 2 |
+
from PIL import Image
|
| 3 |
+
import numpy as np
|
| 4 |
+
import torch
|
| 5 |
+
import torchvision
|
| 6 |
+
from torchvision import transforms
|
| 7 |
|
| 8 |
+
def predict_image(image):
|
| 9 |
+
image = image.convert('RGB')
|
| 10 |
+
test_transforms = transforms.Compose([
|
| 11 |
+
transforms.Resize([224, 224]),
|
| 12 |
+
transforms.ToTensor(), transforms.Normalize(mean=[0.485, 0.456, 0.406], std=[0.229, 0.224, 0.225])
|
| 13 |
+
])
|
| 14 |
+
classes = ('Speed limit (20km/h)',
|
| 15 |
+
'Speed limit (30km/h)',
|
| 16 |
+
'Speed limit (50km/h)',
|
| 17 |
+
'Speed limit (60km/h)',
|
| 18 |
+
'Speed limit (70km/h)',
|
| 19 |
+
'Speed limit (80km/h)',
|
| 20 |
+
'Speed limit (100km/h)',
|
| 21 |
+
'Speed limit (120km/h)')
|
| 22 |
|
| 23 |
+
device = torch.device("cuda" if torch.cuda.is_available() else "cpu")
|
| 24 |
+
model = torch.load('vgg11.pt')
|
| 25 |
+
model.eval()
|
| 26 |
+
image_tensor = test_transforms(image).float()
|
| 27 |
+
image_tensor = image_tensor.unsqueeze_(0)
|
| 28 |
+
input = image_tensor.to(device)
|
| 29 |
+
output = model(input)
|
| 30 |
+
probs = torch.exp(output.data.cpu().squeeze()).numpy()
|
| 31 |
+
return dict(zip(classes, map(float, probs)))
|
| 32 |
+
image = gr.Image(type='pil')
|
| 33 |
+
label = gr.Label()
|
| 34 |
+
examples = ['1.png', '2.png', '3.png']
|
| 35 |
+
intf = gr.Interface(fn=predict_image, inputs=image, outputs=label, examples=examples)
|
| 36 |
+
intf.launch(inline=True)
|