pythontensorflowkerasneural-networkkeras-cv

can't load the saved model YOLOV8Backbone from keras_cv after thefine-tuning


checkpoint_filepath='E:\model.yolo_v8_s_ft.h5'
# backbone = keras_cv.models.YOLOV8Backbone.from_preset("yolo_v8_m_backbone_coco") 
backbone = keras.models.load_model(checkpoint_filepath) 

yolo = keras_cv.models.YOLOV8Detector(
    num_classes=len(class_mapping),
    bounding_box_format="xyxy",
    backbone=backbone,
    fpn_depth=2,)
optimizer = tf.keras.optimizers.Adam(learning_rate=LEARNING_RATE,global_clipnorm=GLOBAL_CLIPNORM,)
yolo.compile(optimizer=optimizer, classification_loss="binary_crossentropy", box_loss="ciou")


Save_mode= keras.callbacks.ModelCheckpoint(filepath=checkpoint_filepath,monitor='val_loss',mode='auto',save_best_only=True,save_freq="epoch")
reduce_lr = keras.callbacks.ReduceLROnPlateau(monitor='val_loss', factor=0.5,patience=3, min_lr=0.0001)
Stop=keras.callbacks.EarlyStopping(monitor="val_loss",patience=7,mode="auto")

yolo.fit(
    train_ds,
    validation_data=val_ds,
    epochs=EPOCH,
    callbacks=[Save_mode, reduce_lr, Stop],
)

raise err:

ValueError: Unknown layer: 'YOLOV8Detector'.

try use tensorflow_hub (backbone = keras.models.load_model(checkpoint_filepath,custom_objects={'YOLOV8Detector':hub.YOLOV8Detector})) - raise err:

AttributeError: module 'tensorflow_hub' has no attribute 'YOLOV8Detector'

try save model as '.tf' - raise err:

ValueError: The filepath provided must end in .keras (Keras model format). Received: filepath=E:\model.yolo_v8_s_ft.tf

from keras.callbacks.ModelCheckpoint


Solution

  • This error happens as Keras doesn't recognise the YOLOV8Detector class. The issue is that you're saving the entire detector model but load it as a backbone.

    Try this load with custo object

    # Save the entire detector
    checkpoint_filepath = 'E:/model.yolo_v8_s_ft.keras'
    yolo.save(checkpoint_filepath)
    
    # Later, load with custom objects
    from keras_cv.models.object_detection.yolo_v8 import YOLOV8Detector
    
    loaded_model = keras.models.load_model(
        checkpoint_filepath,
        custom_objects={'YOLOV8Detector': YOLOV8Detector}
    )