当前位置: 首页 > 知识库问答 >
问题:

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

边银龙
2023-03-14

我编写了以下代码来优化使用TensorRT的TensorFlow 1目标检测模型,然后在Jetson Nano上运行推断。但是,它运行推断,但返回TypeError:只有整数标量数组可以转换为标量索引,而不会在图像上显示识别的对象。

这是我的密码:

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/luigi/Downloads/SSD_MobileNet_300000/pipeline.config'
checkpoint_path = '/home/luigi/Downloads/SSD_MobileNet_300000/model.ckpt'

DATA_DIR = '/home/luigi/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/luigi/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/luigi/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))

以下是错误的一个片段:

我该怎么解决这个问题?

谢谢

暂时还没有答案

 类似资料:
  • 我是相当新的python/Numpy和不完全意识到它。 我一直试图实现一个算法,并在某一点上卡住了,当试图把数组的点积与其转置。我得到的错误是 TypeError:只能将整数标量数组转换为标量索引。 下面是我的代码,供参考。

  • 我为这个问题制作了两个数组的简单示例:是一个一维数组,索引处有标签,对应于nD数组的相同索引。我获取标签2出现的所有索引,并希望检索中的值。 因此,如果我想要标签2,我得到索引0和3,这应该给我相应数组中索引0和3的值。 但是当我想调用我的函数时,我收到一个TypeError@。 我的职能:

  • 尝试在包含279个文件的数据集上执行Kfold cv时,执行k-means后,文件的形状为。我对它进行了重塑,以使其适合svm。现在形状是。尝试Kfold cv方法会给我带来错误 msgstr"只有整数标量数组可以转换为标量索引"

  • 我看到的代码是: 不同变量的值是: ID: 欠条: 阈值:

  • 我有一个名为self的浮动列表。数据,数据我把这个列表交给了peakutils。索引()如下所示: 索引=peakutils.indexes(self.data[_]['smooth_ISA'],thres=0.1,min_dist=50) 但是我得到了这个错误: 只有整数标量数组可以转换为标量索引 你认为发生了什么事? 谢谢

  • 我知道关于这个错误已经有几个问题了。但在这种特殊情况下,我不确定是否已经有了解决我问题的方法。我有这部分代码,我想打印数据帧df的“y”列。发生以下错误:TypeError:只有整数标量数组才能转换为标量索引 可以打印整个数据帧。这看起来像: 这是整个错误消息: 我认为这与numpy阵列有关。提前谢谢你!