Upload inference.py
Browse files- inference.py +31 -0
inference.py
ADDED
@@ -0,0 +1,31 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
import requests
|
2 |
+
from PIL import Image
|
3 |
+
|
4 |
+
from optimum.amd.ryzenai import RyzenAIModelForImageClassification
|
5 |
+
from transformers import PretrainedConfig, pipeline
|
6 |
+
|
7 |
+
import timm
|
8 |
+
import torch
|
9 |
+
|
10 |
+
url = "http://images.cocodataset.org/val2017/000000039769.jpg"
|
11 |
+
image = Image.open(requests.get(url, stream=True).raw)
|
12 |
+
|
13 |
+
quantized_model_path = "mohitsha/timm_resnet18_onnx_quantized_ryzen"
|
14 |
+
|
15 |
+
# The path and name of the runtime configuration file. A default version of this file can be
|
16 |
+
# found in the voe-4.0-win_amd64 folder of the Ryzen AI software installation package under
|
17 |
+
# the name vaip_config.json
|
18 |
+
vaip_config = ".\\vaip_config.json"
|
19 |
+
|
20 |
+
model = RyzenAIModelForImageClassification.from_pretrained(quantized_model_path, vaip_config=vaip_config)
|
21 |
+
|
22 |
+
config = PretrainedConfig.from_pretrained(quantized_model_path)
|
23 |
+
|
24 |
+
# preprocess config
|
25 |
+
data_config = timm.data.resolve_data_config(pretrained_cfg=config.pretrained_cfg)
|
26 |
+
transforms = timm.data.create_transform(**data_config, is_training=False)
|
27 |
+
|
28 |
+
output = model(transforms(image).unsqueeze(0)).logits # unsqueeze single image into batch of 1
|
29 |
+
top5_probabilities, top5_class_indices = torch.topk(torch.softmax(output, dim=1) * 100, k=5)
|
30 |
+
|
31 |
+
print(top5_class_indices)
|