You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

自定义EfficientDet-Lite0模型树莓派运行TensorFlow Lite类型错误排查

解决EfficientDet-Lite0推理时num_boxes转整数的TypeError问题

问题原因

EfficientDet-Lite0的输出张量结构和原代码适配的MobileNetV2不同:num_boxes不是单个标量值,而是带批量维度的数组/张量(比如形状为(1,)的numpy数组或TensorFlow张量),直接用int(num_boxes)转换就会触发"only size-1 arrays can be converted to Python scalars"错误。

解决方案

针对InferenceTensorFlow函数里的num_boxes处理部分,做以下修改:

  1. 提取标量值
    如果num_boxes是TensorFlow张量,先转成numpy数组再取第一个元素;如果已经是numpy数组,直接取索引[0]:

    # 替换原来的num_boxes = int(num_boxes)
    import numpy as np
    # 处理TensorFlow张量或numpy数组的情况
    if hasattr(num_boxes, 'numpy'):
        num_boxes = num_boxes.numpy()
    num_boxes = int(num_boxes[0])
    
  2. 兼容批量推理的鲁棒性处理
    为避免后续因输出结构变化再出问题,增加判断确保只处理有效维度:

    num_boxes = num_boxes.squeeze()  # 压缩所有大小为1的维度
    # 如果压缩后还是数组(比如批量大小>1),取第一个元素(适配单帧推理场景)
    if isinstance(num_boxes, (np.ndarray, list)):
        num_boxes = int(num_boxes[0])
    else:
        num_boxes = int(num_boxes)
    
  3. 同步调整其他输出张量
    EfficientDet-Lite0的boxes、scores、classes输出同样带批量维度,需要同步提取单帧数据:

    # 原代码可能直接用boxes,现在要取第0个批量的结果
    boxes = boxes[0]
    scores = scores[0]
    classes = classes[0]
    

验证方法

修改后先打印输出张量的形状:

print("num_boxes shape:", num_boxes.shape)
print("boxes shape:", boxes.shape)

确认num_boxes压缩后是单个数值,boxes等输出形状为(N, 4)(N为检测框数量)即可。

内容的提问来源于stack exchange,提问作者FireFalcon3469

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.07.31 03:06:29