From 393204fa9497dd478f1bfec5ac6d4409f3dfb86e Mon Sep 17 00:00:00 2001 From: chenweitao_295 Date: Thu, 27 May 2021 14:35:50 +0800 Subject: [PATCH] mobilenetv2 310infer amend --- model_zoo/official/cv/mobilenetv2/README.md | 3 ++- .../official/cv/mobilenetv2/README_CN.md | 3 ++- .../mobilenetv2/ascend310_infer/src/main.cc | 4 ++-- .../official/cv/mobilenetv2/postprocess.py | 24 ++++++++++++------- .../cv/mobilenetv2/scripts/run_infer_310.sh | 14 ++++++----- 5 files changed, 29 insertions(+), 19 deletions(-) diff --git a/model_zoo/official/cv/mobilenetv2/README.md b/model_zoo/official/cv/mobilenetv2/README.md index 7357a589256..f5b117c3dad 100644 --- a/model_zoo/official/cv/mobilenetv2/README.md +++ b/model_zoo/official/cv/mobilenetv2/README.md @@ -252,9 +252,10 @@ Current batch_size can only be set to 1. ```shell # Ascend310 inference -bash run_infer_310.sh [MINDIR_PATH] [DATA_PATH] [DVPP] [DEVICE_ID] +bash run_infer_310.sh [MINDIR_PATH] [DATA_PATH] [LABEL_PATH] [DVPP] [DEVICE_ID] ``` +- `LABEL_PATH` label.txt path. Write a py script to sort the category under the dataset, map the file names under the categories and category sort values,Such as[file name : sort value], and write the mapping results to the labe.txt file. - `DVPP` is mandatory, and must choose from ["DVPP", "CPU"], it's case-insensitive.The size of the picture that MobilenetV2 performs inference is [224, 224], the DVPP hardware limits the width of divisible by 16, and the height is divisible by 2. The network conforms to the standard, and the network can pre-process the image through DVPP. - `DEVICE_ID` is optional, default value is 0. diff --git a/model_zoo/official/cv/mobilenetv2/README_CN.md b/model_zoo/official/cv/mobilenetv2/README_CN.md index d6d7ad2d5b0..a0eba476f8f 100644 --- a/model_zoo/official/cv/mobilenetv2/README_CN.md +++ b/model_zoo/official/cv/mobilenetv2/README_CN.md @@ -259,9 +259,10 @@ python export.py --platform [PLATFORM] --ckpt_file [CKPT_PATH] --file_format [EX ```shell # Ascend310 inference -bash run_infer_310.sh [MINDIR_PATH] [DATA_PATH] [DVPP] [DEVICE_ID] +bash run_infer_310.sh [MINDIR_PATH] [DATA_PATH] [LABEL_PATH] [DVPP] [DEVICE_ID] ``` +- `LABEL_PATH` label.txt存放的路径,写一个py脚本对数据集下的类别名进行排序,对类别下的文件名和类别排序值做映射,例如[文件名:排序值],将映射结果写到labe.txt文件中。 - `DVPP` 为必填项,需要在["DVPP", "CPU"]选择,大小写均可。Mobilenetv2执行推理的图片尺寸为[224, 224],DVPP硬件限制宽为16整除,高为2整除,网络符合标准,网络可以通过DVPP对图像进行前处理。 - `DEVICE_ID` 可选,默认值为0。 diff --git a/model_zoo/official/cv/mobilenetv2/ascend310_infer/src/main.cc b/model_zoo/official/cv/mobilenetv2/ascend310_infer/src/main.cc index ccb333f2250..1d91a2d9926 100644 --- a/model_zoo/official/cv/mobilenetv2/ascend310_infer/src/main.cc +++ b/model_zoo/official/cv/mobilenetv2/ascend310_infer/src/main.cc @@ -117,11 +117,11 @@ int main(int argc, char **argv) { auto resizeShape = {FLAGS_image_height, FLAGS_image_width}; std::shared_ptr resize(new Resize(resizeShape)); auto crop_size = {224, 224}; - std::shared_ptr center_crop(new CenterCrop(center_crop)); + std::shared_ptr center_crop(new CenterCrop(crop_size)); Execute transform({decode, resize, center_crop, normalize, hwc2chw}); auto img = MSTensor(); auto image = ReadFileToTensor(all_files[i]); - composeDecode(image, &img); + transform(image, &img); std::vector model_inputs = model.GetInputs(); inputs.emplace_back(model_inputs[0].Name(), model_inputs[0].DataType(), model_inputs[0].Shape(), img.Data().get(), img.DataSize()); diff --git a/model_zoo/official/cv/mobilenetv2/postprocess.py b/model_zoo/official/cv/mobilenetv2/postprocess.py index 23fab61bbc0..4f31c3ba510 100644 --- a/model_zoo/official/cv/mobilenetv2/postprocess.py +++ b/model_zoo/official/cv/mobilenetv2/postprocess.py @@ -28,24 +28,30 @@ def calcul_acc(labels, preds): return sum(1 for x, y in zip(labels, preds) if x == y) / len(labels) +def read_label(label_path): + label_dict = {} + with open(label_path, 'r') as f: + lines = f.readlines() + for line in lines: + file_name = line.split(':')[0] + label = line.split(':')[1] + label_dict[file_name] = label + return label_dict + + def get_result(result_path, label_path): files = os.listdir(result_path) preds = [] labels = [] - label_dict = {} - with open(label_path, 'w') as f: - lines = f.readlines() - for line in lines: - label_dict[line.split(',')[0]] = line.split(',')[1] + label_dict = read_label(label_path) for file in files: file_name = file.split('.')[0] - label = int(label_dict[file_name + '.JEPG']) + label = int(label_dict[file_name]) labels.append(label) - resultPath = os.path.join(result_path, file) - output = np.fromfile(resultPath, dtype=np.float32) + output = np.fromfile(os.path.join(result_path, file), dtype=np.float32) preds.append(np.argmax(output, axis=0)) acc = calcul_acc(labels, preds) - print("accuracy: {}".format(acc)) + print("total{}, accuracy: {}".format(len(labels), acc)) if __name__ == '__main__': diff --git a/model_zoo/official/cv/mobilenetv2/scripts/run_infer_310.sh b/model_zoo/official/cv/mobilenetv2/scripts/run_infer_310.sh index 806c2d96550..68a0b7179c8 100644 --- a/model_zoo/official/cv/mobilenetv2/scripts/run_infer_310.sh +++ b/model_zoo/official/cv/mobilenetv2/scripts/run_infer_310.sh @@ -14,8 +14,8 @@ # limitations under the License. # ============================================================================ -if [[ $# -lt 3 || $# -gt 4 ]]; then - echo "Usage: bash run_infer_310.sh [MINDIR_PATH] [DATA_PATH] [DVPP] [DEVICE_ID] +if [[ $# -lt 4 || $# -gt 5 ]]; then + echo "Usage: bash run_infer_310.sh [MINDIR_PATH] [DATA_PATH] [LABEL_PATH] [DVPP] [DEVICE_ID] DVPP is mandatory, and must choose from [DVPP|CPU], it's case-insensitive DEVICE_ID is optional, it can be set by environment variable device_id, otherwise the value is zero" exit 1 @@ -30,15 +30,17 @@ get_real_path(){ } model=$(get_real_path $1) data_path=$(get_real_path $2) -DVPP=${3^^} +label_path=$(get_real_path $3) +DVPP=${4^^} device_id=0 -if [ $# == 4 ]; then - device_id=$4 +if [ $# == 5 ]; then + device_id=$5 fi echo "mindir name: "$model echo "dataset path: "$data_path +echo "label path: "$label_path echo "image process mode: "$DVPP echo "device id: "$device_id @@ -85,7 +87,7 @@ function infer() function cal_acc() { - python3.7 ../postprocess.py --result_path=./result_Files --label_path=../label.txt &> acc.log & + python3.7 ../postprocess.py --result_path=./result_Files --label_path=$label_path &> acc.log & } compile_app