diff --git a/research/cv/UNet3+/README_CN.md b/research/cv/UNet3+/README_CN.md index 02c74fe36e6c0cfd6f129a7dceef70738adffd4b..8fa0d505389d66a0d54a630099bb2339dc6ded66 100644 --- a/research/cv/UNet3+/README_CN.md +++ b/research/cv/UNet3+/README_CN.md @@ -18,6 +18,7 @@ - [Mindir推理](#Mindir推理) - [导出模型](#导出模型) - [在Ascend310执行推理](#在Ascend310执行推理) + - [onnx推理](#onnx推理) - [结果](#结果) - [模型描述](#模型描述) - [性能](#性能) @@ -107,6 +108,7 @@ Ascend训练:生成[RANK_TABLE_FILE](https://gitee.com/mindspore/models/tree/m │ ├──run_eval.sh // 推理启动脚本 │ ├──run_train.sh // 训练启动脚本 | ├──run_infer_310.sh // 310推理启动脚本 + |——run_eval_onnx.sh // onnx推理启动脚本 ├── src │ ├──config.py // 配置加载文件 │ ├──dataset.py // 数据集处理 @@ -120,6 +122,7 @@ Ascend训练:生成[RANK_TABLE_FILE](https://gitee.com/mindspore/models/tree/m ├── dataset_preprocess.py // 数据集预处理脚本 ├── postprocess.py // 310精度计算脚本 ├── preprocess.py // 310预处理脚本 + |—— eval_onnx.py // onnx推理脚本 ``` @@ -234,11 +237,11 @@ Ascend训练:生成[RANK_TABLE_FILE](https://gitee.com/mindspore/models/tree/m ### [导出MindIR](#contents) ```shell -python export.py --ckpt_file [CKPT_PATH] --file_name [FILE_NAME] --file_format [FILE_FORMAT] +python export.py --ckpt_file [CKPT_PATH] --file_name [FILE_NAME] --file_format [FILE_FORMAT] --device_target [DEVICE_TARGET] ``` - `ckpt_file`为必填项。 -- `file_format` 必须在 ["AIR", "MINDIR"]中选择。 +- `file_format` 必须在 ["AIR", "MINDIR","ONNX"]中选择。 ### 在Ascend310执行推理 @@ -253,6 +256,19 @@ bash run_infer_310.sh [MINDIR_PATH] [DATA_PATH] [NEED_PREPROCESS] [DEVICE_ID] - `NEED_PREPROCESS` 表示数据是否需要预处理,取值范围为 'y' 或者 'n'。 - `DEVICE_ID` 可选,默认值为0。 +### onnx推理 + +在执行推理前,onnx文件必须通过`export.py`脚本导出。 + +```shell +# onnx inference +bash run_eval_onnx.sh [DATASET_PATH] [ONNX_MODEL] [DEVICE_TARGET] +``` + +- `DATASET_PATH` 为测试集所在的路径 +- `ONNX_MODEL` 为导出的onnx模型所在路径 +- `DEVICE_TARGET` 必须在['Ascend','CPU','GPU']中选择 + ### 结果 推理结果保存在脚本执行的当前路径,你可以在acc.log中看到以下精度计算结果。 diff --git a/research/cv/UNet3+/eval_onnx.py b/research/cv/UNet3+/eval_onnx.py new file mode 100644 index 0000000000000000000000000000000000000000..1cc7a018bc29152c4384b9a142a7f27de7466161 --- /dev/null +++ b/research/cv/UNet3+/eval_onnx.py @@ -0,0 +1,76 @@ +# 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. +# ============================================================================ +"""Run evaluation for a model exported to ONNX""" +import os +import datetime +import onnxruntime as ort +import numpy as np +from mindspore.common import set_seed + +from src.logger import get_logger +from src.dataset import create_Dataset +from src.config import config +from eval import AverageMeter, dice_coef + + +def sigmoid(prediction): + """sigmoid function""" + negative = prediction < 0 + positive = prediction > 0 + sig_prediction = np.zeros_like(prediction) + sig_prediction[negative] = np.exp(prediction[negative]) / (1 + np.exp(prediction[negative])) + sig_prediction[positive] = 1 / (1 + np.exp(-prediction[positive])) + return sig_prediction + + +def create_session(checkpoint_path, target_device): + """create_session for onnx""" + if target_device == "GPU": + providers = ['CUDAExecutionProvider'] + elif target_device in ['CPU', 'Ascend']: + providers = ['CPUExecutionProvider'] + else: + raise ValueError(f"Unsupported target device '{target_device}'.\ + Expected one of: 'CPU', 'GPU', 'Ascend'") + session = ort.InferenceSession(checkpoint_path, providers=providers) + input_name = session.get_inputs()[0].name + return session, input_name + + +def run_onnx_eval(): + """run_onnx_eval""" + set_seed(1) + config.save_dir = os.path.join(config.output_path, + datetime.datetime.now().strftime('%Y-%m-%d_time_%H_%M_%S')) + if not os.path.exists(config.save_dir): + os.makedirs(config.save_dir) + config.logger = get_logger(config.save_dir, "UNet3Plus", 0) + config.logger.save_args(config) + dataset, _ = create_Dataset(data_path=config.val_data_path, aug=0, + batch_size=config.batch_size, + device_num=1, rank=0, shuffle=False) + data_loader = dataset.create_dict_iterator(num_epochs=1, output_numpy=True) + dices = AverageMeter() + session, input_name = create_session(config.file_name, config.device_target) + for episode, data in enumerate(data_loader): + y_pred = sigmoid(session.run(None, {input_name: data['image']})[0]) + dice = dice_coef(y_pred, data['mask']) + config.logger.info("episode %d : dice = %f ", episode, dice) + dices.update(dice, config.batch_size) + config.logger.info("Final dice: %s", str(dices.avg)) + + +if __name__ == "__main__": + run_onnx_eval() diff --git a/research/cv/UNet3+/scripts/run_eval_onnx.sh b/research/cv/UNet3+/scripts/run_eval_onnx.sh new file mode 100644 index 0000000000000000000000000000000000000000..bef02703f9e1f25d20dafec0bbe92f31c9187831 --- /dev/null +++ b/research/cv/UNet3+/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 [ $# != 3 ];then + echo "Usage:bash scripts/eval_onnx.sh [DATASET_PATH] [ONNX_MODEL] [DEVICE_TARGET]" + exit 1 +fi + + +get_real_path(){ + if [ "${1:0:1}" == "/" ];then + echo "$1" + else + realpath -m "$PWD/$1" + fi +} + +DATASET_PATH=$(get_real_path "$1") +ONNX_MODEL=$(get_real_path "$2") +DEVICE_TARGET=${3} + +if [ ! -d "$DATASET_PATH" ];then + echo "error:DATASET_PATH=${DATASET_PATH} is not a directory" + exit 1 +fi + +if [ ! -f "$ONNX_MODEL" ];then + echo "error:ONNX_MODEL=${ONNX_MODEL} is not a file" + exit 1 +fi + +python ../eval_onnx.py \ + --val_data_path="$DATASET_PATH" \ + --file_name="$ONNX_MODEL" \ + --device_target="$DEVICE_TARGET" > eval_onnx.log 2>&1 & diff --git a/research/cv/UNet3+/src/dataset.py b/research/cv/UNet3+/src/dataset.py index eb8ccb3df182e78797906b8c19c294424d509602..d2b2d75ca201e052879f80959574ffdddfb04b2c 100644 --- a/research/cv/UNet3+/src/dataset.py +++ b/research/cv/UNet3+/src/dataset.py @@ -18,7 +18,6 @@ import glob import random import numpy as np from skimage.io import imread -from skimage import color import mindspore.dataset as ds import mindspore.dataset.vision as CV @@ -48,7 +47,6 @@ class Dataset: image = image[::-1, :, :].copy() mask = mask[::-1, :].copy() - image = color.gray2rgb(image) mask = mask[:, :, np.newaxis] return image, mask