带有 TypeError 的 TensorRT 对象检测:只能将整数标量数组转换为标量索引

Posted

技术标签:

【中文标题】带有 TypeError 的 TensorRT 对象检测:只能将整数标量数组转换为标量索引【英文标题】:TensorRT Object Detection with TypeError: only integer scalar arrays can be converted to a scalar index 【发布时间】:2022-01-17 01:15:59 【问题描述】:

我编写了以下代码,以使用 TensorRT 优化 TensorFlow 1 对象检测模型,然后在 Jetson Nano 上运行推理。但是,它运行推理但返回 TypeError: only integer scalar arrays can be converted to a scalar 索引而不在图像上显示识别的对象。

这是我的代码:

from PIL import Image
import sys
import os
import urllib
import tensorflow.contrib.tensorrt as trt
import matplotlib
matplotlib.use('Agg')
import matplotlib.pyplot as plt
import matplotlib.patches as patches
import tensorflow as tf
import numpy as np
import time
from tf_trt_models.detection import download_detection_model, build_detection_graph

%matplotlib inline

config_path = '/home/dev/Downloads/SSD_MobileNet_300000/pipeline.config'
checkpoint_path = '/home/dev/Downloads/SSD_MobileNet_300000/model.ckpt'

DATA_DIR = '/home/dev/Downloads'

frozen_graph, input_names, output_names = build_detection_graph(
    config=config_path,
    checkpoint=checkpoint_path,
    score_threshold=0.1,
    batch_size=1
)

print(output_names)

trt_graph = trt.create_inference_graph(
    input_graph_def=frozen_graph,
    outputs=output_names,
    max_batch_size=1,
    max_workspace_size_bytes=1 << 25,
    precision_mode='FP16',
    minimum_segment_size=50
)

with open('/home/dev/Downloads/SSD_MobileNet_300000/frozen_inference_graph.pb', 'wb') as f:
    f.write(trt_graph.SerializeToString())

tf_config = tf.ConfigProto()
tf_config.gpu_options.allow_growth = True

tf_sess = tf.Session(config=tf_config)

tf.import_graph_def(trt_graph, name='')

tf_input = tf_sess.graph.get_tensor_by_name(input_names[0] + ':0')
tf_scores = tf_sess.graph.get_tensor_by_name('detection_scores:0')
tf_boxes = tf_sess.graph.get_tensor_by_name('detection_boxes:0')
tf_classes = tf_sess.graph.get_tensor_by_name('detection_classes:0')
tf_num_detections = tf_sess.graph.get_tensor_by_name('num_detections:0')

image = Image.open('/home/dev/Downloads/test/P2794.png')

plt.imshow(image)

image_resized = np.array(image.resize((320, 320)))
image = np.array(image)

scores, boxes, classes, num_detections = tf_sess.run([tf_scores, tf_boxes, tf_classes, tf_num_detections], feed_dict=
    tf_input: image_resized[None, ...]
)

boxes = boxes[0] # index by 0 to remove batch dimension
scores = scores[0]
classes = classes[0]
num_detections = num_detections[0]

fig = plt.figure()
ax = fig.add_subplot(1, 1, 1)

ax.imshow(image)

# plot boxes exceeding score threshold
for i in range(num_detections):
    # scale box to image coordinates
    box = boxes[i] * np.array([image.shape[0], image.shape[1], image.shape[0], image.shape[1]])

    # display rectangle
    patch = patches.Rectangle((box[1], box[0]), box[3] - box[1], box[2] - box[0], color='g', alpha=0.3)
    ax.add_patch(patch)

    # display class index and score
    plt.text(x=box[1] + 10, y=box[2] - 10, s='%d (%0.2f) ' % (classes[i], scores[i]), color='w')

plt.show()

num_samples = 1

t0 = time.time()
for i in range(num_samples):
    scores, boxes, classes, num_detections = tf_sess.run([tf_scores, tf_boxes, tf_classes, tf_num_detections], feed_dict=
        tf_input: image_resized[None, ...]
    )
t1 = time.time()
print('Average runtime: %f seconds' % (float(t1 - t0) / num_samples))

这是错误的sn-p:

我该如何解决这个问题?

谢谢!

【问题讨论】:

【参考方案1】:

通过将num_detection = num_detections[0]更改为num_detection = int(num_detections[0])解决了这个问题

【讨论】:

以上是关于带有 TypeError 的 TensorRT 对象检测:只能将整数标量数组转换为标量索引的主要内容,如果未能解决你的问题,请参考以下文章

带有 TensorRT 的 C++ Tensorflow API

如何将带有 grid_sample 的模型转换为带有 INT8 量化的 TensorRT?

如何设置本地模型存储库 - 带有 Minio 的 Tensorrt 推理服务器

C++ TensorRT 批量推理给出了奇怪的结果

模型推理T4 上商汤 OpenPPL vs TensorRT7 vs TensorRT8 测评

TensorRT - TensorFlow 反序列化失败,verifyHeader 中出现序列化错误