trevorhobenshield commited on
Commit
4e46b4b
·
1 Parent(s): c364b4f

Update README.md

Browse files
Files changed (1) hide show
  1. README.md +40 -1
README.md CHANGED
@@ -6,4 +6,43 @@ datasets:
6
  - imagenet-1k
7
  ---
8
 
9
- See: https://huggingface.co/timm/edgenext_small.usi_in1k
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
6
  - imagenet-1k
7
  ---
8
 
9
+ See: https://huggingface.co/timm/edgenext_small.usi_in1k
10
+
11
+ ```python
12
+ from urllib.request import urlopen
13
+
14
+ import einops
15
+ import numpy as np
16
+ import onnxruntime as ort
17
+ from PIL import Image
18
+
19
+ def softmax(x):
20
+ y = np.exp(x - np.max(x))
21
+ return y / y.sum(axis=0)
22
+
23
+ IMG_URL = 'https://huggingface.co/datasets/huggingface/documentation-images/resolve/main/beignets-task-guide.png'
24
+ IN1K_CLASSES_URL = 'https://storage.googleapis.com/bit_models/ilsvrc2012_wordnet_lemmas.txt'
25
+
26
+ session = ort.InferenceSession('edgenext_small.usi_in1k.ort')
27
+ # session = ort.InferenceSession('edgenext_small.usi_in1k.onnx')
28
+
29
+ labels = urlopen(IN1K_CLASSES_URL).read().decode().splitlines()
30
+ img = np.array(
31
+ Image.open(urlopen(IMG_URL))
32
+ .resize(session._sess.inputs_meta[0].shape[2:])
33
+ )
34
+
35
+ # e.g. in1k norm stats
36
+ mean = .485, .456, .406
37
+ sd = .229, .224, .225
38
+ img = (img / 255. - mean) / sd
39
+
40
+ # to clearly illustrate format ort expects
41
+ img = einops.rearrange(img, 'h w c -> 1 c h w').astype(np.float32)
42
+ out = session.run(None, {session.get_inputs()[0].name: img})
43
+
44
+ out = softmax(out[0][0])
45
+ topk = np.argsort(out)[::-1][:5]
46
+ for i in topk:
47
+ print(f'{out[i]:.2f}', labels[i])
48
+ ```