在将torch模型转换为MNN模型之后,我们可以有C++,Python API两种方式进行验证转换模型的准确性以及推理时间的比较,本文档主要实现的是借助python API的方式。之前一直使用的是creatsession的方式,没想到各种坑,最后发现借助MNN.expr很容易就实现了MNN的推理实现,分享给大家
import MNN.expr as F
import cv2
import torch
import numpy as np
mnn_model_path = './yolo.mnn'
image_path = './input.jpeg'
def process(image, size):
image_resize = cv2.resize(image, size).astype(float)
image_resize /= 255
input_data = np.array(image_resize)
input_data = input_data.transpose((2, 0, 1))
input_data = np.expand_dims(input_data, 0)
return input_data
def m_yolo_inference(image, m_path):
vars = F.load_as_dict(m_path)
inputVar = vars["input"]
if inputVar.data_format == F.NC4HW4:
inputVar.reorder(F.NCHW)
inputVar.write(image.tolist())
outputVar0 = vars['output']
if outputVar0.data_format == F.NC4HW4:
outputVar0 = F.convert(outputVar0, F.NCHW)
outputVar1 = vars['187']
if outputVar1.data_format == F.NC4HW4:
outputVar1 = F.convert(outputVar1, F.NCHW)
return torch.from_numpy(outputVar0.read()), torch.from_numpy(outputVar1.read())
def main():
image = process(image_path, (320, 320))
out1, out2 = m_yolo_inference(image, mnn_model_path)
print(type(out1))
if __name__ == "__main__":
main()