【问题标题】:"IndexError: index 10 is out of bounds for axis 0 with size 10" when running inference on Coral Dev Board在 Coral Dev Board 上运行推理时,“IndexError: index 10 is out of bounds for axis 0 with size 10”
【发布时间】:2023-03-12 03:11:01
【问题描述】:

我正在尝试在 Coral 开发板上运行量化和 Edge-TPU 编译的 Tensorflow 对象检测模型。

我的代码:

import time
import os

from PIL import Image
from PIL import ImageDraw

from pycoral.adapters import common
from pycoral.adapters import detect
from pycoral.utils.dataset import read_label_file
from pycoral.utils.edgetpu import make_interpreter

model_path = '/mnt/ssd1/mobilenet_v2_1.0_224_quant_edgetpu.tflite'
label_path = '/mnt/ssd1/meteor-labels.txt'
img_directory = "/mnt/ssd1/test_images/"
img_filenames = os.listdir(img_directory)
count = 5
threshold = 0.2
output_path = "/mnt/ssd1/detection_output/"

labels = read_label_file(label_path) if label_path else {}
interpreter = make_interpreter(model_path)
interpreter.allocate_tensors()


def draw_objects(draw, objs, label_data):
    """Draws the bounding box and label for each object."""
    for obj in objs:
        bbox = obj.bbox
        draw.rectangle([(bbox.xmin, bbox.ymin), (bbox.xmax, bbox.ymax)],
                       outline='red')
        draw.text((bbox.xmin + 10, bbox.ymin + 10),
                  '%s\n%.2f' % (label_data.get(obj.id, obj.id), obj.score),
                  fill='red')


def run_inference(image, index):
    _, scale = common.set_resized_input(
        interpreter, image.size, lambda size: image.resize(size, Image.ANTIALIAS))

    print('----INFERENCE TIME----')
    print('Note: The first inference is slow because it includes',
          'loading the model into Edge TPU memory.')
    for _ in range(5):
        start = time.perf_counter()
        interpreter.invoke()
        inference_time = time.perf_counter() - start
        objs = detect.get_objects(interpreter, threshold, scale)
        print('%.2f ms' % (inference_time * 1000))

    print('-------RESULTS--------')
    if not objs:
        print('No objects detected')

    for obj in objs:
        print(labels.get(obj.id, obj.id))
        print('  id:    ', obj.id)
        print('  score: ', obj.score)
        print('  bbox:  ', obj.bbox)

    if output_path:
        image = image.convert('RGB')
        draw_objects(ImageDraw.Draw(image), objs, labels)
        image.save(os.path.join(output_path, f"{index}.jpg"))
        # image.show()


for i, path in enumerate(img_filenames):
    run_inference(Image.open(os.path.join(img_directory, path)).convert('RGB'), i)

当通过“mdt shell”运行时,抛出如下错误:

----INFERENCE TIME----
Note: The first inference is slow because it includes loading the model into Edge TPU memory.
Traceback (most recent call last):
  File "detect_devboard.py", line 86, in <module>
    run_inference(Image.open(os.path.join(img_directory, path)).convert('RGB'), i)
  File "detect_devboard.py", line 65, in run_inference
    objs = detect.get_objects(interpreter, threshold, scale)
  File "/usr/lib/python3/dist-packages/pycoral/adapters/detect.py", line 237, in get_objects
    return [make(i) for i in range(count) if scores[i] >= score_threshold]
  File "/usr/lib/python3/dist-packages/pycoral/adapters/detect.py", line 237, in <listcomp>
    return [make(i) for i in range(count) if scores[i] >= score_threshold]
IndexError: index 10 is out of bounds for axis 0 with size 10

开发板运行 Mendel Linux 并安装了 Python 3.7.3 和 pycoral 2.0.0。

如何才能成功运行推理?

【问题讨论】:

  • 欢迎回到 Stack Overflow。这看起来像是 Pycoral 开发人员的问题,而不是这里。该消息清楚地暗示range(count) 没有为scores 创建正确的索引;大概count 应该等于len(scores) 但不是。但是,所有这些都是库代码中的变量,而不是您的代码;并且它们与您传入的参数没有明显的联系。您应该首先检查文档,然后询问他们所拥有的任何问题跟踪器等有关此类错误的最终原因。
  • 好的,所以我联系了 Coral 支持,他们帮助我解决了问题。我现在将在下面发布一个答案,以帮助将来遇到此问题的任何人。

标签: python tensorflow tensorflow-lite edge-tpu


【解决方案1】:

这似乎是 PyCoral API 中的一个错误。为了解决这个问题,我用这个更新的行替换了“detect.py”文件中的最后一行(在我的例子中位于“/usr/lib/python3/dist-packages/pycoral/adapters/detect.py”) :

return [make(i) for i in range(len(scores)) if scores[i] &gt;= score_threshold]

【讨论】:

  • 希望他们能在下一个版本中修复这个问题。
猜你喜欢
  • 2017-04-09
  • 2021-12-17
  • 1970-01-01
  • 2019-09-21
  • 2020-03-13
  • 2020-05-22
  • 2017-05-20
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多