简体   繁体   English

在 Coral Dev Board 上运行推理时,“IndexError: index 10 is out of bounds for axis 0 with size 10”

[英]"IndexError: index 10 is out of bounds for axis 0 with size 10" when running inference on Coral Dev Board

I'm trying to run a quantized and Edge-TPU-compiled Tensorflow object detection model on a Coral Dev Board.我正在尝试在 Coral 开发板上运行量化和 Edge-TPU 编译的 Tensorflow object 检测 model。

My Code:我的代码:

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)

When running it via "mdt shell", it throws the following error:通过“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

The Dev Board runs Mendel Linux and has Python 3.7.3 and pycoral 2.0.0 installed.开发板运行 Mendel Linux 并安装了 Python 3.7.3 和 pycoral 2.0.0。

What can I do to be able to successfully run the inference?我该怎么做才能成功运行推理?

It seems to be a bug in the PyCoral API.这似乎是 PyCoral API 中的一个错误。 To solve the issue, I replaced the last line from the "detect.py"-file (in my case located in "/usr/lib/python3/dist-packages/pycoral/adapters/detect.py") with this updated line:为了解决这个问题,我用这个更新的行替换了“detect.py”文件中的最后一行(在我的例子中位于“/usr/lib/python3/dist-packages/pycoral/adapters/detect.py”) :

return [make(i) for i in range(len(scores)) if scores[i] >= score_threshold]

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM