diff --git a/model_zoo/official/cv/alexnet/README.md b/model_zoo/official/cv/alexnet/README.md index f8830608bc9..39450a50fae 100644 --- a/model_zoo/official/cv/alexnet/README.md +++ b/model_zoo/official/cv/alexnet/README.md @@ -12,6 +12,10 @@ - [Training](#training) - [Evaluation Process](#evaluation-process) - [Evaluation](#evaluation) + - [Inference Process](#inference-process) + - [Export MindIR](#export-mindir) + - [Infer on Ascend310](#infer-on-ascend310) + - [Result](#result) - [Model Description](#model-description) - [Performance](#performance) - [Evaluation Performance](#evaluation-performance) @@ -191,7 +195,7 @@ Before running the command below, please check the checkpoint path used for eval 'Accuracy': 0.88512 ``` -## Inference Process +## [Inference Process](#contents) ### [Export MindIR](#contents) @@ -202,22 +206,23 @@ python export.py --config_path [CONFIG_PATH] --ckpt_file [CKPT_PATH] --file_name The ckpt_file parameter is required, `EXPORT_FORMAT` should be in ["AIR", "MINDIR"] -### Infer on Ascend310 +### [Infer on Ascend310](#contents) Before performing inference, the mindir file must be exported by `export.py` script. We only provide an example of inference using MINDIR model. Current batch_Size for imagenet2012 dataset can only be set to 1. ```shell # Ascend310 inference -bash run_infer_310.sh [MINDIR_PATH] [DATASET_PATH] [NEED_PREPROCESS] [DEVICE_ID] +bash run_infer_310.sh [MINDIR_PATH] [DATASET_NAME] [DATASET_PATH] [NEED_PREPROCESS] [DEVICE_ID] ``` - `MINDIR_PATH` specifies path of used "MINDIR" OR "AIR" model. +- `DATASET_NAME` specifies datasets used to infer. value can be chosen between 'cifar10' and 'imagenet2012', defaulted is 'cifar10' - `DATASET_PATH` specifies path of cifar10 datasets - `NEED_PREPROCESS` means weather need preprocess or not, it's value is 'y' or 'n', if you choose y, the cifar10 dataset will be processed in bin format, the imagenet2012 dataset will generate label json file. - `DEVICE_ID` is optional, default value is 0. -### result +### [Result](#contents) Inference result is saved in current path, you can find result like this in acc.log file. diff --git a/model_zoo/official/cv/alexnet/README_CN.md b/model_zoo/official/cv/alexnet/README_CN.md index c8ea04aedc0..56b81dc6f70 100644 --- a/model_zoo/official/cv/alexnet/README_CN.md +++ b/model_zoo/official/cv/alexnet/README_CN.md @@ -15,6 +15,10 @@ - [训练](#训练) - [评估过程](#评估过程) - [评估](#评估) + - [推理过程](#推理过程) + - [导出MindIR](#导出MindIR) + - [在Ascend310执行推理](#在Ascend310执行推理) + - [结果](#结果) - [模型描述](#模型描述) - [性能](#性能) - [评估性能](#评估性能) @@ -261,7 +265,7 @@ train.py和config.py中主要参数如下: ## 推理过程 -### [导出MindIR](#contents) +### 导出MindIR ```shell python export.py --config_path [CONFIG_PATH] --ckpt_file [CKPT_PATH] --file_name [FILE_NAME] --file_format [FILE_FORMAT] @@ -277,10 +281,11 @@ python export.py --config_path [CONFIG_PATH] --ckpt_file [CKPT_PATH] --file_name ```shell # Ascend310 inference -bash run_infer_310.sh [MINDIR_PATH] [DATASET_PATH] [NEED_PREPROCESS] [DEVICE_ID] +bash run_infer_310.sh [MINDIR_PATH] [DATASET_NAME] [DATASET_PATH] [NEED_PREPROCESS] [DEVICE_ID] ``` - `MINDIR_PATH` mindir文件路径 +- `DATASET_NAME` 使用的推理数据集名称,默认为`cifar10`,可在`cifar10`或者`imagenet2012`中选择 - `DATASET_PATH` 推理数据集路径 - `NEED_PREPROCESS` 表示数据集是否需要预处理,可在`y`或者`n`中选择,如果选择`y`,cifar10数据集将被处理为bin格式。 - `DEVICE_ID` 可选,默认值为0。 diff --git a/model_zoo/official/cv/alexnet/export.py b/model_zoo/official/cv/alexnet/export.py index c8c529e5061..9cf09586bdf 100644 --- a/model_zoo/official/cv/alexnet/export.py +++ b/model_zoo/official/cv/alexnet/export.py @@ -30,10 +30,16 @@ if config.device_target == "Ascend": context.set_context(device_id=config.device_id) if __name__ == '__main__': - net = AlexNet(num_classes=config.num_classes) - - param_dict = load_checkpoint(config.ckpt_file) - load_param_into_net(net, param_dict) - - input_arr = Tensor(np.zeros([config.batch_size, 3, config.image_height, config.image_width]), ms.float32) - export(net, input_arr, file_name=config.file_name, file_format=config.file_format) + if config.dataset_name == 'imagenet': + net = AlexNet(num_classes=config.num_classes) + param_dict = load_checkpoint(config.ckpt_file) + load_param_into_net(net, param_dict) + input_arr = Tensor(np.zeros([1, 3, config.image_height, config.image_width]), ms.float32) + export(net, input_arr, file_name=config.file_name, file_format=config.file_format) + else: + net = AlexNet(num_classes=config.num_classes) + param_dict = load_checkpoint(config.ckpt_file) + load_param_into_net(net, param_dict) + input_arr = Tensor(np.zeros([config.batch_size, 3, config.image_height, config.image_width]), ms.float32) + export(net, input_arr, file_name=config.file_name, file_format=config.file_format) + \ No newline at end of file diff --git a/model_zoo/official/cv/alexnet/postprocess.py b/model_zoo/official/cv/alexnet/postprocess.py index 5a962bad76c..a5c9661a1a1 100644 --- a/model_zoo/official/cv/alexnet/postprocess.py +++ b/model_zoo/official/cv/alexnet/postprocess.py @@ -15,26 +15,49 @@ """postprocess for 310 inference""" import os import argparse +import json import numpy as np -from mindspore.nn import Top1CategoricalAccuracy +from mindspore.nn import Top1CategoricalAccuracy, Top5CategoricalAccuracy from src.model_utils.config import config as cfg - -batch_size = 1 parser = argparse.ArgumentParser(description="postprocess") label_path = "./preprocess_Result/cifar10_label_ids.npy" parser.add_argument("--result_dir", type=str, default="./result_Files", help="result files path.") +parser.add_argument('--dataset_name', type=str, choices=["cifar10", "imagenet2012"], default="cifar10") parser.add_argument("--label_dir", type=str, default=label_path, help="image file path.") parser.add_argument("--config_path", type=str, default="../default_config.yaml", help="config file path.") -parser.add_argument('--dataset_name', type=str, choices=["cifar10", "imagenet2012"], default="cifar10") args = parser.parse_args() + cfg.config_path = args.config_path +def calcul_acc(lab, preds): + return sum(1 for x, y in zip(lab, preds) if x == y) / len(lab) if __name__ == '__main__': - top1_acc = Top1CategoricalAccuracy() - rst_path = args.result_dir - labels = np.load(args.label_dir, allow_pickle=True) - for idx, label in enumerate(labels): - f_name = os.path.join(rst_path, "alexnet_data_bs" + str(cfg.batch_size) + "_" + str(idx) + "_0.bin") - pred = np.fromfile(f_name, np.float32) - pred = pred.reshape(cfg.batch_size, int(pred.shape[0] / cfg.batch_size)) - top1_acc.update(pred, labels[idx]) - print("acc: ", top1_acc.eval()) + if args.dataset_name == "cifar10": + top1_acc = Top1CategoricalAccuracy() + rst_path = args.result_dir + labels = np.load(args.label_dir, allow_pickle=True) + for idx, label in enumerate(labels): + f_name = os.path.join(rst_path, "alexnet_data_bs" + str(cfg.batch_size) + "_" + str(idx) + "_0.bin") + pred = np.fromfile(f_name, np.float32) + pred = pred.reshape(cfg.batch_size, int(pred.shape[0] / cfg.batch_size)) + top1_acc.update(pred, labels[idx]) + print("acc: ", top1_acc.eval()) + else: + batch_size = 1 + top1_acc = Top1CategoricalAccuracy() + rst_path = args.result_dir + label_list = [] + pred_list = [] + file_list = os.listdir(rst_path) + top5_acc = Top5CategoricalAccuracy() + with open('./preprocess_Result/imagenet_label.json', "r") as label: + labels = json.load(label) + for f in file_list: + label = f.split("_0.bin")[0] + ".JPEG" + label_list.append(labels[label]) + pred = np.fromfile(os.path.join(rst_path, f), np.float32) + pred = pred.reshape(batch_size, int(pred.shape[0] / batch_size)) + top1_acc.update(pred, [labels[label],]) + top5_acc.update(pred, [labels[label],]) + print("Top1 acc: ", top1_acc.eval()) + print("Top5 acc: ", top5_acc.eval()) + \ No newline at end of file diff --git a/model_zoo/official/cv/alexnet/preprocess.py b/model_zoo/official/cv/alexnet/preprocess.py index 6324263272f..b6d845575d5 100644 --- a/model_zoo/official/cv/alexnet/preprocess.py +++ b/model_zoo/official/cv/alexnet/preprocess.py @@ -15,6 +15,7 @@ """preprocess""" import os import argparse +import json import numpy as np from src.model_utils.config import config from src.dataset import create_dataset_cifar10 @@ -22,14 +23,32 @@ parser = argparse.ArgumentParser('preprocess') parser.add_argument('--dataset_name', type=str, choices=["cifar10", "imagenet2012"], default="cifar10") parser.add_argument('--data_path', type=str, default='', help='eval data dir') parser.add_argument("--config_path", type=str, default="../default_config.yaml", help="config file path.") -result_path = './preprocess_Result/' #parser.add_argument('--result_path', type=str, default='./preprocess_Result/', help='result path') +def create_label(result_path, dir_path): + print("[WARNING] Create imagenet label. Currently only use for Imagenet2012!") + dirs = os.listdir(dir_path) + file_list = [] + for file in dirs: + file_list.append(file) + file_list = sorted(file_list) + total = 0 + img_label = {} + for i, file_dir in enumerate(file_list): + files = os.listdir(os.path.join(dir_path, file_dir)) + for f in files: + img_label[f] = i + total += len(files) + json_file = os.path.join(result_path, "imagenet_label.json") + with open(json_file, "w+") as label: + json.dump(img_label, label) + print("[INFO] Completed! Total {} data.".format(total)) + args = parser.parse_args() config.config_path = args.config_path if __name__ == "__main__": if args.dataset_name == "cifar10": dataset = create_dataset_cifar10(config, args.data_path, batch_size=config.batch_size, status="eval") - img_path = os.path.join(result_path, "00_data") + img_path = os.path.join('./preprocess_Result/', "00_data") os.makedirs(img_path) label_list = [] for idx, data in enumerate(dataset.create_dict_iterator(output_numpy=True)): @@ -37,6 +56,8 @@ if __name__ == "__main__": file_path = os.path.join(img_path, file_name) data["image"].tofile(file_path) label_list.append(data["label"]) - np.save(os.path.join(result_path, "cifar10_label_ids.npy"), label_list) + np.save(os.path.join('./preprocess_Result/', "cifar10_label_ids.npy"), label_list) print("=" * 20, "export bin files finished", "=" * 20) + else: + create_label('./preprocess_Result/', args.data_path) \ No newline at end of file diff --git a/model_zoo/official/cv/alexnet/scripts/run_infer_310.sh b/model_zoo/official/cv/alexnet/scripts/run_infer_310.sh index 508be768cd6..d32e4eda08b 100644 --- a/model_zoo/official/cv/alexnet/scripts/run_infer_310.sh +++ b/model_zoo/official/cv/alexnet/scripts/run_infer_310.sh @@ -14,8 +14,9 @@ # limitations under the License. # ============================================================================ -if [[ $# -lt 3 || $# -gt 4 ]]; then - echo "Usage: bash run_infer_310.sh [MINDIR_PATH] [DATASET_PATH] [NEED_PREPROCESS] [DEVICE_ID] +if [[ $# -lt 4 || $# -gt 5 ]]; then + echo "Usage: bash run_infer_310.sh [MINDIR_PATH] [DATASET_NAME] [DATASET_PATH] [NEED_PREPROCESS] [DEVICE_ID] + DATASET_NAME can choose from ['cifar10', 'imagenet2012']. NEED_PREPROCESS means weather need preprocess or not, it's value is 'y' or 'n'. DEVICE_ID is optional, it can be set by environment variable device_id, otherwise the value is zero" exit 1 @@ -29,26 +30,27 @@ get_real_path(){ fi } model=$(get_real_path $1) -dataset_name='cifar10' +if [ $2 == 'cifar10' ] || [ $2 == 'imagenet2012' ]; then + dataset_name=$2 +else + echo "DATASET_NAME can choose from ['cifar10', 'imagenet2012']" + exit 1 +fi +dataset_path=$(get_real_path $3) -dataset_path=$(get_real_path $2) - -if [ "$3" == "y" ] || [ "$3" == "n" ];then - need_preprocess=$3 +if [ "$4" == "y" ] || [ "$4" == "n" ];then + need_preprocess=$4 else echo "weather need preprocess or not, it's value must be in [y, n]" exit 1 fi device_id=0 -if [ $# == 4 ]; then - device_id=$4 +if [ $# == 5 ]; then + device_id=$5 fi -BASEPATH=$(dirname "$(pwd)") -config_path=$BASEPATH"/default_config.yaml" -echo "base path :"$BASEPATH -echo "config path :"$config_path + echo "mindir name: "$model echo "dataset name: "$dataset_name echo "dataset path: "$dataset_path @@ -57,22 +59,17 @@ echo "device id: "$device_id export ASCEND_HOME=/usr/local/Ascend/ if [ -d ${ASCEND_HOME}/ascend-toolkit ]; then - export PATH=$ASCEND_HOME/ascend-toolkit/latest/fwkacllib/ccec_compiler/bin:$ASCEND_HOME/ascend-toolkit/latest/atc/bin:$PATH - export LD_LIBRARY_PATH=/usr/local/lib:$ASCEND_HOME/ascend-toolkit/latest/atc/lib64:$ASCEND_HOME/ascend-toolkit/latest/fwkacllib/lib64:$ASCEND_HOME/driver/lib64:$ASCEND_HOME/add-ons:$LD_LIBRARY_PATH + export PATH=$ASCEND_HOME/fwkacllib/bin:$ASCEND_HOME/fwkacllib/ccec_compiler/bin:$ASCEND_HOME/ascend-toolkit/latest/fwkacllib/ccec_compiler/bin:$ASCEND_HOME/ascend-toolkit/latest/atc/bin:$PATH + export LD_LIBRARY_PATH=$ASCEND_HOME/fwkacllib/lib64:/usr/local/lib:$ASCEND_HOME/ascend-toolkit/latest/atc/lib64:$ASCEND_HOME/ascend-toolkit/latest/fwkacllib/lib64:$ASCEND_HOME/driver/lib64:$ASCEND_HOME/add-ons:$LD_LIBRARY_PATH export TBE_IMPL_PATH=$ASCEND_HOME/ascend-toolkit/latest/opp/op_impl/built-in/ai_core/tbe - export PYTHONPATH=${TBE_IMPL_PATH}:$ASCEND_HOME/ascend-toolkit/latest/fwkacllib/python/site-packages:$PYTHONPATH + export PYTHONPATH=$ASCEND_HOME/fwkacllib/python/site-packages:${TBE_IMPL_PATH}:$ASCEND_HOME/ascend-toolkit/latest/fwkacllib/python/site-packages:$PYTHONPATH export ASCEND_OPP_PATH=$ASCEND_HOME/ascend-toolkit/latest/opp else - export PATH=$ASCEND_HOME/atc/ccec_compiler/bin:$ASCEND_HOME/atc/bin:$PATH - export LD_LIBRARY_PATH=/usr/local/lib:$ASCEND_HOME/atc/lib64:$ASCEND_HOME/acllib/lib64:$ASCEND_HOME/driver/lib64:$ASCEND_HOME/add-ons:$LD_LIBRARY_PATH - export PYTHONPATH=$ASCEND_HOME/atc/python/site-packages:$PYTHONPATH + export PATH=$ASCEND_HOME/fwkacllib/bin:$ASCEND_HOME/fwkacllib/ccec_compiler/bin:$ASCEND_HOME/atc/ccec_compiler/bin:$ASCEND_HOME/atc/bin:$PATH + export LD_LIBRARY_PATH=$ASCEND_HOME/fwkacllib/lib64:/usr/local/lib:$ASCEND_HOME/atc/lib64:$ASCEND_HOME/acllib/lib64:$ASCEND_HOME/driver/lib64:$ASCEND_HOME/add-ons:$LD_LIBRARY_PATH + export PYTHONPATH=$ASCEND_HOME/fwkacllib/python/site-packages:$ASCEND_HOME/atc/python/site-packages:$PYTHONPATH export ASCEND_OPP_PATH=$ASCEND_HOME/opp fi -export SLOG_PRINT_to_STDOUT=0 -export GLOG_v=2 -export DUMP_GE_GRAPH=2 - - export ASCEND_HOME=/usr/local/Ascend export PATH=$ASCEND_HOME/fwkacllib/ccec_compiler/bin:$ASCEND_HOME/fwkacllib/bin:$ASCEND_HOME/toolkit/bin:$PATH @@ -86,15 +83,13 @@ export NPU_HOST_LIB=/usr/local/Ascend/acllib/lib64/stub export ASCEND_OPP_PATH=/usr/local/Ascend/opp export ASCEND_AICPU_PATH=/usr/local/Ascend export LD_LIBRARY_PATH=/usr/local/lib64/:$LD_LIBRARY_PATH - - function preprocess_data() { if [ -d preprocess_Result ]; then rm -rf ./preprocess_Result fi mkdir preprocess_Result - python3.7 ../preprocess.py --config_path=$config_path --dataset_name=$dataset_name --data_path=$dataset_path + python3.7 ../preprocess.py --dataset_name=$dataset_name --data_path=$dataset_path #--result_path=./preprocess_Result/ } function compile_app() @@ -115,13 +110,16 @@ function infer() mkdir result_Files mkdir time_Result - ../ascend310_infer/out/main --mindir_path=$model --dataset_name=$dataset_name --input0_path=./preprocess_Result/00_data --device_id=$device_id &> infer.log - + if [ "$dataset_name" == "cifar10" ]; then + ../ascend310_infer/out/main --mindir_path=$model --dataset_name=$dataset_name --input0_path=./preprocess_Result/00_data --device_id=$device_id &> infer.log + else + ../ascend310_infer/out/main --mindir_path=$model --dataset_name=$dataset_name --input0_path=$dataset_path --device_id=$device_id &> infer.log + fi } function cal_acc() { - python3.7 ../postprocess.py --dataset_name=$dataset_name &> acc.log + python3.7 ../postprocess.py --dataset_name=$dataset_name &> acc.log } if [ $need_preprocess == "y" ]; then