rrferrglgf-NLP / README.md
DQLiu's picture
Upload folder using huggingface_hub
4512767 verified
|
raw
history blame
2.62 kB
## MobileNetV2
MobileNetV2 是在分辨率 224x224 的 ImageNet-1k 数据集上预训练的图像分类模型。https://github.com/tensorflow/models/tree/master/research/slim/nets/mobilenet
我们开发了一个 AMD Ryzen AI (https://ryzenai.docs.amd.com/en/latest/) 支持的修改版本 - https://huggingface.co/models?search=mobilenet_v2
详情可参照 https://huggingface.co/amd/mobilenet_v2_1.0_224
## 安装
按照 https://ryzenai.docs.amd.com/en/latest/inst.html 为 Ryzen AI 准备环境。
运行以下脚本安装pre-requisites包
```shell
pip install -r requirements.txt
```
## 推理
推理一张图片
```shell
import sys
import onnxruntime
import torch
import torchvision.transforms as transforms
from PIL import Image
image_path = sys.argv[1]
onnx_model = sys.argv[2]
normalize = transforms.Normalize(
mean=[0.485, 0.456, 0.406], std=[0.229, 0.224, 0.225])
img_transformer = transforms.Compose([
transforms.Resize(256),
transforms.CenterCrop(224),
transforms.ToTensor(),
normalize])
img_tensor = img_transformer(Image.open(image_path)).unsqueeze(0)
img_tensor = torch.permute(img_tensor, (0, 2, 3, 1))
so = onnxruntime.SessionOptions()
ort_session = onnxruntime.InferenceSession(
onnx_model, so,
providers=['CPUExecutionProvider'],
provider_options=None)
input = img_tensor.numpy()
ort_input = {ort_session.get_inputs()[0].name: input}
output = ort_session.run(None, ort_input)
top5_probabilities, top5_class_indices = torch.topk(torch.nn.functional.softmax(torch.tensor(output[0])), k=5)
```
vaip_config.json 来自于Ryzen AI 安装包 https://ryzenai.docs.amd.com/en/latest/inst.html, voe-4.0-win_amd64 of ryzen-ai-sw-1.0.zip.
使用 eval_onnx.py 推理 ImageNet validation dataset (50,000 Images).
测试量化模型CPU
```shell
python eval_onnx.py --onnx_model=./mobilenetv2_int8.onnx --data_dir=./{DATA_PATH}
```
测试量化模型IPU
```shell
python eval_onnx.py --onnx_model=./mobilenetv2_int8.onnx --data_dir=./{DATA_PATH} --ipu --provider_config Path\To\vaip_config.json
Metric Accuracy on IPU
top1& top5 accuracy 75.62% / 92.52%
```
DATA_PATH: ImageNet validation目录
### 引用
```shell
@article{MobileNet v2,
author = {Mark Sandler and
Andrew G. Howard and
Menglong Zhu and
Andrey Zhmoginov and
Liang{-}Chieh Chen},
title = {MobileNetV2: Inverted Residuals and Linear Bottlenecks},
year = {2018},
url = {http://arxiv.org/abs/1801.04381},
}
```