diff --git a/research/cv/ibnnet/README_CN.md b/research/cv/ibnnet/README_CN.md index d24f8659bcf87b288916527cf42765785e493a66..37402f7c55f38980c11bc6f424c213f655040be6 100644 --- a/research/cv/ibnnet/README_CN.md +++ b/research/cv/ibnnet/README_CN.md @@ -91,7 +91,8 @@ bash scripts/run_eval.sh ├── run_standalone_train.sh // 用于单机训练的shell脚本 ├── run_standalone_train.sh // 用于GPU单机训练的shell脚本 ├── run_eval.sh // 用于评估的shell脚本 - └── run_eval.sh // 用于GPU评估的shell脚本 + ├── run_eval.sh // 用于GPU评估的shell脚本 + └── run_eval_onnx.sh // 用于评估onnx的shell脚本 ├── src ├── loss.py //损失函数 ├── lr_generator.py //生成学习率 @@ -102,6 +103,7 @@ bash scripts/run_eval.sh ├── pth2ckpt.py //转换pth文件为ckpt文件 ├── export.py ├── eval.py // 测试脚本 + ├── eval_onnx.py // onnx模型评估脚本 ├── train.py // 训练脚本 ├── preprocess.py // 310推理数据预处理 ├── preprocess.py // 310推理数据后处理 @@ -210,6 +212,31 @@ python export.py --ckpt_file [CKPT_PATH] --file_name [FILE_NAME] --file_format [ 参数`ckpt_file` 是必需的,`FILE_FORMAT` 必须在 ["AIR", "MINDIR"]中进行选择。 +## onnx模型导出与推理 + +- 导出 ONNX: + +```shell +python export.py --ckpt_file [CKPT_PATH] --file_format [FILE_FORMAT] --device_target [DEVICE_TARGET] +e.g. python export.py --ckpt_file ibnnet_top1acc77.12_top5acc93.58.ckpt --file_format ONNX --device_target GPU +``` + +- 运行推理-python方式: + +```shell +python ./eval_onnx.py --onnx_path=[ONNX_PATH] --dataset_path=[DATASET_PATH] --device_target=[DEVICE_TARGET] +e.g. python ./eval_onnx.py --onnx_path=./ibnnet.onnx --dataset_path=../ghost/dataset/ILSVRC2012_img_val --device_target=GPU +``` + +- 运行推理-bash方式: + +```shell +bash run_eval_onnx.sh [ONNX_PATH] [DATASET_PATH] [DEVICE_TARGET] +bash run_eval_onnx.sh ./ibnnet.onnx ../ghost/dataset/ILSVRC2012_img_val/ GPU +``` + +- 评估结果将存放在 eval_onnx.log 中. + # 推理过程 ## 用法 @@ -270,4 +297,3 @@ bash run_310_infer.sh [MINDIR_PATH] [DATASET_PATH] # ModelZoo主页 请浏览官网[主页](https://gitee.com/mindspore/models)。 - diff --git a/research/cv/ibnnet/eval_onnx.py b/research/cv/ibnnet/eval_onnx.py new file mode 100644 index 0000000000000000000000000000000000000000..6b21b12862a829b7b937a04414aa46d59389bc4b --- /dev/null +++ b/research/cv/ibnnet/eval_onnx.py @@ -0,0 +1,74 @@ +# Copyright 2022 Huawei Technologies Co., Ltd +# +# Licensed under the Apache License, Version 2.0 (the "License"); +# you may not use this file except in compliance with the License. +# You may obtain a copy of the License at +# +# http://www.apache.org/licenses/LICENSE-2.0 +# +# Unless required by applicable law or agreed to in writing, software +# distributed under the License is distributed on an "AS IS" BASIS, +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +# See the License for the specific language governing permissions and +# limitations under the License. +# ============================================================================ +""" +eval +""" + +import argparse +import mindspore.nn as nn +import onnxruntime as ort + +from src.dataset import create_evalset + + + +def create_session(onnx_path, target_device): + if target_device == 'GPU': + providers = ['CUDAExecutionProvider'] + elif target_device == 'CPU': + providers = ['CPUExecutionProvider'] + else: + raise ValueError( + f'Unsupported target device {target_device}, ' + f'Expected one of: "CPU", "GPU"' + ) + session = ort.InferenceSession(onnx_path, providers=providers) + input_name = session.get_inputs()[0].name + return session, input_name + +def onnx_eval(onnx_path, dataset_path, batch_size, device_target): + # create dataset + ds = create_evalset(dataset_path=dataset_path, + do_train=False, + repeat_num=1, + batch_size=batch_size, + target=device_target) + # load onnx + session, input_name = create_session(onnx_path=onnx_path, + target_device=device_target) + # evaluation + metrics = { + 'top-1 accuracy': nn.Top1CategoricalAccuracy(), + 'top-5 accuracy': nn.Top5CategoricalAccuracy(), + } + for batch in ds.create_dict_iterator(num_epochs=1, output_numpy=True): + y_pred = session.run(None, {input_name: batch['image']})[0] + for metric in metrics.values(): + metric.update(y_pred, batch['label']) + return {name: metric.eval() for name, metric in metrics.items()} + +if __name__ == '__main__': + parser = argparse.ArgumentParser(description='Image classification') + # onnx parameter + parser.add_argument('--dataset_path', type=str, default=None, help='Dataset path') + parser.add_argument('--onnx_path', type=str, default=None, help='ONNX file path') + parser.add_argument('--device_target', type=str, default='GPU', help='Device target') + parser.add_argument('--batch_size', type=int, default=1, help='Batch size') + + args = parser.parse_args() + + results = onnx_eval(args.onnx_path, args.dataset_path, args.batch_size, args.device_target) + for name, value in results.items(): + print(f'{name}: {value:.4f}') diff --git a/research/cv/ibnnet/requirements.txt b/research/cv/ibnnet/requirements.txt index 08ed5eeb4b9f080b780db7d3e0af6712866c0493..5de5f677ee9d5e41e096fbd38c6016f7774f85f4 100644 --- a/research/cv/ibnnet/requirements.txt +++ b/research/cv/ibnnet/requirements.txt @@ -1 +1,2 @@ -torch \ No newline at end of file +torch +onnxruntime-gpu \ No newline at end of file diff --git a/research/cv/ibnnet/scripts/run_eval_onnx.sh b/research/cv/ibnnet/scripts/run_eval_onnx.sh new file mode 100644 index 0000000000000000000000000000000000000000..563b05a23178c4cbf83edd24d833bc87b5a536d0 --- /dev/null +++ b/research/cv/ibnnet/scripts/run_eval_onnx.sh @@ -0,0 +1,47 @@ +#!/bin/bash +# Copyright 2022 Huawei Technologies Co., Ltd +# +# Licensed under the Apache License, Version 2.0 (the "License"); +# you may not use this file except in compliance with the License. +# You may obtain a copy of the License at +# +# http://www.apache.org/licenses/LICENSE-2.0 +# +# Unless required by applicable law or agreed to in writing, software +# distributed under the License is distributed on an "AS IS" BASIS, +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +# See the License for the specific language governing permissions and +# limitations under the License. +# ============================================================================ + +if [[ $# -ne 3 ]]; then + echo "Usage: bash run_infer_onnx.sh [ONNX_PATH] [DATASET_PATH] [DEVICE_TARGET]" +exit 1 +fi + +get_real_path(){ + if [ "${1:0:1}" == "/" ]; then + echo "$1" + else + echo "$(realpath -m $PWD/$1)" + fi +} +onnx_path=$(get_real_path $1) +dataset_path=$(get_real_path $2) +device_target=$3 + +echo "onnx_path: "$onnx_path +echo "dataset_path: "$dataset_path +echo "device_target: "$device_target + +function infer() +{ + python ./eval_onnx.py --onnx_path=$onnx_path \ + --dataset_path=$dataset_path \ + --device_target=$device_target &> eval_onnx.log 2>&1 +} +infer +if [ $? -ne 0 ]; then + echo " execute inference failed" + exit 1 +fi \ No newline at end of file