Skip to content

paddledetection使用ppyoloe导出的模型无法正常使用 #2598

@hejun126

Description

@hejun126

问题描述 Please describe your issue
完全无法识别,训练命令:python tools/train.py -c configs/ppyoloe/ppyoloe_plus_crn_s_80e_coco.yml --eval
导出命令:python tools/export_model.py -c configs/ppyoloe/ppyoloe_plus_crn_s_80e_coco.yml -o weights=output/best_model/model.pdparams。

使用config = paddle_infer.Config(model_file, params_file)这种模式没有问题。
代码如下:
time1 = time.time()

模型路径
model_dir = "output_inference/ppyoloe_plus_crn_s_80e_coco"
model_file = os.path.join(model_dir, "model.pdmodel")
params_file = os.path.join(model_dir, "model.pdiparams")

初始化推理配置
config = paddle_infer.Config(model_file, params_file)
config.enable_use_gpu(200, 0)
config.use_gpu()
config.rt

使用 GPU,设置初始显存大小为 100 MB,设备 ID 为 0
如果使用 CPU,可以启用以下代码:
config.disable_gpu()
创建预测器
predictor = paddle_infer.create_predictor(config)

图片预处理
def preprocess(image_path, target_size=(640, 640)):

读取图片并转换为 RGB 格式

image = Image.open(image_path).convert("RGB")

调整图片尺寸为目标尺寸

image = image.resize(target_size, Resampling.BILINEAR)

归一化

image_np = np.array(image).astype("float32") / 255.0
mean = np.array([0, 0, 0], dtype="float32").reshape((1, 1, 3))
std = np.array([1, 1, 1], dtype="float32").reshape((1, 1, 3))
image_np = (image_np - mean) / std

转换为 CHW 格式

image_np = image_np.transpose((2, 0, 1)) # HWC -> CHW
image_np = np.expand_dims(image_np, axis=0) # 增加 batch 维度
return image_np

解析输出结果
def parse_output(output_data, threshold=0.75):
results = []
for row in output_data:
batch_id, score, x_min, y_min, x_max, y_max = row
if score > threshold: # 过滤低置信度的结果

宽度处理

x_min = float(x_min) * 1280 / 640
x_max = float(x_max) * 1280 / 640
width = x_max - x_min
if width > 330:
continue
results.append({
"box": [x_min, float(y_min) * 720 / 640, x_max, float(y_max) * 720 / 640], # 检测框坐标
"score": float(score), # 置信度分数
"label": int(batch_id) # 假设 batch_id 可以作为类别标签
})
return results

folder_path = r'D:\PythonProject\img_save'
newDir = r'D:\PythonProject\paddle_image'

获取所有文件列表
files = os.listdir(folder_path)

筛选图片文件
image_files = [f for f in files if f.lower().endswith(('.jpg', '.png', '.jpeg', '.bmp'))]

label_data = {
"label": 'null',
"x": -1,
"y": -1,
"score":0.0,
}

labels = ('mfzh', 'dgp', 'mxd', 'qmly', 'kbfl', 'slfy', 'lhbl', 'qnngg', 'hyss', 'xljm', 'klbj', 'jsym')

获取输入和输出句柄
input_names = predictor.get_input_names()
input_tensor1 = predictor.get_input_handle(input_names[0])
scale_factor=np.array([[1.0,1.0]],dtype=np.float32)
input_tensor2 = predictor.get_input_handle(input_names[1])
input_tensor2.copy_from_cpu(scale_factor)
output_names = predictor.get_output_names()

for img_file in image_files:
img_path = os.path.join(folder_path, img_file)
input_data = preprocess(img_path)

设置输入数据

input_tensor1.copy_from_cpu(input_data)

print(output_names)

执行推理

predictor.run()

获取输出数据

output_tensor = predictor.get_output_handle(output_names[0])
output_data = output_tensor.copy_to_cpu()

#解析并打印结果
results = parse_output(output_data)
if len(results) == 0:
continue

label_data_list = list()
for result in results:
#print(f"Class: {result['label']}, Score: {result['score']:.4f}, Box: {result['box']}")
label_data_d = label_data.copy()
label_data_d["x"] = int(result['box'][0])
label_data_d["y"] = int(result['box'][1])
label_data_d["label"] = labels[result['label']]
label_data_d["score"] = result["score"]
label_data_list.append(label_data_d)

try:
dst = newDir + "/" + img_file
shutil.copy2(img_path, dst)
except Exception as e:
print(f"复制失败: {img_path}, 错误: {e}")

print(img_file)
print(label_data_list)
time2 = time.time()
print(time2 - time1)。

使用fastdeploy无法正常识别目标,图片为1280*720。
733.184082,594.169983, 1563.229370, 719.260498, 0.010073, 2 输出坐标1563都超过了1280

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions