使用PaddlePaddle训练好模型之后如何使用呢?
收藏
在飞桨社区和AI Studio官网中可以找到很多如何使用飞桨搭建和训练深度神经网络模型的例子和教程,但是训练好模型之后如何使用呢?
答案是使用飞桨原生推理库:Paddle Inference,具体步骤如下:
1. 使用paddle.jit.save(layer, path, input_spec=None, **configs)保存模型;
2. 实现对输入数据的预处理方法;
3. 对输入数据进行推理。
例子如下:
import cv2
import numpy as np
from typing import Tuple, List, Union
import paddle.inference as paddle_infer
class Predictor:
def __init__(self, model_path, params_path):
# 创建config
config = paddle_infer.Config(model_path, params_path)
# 根据config创建predictor
self.predictor = paddle_infer.create_predictor(config)
# 获取输入名称及handle
input_names = self.predictor.get_input_names()
self.input_handle = self.predictor.get_input_handle(input_names[0])
# 获取输出名称及handle
output_names = self.predictor.get_output_names()
self.output_handle = self.predictor.get_output_handle(output_names[0])
def __call__(self, image):
# 将图片填充成方形
image = self.image_square(image)
# 预处理
image = self.image_normalize(image)
self.input_handle.copy_from_cpu(image)
# 运行predictor
self.predictor.run()
# 获取输出
output_data = self.output_handle.copy_to_cpu()
return output_data[0]
@staticmethod
def image_normalize(img: np.ndarray,
size: Union[Tuple[int], List[int]] = (224, 224),
scale: float = 1.0/255.0,
mean: Union[Tuple[float], List[float]] = (0.485, 0.456, 0.406),
std: Union[Tuple[float], List[float]] = (0.229, 0.224, 0.225)) -> np.ndarray:
"""
将图片resize、Normalize、Transpose
:param img: 待处理图片
:param size: 图片调整目标大小
:param scale: 图片像素值大小缩放
:param mean: 图片归一化均值
:param std: 图片归一化方差
:return:
"""
img = cv2.resize(img, size).astype('float32')
img = (img * scale - mean) / std
img = img.transpose((2, 0, 1))
return np.array([img], dtype='float32')
@staticmethod
def image_square(image: np.ndarray,
target_size: Union[int, List[int], Tuple[int], None] = None,
interpolation: int = cv2.INTER_LINEAR,
fill_color: Union[Tuple[int], List[int]] = (0, 0, 0)) -> np.ndarray:
"""
使用fill_color补齐图片短边,是图片变成方形
:param image: 待处理图片
:param target_size: 图片目标大小,传入None表示为(long_size, long_size),long_size:图片长边大小
:param interpolation: 插值方式 可选:cv2.INTER_LINEAR, cv2.INTER_NEAREST, cv2.INTER_AREA, cv2.INTER_CUBIC, cv2.INTER_LANCZOS4
:param fill_color: 当shape_change为False时,填充颜色,仅在shape_change为False时生效,默认为(0,0,0)黑色
:return:
"""
if isinstance(target_size, int):
target_size = [target_size, target_size]
height, weight, _ = image.shape
long_size = max(height, weight)
# 计算两边须填充的像素数量
top, bottom, left, right = 0, 0, 0, 0
if height < long_size:
top = (long_size - height) // 2
bottom = (long_size - height) - top
elif weight < long_size: # 由于long_size = max(height, weight),因此可以用elif
left = (long_size - weight) // 2
right = (long_size - weight) - left
# cv2.BORDER_CONSTANT表示边界颜色由value指定
image = cv2.copyMakeBorder(image, top, bottom, left, right, cv2.BORDER_CONSTANT, value=fill_color)
this_target_size = [long_size, long_size] if target_size is None else target_size
image = cv2.resize(image, (this_target_size[0], this_target_size[1]), interpolation=interpolation)
return image
if __name__ == "__main__":
predictor = Predictor('inference/models/angles_infer/inference.pdmodel',
'inference/models/angles_infer/inference.pdiparams')
image_path = r'C:\Users\admin\Desktop\1.jpg'
image_0_bgr = cv2.imdecode(np.fromfile(image_path, dtype=np.uint8), cv2.IMREAD_COLOR)
image_0 = cv2.cvtColor(image_0_bgr, cv2.COLOR_BGR2RGB)
image_90 = cv2.rotate(image_0, cv2.ROTATE_90_CLOCKWISE)
image_180 = cv2.rotate(image_90, cv2.ROTATE_90_CLOCKWISE)
image_270 = cv2.rotate(image_180, cv2.ROTATE_90_CLOCKWISE)
images = [image_0, image_90, image_180, image_270]
result = []
for i, image in enumerate(images):
cls_result = predictor(image)
cls_result = np.argmax(cls_result)
result.append(cls_result)
print(result)
DeepGeGe
已解决
2#
回复于2021-12
其实PaddlePaddle框架本身就可以用来做推理,只需要使用model.eval()改成评估模式就可以了。但是实际部署时候,一般会使用Paddle Inference,因为其针对不同平台不同的应用场景进行了深度的适配优化,做到高吞吐、低时延。总得来说,就是Paddle Inference针对推理做了很多优化。
0
收藏
请登录后评论
其实PaddlePaddle框架本身就可以用来做推理,只需要使用model.eval()改成评估模式就可以了。但是实际部署时候,一般会使用Paddle Inference,因为其针对不同平台不同的应用场景进行了深度的适配优化,做到高吞吐、低时延。总得来说,就是Paddle Inference针对推理做了很多优化。