RDKX3部署这个yolov8

我来帮你分析这个问题。先搜索一下社区是否有类似的 YOLOv8 部署问题。

问题定位

你的错误非常明确:bboxes_scale 数组为空,导致无法进行后处理计算。

ValueError: operands could not be broadcast together with shapes (6,64) (1,0)

核心原因

从日志看:

[RDK_YOLO] [10:13:36.304] [INFO] self.s_bboxes_scale.shape=(1, 0), self.m_bboxes_scale.shape=(1, 0), self.l_bboxes_scale.shape=(1, 0)

bboxes_scale 全为 (1, 0),说明代码在初始化时没有正确计算 scale 值。这通常是因为:

  1. 模型输出头数量不匹配:你的模型输出是 6 个头(3 个 bbox + 3 个 cls),但代码期望的 anchor 配置与实际不符
  2. 类别数配置错误:你设置了 classes_num=4,但模型实际输出可能与预期不同
  3. 使用了错误的 demo 代码YOLOv8_Detect.py 可能不兼容你的模型版本

解决方案

方案 1:使用官方推荐的 YOLOv8 demo(推荐)

直接换用 YOLOv8_Detect_YUV420SP.py,这个版本更稳定:

cd ~/rdk_model_zoo-rdk_x3/demos/detect/YOLOv8/
python3 YOLOv8_Detect_YUV420SP.py \
  --model_path best0806.bin \
  --test_img saving_0000.jpg \
  --classes_num 4 \
  --conf_thres 0.5 \
  --iou_thres 0.45

如果这个也报错,说明模型本身有问题。


方案 2:检查模型输出维度

先确认你的模型输出是否正确:

# 使用 hrt_model_exec 查看模型输出
hrt_model_exec --model best0806.bin --input saving_0000.jpg

正确的 YOLOv8 输出应该是 6 个 tensor

  • bbox_P3: (1, 64, 80, 80)
  • cls_P3: (1, classes_num, 80, 80)
  • bbox_P4: (1, 64, 40, 40)
  • cls_P4: (1, classes_num, 40, 40)
  • bbox_P5: (1, 64, 20, 20)
  • cls_P5: (1, classes_num, 20, 20)

你的日志显示 cls_P3 的 shape 是 (1, 4, 80, 80),说明 classes_num=4 是对的。


方案 3:修改 YOLOv8_Detect.py 的初始化逻辑

问题出在 __init__ 函数中 bboxes_scale 的计算。找到这段代码:

# 大约在 YOLOv8_Detect.py 的 __init__ 方法中
self.s_bboxes_scale = np.zeros((1, 0), dtype=np.float32)
self.m_bboxes_scale = np.zeros((1, 0), dtype=np.float32)
self.l_bboxes_scale = np.zeros((1, 0), dtype=np.float32)

修改为(根据你的输入尺寸 640x640):

# 计算 scale (640 / feature_map_size)
self.s_bboxes_scale = np.ones((1, 1), dtype=np.float32) * (640 / 80)  # 8.0
self.m_bboxes_scale = np.ones((1, 1), dtype=np.float32) * (640 / 40)  # 16.0
self.l_bboxes_scale = np.ones((1, 1), dtype=np.float32) * (640 / 20)  # 32.0

方案 4:直接使用官方 Model Zoo 最新代码

你的代码可能是旧版本。重新拉取最新代码:

cd ~
rm -rf rdk_model_zoo-rdk_x3
git clone -b rdk_x3 https://github.com/D-Robotics/rdk_model_zoo.git rdk_model_zoo-rdk_x3
cd rdk_model_zoo-rdk_x3/demos/detect/YOLOv8/

# 重新下载模型(如果 README 有说明)
bash download_models.sh  # 或者按 README 手动下载

# 运行测试
python3 YOLOv8_Detect.py --model_path <你的模型.bin> --test_img <测试图片> --classes_num 4

快速排查清单

检查项 正确值 你的值
模型输出头数量 6 个 :white_check_mark: 6 个
classes_num 4 :white_check_mark: 4
输入尺寸 640x640 :white_check_mark: 640x640
bboxes_scale 非空 :cross_mark: 全为 (1,0)
NumPy 版本 1.17.3~1.25.0 :warning: 1.26.4 (警告但可用)

建议操作顺序

  1. 先换 YOLOv8_Detect_YUV420SP.py 试试(最可能解决)
  2. 如果不行,重新 clone 最新 Model Zoo 代码
  3. 再不行,手动修复 bboxes_scale 初始化
  4. 最后考虑重新转换模型(检查 ONNX 导出是否正确)

相关资源

建议你先试方案 1,有结果后再反馈。如果还有问题,把你的 YOLOv8_Detect.py__init__postProcess 函数贴出来,我帮你具体改。