"IndexError: index 10 is out of bounds for axis 0 with size 10" when running inference on Coral Dev Board
"IndexError: index 10 is out of bounds for axis 0 with size 10" when running inference on Coral Dev Board
我正在尝试 运行 在 Coral Dev Board 上使用量化和 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
开发板 运行s Mendel Linux 并安装了 Python 3.7.3 和 pycoral 2.0.0。
我该怎么做才能成功运行推理?
这似乎是 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] >= score_threshold]
我正在尝试 运行 在 Coral Dev Board 上使用量化和 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
开发板 运行s Mendel Linux 并安装了 Python 3.7.3 和 pycoral 2.0.0。
我该怎么做才能成功运行推理?
这似乎是 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] >= score_threshold]