[PaddlePaddle/Paddle]使用paddle.inference报错

2024-03-22 786 views
0
请提出你的问题 Please ask your question

InvalidArgumentError: The type of data we are trying to retrieve does not match the type of data currently contained in the container. [Hint: Expected dtype() == paddle::experimental::CppTypeToDataType::Type(), but received dtype():10 != paddle::experimental::CppTypeToDataType::Type():12.] (at /paddle/paddle/phi/core/dense_tensor.cc:137) [operator < elementwise_mul > error]

我的理解是,我的输入处理的不正确, 模型为使用paddledetection训练后export的model.pdmodel,model.pdiparams其中,有个疑问是infer_cfg.yml没有被用到, 其次,处理图片输入的时候,具体需要如何处理呢?我的处理如下: img_file = "Images/1.jpg" content = None with open(img_file, "rb") as f: content = f.read() raw_img = np.asarray(bytearray(BytesIO(content).read()), dtype="uint8") img = cv2.imdecode(raw_img, cv2.IMREAD_COLOR) img = cv2.resize(img, (640, 640))

img = (img / 255.0 - 0.5) / 0.5

img = (img / 255.0) / 1.0 img = img.transpose((2, 0, 1)) img = np.expand_dims(img, axis=0).astype('float32')

回答

9

你好,可以确认一下训练时是不是用到了混合精度策略,但是在inference时没有用到,导致了 elementwise_mul 的 X 和 Y 的数据类型不匹配而导致的报错。

1

您好,训练的时候有使用--amp混合精度策略,那么inference的时候应该怎么调整呢? 有更加全面一点的paddle.inference教程吗,刚入门有很多不懂

3

InvalidArgumentError: The split Op's Input Variable X contains uninitialized Tensor. [Hint: Expected t->IsInitialized() == true, but received t->IsInitialized():0 != true:1.] (at /paddle/paddle/fluid/framework/operator.cc:2094) [operator < split > error]

您好,将预测精度设置为FP16之后,出现上述问题,请问是什么原因造成的呢?是哪部分没有Initialized嘛?

8
paddle_inference

import numpy as np import cv2 import os from ai.config.log import get_logger from ai.env import alg_env from io import BytesIO

引用 paddle inference 预测库

import paddle.inference as paddle_infer

logger = get_logger(name)

class Alg(object): def init(self, alg_path=None):

    logger.info(f"{'*' * 10} {alg_path}")
    # paddle版本信息
    print(paddle_infer.get_version())
    base_dir = alg_env.get_py_base_dir()
    if alg_path is None:
        pass
    alg_path = os.path.join(base_dir, "algs", "FloatingObjectDetection")
    self.numtoen = {0: 'nothing', 1: 'plant',
                   2: 'garbage', 3: 'else'}
    self.numtocn = {0: '无目标', 1: '植物类漂浮物',
                   2: '垃圾类漂浮物', 3: '其它类漂浮物'}
    self.model_file = os.path.join(alg_path, "m/model.pdmodel")
    self.params_file = os.path.join(alg_path, "m/model.pdiparams")
    self.config_file = os.path.join(alg_path, "m/infer_cfg.yml")

# 模型初始化,返回是否成功
def init_model(self, resource):
    # 根据给定路径加载模型
    init_success = False
    # 创建 config
    config = paddle_infer.Config(self.model_file, self.params_file)
    # 使用gpu预测,预测精度设置为FP16
    config.enable_use_gpu(1000, 0)
    config.exp_enable_use_gpu_fp16()
    # 根据 config 创建 predictor
    self.model = paddle_infer.create_predictor(config)
    if self.model:
        init_success = True
        print('模型初始化成功')
    return init_success

# 返回list[报警名,报警中文名,置信度,图片宽,图片高,*bbox]
def predict_img(self, img, task_id, img_w, img_h):
    result_list = []

    # 获取输入的名称
    input_names = self.model.get_input_names()
    # print("input_names:",input_names)
    input_handle = self.model.get_input_handle(input_names[0])
    # 从 CPU 获取数据,设置到 Tensor 内部
    input_handle.copy_from_cpu(img)
    # 执行predictor
    self.model.run()
    # 获取输出
    output_names = self.model.get_output_names()
    output_handle = self.model.get_output_handle(output_names[0])
    output_data = output_handle.copy_to_cpu()  # numpy.ndarray类型
    # 结果整理
    result_list = [output_names, output_handle, output_data]
    # 结果保存
    return result_list

def main(): alg = Alg() if alg.init_model(""):

    img_file = "Images/1.jpg"
    content = None
    with open(img_file, "rb") as f:
        content = f.read()
    raw_img = np.asarray(bytearray(BytesIO(content).read()), dtype="uint8")
    img = cv2.imdecode(raw_img, cv2.IMREAD_COLOR)
    img = cv2.resize(img, (640, 640))
    # img = (img / 255.0 - 0.5) / 0.5
    # img = (img / 255.0) / 1.0
    img = img.transpose((2, 0, 1))
    # 这里的astype要和初始化模型时声明的类型一致!
    img = np.expand_dims(img, axis=0).astype('float16')
    print(img.shape)
    print(img)
    data_result = alg.predict_img(img, 'xx0001', img.shape[1], img.shape[0])
    print(f"result:{data_result}")

if name == "main": main()

这是我的测试程序

7

训练的指令是: image 是否意味着我是用GPU推理的时候一定要使用TRT

8

我现在想先把程序跑起来... 上述报错的问题应该是初始化的时候,infer_cfg.yml这个文件我并没有使用到,但是我看了官方的文档,作为初学者,很难将文件解耦开...看了半天也没找到infer_cfg.yml文件具体怎么进行初始化55555555

4

如果是具体 PaddleDetection 套件使用问题可以在套件的 repo issue 里再提问一下。