首页 PaddleHub 帖子详情
docker cpu版 文字识别预测特别慢
收藏
快速回复
PaddleHub 问答一键预测 825 2
docker cpu版 文字识别预测特别慢
收藏
快速回复
PaddleHub 问答一键预测 825 2

我是下载的官方paddlepaddle docker,然后安装了paddlehub,下载使用chinese_ocr_db_crnn_server模型识别图片,一张1920×1080的图要40多秒,这正常吗?

还有一个问题,用python写加载模型预测的时候,_init_咋写,enable_mkldnn怎么开?

 

新手刚体验,请各位指点

0
收藏
回复
全部评论(2)
时间顺序
JavaRoom
#2 回复于2021-12

可以采用以下办法:

1.使用mobile小模型

2.采用高配置显卡

3.图像大小弄小点

4.关于模型预测可以参考 https://www.paddlepaddle.org.cn/paddle/paddleinference

5.开启enable_mkldnn,开启 MKLDNN 加速选项,一般是使用 CPU 进行预测的时候,如果机器支持 MKLDNN 可以开启。

就一句话

config.enable_mkldnn()

6.init写法因人而异,最好给出具体的情况。

0
回复
DeepGeGe
#3 回复于2021-12

1. 时间正常,因为你用的是chinese_ocr_db_crnn_server,而不是chinese_ocr_db_crnn_mobile,而且图片那么大;

2. Python加载预测,是指训练好模型之后,拿来用吗?给你个例子,我前一阵子刚好做过:

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)
0
回复
需求/bug反馈?一键提issue告诉我们
发现bug?如果您知道修复办法,欢迎提pr直接参与建设飞桨~
在@后输入用户全名并按空格结束,可艾特全站任一用户