Commit
·
3b3c76a
1
Parent(s):
1ba260e
init model
Browse files- README.md +72 -0
- eval_onnx.py +164 -0
- mnasnet_b1_int.onnx +3 -0
- requirements.txt +8 -0
README.md
ADDED
|
@@ -0,0 +1,72 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
---
|
| 2 |
+
license: apache-2.0
|
| 3 |
+
datasets:
|
| 4 |
+
- imagenet-1k
|
| 5 |
+
metrics:
|
| 6 |
+
- accuracy
|
| 7 |
+
tags:
|
| 8 |
+
- RyzenAI
|
| 9 |
+
- vision
|
| 10 |
+
- classification
|
| 11 |
+
- pytorch
|
| 12 |
+
- timm
|
| 13 |
+
---
|
| 14 |
+
|
| 15 |
+
# MNASNet_b1
|
| 16 |
+
Quantized MNASNet_b1 model that could be supported by [AMD Ryzen AI](https://ryzenai.docs.amd.com/en/latest/).
|
| 17 |
+
|
| 18 |
+
|
| 19 |
+
## Model description
|
| 20 |
+
MNASNet was first introduced in the paper [MnasNet: Platform-Aware Neural Architecture Search for Mobile](https://arxiv.org/abs/1807.11626).
|
| 21 |
+
|
| 22 |
+
The model implementation is from [timm](https://huggingface.co/timm/mnasnet_100.rmsp_in1k).
|
| 23 |
+
|
| 24 |
+
|
| 25 |
+
## How to use
|
| 26 |
+
|
| 27 |
+
### Installation
|
| 28 |
+
|
| 29 |
+
Follow [Ryzen AI Installation](https://ryzenai.docs.amd.com/en/latest/inst.html) to prepare the environment for Ryzen AI.
|
| 30 |
+
Run the following script to install pre-requisites for this model.
|
| 31 |
+
|
| 32 |
+
```bash
|
| 33 |
+
pip install -r requirements.txt
|
| 34 |
+
```
|
| 35 |
+
|
| 36 |
+
### Data Preparation
|
| 37 |
+
|
| 38 |
+
Follow [ImageNet](https://huggingface.co/datasets/imagenet-1k) to prepare dataset.
|
| 39 |
+
|
| 40 |
+
### Model Evaluation
|
| 41 |
+
|
| 42 |
+
```python
|
| 43 |
+
python eval_onnx.py --onnx_model mnasnet_b1_int.onnx --ipu --provider_config Path\To\vaip_config.json --data_dir /Path/To/Your/Dataset
|
| 44 |
+
```
|
| 45 |
+
|
| 46 |
+
### Performance
|
| 47 |
+
|
| 48 |
+
|Metric |Accuracy on IPU|
|
| 49 |
+
| :----: | :----: |
|
| 50 |
+
|Top1/Top5| 73.85% / 91.82% |
|
| 51 |
+
|
| 52 |
+
|
| 53 |
+
```bibtex
|
| 54 |
+
@misc{rw2019timm,
|
| 55 |
+
author = {Ross Wightman},
|
| 56 |
+
title = {PyTorch Image Models},
|
| 57 |
+
year = {2019},
|
| 58 |
+
publisher = {GitHub},
|
| 59 |
+
journal = {GitHub repository},
|
| 60 |
+
doi = {10.5281/zenodo.4414861},
|
| 61 |
+
howpublished = {\url{https://github.com/huggingface/pytorch-image-models}}
|
| 62 |
+
}
|
| 63 |
+
```
|
| 64 |
+
```bibtex
|
| 65 |
+
@inproceedings{tan2019mnasnet,
|
| 66 |
+
title={Mnasnet: Platform-aware neural architecture search for mobile},
|
| 67 |
+
author={Tan, Mingxing and Chen, Bo and Pang, Ruoming and Vasudevan, Vijay and Sandler, Mark and Howard, Andrew and Le, Quoc V},
|
| 68 |
+
booktitle={Proceedings of the IEEE/CVF conference on computer vision and pattern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
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
#!/usr/bin/env python
|
| 2 |
+
|
| 3 |
+
from typing import Tuple
|
| 4 |
+
|
| 5 |
+
import argparse
|
| 6 |
+
import onnxruntime
|
| 7 |
+
import os
|
| 8 |
+
import sys
|
| 9 |
+
import time
|
| 10 |
+
import torch
|
| 11 |
+
import torchvision.datasets as datasets
|
| 12 |
+
import torchvision.transforms as transforms
|
| 13 |
+
|
| 14 |
+
from torch.utils.data import DataLoader
|
| 15 |
+
from tqdm import tqdm
|
| 16 |
+
|
| 17 |
+
parser = argparse.ArgumentParser()
|
| 18 |
+
parser.add_argument(
|
| 19 |
+
"--onnx_model", default="model.onnx", help="Input onnx model")
|
| 20 |
+
parser.add_argument(
|
| 21 |
+
"--data_dir",
|
| 22 |
+
default="/workspace/dataset/imagenet",
|
| 23 |
+
help="Directory of dataset")
|
| 24 |
+
parser.add_argument(
|
| 25 |
+
"--batch_size", default=1, type=int, help="Evaluation batch size")
|
| 26 |
+
parser.add_argument(
|
| 27 |
+
"--ipu",
|
| 28 |
+
action="store_true",
|
| 29 |
+
help="Use IPU for inference.",
|
| 30 |
+
)
|
| 31 |
+
parser.add_argument(
|
| 32 |
+
"--provider_config",
|
| 33 |
+
type=str,
|
| 34 |
+
default="vaip_config.json",
|
| 35 |
+
help="Path of the config file for seting provider_options.",
|
| 36 |
+
)
|
| 37 |
+
args = parser.parse_args()
|
| 38 |
+
|
| 39 |
+
class AverageMeter(object):
|
| 40 |
+
"""Computes and stores the average and current value"""
|
| 41 |
+
|
| 42 |
+
def __init__(self, name, fmt=':f'):
|
| 43 |
+
self.name = name
|
| 44 |
+
self.fmt = fmt
|
| 45 |
+
self.reset()
|
| 46 |
+
|
| 47 |
+
def reset(self):
|
| 48 |
+
self.val = 0
|
| 49 |
+
self.avg = 0
|
| 50 |
+
self.sum = 0
|
| 51 |
+
self.count = 0
|
| 52 |
+
|
| 53 |
+
def update(self, val, n=1):
|
| 54 |
+
self.val = val
|
| 55 |
+
self.sum += val * n
|
| 56 |
+
self.count += n
|
| 57 |
+
self.avg = self.sum / self.count
|
| 58 |
+
|
| 59 |
+
def __str__(self):
|
| 60 |
+
fmtstr = '{name} {val' + self.fmt + '} ({avg' + self.fmt + '})'
|
| 61 |
+
return fmtstr.format(**self.__dict__)
|
| 62 |
+
|
| 63 |
+
def accuracy(output: torch.Tensor,
|
| 64 |
+
target: torch.Tensor,
|
| 65 |
+
topk: Tuple[int] = (1,)) -> Tuple[float]:
|
| 66 |
+
"""Computes the accuracy over the k top predictions for the specified values of k.
|
| 67 |
+
Args:
|
| 68 |
+
output: Prediction of the model.
|
| 69 |
+
target: Ground truth labels.
|
| 70 |
+
topk: Topk accuracy to compute.
|
| 71 |
+
|
| 72 |
+
Returns:
|
| 73 |
+
Accuracy results according to 'topk'.
|
| 74 |
+
"""
|
| 75 |
+
|
| 76 |
+
with torch.no_grad():
|
| 77 |
+
maxk = max(topk)
|
| 78 |
+
batch_size = target.size(0)
|
| 79 |
+
|
| 80 |
+
_, pred = output.topk(maxk, 1, True, True)
|
| 81 |
+
pred = pred.t()
|
| 82 |
+
correct = pred.eq(target.view(1, -1).expand_as(pred))
|
| 83 |
+
|
| 84 |
+
res = []
|
| 85 |
+
for k in topk:
|
| 86 |
+
correct_k = correct[:k].contiguous().view(-1).float().sum(0, keepdim=True)
|
| 87 |
+
res.append(correct_k.mul_(100.0 / batch_size))
|
| 88 |
+
return res
|
| 89 |
+
|
| 90 |
+
def prepare_data_loader(data_dir: str,
|
| 91 |
+
batch_size: int = 100,
|
| 92 |
+
workers: int = 8) -> torch.utils.data.DataLoader:
|
| 93 |
+
"""Returns a validation data loader of ImageNet by given `data_dir`.
|
| 94 |
+
|
| 95 |
+
Args:
|
| 96 |
+
data_dir: Directory where images stores. There must be a subdirectory named
|
| 97 |
+
'validation' that stores the validation set of ImageNet.
|
| 98 |
+
batch_size: Batch size of data loader.
|
| 99 |
+
workers: How many subprocesses to use for data loading.
|
| 100 |
+
|
| 101 |
+
Returns:
|
| 102 |
+
An object of torch.utils.data.DataLoader.
|
| 103 |
+
"""
|
| 104 |
+
|
| 105 |
+
valdir = os.path.join(data_dir, 'validation')
|
| 106 |
+
|
| 107 |
+
normalize = transforms.Normalize(
|
| 108 |
+
mean=[0.485, 0.456, 0.406], std=[0.229, 0.224, 0.225])
|
| 109 |
+
val_dataset = datasets.ImageFolder(
|
| 110 |
+
valdir,
|
| 111 |
+
transforms.Compose([
|
| 112 |
+
transforms.Resize(256),
|
| 113 |
+
transforms.CenterCrop(224),
|
| 114 |
+
transforms.ToTensor(),
|
| 115 |
+
normalize,
|
| 116 |
+
]))
|
| 117 |
+
|
| 118 |
+
return torch.utils.data.DataLoader(
|
| 119 |
+
val_dataset,
|
| 120 |
+
batch_size=batch_size,
|
| 121 |
+
shuffle=False,
|
| 122 |
+
num_workers=workers,
|
| 123 |
+
pin_memory=True)
|
| 124 |
+
|
| 125 |
+
def val_imagenet():
|
| 126 |
+
"""Validate ONNX model on ImageNet dataset."""
|
| 127 |
+
print(f'Current onnx model: {args.onnx_model}')
|
| 128 |
+
|
| 129 |
+
if args.ipu:
|
| 130 |
+
providers = ["VitisAIExecutionProvider"]
|
| 131 |
+
provider_options = [{"config_file": args.provider_config}]
|
| 132 |
+
else:
|
| 133 |
+
providers = ['CUDAExecutionProvider', 'CPUExecutionProvider']
|
| 134 |
+
provider_options = None
|
| 135 |
+
ort_session = onnxruntime.InferenceSession(
|
| 136 |
+
args.onnx_model, providers=providers, provider_options=provider_options)
|
| 137 |
+
|
| 138 |
+
val_loader = prepare_data_loader(args.data_dir, args.batch_size)
|
| 139 |
+
|
| 140 |
+
top1 = AverageMeter('Acc@1', ':6.2f')
|
| 141 |
+
top5 = AverageMeter('Acc@5', ':6.2f')
|
| 142 |
+
|
| 143 |
+
start_time = time.time()
|
| 144 |
+
val_loader = tqdm(val_loader, file=sys.stdout)
|
| 145 |
+
with torch.no_grad():
|
| 146 |
+
for batch_idx, (images, targets) in enumerate(val_loader):
|
| 147 |
+
inputs, targets = images.numpy(), targets
|
| 148 |
+
ort_inputs = {ort_session.get_inputs()[0].name: inputs}
|
| 149 |
+
|
| 150 |
+
outputs = ort_session.run(None, ort_inputs)
|
| 151 |
+
outputs = torch.from_numpy(outputs[0])
|
| 152 |
+
|
| 153 |
+
acc1, acc5 = accuracy(outputs, targets, topk=(1, 5))
|
| 154 |
+
top1.update(acc1, images.size(0))
|
| 155 |
+
top5.update(acc5, images.size(0))
|
| 156 |
+
|
| 157 |
+
current_time = time.time()
|
| 158 |
+
print('Test Top1 {:.2f}%\tTop5 {:.2f}%\tTime {:.2f}s\n'.format(
|
| 159 |
+
float(top1.avg), float(top5.avg), (current_time - start_time)))
|
| 160 |
+
|
| 161 |
+
return top1.avg, top5.avg
|
| 162 |
+
|
| 163 |
+
if __name__ == '__main__':
|
| 164 |
+
val_imagenet()
|
mnasnet_b1_int.onnx
ADDED
|
@@ -0,0 +1,3 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
version https://git-lfs.github.com/spec/v1
|
| 2 |
+
oid sha256:a4784fa94c18ad2cb8c91081663cbdc825ba7bc61d1d913abd50d0fe0ff84949
|
| 3 |
+
size 17571696
|
requirements.txt
ADDED
|
@@ -0,0 +1,8 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
torch==1.12.0
|
| 2 |
+
torchsummary==1.5.1
|
| 3 |
+
torchvision==0.13.0
|
| 4 |
+
Pillow==9.4.0
|
| 5 |
+
onnx==1.14.0
|
| 6 |
+
# onnxruntime-gpu==1.14.1
|
| 7 |
+
timm==0.9.8
|
| 8 |
+
tqdm==4.64.0
|