Diego Carpintero
commited on
Commit
·
c726874
1
Parent(s):
bd42f73
add app.py
Browse files
app.py
CHANGED
|
@@ -1,7 +1,46 @@
|
|
| 1 |
import gradio as gr
|
|
|
|
|
|
|
| 2 |
|
| 3 |
-
|
| 4 |
-
|
| 5 |
|
| 6 |
-
|
| 7 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
import gradio as gr
|
| 2 |
+
import torch
|
| 3 |
+
from model import *
|
| 4 |
|
| 5 |
+
from PIL import Image
|
| 6 |
+
import torchvision.transforms as transforms
|
| 7 |
|
| 8 |
+
|
| 9 |
+
title = "Fashion Image Recognition"
|
| 10 |
+
inputs = gr.components.Image()
|
| 11 |
+
outputs = gr.components.Label()
|
| 12 |
+
examples = "examples"
|
| 13 |
+
|
| 14 |
+
model = torch.load("model/fashion.mnist.base.pt", map_location=torch.device("cpu"))
|
| 15 |
+
|
| 16 |
+
# Images need to be transformed to an approximated `fashion MNIST` dataset format
|
| 17 |
+
# see https://arxiv.org/abs/1708.07747
|
| 18 |
+
transform = transforms.Compose(
|
| 19 |
+
[
|
| 20 |
+
transforms.Resize((28, 28)),
|
| 21 |
+
transforms.Grayscale(),
|
| 22 |
+
transforms.ToTensor(),
|
| 23 |
+
transforms.Normalize((0.5,), (0.5,)), # Normalization
|
| 24 |
+
transforms.Lambda(lambda x: 1.0 - x), # Invert colors
|
| 25 |
+
transforms.Lambda(lambda x: x[0]),
|
| 26 |
+
transforms.Lambda(lambda x: x.unsqueeze(0)),
|
| 27 |
+
]
|
| 28 |
+
)
|
| 29 |
+
|
| 30 |
+
|
| 31 |
+
def predict(img):
|
| 32 |
+
img = transform(Image.fromarray(img))
|
| 33 |
+
predictions = model.predictions(img)
|
| 34 |
+
return predictions
|
| 35 |
+
|
| 36 |
+
|
| 37 |
+
with gr.Blocks() as demo:
|
| 38 |
+
with gr.Tab("Garment Prediction"):
|
| 39 |
+
gr.Interface(
|
| 40 |
+
fn=predict,
|
| 41 |
+
inputs=inputs,
|
| 42 |
+
outputs=outputs,
|
| 43 |
+
examples=examples,
|
| 44 |
+
).queue(default_concurrency_limit=5)
|
| 45 |
+
|
| 46 |
+
demo.launch()
|