diff --git a/official/cv/inceptionv4/README.md b/official/cv/inceptionv4/README.md index 53b718536a8895c1ee3b864a41215b8c934e104c..14b7e257b1fea816a80df859037bd15b2a8a35d6 100644 --- a/official/cv/inceptionv4/README.md +++ b/official/cv/inceptionv4/README.md @@ -34,11 +34,13 @@ The overall network architecture of InceptionV4 is show below: Dataset used can refer to paper. +- Dataset: ImageNet2012 - Dataset size: 125G, 1250k colorful images in 1000 classes - Train: 120G, 1200k images - Test: 5G, 50k images - Data format: RGB images. - Note: Data will be processed in src/dataset.py +- Data path: http://www.image-net.org/download-images # [Features](#contents) @@ -193,6 +195,7 @@ For FP16 operators, if the input data type is FP32, the backend of MindSpore wil ├─run_standalone_train_ascend.sh # launch standalone training with ascend platform(1p) ├─run_distribute_train_ascend.sh # launch distributed training with ascend platform(8p) ├─run_infer_310.sh # shell script for 310 inference + ├─run_onnx_eval.sh # shell script for onnx evaluating └─run_eval_ascend.sh # launch evaluating with ascend platform ├─src ├─dataset.py # data preprocessing @@ -207,6 +210,7 @@ For FP16 operators, if the input data type is FP32, the backend of MindSpore wil ├─default_config_cpu.yaml # Training parameter profile(cpu) ├─default_config_gpu.yaml # Training parameter profile(gpu) ├─eval.py # eval net + ├─eval_onnx.py # onnx eval ├─export.py # export checkpoint, surpport .onnx, .air, .mindir convert ├─postprogress.py # post process for 310 inference └─train.py # train net @@ -383,7 +387,7 @@ metric: {'Loss': 0.8144, 'Top1-Acc': 0.8009, 'Top5-Acc': 0.9457} python export.py --config_path [CONFIG_FILE] --ckpt_file [CKPT_PATH] --device_target [DEVICE_TARGET] --file_format[EXPORT_FORMAT] ``` -`EXPORT_FORMAT` should be in ["AIR", "MINDIR"] +`EXPORT_FORMAT` should be in ["AIR", "MINDIR", "ONNX"] ## Inference Process @@ -398,6 +402,16 @@ bash run_infer_310.sh [MINDIR_PATH] [DATA_PATH] [ANN_FILE] [DEVICE_ID] -NOTE:Ascend310 inference use Imagenet dataset . The label of the image is the number of folder which is started from 0 after sorting. +Before performing inference, the model file must be exported by export script. + +```shell +# ONNX inference +bash scripts/run_onnx_eval.sh [DATA_PATH] [DATASET_TYPE] [DEVICE_TYPE] [FILE_TYPE] [ONNX_PATH] +# example: bash scripts/run_onnx_eval.sh /path/to/dataset imagenet GPU ONNX /path/to/inceptionv4.onnx +``` + +-NOTE:ONNX inference use Imagenet dataset . + ### result Inference result is saved in current path, you can find result like this in acc.log file. diff --git a/official/cv/inceptionv4/default_config_cpu.yaml b/official/cv/inceptionv4/default_config_cpu.yaml index 9d7699ec86596341c4ec144baa2b2bfa5d16770b..b33ea2939b1e6baca5c2ee85d64d176c535b2271 100644 --- a/official/cv/inceptionv4/default_config_cpu.yaml +++ b/official/cv/inceptionv4/default_config_cpu.yaml @@ -18,7 +18,7 @@ resume: '' is_distributed: False device_id: 0 platform: 'CPU' -file_name: 'inceptionv3' +file_name: 'inceptionv4' file_format: 'MINDIR' width: 299 height: 299 diff --git a/official/cv/inceptionv4/default_config_gpu.yaml b/official/cv/inceptionv4/default_config_gpu.yaml index 736863ab109c269b77942d23a154c27659258840..41f310aee57bf7db40b88804207abfbe846a6329 100644 --- a/official/cv/inceptionv4/default_config_gpu.yaml +++ b/official/cv/inceptionv4/default_config_gpu.yaml @@ -8,6 +8,8 @@ output_path: "/cache/train" load_path: "/cache/checkpoint_path" device_target: GPU enable_profiling: False +num_parallel_workers: 1 +group_size: 1 # ============================================================================== dataset_path: "/cache/data" @@ -18,7 +20,7 @@ resume: '' is_distributed: False device_id: 0 platform: 'GPU' -file_name: 'inceptionv3' +file_name: 'inceptionv4' file_format: 'MINDIR' width: 299 height: 299 diff --git a/official/cv/inceptionv4/eval_onnx.py b/official/cv/inceptionv4/eval_onnx.py new file mode 100644 index 0000000000000000000000000000000000000000..be3a002a71fabbabae6f063e22993cca05c8d25e --- /dev/null +++ b/official/cv/inceptionv4/eval_onnx.py @@ -0,0 +1,75 @@ +# 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. +# ============================================================================ +"""evaluate_onnx""" +import mindspore.nn as nn +import onnxruntime as ort + + +from src.model_utils.config import get_config +from src.dataset import create_dataset_cifar10, create_dataset_imagenet + + +def create_session(checkpoint_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(checkpoint_path, providers=providers) + + input_name = session.get_inputs()[0].name + return session, input_name + + +def run_eval(cfg, checkpoint_path, dataset_name, data_dir, num_parallel_workers, target_device): + + session, input_name = create_session(checkpoint_path, target_device) + cfg.batch_size = 1 + if dataset_name == 'cifar10': + dataset = create_dataset_cifar10(data_dir, False, cfg, repeat_num=1) + metrics = { + 'accuracy': nn.Accuracy(), + } + elif dataset_name == 'imagenet': + dataset = create_dataset_imagenet(data_dir, False, cfg, repeat_num=1) + metrics = { + 'top-1 accuracy': nn.Top1CategoricalAccuracy(), + 'top-5 accuracy': nn.Top5CategoricalAccuracy(), + } + else: + raise ValueError(f'Unknown dataset: {dataset_name}') + + for batch in dataset.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__': + config = get_config() + + results = run_eval(config, config.file_name, config.ds_type, config.data_path, + config.num_parallel_workers, config.device_target) + + + for name, value in results.items(): + print(f'{name}: {value:.5f}') diff --git a/official/cv/inceptionv4/scripts/run_onnx_eval.sh b/official/cv/inceptionv4/scripts/run_onnx_eval.sh new file mode 100644 index 0000000000000000000000000000000000000000..66f07bf4b82807b41e30da3aaa7f719182685b39 --- /dev/null +++ b/official/cv/inceptionv4/scripts/run_onnx_eval.sh @@ -0,0 +1,55 @@ +#!/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. +# ============================================================================ + +echo "==============================================================================================================" +echo "Please run the script as: " +echo "bash scripts/run_onnx_eval.sh [DATA_PATH] [DATASET_TYPE] [DEVICE_TYPE] [FILE_TYPE] [ONNX_PATH]" +echo "[DATASET_TYPE] should be in [imagenet, cifar10]" +echo "for example: bash scripts/run_onnx_eval.sh /path/ImageNet2012/validation imagenet GPU ONNX /path/inceptionv4.onnx " +echo "==============================================================================================================" + +if [ $# != 5 ] +then + echo "bash scripts/run_onnx_eval.sh [DATA_PATH] [DATASET_TYPE] [DEVICE_TYPE] [FILE_TYPE] [ONNX_PATH]" +exit 1 +fi + +DATA_PATH=$1 +DATASET_TYPE=$2 +DEVICE_TYPE=$3 +FILE_TYPE=$4 +ONNX_PATH=$5 + +get_real_path(){ + if [ "${1:0:1}" == "/" ]; then + echo "$1" + else + echo "$(realpath -m $PWD/$1)" + fi +} + +BASE_PATH=$(cd ./"`dirname $0`" || exit; pwd) +config_path="${BASE_PATH}/../default_config_gpu.yaml" + +echo "start evaluation" + +python eval_onnx.py \ + --config_path=$config_path \ + --data_path=$DATA_PATH \ + --ds_type=$DATASET_TYPE \ + --device_target=$DEVICE_TYPE \ + --file_format=$FILE_TYPE \ + --file_name=$ONNX_PATH &> output.eval_onnx.log &