paddle2.4.0, paddle2.4.1推理Cascade_RCNN_ResNet50_FPN(基于paddle2.2.2训练)模型报错,paddle2.3.2推理该模型正常

v9tzhpje  于 5个月前  发布在  其他
关注(0)|答案(3)|浏览(94)

bug描述 Describe the Bug

这边有个基于paddle2.2.2训练的Cascade_RCNN_ResNet50_FPN模型,在paddle2.3.2版本上正常推理,在paddle2.4.0和paddle2.4.1上推理失败(python和c++都试过,一样的报错):
失败截图如下:

这里需要特别说明的是,这个报错针对指定图片,附件的压缩包里提供了两张图片,尺寸相同,但是cascade.jpeg正常推理,只有cascade2.jpeg会复现上图的问题
烦请帮忙看看是什么原因

这边把模型和前处理文件以及测试图片放在了下面的tar包
https://aipe-easyedge-public.bj.bcebos.com/paddle_issue/cascade_issue.tar.gz

其他补充信息 Additional Supplementary Information

No response

alen0pnh

alen0pnh1#

您好,我们已经收到了您的问题,会安排技术人员尽快解答您的问题,请耐心等待。请您再次检查是否提供了清晰的问题描述、复现代码、环境&版本、报错信息等。同时,您也可以通过查看 官网API文档常见问题历史IssueAI社区 来寻求解答。祝您生活愉快~

Hi! We've received your issue and please be patient to get responded. We will arrange technicians to answer your questions as soon as possible. Please make sure that you have posted enough message to demo your request. You may also check out the APIFAQGithub Issue and AI community to get the answer.Have a nice day!

0mkxixxg

0mkxixxg2#

你好请问能提供下python脚本运行吗?

rkue9o1l

rkue9o1l3#

# -*- coding: utf-8 -*-

import sys
import os
import cv2
import numpy as np
import paddle
from paddle.inference import Config, DataType
from paddle.inference import create_predictor

if __name__ == "__main__":
    if len(sys.argv) < 4:
        print("Usage: {model_dir} {pic} {threshold}")

    model_dir = sys.argv[1]
    pic = sys.argv[2]
    threshold = sys.argv[3]

    # 载入模型
    config = Config(
        os.path.join(model_dir, "model.pdmodel"),
        os.path.join(model_dir, "model.pdiparams"),
    )
    config.switch_ir_optim(True)
    config.switch_specify_input_names(True)
    config.switch_ir_debug(False)
    config.disable_glog_info()
    config.disable_gpu()

    predictor = create_predictor(config)
    feed_target_names = predictor.get_input_names()

    # 处理输入图片
    img = cv2.imread(pic)
    img = cv2.cvtColor(img, cv2.COLOR_BGR2RGB)
    # 对图片做Resize处理,当输入图片尺寸为(640x426)时, 等价于Keep_ratio resize
    img_scale_x = float(800) / img.shape[1]
    img_scale_y = float(532) / img.shape[0]
    img_info = [532, 800, img_scale_x] # 因为keep_ratio,所以img_scale_x == img_scale_y
    img = cv2.resize(np.asarray(img), None, None, fx=img_scale_x, fy=img_scale_y, interpolation=2)
    img = np.array(img).astype(np.float32)
    # 对图片做归一化
    mean = [123.675, 116.28, 103.53]
    scale = [0.017124753831663668, 0.01750700280112045, 0.017429193899782137]
    img_mean = np.array(mean).astype("float32")[np.newaxis, np.newaxis, :]
    img_scale = np.array(scale).astype("float32")[np.newaxis, np.newaxis, :]
    img -= img_mean
    img = img * img_scale
    # 对图片做补pad对齐到32操作,补齐到(800, 544)
    img_h, img_w = img.shape[:2]
    padded_img = np.zeros((544, 800, 3), dtype=np.float32)
    padded_img[:img_h, :img_w, :] = img

    # 图片喂给模型
    print("Switch to CHW")
    data = np.swapaxes(padded_img, 1, 2)
    data = np.swapaxes(data, 1, 0)
    _, data_h, data_w = data.shape
    for index, name in enumerate(feed_target_names):
        if name == "image":
            tensor = predictor.get_input_handle("image")
            tensor.reshape([1, 3, data_h, data_w])
            tensor.copy_from_cpu(np.array([data.copy()]).astype("float32"))
        elif name == "im_shape":
            tensor = predictor.get_input_handle("im_shape")
            tensor.reshape([1, 2])
            tensor.copy_from_cpu(
                np.array([img_info[:2]]).astype("float32")
            )  # 图像经过resize后的大小
        elif name == "scale_factor":
            # 输入图像大小比真实图像大小
            np_data = np.array([[1.25, 1.25]]).astype("float32")
            tensor = predictor.get_input_handle("scale_factor")
            tensor.reshape([1, 2])
            tensor.copy_from_cpu(np_data)

    predictor.run()

    # 获取推理结果
    output_names = predictor.get_output_names()
    print("output_names:", output_names)
    output_tensors = [predictor.get_output_handle(name_i) for name_i in output_names]

    # 整理输出
    output_datas = [tensor_j.copy_to_cpu() for tensor_j in output_tensors]
    ret = np.array(output_datas[0])
    idx = ret[:, 1].argsort()[::-1]
    ma = ret[idx]
    for item in ma:
        if float(item[1]) < float(threshold):
            continue
        print("index: ", int(item[0]), ", prob: ", float(item[1]), ", [x1, y1, x2, y2]:", [float(item[2]), float(item[3]), float(item[4]), float(item[5])])
    print("done")

可以参考上面这个脚本
python ./paddle_inference.py ./ ./cascade.jpeg 0.3 正常推理
python ./paddle_inference.py ./ ./cascade2.jpeg 0.3 在paddle2.3.2版本正常推理,在paddle2.4.0, 2.4.1版本会报错上面的错

相关问题