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?
boxes.numpy()where it may run in graph mode. So please add complete code. - Kaveh