diff --git a/contrib/msame/README.md b/contrib/msame/README.md deleted file mode 100644 index 7cf1d702d801e70dc5ef0822d3dcdec88eff5c52..0000000000000000000000000000000000000000 --- a/contrib/msame/README.md +++ /dev/null @@ -1,137 +0,0 @@ -# 模型推理工具 - -## 1 介绍 -基于MindX SDK实现开发模型推理工具,用于测试om模型推理,本例为msame的python版本实现。 -[msame-C++工具链接](https://gitee.com/ascend/tools/tree/master/msame) - -### 1.1 支持的产品 -本项目以昇腾Atlas 500 A2/Atlas 200I DK A2为主要的硬件平台。 - -### 1.2 支持的版本 - -推荐系统为ubuntu 22.04。 - -| 软件名称 | 版本 | -| -------- | ------ | -| python | 3.9.2 | -| MindX SDK | 5.0RC1 | -| CANN | 310使用6.3.RC1
310B使用6.2.RC1 | - - - - -### 1.3 适用场景 - -使用于单输入或多输入模型推理 - - -### 1.4 代码目录结构与说明 - -``` -. -├── img -│ ├── error1.jpg // msame-C++推理结果 -│   │── error2.jpg // 本例(msame-python)推理结果 -│   │── error3.jpg // 输入不一致报错截图 -│   │── process.jpg // 流程图 -├── set_env.sh // 需要设置的环境变量 -├── msame.py // 模型推理工具代码 -├── README.md // ReadMe -``` - - - - -### 1.5 技术实现流程图 - -![image-20220401173124980](./img/process.png) - - - - - -## 2 环境依赖 - -软件依赖说明: - -| 依赖软件 | 版本 | 说明 | -| -------- | ------ | ---------------------- | -| numpy | 1.21.2 | 将数据保存为二进制文件 | - -在编译运行项目前,需要设置环境变量: - -``` -. /usr/local/Ascend/ascend-toolkit/set_env.sh #toolkit默认安装路径,根据实际安装路径修改 -. ${SDK_INSTALL_PATH}/mxVision/set_env.sh -``` - -## 3 依赖安装 - -``` -pip install numpy == 1.21.2 -``` - -## 4 准备工作 - -[单输入模型yolov3样例]: -[多输入模型pointnet样例]: - -注:多输入样例在输入时,多个输入用 , 隔开。 - -## 5 编译与运行 -示例步骤如下: -**步骤1** 设置环境变量 - -**步骤2** 运行 - -``` -python3.9 msame.py --model xxx --input xxx --output xxx --loop xxx --outfmt xxx -``` -在输出路径下成功输出预期的“.txt”或“.bin”则运行成功,否则报错。 -参数说明: -``` ---input -功能说明:模型的输入路径 参数值:bin或npy文件路径与文件名 示例:--input dog.npy ---output -功能说明:模型的输出路径 参数值:bin或txt文件路径 示例:--output . ---model -功能说明:om模型的路径 参数值:模型路径与模型名称 示例:--model yolov3.om ---outfmt -功能说明:模型的输出格式 参数值:TXT 或 BIN 示例:--outfmt TXT ---loop -功能说明:执行推理的次数 默认为1 参数值:正整数 示例:--loop 2 ---device -功能说明:执行代码的设备编号 参数值:自然数 示例:--device 1 -``` -单输入模型: -以yolov3模型npy文件输入作为示例参考: - -``` -python3.9 msame.py --model yolov3_tf_bs1_fp16.om --input dog.npy --output test --outfmt TXT -``` -执行成功后,在test目录下生成yolov3_tf_bs1_fp16_0.txt,yolov3_tf_bs1_fp16_1.txt, yolov3_tf_bs1_fp16_2.txt -输出文件的个数与模型的输出有关。 - -多输入模型: - -以pointcnn模型bin文件输入作为示例参考: - -``` -python3.9 msame.py --model pointcnn_bs64.om --output test --outfmt BIN --input pointcnn.bin,pointcnn.bin -``` - -执行成功后,在test目录下生成pointcnn_bs64_0.bin - -## 6 常见问题 -### 6.1 存储为txt格式时可能会出现第六位开始的误差,可以忽略此问题。 - 执行msame-C++输出结果: -![image-20220401173124980](./img/error1.png) - 执行本例(msame-python)输出结果: -![image-20220401173124980](./img/error2.png) -### 6.2 模型需要的输入与提供的数据不一致 -![image-20220401173124980](./img/error3.png) -问题描述: -模型需要的输入shape与提供的数据shape不一致。 -解决方案: -可在msame.py中自行输出m.input_shape查看模型需要的输入shape,m.input_dtype查看数据的shape。 -自行编写脚本查看bin或npy文件的shape和dtype,与模型的shape和dtype对齐。 diff --git a/contrib/msame/img/error1.png b/contrib/msame/img/error1.png deleted file mode 100644 index 34fa6a184e9147ee7a853f09fd4a077cd842936d..0000000000000000000000000000000000000000 Binary files a/contrib/msame/img/error1.png and /dev/null differ diff --git a/contrib/msame/img/error2.png b/contrib/msame/img/error2.png deleted file mode 100644 index c51c673bc5fbac7f7f434a31bba5d56266fc91f0..0000000000000000000000000000000000000000 Binary files a/contrib/msame/img/error2.png and /dev/null differ diff --git a/contrib/msame/img/error3.png b/contrib/msame/img/error3.png deleted file mode 100644 index 026fb9b25292899859f5ddd1cadc52ad1baa612e..0000000000000000000000000000000000000000 Binary files a/contrib/msame/img/error3.png and /dev/null differ diff --git a/contrib/msame/img/process.png b/contrib/msame/img/process.png deleted file mode 100644 index b37ecab6754729aa1ab8c723e7214cc79ee7aac6..0000000000000000000000000000000000000000 Binary files a/contrib/msame/img/process.png and /dev/null differ diff --git a/contrib/msame/msame.py b/contrib/msame/msame.py deleted file mode 100644 index 320ebdb846d9fb8370035afcc65184d8dbb78226..0000000000000000000000000000000000000000 --- a/contrib/msame/msame.py +++ /dev/null @@ -1,383 +0,0 @@ -# -*- coding: utf-8 -*- -# Copyright 2020 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. -# ============================================================================ -import time -import os -import sys -import stat -import argparse -import numpy as np -import mindx.sdk as sdk - - -def parse_args(): - parser = argparse.ArgumentParser(description='msame-python') - parser.add_argument('--input', required=False, type=str, default='') - parser.add_argument('--model', required=True, type=str, help='model is necessary') - parser.add_argument('--output', required=True, type=str, help='check out your output path') - parser.add_argument('--outfmt', required=True, type=str, help='your output format must in "TXT" or "BIN"') - parser.add_argument('--loop', required=False, type=int, default=1) - parser.add_argument('--device', required=False, type=int, default=0) - - my_args = parser.parse_args() - return my_args - - -type_map = {} -type_map['dtype.uint8'] = np.uint8 -type_map['dtype.float32'] = np.float32 -type_map['dtype.float16'] = np.float16 -type_map['dtype.int8'] = np.int8 -type_map['dtype.int32'] = np.int32 -type_map['dtype.int16'] = np.int16 -type_map['dtype.uint16'] = np.uint16 -type_map['dtype.uint32'] = np.uint32 -type_map['dtype.int64'] = np.int64 -type_map['dtype.uint64'] = np.uint64 -type_map['dtype.double'] = np.double - -args = parse_args() -loop = args.loop -_filepath = args.model -_output = args.output -_datatype = args.outfmt -device_id = args.device - - -def infer(saves): - if device_id < 0 or device_id >= 4: - print("device_id {0} is false ,please check".format(device_id)) - sys.exit(0) - types_output = [] - if not os.path.exists(_output): - os.makedirs(_output) - m = sdk.model(_filepath, device_id) - index = 0 - for i in range(m.output_num): - types_output.append([]) - types_output[index].append(str(m.output_dtype(i))) - index += 1 - types_input = str(m.input_dtype(0)) - multi = 1 - f = [] - if args.input != '': - mm = args.input.split(',') - for mk in mm: - if os.path.isdir(mk): - continue - elif os.path.isfile(mk): - continue - else: - print("Error: file doesn't exit") - sys.exit(0) - if m.input_num == 1: - if os.path.isdir(args.input): - isf = 0 - for fi in os.listdir(args.input): - isf += 1 - try: - t = get_t(args.input+'/'+fi, m, type_map[types_input]) - except KeyError: - print("KeyError") - path = fi.split('.')[0] - one_time = t_save(path, m, t, saves, types_output) - if isf == 0: - print("It's an empty folder, please check your input") - sys.exit(0) - elif args.input == '': - try: - t = get_input_num(m, type_map[types_input]) - except KeyError: - print("KeyError") - path = _filepath - one_time = t_save(path, m, t, saves, types_output) - else: - if not os.path.isfile(args.input): - print("please check your file!") - sys.exit(0) - try: - t = get_t(args.input, m, type_map[types_input]) - except KeyError: - print("KeyError") - if len(args.input.split('/')) == 1: - path = args.input - else: - path = args.input.split('/')[-1].split('.')[0] - one_time = t_save(path, m, t, saves, types_output) - else: - try: - if len(args.input.split(',')) > 1 and os.path.isdir(args.input.split(',')[0]): - index1 = 0 - for fi in args.input.split(','): - indexs = 0 - index1 += 1 - for fii in os.listdir(fi): - indexs += 1 - if indexs == 0: - print("It's an empty dir,please check your input!") - sys.exit(0) - t = get_multi_dir(m, type_map[types_input], fi) - path = fi - one_time = t_save(path, m, t, saves, types_output) - elif os.path.isdir(args.input): - index1 = 0 - for fis in os.listdir(args.input): - index1 += 1 - if index1 == 0: - print("It's an empty dir,please check your input!") - sys.exit(0) - t = get_input_num(m, type_map[types_input]) - path = _filepath - one_time = t_save(path, m, t, saves, types_output) - else: - t = get_input_num(m, type_map[types_input]) - path = _filepath - one_time = t_save(path, m, t, saves, types_output) - except KeyError: - print("KeyError") - return one_time - - - -def get_multi_dir(m, input_type, dirs): - a_names = [] - t = [] - for di in os.listdir(dirs): - a_names.extend(get_files(dirs + '/' + di)) - for ai in a_names: - if ai.split('.')[-1] == 'bin': - t.append(get_bins(ai, input_type)) - else: - t.append(get_npy(ai, input_type)) - return t - - -def get_input_num(m, input_type): - inputsize = [] - index = 0 - for j in range(m.input_num): - inputsize.append(1) - for k in m.input_shape(j): - inputsize[index] *= k - index += 1 - types_input = str(m.input_dtype(0)) - files_name = [] - tis = [] - t = [] - if args.input == '': - for i in inputsize: - try: - tas = [] - tas.append(np.zeros(i, type_map[types_input])) - t.append(tas) - except KeyError: - print("KeyError") - tis.extend(t) - else: - binfile = args.input - all_names = [] - if len(binfile.split(',')) > 1: - multi_bin = binfile.split(',') - for i in multi_bin: - files_name.append(i) - all_names.extend(get_files(i)) - if len(all_names) == 0: - print("It's an empty folder,please check your input") - sys.exit(0) - else: - files_name.append(binfile) - all_names.extend(get_files(binfile)) - - if len(all_names[0]) == 0: - print("It's an empty folder, please check your input") - sys.exit(0) - - for mul in all_names: - if mul.split('.')[1] == "bin": - t.append(get_bins(mul, input_type)) - elif mul.split('.')[1] == "npy": - t.append(get_npy(mul, input_type)) - tis = t - return tis - - -def t_save(path, m, t, saves, types_output): - multi = 1 - for p in m.input_shape(0): - multi = multi * p - if t[0][0].shape[0] != multi : - print("Error : Please check the input shape and input dtype") - sys.exit(0) - if m.input_num == 1: - tim = sdk.Tensor(t[0][0].reshape(m.input_shape(0))) - tim.to_device(device_id) - else: - if len(t) != m.input_num: - print("Error : Please check the input shape and input dtype") - sys.exit(0) - tim = [] - idx = 0 - for bs in t: - bs = sdk.Tensor(bs[0].reshape(m.input_shape(idx))) - bs.to_device(device_id) - tim.append(bs) - idx += 1 - last_time = time.time() - outputs = m.infer(tim) - now_time = time.time() - one_times = now_time-last_time - outputs[0].to_host() - nums, _shape = get_nums(outputs, types_output) - if saves: - print(_output) - save_files(path, outputs, _output, _datatype, nums, _shape, types_output) - return one_times - - -def get_t(name, m, input_type): - ti = [] - if name.split('.')[1] == 'bin': - ti.append(get_bins(name, input_type)) - elif name.split('.')[1] == 'npy': - ti.append(get_npy(name, input_type)) - return ti - - -def get_bins(f, input_type): - files_bin = [] - bins = [] - if os.path.isdir(f): - for s in os.listdir(f): - files_bin.append(np.fromfile(f+"/"+s, dtype=input_type).flatten()) - else: - files_bin.append(np.fromfile(f, dtype=input_type).flatten()) - return files_bin - - -def get_npy(f, input_type): - files_npy = [] - bins = [] - if os.path.isdir(f): - for s in os.listdir(f): - files_npy.append(np.load(f+"/"+s).flatten()) - else: - files_npy.append(np.load(f).flatten()) - return files_npy - - -def get_files(binfile): - all_name = [] - if os.path.isdir(binfile): - for s in os.listdir(binfile): - all_name.append(binfile + '/' + s) - elif os.path.isfile(binfile): - all_name.append(binfile) - return all_name - - -def get_nums(outputs, types_output): - index = 0 - num_list = [] - shape = [] - #get shape - - for ij in outputs: - ij.to_host() - try: - num = np.array(ij).astype(type_map[types_output[index][0]]) - except KeyError: - print("KeyError") - num = num.flatten() - try: - num.dtype = type_map[types_output[index][0]] - except KeyError: - print("KeyError") - shape.append(ij.shape[-1]) - num_list.append(num) - index += 1 - #to list - i_index = 0 - nums = [] - for i in num_list: - output_desc = len(i) - nums.append([]) - for j in range(int(output_desc/shape[i_index])): - for k in range(j*shape[i_index], (j+1)*shape[i_index]): - nums[i_index].append(num_list[i_index][k]) - i_index += 1 - return (nums, shape) - - -def save_files(_filepath, outputs, _output, _datatype, nums, _shape, types_output): - #TXT - my_time = time.strftime("%Y%m%d%H%M%S", time.localtime( time.time() )) - if _datatype == 'TXT' or _datatype == 'txt': - i_index = 0 - for ik in nums: - f = os.open(_output+'/'+_filepath.split('.')[0]+'_'+my_time+'_'+str(i_index)+".txt", - os.O_RDWR | os.O_APPEND | os.O_CREAT, stat.S_IRWXU) - os.chmod(_output+'/'+_filepath.split('.')[0]+'_'+my_time+'_'+str(i_index)+".txt", stat.S_IRWXU) - output_desc = len(ik) - for j in range(int(output_desc/_shape[i_index])): - for k in range(j*_shape[i_index], (j+1)*_shape[i_index]): - os.write(f, str.encode(str(nums[i_index][k])+' ')) - os.write(f, str.encode('\n')) - i_index += 1 - else: - #BIN - i_index = 0 - for i in outputs: - i.to_host() - num = np.array(i) - num = num.flatten() - num.tofile(_output+'/'+_filepath.split('.')[0]+'_'+my_time+'_'+str(i_index)+".bin") - i_index += 1 - - -def get_array(files_bin, input_type): - new_files = [] - #make new bin - a = len(files_bin) - for im in range(a): - if im == 0: - new_files.append(files_bin[0]) - continue - else: - a = files_bin[im] - b = new_files[im-1] - new_files.append(np.concatenate((a, b), axis=0)) - bins = np.array(new_files[-1]).astype(input_type) - return bins - - - -if __name__ == '__main__': - TIMES = 0.0 - TRANS = 1000 - SUM = 0 - SAVES = True - if loop <= 0: - print("please check your loop!") - sys.exit(0) - for mj in range(loop): - nowtimes = time.time() - TIMES = infer(SAVES) - SUM += TIMES - SAVES = False - print("loop {0} : Inference time: {1:f} ms".format(mj, TIMES*TRANS)) - print("infer success!") - print("Inference average time: {0:f} ms".format(SUM/loop*TRANS)) - -