Update app.py
Browse files
app.py
CHANGED
|
@@ -4,17 +4,22 @@ model = torch.hub.load('pytorch/vision:v0.6.0', 'resnet18', pretrained=True).eva
|
|
| 4 |
|
| 5 |
|
| 6 |
import requests
|
| 7 |
-
|
| 8 |
from torchvision import transforms
|
| 9 |
|
| 10 |
# Download human-readable labels for ImageNet.
|
| 11 |
response = requests.get("https://git.io/JJkYN")
|
| 12 |
-
labels
|
| 13 |
-
|
| 14 |
-
def
|
| 15 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 16 |
with torch.no_grad():
|
| 17 |
-
prediction = torch.nn.functional.softmax(model(
|
| 18 |
confidences = {labels[i]: float(prediction[i]) for i in range(1000)}
|
| 19 |
return confidences
|
| 20 |
|
|
@@ -29,7 +34,7 @@ with gr.Blocks(title="Image Classification for 1000 Objects", css=".gradio-conta
|
|
| 29 |
output_label = gr.Label(label="Probabilities", num_top_classes=3)
|
| 30 |
|
| 31 |
send_btn = gr.Button("Infer")
|
| 32 |
-
send_btn.click(fn=
|
| 33 |
|
| 34 |
with gr.Row():
|
| 35 |
gr.Examples(['./lion.jpg'] , label='Sample images : Lion', inputs=input_image)
|
|
|
|
| 4 |
|
| 5 |
|
| 6 |
import requests
|
| 7 |
+
import PIL
|
| 8 |
from torchvision import transforms
|
| 9 |
|
| 10 |
# Download human-readable labels for ImageNet.
|
| 11 |
response = requests.get("https://git.io/JJkYN")
|
| 12 |
+
labels = response.text.split("\n")
|
| 13 |
+
|
| 14 |
+
def classify_image(image_filepath):
|
| 15 |
+
PIL_image = PIL.Image.open(image_filepath).convert('RGB')
|
| 16 |
+
transformations = transforms.Compose([
|
| 17 |
+
transforms.Resize(size = (224,224)),
|
| 18 |
+
transforms.ToTensor(),
|
| 19 |
+
])
|
| 20 |
+
image_tensors = transformations(PIL_image).unsqueeze(0)
|
| 21 |
with torch.no_grad():
|
| 22 |
+
prediction = torch.nn.functional.softmax(model(image_tensors)[0], dim=0)
|
| 23 |
confidences = {labels[i]: float(prediction[i]) for i in range(1000)}
|
| 24 |
return confidences
|
| 25 |
|
|
|
|
| 34 |
output_label = gr.Label(label="Probabilities", num_top_classes=3)
|
| 35 |
|
| 36 |
send_btn = gr.Button("Infer")
|
| 37 |
+
send_btn.click(fn=classify_image, inputs=input_image, outputs=output_label)
|
| 38 |
|
| 39 |
with gr.Row():
|
| 40 |
gr.Examples(['./lion.jpg'] , label='Sample images : Lion', inputs=input_image)
|