其他分享
首页 > 其他分享> > Paddle2.0:使用动转静完成模型部署

Paddle2.0:使用动转静完成模型部署

作者:互联网

引入

动态图vs静态图

运行速度对比

动态图动转静(paddle.jit.load)动转静(PaddleInference)动转静(PaddleInference+MKLDNN)
2.1827 s1.9110 s1.8756 s0.5750 s

动态图模型

# 模型预测
import os
import time
import paddle

# 从模型代码中导入模型
from u2net import U2NETP

# 实例化模型
model = U2NETP()

# 加载预训练模型参数
model.set_dict(paddle.load([path to the pretrained model]))

# 将模型设置为评估状态
model.eval()

# 准备模型输入
x = paddle.randn([1, 3, 320, 320])

# 模型预测
d0, _, _, _, _, _, _ = model(x)

# 打印输出的形状
print(d0.shape)
[1, 1, 320, 320]
# 计算预测时间
start = time.time()
out = model(x)
end = time.time()
print('predict time: %.04f s' % (end - start))
predict time: 2.3866 s

模型动转静

# 定义输入数据
input_spec = paddle.static.InputSpec(shape=[None, 3, 320, 320], dtype='float32', name='image')

# 动态图转静态图
model = paddle.jit.to_static(model, input_spec=[input_spec])

# 模型预测
d0, _, _, _, _, _, _ = model(x)

# 打印输出的形状
print(d0.shape)
[1, 1, 320, 320]
# 计算预测时间
start = time.time()
out = model(x)
end = time.time()
print('predict time: %.04f s' % (end - start))
predict time: 1.9833 s

静态图模型保存

# 保存推理模型
paddle.jit.save(model, [path to the save model])

# 打印保存的模型文件名
print(os.listdir([save dir]))
['u2netp.pdiparams.info', 'u2netp.pdiparams', 'u2netp.pdmodel']

模型可视化

静态图模型加载

model = paddle.jit.load([path to the save model])
model.eval()

# 模型预测
# 作为演示这里使用随机数作为输入
d0, _, _, _, _, _, _ = model(x)

# 打印输出的形状
print(d0.shape)
[1, 1, 320, 320]
# 计算预测时间
start = time.time()
out = model(x)
end = time.time()
print('predict time: %.04f s' % (end - start))
predict time: 1.9530 s

PaddleInference预测部署

# 安装PaddleQuickInference
!pip install ppqi -i https://pypi.python.org/simple
# 不启用MKLDNN加速
import numpy as np
from ppqi import InferenceModel

model = InferenceModel(
    modelpath=[path to the save model], 
    use_gpu=False, 
    use_mkldnn=False
)
model.eval()

x = np.random.randn(1, 3, 320, 320).astype('float32')

d0, _, _, _, _ ,_, _ = model(x)

# 打印输出的形状
print(d0.shape)
(1, 1, 320, 320)
# 计算预测时间
start = time.time()
out = model(x)
end = time.time()
print('predict time: %.04f s' % (end - start))
predict time: 1.8739 s
# 启用MKLDNN加速
model = InferenceModel(
    modelpath=[path to the save model], 
    use_gpu=False, 
    use_mkldnn=True
)
model.eval()

d0, _, _, _, _ ,_, _ = model(x)

# 打印输出的形状
print(d0.shape)
(1, 1, 320, 320)
# 计算预测时间
start = time.time()
out = model(x)
end = time.time()
print('predict time: %.04f s' % (end - start))
predict time: 0.5673 s

部署实例

import cv2
import time
import numpy as np
import matplotlib.pyplot as plt

from ppqi import InferenceModel
from processor import preprocess, postprocess

# 输入输出设置
img_path = [path to the input image]
output_dir = [output dir]

# 数据预处理
img = preprocess(img_path)

# 加载模型
model = InferenceModel(
    modelpath=[path to the save model], 
    use_gpu=False, 
    use_mkldnn=True
)
model.eval()

# 模型推理
start = time.time()
d0, _, _, _, _ ,_, _ = model(img)
end = time.time()
print('predict time: %.04f s' % (end - start))

# 结果后处理
mask_path, result_path = postprocess(d0, img_path, output_dir)

# 图像显示
img = np.concatenate([
    cv2.imread(img_path),
    cv2.imread(mask_path),
    cv2.imread(result_path)
], 1)
plt.axis('off')
plt.imshow(cv2.cvtColor(img, cv2.COLOR_BGR2RGB))
plt.show()
predict time: 0.7955 s

在这里插入图片描述

总结

标签:动转静,Paddle2.0,模型,320,动态图,time,path,model
来源: https://blog.csdn.net/jm_12138/article/details/112396552