【问题标题】:AttributeError: 'Tensor' object has no attribute 'numpy' while loading keras modelAttributeError:“张量”对象在加载 keras 模型时没有属性“numpy”
【发布时间】: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


【解决方案1】:

好的,我只是解决了问题 当我保存模型时,它以某种方式保存在图形模式下,这就是为什么我不能调用 box.numpy() 的原因。 所以我所做的只是加载权重并重建模型,问题就消失了。 但我还是想知道是什么原因造成的

保存模型代码:

STRIDES, ANCHORS, NUM_CLASS, XYSCALE = utils.load_config(FLAGS)
input_layer = tf.keras.layers.Input([FLAGS.input_size, FLAGS.input_size, 3])
feature_maps = YOLO(input_layer, NUM_CLASS, FLAGS.model, FLAGS.tiny)
bbox_tensors = []
prob_tensors = []
for i, fm in enumerate(feature_maps):
  if i == 0:
    output_tensors = decode(fm, FLAGS.input_size // 8, NUM_CLASS, STRIDES, ANCHORS, i, XYSCALE, FLAGS.framework)
  elif i == 1:
    output_tensors = decode(fm, FLAGS.input_size // 16, NUM_CLASS, STRIDES, ANCHORS, i, XYSCALE, FLAGS.framework)
  else:
    output_tensors = decode(fm, FLAGS.input_size // 32, NUM_CLASS, STRIDES, ANCHORS, i, XYSCALE, FLAGS.framework)
  bbox_tensors.append(output_tensors[0])
  prob_tensors.append(output_tensors[1])
pred_bbox = tf.concat(bbox_tensors, axis=1)
pred_prob = tf.concat(prob_tensors, axis=1)
boxes, pred_conf = filter_boxes(pred_bbox, 
  pred_prob,`score_thershold=FLAGS.score_thres, 
  input_shape=tf.constant([FLAGS.input_size, FLAGS.input_size]))
  pred = tf.concat([boxes, pred_conf], axis=-1)
model = tf.keras.Model(input_layer, pred)
utils.load_weights(model, FLAGS.weights, FLAGS.model, FLAGS.tiny)
model.summary()
model.save(FLAGS.output)

【讨论】:

    猜你喜欢
    • 2019-02-20
    • 1970-01-01
    • 1970-01-01
    • 2019-05-21
    • 2020-06-06
    • 1970-01-01
    相关资源
    最近更新 更多