【发布时间】:2021-10-12 22:22:30
【问题描述】:
我在 tf2 中使用预训练的 yolov4 keras 模型进行对象检测 所以我加载了模型并称之为
new_model = tf.keras.models.load_model('./checkpoints/yolov4-416')
pred_bbox = new_model(image_data)
for key, value in pred_bbox.items():
然后发生错误:
AttributeError: 'Tensor' 对象没有属性 'items'
所以我修改了我的代码
new_model = tf.keras.models.load_model('./checkpoints/yolov4-416')
prediction_list = new_model.predict(image_data)
prediction_dict = {name: pred for name, pred in zip(new_model.output_names, prediction_list)}
for key, value in pred_bbox.items():
boxes = value[:, :, 0:4]
pred_conf = value[:, :, 4:]
boxes, scores, classes, valid_detections = tf.image.combined_non_max_suppression(
boxes=tf.reshape(boxes, (tf.shape(boxes)[0], -1, 1, 4)),
scores=tf.reshape( pred_conf,
(tf.shape(pred_conf)[0], -1, tf.shape(pred_conf)[-1])),
max_output_size_per_class=50,
max_total_size=50,
iou_threshold=FLAGS.iou,
score_threshold=FLAGS.score
)
pred_bbox = [boxes.numpy(), scores.numpy(), classes.numpy(), valid_detections.numpy()]
image = utils.draw_bbox(original_image, pred_bbox)
在 box.numpy() 被调用之前它做得很好 又发生了一个错误
AttributeError: 'Tensor' 对象没有属性 'numpy'
我想知道如何解决这个问题,我认为是因为它处于图形模式而不是急切执行?
【问题讨论】:
-
是的。这是因为图形模式。但是您是在 tf2 中使用 tf1 还是禁用了 Eager 模式,还是使用了@tf.function?
-
我不明白你在哪里调用了 box.numpy()
-
正如@BestDogeStackoverflow 所说,您可以调用
boxes.numpy(),它可以在图形模式下运行。所以请添加完整的代码。 -
@Kaveh 我用的是 tf2,默认应该是 Eager 模式
-
您使用的 tensorflow 的确切版本是什么?
标签: python keras tensorflow2.0