本文介绍Paddle2.0动转静功能,能兼顾动态图调试方便与静态图部署高效的优势。通过实例展示动态图转静态图、导出推理模型及部署过程,并测试不同部署方式的效率。在CPU平台用U2Netp模型测试,动态图耗时2.1827s,而动转静结合PaddleInference与mkldnn仅需0.5750s,还含完整操作步骤。
☞☞☞AI 智能聊天, 问答助手, AI 智能搜索, 免费无限量使用 DeepSeek R1 模型☜☜☜

| 动态图 | 动转静(paddle.jit.load) | 动转静(PaddleInference) | 动转静(PaddleInference+mkldnn) |
|---|---|---|---|
| 2.1827 s | 1.9110 s | 1.8756 s | 0.5750 s |
# 模型预测import osimport timeimport paddle# 从模型代码中导入模型from u2net import U2NETP# 实例化模型model = U2NETP()# 加载预训练模型参数model.set_dict(paddle.load('data/data64051/u2netp.pdparams'))# 将模型设置为评估状态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([None, 3, 320, 320], 'float32', '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, 'inference_models/u2netp')# 打印保存的模型文件名print(os.listdir('inference_models'))['u2netp.pdiparams.info', 'u2netp.pdiparams', 'u2netp.pdmodel']
model = paddle.jit.load('inference_models/u2netp')
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
# 安装PaddleQuickInference!pip install ppqi -i https://pypi.python.org/simple
# 不启用MKLDNN加速import numpy as npfrom ppqi import InferenceModel
model = InferenceModel(
modelpath='inference_models/u2netp',
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='inference_models/u2netp',
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
%matplotlib inlineimport cv2import timeimport numpy as npimport matplotlib.pyplot as pltfrom ppqi import InferenceModelfrom processor import preprocess, postprocess# 输入输出设置img_path = 'test.bmp'output_dir = 'output'# 数据预处理img = preprocess(img_path)# 加载模型model = InferenceModel(
modelpath='inference_models/u2netp',
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
<Figure size 432x288 with 1 Axes>
以上就是Paddle2.0:使用动转静完成模型部署的详细内容,更多请关注php中文网其它相关文章!
每个人都需要一台速度更快、更稳定的 PC。随着时间的推移,垃圾文件、旧注册表数据和不必要的后台进程会占用资源并降低性能。幸运的是,许多工具可以让 Windows 保持平稳运行。
Copyright 2014-2025 https://www.php.cn/ All Rights Reserved | php.cn | 湘ICP备2023035733号