Spaces:
Runtime error
Runtime error
Commit
路
f9a5b44
1
Parent(s):
f4d11d9
cleanup
Browse files
README.md
CHANGED
@@ -1,16 +1,5 @@
|
|
1 |
-
|
2 |
-
|
3 |
-
emoji: 馃殌
|
4 |
-
colorFrom: indigo
|
5 |
-
colorTo: pink
|
6 |
-
sdk: gradio
|
7 |
-
sdk_version: 2.9.4
|
8 |
-
app_file: app.py
|
9 |
-
pinned: false
|
10 |
-
license: afl-3.0
|
11 |
-
---
|
12 |
-
|
13 |
-
Check out the configuration reference at https://huggingface.co/docs/hub/spaces#reference
|
14 |
|
15 |
### References
|
16 |
* https://huggingface.co/docs/hub/spaces#manage-app-with-github-actions
|
|
|
1 |
+
# Image Recognition Demo
|
2 |
+
This is a simple demo of an image recognition system built with Gradio and served on HuggingFace Spaces.
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
3 |
|
4 |
### References
|
5 |
* https://huggingface.co/docs/hub/spaces#manage-app-with-github-actions
|
app.py
CHANGED
@@ -1,8 +1,9 @@
|
|
|
|
1 |
import torch
|
|
|
|
|
2 |
from PIL import Image
|
3 |
from torchvision import transforms
|
4 |
-
import gradio as gr
|
5 |
-
import os
|
6 |
|
7 |
|
8 |
"""
|
@@ -11,14 +12,17 @@ https://huggingface.co/spaces/pytorch/ResNet/tree/main
|
|
11 |
https://www.gradio.app/image_classification_in_pytorch/
|
12 |
"""
|
13 |
|
|
|
14 |
os.system("wget https://raw.githubusercontent.com/pytorch/hub/master/imagenet_classes.txt")
|
15 |
|
|
|
16 |
model = torch.hub.load('pytorch/vision:v0.10.0', 'resnet18', pretrained=True)
|
17 |
model.eval()
|
18 |
|
19 |
# Download an example image from the pytorch website
|
20 |
torch.hub.download_url_to_file("https://github.com/pytorch/hub/raw/master/images/dog.jpg", "dog.jpg")
|
21 |
|
|
|
22 |
def inference(input_image):
|
23 |
preprocess = transforms.Compose([
|
24 |
transforms.Resize(256),
|
@@ -29,7 +33,7 @@ def inference(input_image):
|
|
29 |
input_tensor = preprocess(input_image)
|
30 |
input_batch = input_tensor.unsqueeze(0) # create a mini-batch as expected by the model
|
31 |
|
32 |
-
#
|
33 |
if torch.cuda.is_available():
|
34 |
input_batch = input_batch.to('cuda')
|
35 |
model.to('cuda')
|
@@ -49,13 +53,16 @@ def inference(input_image):
|
|
49 |
result[categories[top5_catid[i]]] = top5_prob[i].item()
|
50 |
return result
|
51 |
|
|
|
52 |
inputs = gr.inputs.Image(type='pil')
|
53 |
outputs = gr.outputs.Label(type="confidences",num_top_classes=5)
|
54 |
|
|
|
55 |
title = "Image Recognition Demo"
|
56 |
description = "This is a prototype application which demonstrates how artifical intelligence based systems can recognize what object(s) is present in an image. This fundamental task in computer vision known as `Image Classification` has applications stretching from autonomous vehicles to medical imaging. To use it, simply upload your image, or click one of the examples images to load them, which I took at Montr茅al Biod么me! Read more at the links below."
|
57 |
article = "<p style='text-align: center'><a href='https://arxiv.org/abs/1512.03385' target='_blank'>Deep Residual Learning for Image Recognition</a> | <a href='https://github.com/pytorch/vision/blob/main/torchvision/models/resnet.py' target='_blank'>Github Repo</a></p>"
|
58 |
|
|
|
59 |
gr.Interface(inference,
|
60 |
inputs,
|
61 |
outputs,
|
|
|
1 |
+
import os
|
2 |
import torch
|
3 |
+
import gradio as gr
|
4 |
+
|
5 |
from PIL import Image
|
6 |
from torchvision import transforms
|
|
|
|
|
7 |
|
8 |
|
9 |
"""
|
|
|
12 |
https://www.gradio.app/image_classification_in_pytorch/
|
13 |
"""
|
14 |
|
15 |
+
# Get classes list
|
16 |
os.system("wget https://raw.githubusercontent.com/pytorch/hub/master/imagenet_classes.txt")
|
17 |
|
18 |
+
# Load PyTorch model
|
19 |
model = torch.hub.load('pytorch/vision:v0.10.0', 'resnet18', pretrained=True)
|
20 |
model.eval()
|
21 |
|
22 |
# Download an example image from the pytorch website
|
23 |
torch.hub.download_url_to_file("https://github.com/pytorch/hub/raw/master/images/dog.jpg", "dog.jpg")
|
24 |
|
25 |
+
# Inference!
|
26 |
def inference(input_image):
|
27 |
preprocess = transforms.Compose([
|
28 |
transforms.Resize(256),
|
|
|
33 |
input_tensor = preprocess(input_image)
|
34 |
input_batch = input_tensor.unsqueeze(0) # create a mini-batch as expected by the model
|
35 |
|
36 |
+
# Move the input and model to GPU for speed if available
|
37 |
if torch.cuda.is_available():
|
38 |
input_batch = input_batch.to('cuda')
|
39 |
model.to('cuda')
|
|
|
53 |
result[categories[top5_catid[i]]] = top5_prob[i].item()
|
54 |
return result
|
55 |
|
56 |
+
# Define ins outs placeholders
|
57 |
inputs = gr.inputs.Image(type='pil')
|
58 |
outputs = gr.outputs.Label(type="confidences",num_top_classes=5)
|
59 |
|
60 |
+
# Define style
|
61 |
title = "Image Recognition Demo"
|
62 |
description = "This is a prototype application which demonstrates how artifical intelligence based systems can recognize what object(s) is present in an image. This fundamental task in computer vision known as `Image Classification` has applications stretching from autonomous vehicles to medical imaging. To use it, simply upload your image, or click one of the examples images to load them, which I took at Montr茅al Biod么me! Read more at the links below."
|
63 |
article = "<p style='text-align: center'><a href='https://arxiv.org/abs/1512.03385' target='_blank'>Deep Residual Learning for Image Recognition</a> | <a href='https://github.com/pytorch/vision/blob/main/torchvision/models/resnet.py' target='_blank'>Github Repo</a></p>"
|
64 |
|
65 |
+
# Run inference
|
66 |
gr.Interface(inference,
|
67 |
inputs,
|
68 |
outputs,
|