paddlelite使用pytorch训练好的模型

之前写过一篇paddlelite使用TF训练好的模型

paddlelite没法直接转换pytorch的模型,需要先转换成onnx模型,paddlelite是可以转换onnx模型的。

这里 直接使用pytorch转换onnx的例子https://pytorch.org/tutorials/advanced/super_resolution_with_onnxruntime.html

运行成功后会得到一个名为super_resolution.onnx的 文件

然后使用paddlelite进行转换,和TF那个流程一样

x2paddle --framework=onnx --model=super_resolution.onnx --save_dir=pd_model

成功后进行优化:

paddle_lite_opt --model_dir=./pd_model/inference_model --optimize_out=super_resolution --optimize_out_type=naive_buffer

会生成一个名为super_resolution.nb的文件

 接下和之前TF那篇一样,测试一下:

from paddlelite.lite import *
from  PIL import Image
import numpy as np
 
config = MobileConfig()
config.set_model_from_file('super_resolution.nb')
predictor = create_paddle_predictor(config)
image = Image.open('321.jpg')
resized_image = image.resize((224, 224), Image.BILINEAR)
print(np.array(resized_image).shape)
image_data = np.array(resized_image).flatten().tolist()
input_tensor = predictor.get_input(0)
input_tensor.resize([1,3, 224, 224])
input_tensor.set_float_data(image_data)
predictor.run()
output_tensor = predictor.get_output(0)

猜你喜欢

转载自blog.csdn.net/zhou_438/article/details/109077280