Saving and loading kerascv yolo model (TypeError: '_DictWrapper' object is not callable)

129 views Asked by At

I created an object detection model with keras_cv using this tutorial (https://keras.io/examples/vision/yolov8/). I want to save my model and load it.

backbone = keras_cv.models.YOLOV8Backbone.from_preset("yolo_v8_xs_backbone_coco")
yolo = keras_cv.models.YOLOV8Detector(
    num_classes=len(class_mapping),
    bounding_box_format="xyxy",
    backbone=backbone,
    fpn_depth=1,
)
optimizer = tf.keras.optimizers.Adam(
    learning_rate=LEARNING_RATE,
    global_clipnorm=GLOBAL_CLIPNORM,
)

yolo.compile(
    optimizer=optimizer, classification_loss="binary_crossentropy", box_loss="ciou"
)


stopping_patience = 15
stopping_delta = 0.01
early_stopping = tf.keras.callbacks.EarlyStopping(monitor='val_loss',
                                                  patience=stopping_patience,
                                                  min_delta=stopping_delta,
                                                  verbose=1,
                                                  restore_best_weights=True)

history = yolo.fit(
    train_ds,
    validation_data=val_ds,
    epochs=EPOCH,
    callbacks=[EvaluateCOCOMetricsCallback(val_ds, "model_v2"), early_stopping],
)
# saving
yolo.save("myyolov8model.keras")
# loading
model = load_model("myyolov8model_withtraindata_v3.keras",
            custom_objects={'YOLOV8Detector': keras_cv.models.YOLOV8Detector}, compile=False)
model.compile(
        optimizer=optimizer, classification_loss="binary_crossentropy", box_loss="ciou")
visualize_detections(model, dataset=val_ds, bounding_box_format="xyxy", class_mapping=class_mapping)

The error occurs in 'visualize_detections':

 TypeError:   File "C:\Users\marlauwe\AppData\Local\anaconda3\Lib\site-packages\keras_cv\models\object_detection\yolo_v8\yolo_v8_detector.py", line 609, in decode_predictions
    return self.prediction_decoder(box_preds, scores)
           ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
TypeError: in user code:

    File "C:\Users\marlauwe\AppData\Local\anaconda3\Lib\site-packages\keras\src\engine\training.py", line 2416, in predict_function  *
        return step_function(self, iterator)
    File "C:\Users\marlauwe\AppData\Local\anaconda3\Lib\site-packages\keras\src\engine\training.py", line 2401, in step_function  **
        outputs = model.distribute_strategy.run(run_step, args=(data,))
    File "C:\Users\marlauwe\AppData\Local\anaconda3\Lib\site-packages\keras\src\engine\training.py", line 2389, in run_step  **
        outputs = model.predict_step(data)
    File "C:\Users\marlauwe\AppData\Local\anaconda3\Lib\site-packages\keras_cv\models\object_detection\yolo_v8\yolo_v8_detector.py", line 616, in predict_step
        return self.decode_predictions(outputs, args[-1])
    File "C:\Users\marlauwe\AppData\Local\anaconda3\Lib\site-packages\keras_cv\models\object_detection\yolo_v8\yolo_v8_detector.py", line 609, in decode_predictions
        return self.prediction_decoder(box_preds, scores)

    TypeError: '_DictWrapper' object is not callable

However, if I run 'visualize_detections' without saving and loading, everything works fine.

I don't understand what happens when I save and load the model. How can I adjust my code so the script works?

0

There are 0 answers