|
import requests |
|
from PIL import Image |
|
|
|
from optimum.amd.ryzenai import RyzenAIModelForImageClassification |
|
from transformers import PretrainedConfig, pipeline |
|
|
|
import timm |
|
import torch |
|
|
|
url = "http://images.cocodataset.org/val2017/000000039769.jpg" |
|
image = Image.open(requests.get(url, stream=True).raw) |
|
|
|
quantized_model_path = "mohitsha/timm-resnet18-onnx-quantized-ryzen" |
|
|
|
|
|
|
|
|
|
vaip_config = ".\\vaip_config.json" |
|
|
|
model = RyzenAIModelForImageClassification.from_pretrained(quantized_model_path, vaip_config=vaip_config) |
|
|
|
config = PretrainedConfig.from_pretrained(quantized_model_path) |
|
|
|
|
|
data_config = timm.data.resolve_data_config(pretrained_cfg=config.pretrained_cfg) |
|
transforms = timm.data.create_transform(**data_config, is_training=False) |
|
|
|
output = model(transforms(image).unsqueeze(0)).logits |
|
top5_probabilities, top5_class_indices = torch.topk(torch.softmax(output, dim=1) * 100, k=5) |
|
|
|
print(top5_class_indices) |