atc转om正常,结果完全不对。
背景:
在使用aipro推理自己训练的一个lprnet做车牌识别时各个流程正常,但是最终推理结果不对。
错误原因:
使用numpy对图片预处理后,传入的图片数据内存不连续。
修改代码
在数据预处理时添加一行代码,使图片在内存中连续。
def preprocess_image(image):
img = cv2.resize(image, (94, 48))
img = img.astype(np.float32)
img -= 127.5
img *= 0.0078125
# img = img[:, :, np.newaxis]
img = img.transpose((2, 0, 1)) # (3, 96, 96)
img = img[np.newaxis,:,:,:]
# --------------- 需要添加的代码 --------------- #
img = np.ascontiguousarray(img, dtype=np.float32)
# -------------------- end -------------------- #
return img
atc转om正常,推理报错。
背景
想尝试一下yolov5官方的onnx模型,与香橙派samples里的模型做对比。
报错信息
[INFO] acl init success
[INFO] open device 0 success
[INFO] load model /home/HwHiAiUser/samples/aipro-sample-code/yolo/data/weight/yolo_official/yolo.om success
[INFO] create model description success
[ERROR] Check i:0 name:images in size:4915200 needsize:2457600 not match
[ERROR] Check InVector failed ret:-1
Traceback (most recent call last):
File "/home/HwHiAiUser/samples/aipro-sample-code/yolo/main.py", line 114, in <module>
infer_video(video_path, model, labels_dict, cfg)
File "/home/HwHiAiUser/samples/aipro-sample-code/yolo/main.py", line 83, in infer_video
image_pred = infer_frame_with_vis(img_frame, model, labels_dict, cfg, bgr2rgb=True)
File "/home/HwHiAiUser/samples/aipro-sample-code/yolo/main.py", line 51, in infer_frame_with_vis
output = model.infer([img])[0]
File "/usr/local/miniconda3/lib/python3.9/site-packages/ais_bench/infer/interface.py", line 162, in infer
return self.run(inputs, out_array=True)
File "/usr/local/miniconda3/lib/python3.9/site-packages/ais_bench/infer/interface.py", line 94, in run
outputs = self.session.run(self.outputs_names, inputs)
RuntimeError: [-1][ACL: general failure]
[INFO] unload model success, model Id is 1
[INFO] end to destroy context
[INFO] end to reset device is 0
[INFO] end to finalize acl
报错分析
[ERROR] Check i:0 name:images in size:4915200 needsize:2457600 not match
从上面的错误分析,应该是数据与预期的不一致。类似于图片尺寸不对时的报错。
debug查看一下输入数据的相关信息。发现真有“4915200”这个数值(np.nbytes)。
百度后发现这个数值与输入数据的数据类型相关,且4915200 / 2457600 = 2。所以需要把数据从float32修改为为float16。
修改代码。
def preprocess_image(image, cfg, bgr2rgb=True):
"""图片预处理"""
img, scale_ratio, pad_size = letterbox(image, new_shape=cfg['input_shape'])
if bgr2rgb:
img = img[:, :, ::-1]
img = img.transpose(2, 0, 1) # HWC2CHW
# --------------- 需要修改的代码 --------------- #
img = np.ascontiguousarray(img, dtype=np.float32)
# ------------------- 修改为 ------------------- #
img = np.ascontiguousarray(img, dtype=np.float16)
# -------------------- end -------------------- #
return img, scale_ratio, pad_size