Spaces:
Sleeping
Sleeping
Create app.py
Browse files
app.py
ADDED
|
@@ -0,0 +1,28 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import torch
|
| 2 |
+
import torchvision import transforms, models
|
| 3 |
+
from PIL import Image
|
| 4 |
+
import fradio as gr
|
| 5 |
+
|
| 6 |
+
#model initialization
|
| 7 |
+
model= models.resnet18(pretrained=True)
|
| 8 |
+
|
| 9 |
+
model.fc = torch.nn.Linear(model.fc.in_features,2)
|
| 10 |
+
state_dict=torch.load('up500Model.pt', map_location='cpu')
|
| 11 |
+
model.load_state_dict(state_dict)
|
| 12 |
+
model.eval
|
| 13 |
+
|
| 14 |
+
#predictions
|
| 15 |
+
imgTransforms = transforms.Comporse([transforms.Resize(256), transforms.CenterCrop(224), trasforms.ToTensor(),transforms.Normalize([0.485,0.456,0.406],[0.229,0.224,0.225])])
|
| 16 |
+
|
| 17 |
+
LABELS =['Fiat 500', 'VW Up']
|
| 18 |
+
def predict(inp):
|
| 19 |
+
inp= Image.fromarray(inp.astype('unit8'), 'RGB')
|
| 20 |
+
inp =imgTransfroms(inp).unsqueeze(0)
|
| 21 |
+
|
| 22 |
+
with torch.no_grad():
|
| 23 |
+
predictions = torch.nn.functional.softmax(model(inp)[0])
|
| 24 |
+
|
| 25 |
+
return {LABELS[i]: float(predictions[i]) for i in range(2)}
|
| 26 |
+
examples=[["fiat500.jpg"],["VWUP.jpg"]]
|
| 27 |
+
interface = gr.Interface(predict, input='image', outputs'label', title='App', description= 'upload your car image', examples=examples, cache_examples= False)
|
| 28 |
+
interface.launch()
|