1
votes

I'm using a pretrained yolov4 keras model for object detection in tf2 So I loaded the model and call it like

new_model = tf.keras.models.load_model('./checkpoints/yolov4-416')
pred_bbox = new_model(image_data)
for key, value in pred_bbox.items():

then an error occurred:

AttributeError: 'Tensor' object has no attribute 'items'

So I modified my code

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)

It was doing great until box.numpy() is called and another error occurred

AttributeError: 'Tensor' object has no attribute 'numpy'

I wonder how to solve this, and I think it's because it is in graph mode instead of eager execution?

1
Yes. It's because of graph mode. But are you using tf1 or disabled eager mode in tf2 or using @tf.function? - Kaveh
i don't understand where did you called boxes.numpy() - BestDogeStackoverflow
As @BestDogeStackoverflow said, you may called boxes.numpy() where it may run in graph mode. So please add complete code. - Kaveh
@Kaveh I used tf2 and it should be eager mode in default - jimmy12487
What is the exact version of tensorflow you are using ? - Kishore

1 Answers

0
votes

OK I just kind of solve the problem When I'm saving model, it's somehow saved in graph mode, which is why I can't call boxes.numpy(). So what I do is just load weights and rebuild the model and the problem is just gone. But I still want to know what makes it wrong

save_model code:

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)