MacSuperior
commited on
Commit
·
35752ce
1
Parent(s):
0629dd7
Initial Deploy
Browse files- README.md +4 -4
- app.py +33 -0
- converted_model.onnx +3 -0
- requirements.txt +6 -0
- resnet50.onnx +3 -0
README.md
CHANGED
@@ -1,13 +1,13 @@
|
|
1 |
---
|
2 |
-
title: ONNX Demo
|
3 |
emoji: 💻
|
4 |
-
colorFrom:
|
5 |
-
colorTo:
|
6 |
sdk: gradio
|
7 |
sdk_version: 5.29.0
|
8 |
app_file: app.py
|
9 |
pinned: false
|
10 |
-
short_description:
|
11 |
---
|
12 |
|
13 |
Check out the configuration reference at https://huggingface.co/docs/hub/spaces-config-reference
|
|
|
1 |
---
|
2 |
+
title: ResNet-50 ONNX Demo
|
3 |
emoji: 💻
|
4 |
+
colorFrom: blue
|
5 |
+
colorTo: red
|
6 |
sdk: gradio
|
7 |
sdk_version: 5.29.0
|
8 |
app_file: app.py
|
9 |
pinned: false
|
10 |
+
short_description: Classify Images with a ResNet-50 ONNX model.
|
11 |
---
|
12 |
|
13 |
Check out the configuration reference at https://huggingface.co/docs/hub/spaces-config-reference
|
app.py
ADDED
@@ -0,0 +1,33 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
import numpy as np
|
2 |
+
import onnxruntime as ort
|
3 |
+
import gradio as gr
|
4 |
+
from PIL import Image
|
5 |
+
from torchvision.models import ResNet50_Weights
|
6 |
+
|
7 |
+
weights = ResNet50_Weights.DEFAULT
|
8 |
+
preprocess = weights.transforms() # Necessary input transformations
|
9 |
+
ort_session = ort.InferenceSession("resnet50.onnx", providers=["CPUExecutionProvider"])
|
10 |
+
|
11 |
+
def preprocess_inputs(img: Image):
|
12 |
+
img = preprocess(img)
|
13 |
+
img_array = np.array(img).astype(np.float32)
|
14 |
+
img_array = np.expand_dims(img_array, axis=0)
|
15 |
+
return img_array
|
16 |
+
|
17 |
+
def predict(img):
|
18 |
+
img = preprocess_inputs(img)
|
19 |
+
ort_inputs = {ort_session.get_inputs()[0].name: img}
|
20 |
+
ort_outputs = ort_session.run(None, ort_inputs)
|
21 |
+
|
22 |
+
#probs = np.exp(ort_outputs) / np.sum(np.exp(ort_outputs)) # softmax
|
23 |
+
|
24 |
+
label_index = np.argmax(ort_outputs[0], axis=1).item()
|
25 |
+
predicted_label = weights.meta["categories"][label_index]
|
26 |
+
return predicted_label
|
27 |
+
|
28 |
+
|
29 |
+
demo = gr.Interface(predict, gr.Image(type="pil", image_mode="RGB"), gr.Label(),
|
30 |
+
title="ResNet-50 Using onnxruntime",
|
31 |
+
description="Upload any image and see if resnet-50 can classify it! (1000 possible image classes)",
|
32 |
+
article="Part of a tutorial on [how to deploy an ONNX mode to Hugging Face](https://liamgroen.nl/posts/day-6-deploying-model-to-huggingface-spaces-through-onnx/index.html)")
|
33 |
+
demo.launch()
|
converted_model.onnx
ADDED
@@ -0,0 +1,3 @@
|
|
|
|
|
|
|
|
|
1 |
+
version https://git-lfs.github.com/spec/v1
|
2 |
+
oid sha256:721de5ee3110562d2a2fa96cd93ab0ef72694b2f9b734ed62b59067f0721a92c
|
3 |
+
size 2680296
|
requirements.txt
ADDED
@@ -0,0 +1,6 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
torch
|
2 |
+
torchvision
|
3 |
+
onnx
|
4 |
+
onnxruntime
|
5 |
+
onnxscript
|
6 |
+
gradio
|
resnet50.onnx
ADDED
@@ -0,0 +1,3 @@
|
|
|
|
|
|
|
|
|
1 |
+
version https://git-lfs.github.com/spec/v1
|
2 |
+
oid sha256:a0e1f07b5c3758b512755af0e74886f600e05242000a3d80e2892700477a7c27
|
3 |
+
size 102146392
|