Loading saved model fails
I've trained a model and saved it in .h5 format. when I try loading it I received this error
ValueError Traceback (most recent call last)
~\AppData\Local\Temp/ipykernel_588/661726548.py in module
9 # returns a compiled model
10 # identical to the previous one
--- 11 reconstructed_model = keras.models.load_model(./custom_model.h5)
~\Anaconda3\lib\site-packages\keras\utils\traceback_utils.py in error_handler(*args, **kwargs)
65 except Exception as e: # pylint: disable=broad-except
66 filtered_tb = _process_traceback_frames(e.__traceback__)
--- 67 raise e.with_traceback(filtered_tb) from None
68 finally:
69 del filtered_tb
~\Anaconda3\lib\site-packages\keras\utils\generic_utils.py in class_and_config_for_serialized_keras_object(config, module_objects, custom_objects, printable_module_name)
560 cls = get_registered_object(class_name, custom_objects, module_objects)
561 if cls is None:
-- 562 raise ValueError(
563 f'Unknown {printable_module_name}: {class_name}. Please ensure this '
564 'object is passed to the `custom_objects` argument. See '
ValueError: Unknown loss function: custom_loss_. Please ensure this object is passed to the `custom_objects` argument. See https://www.tensorflow.org/guide/keras/save_and_serialize#registering_the_custom_object for details.
saving line of code is customModel.mod_save(./custom_model.h5)
loading line of code is reconstructed_model = keras.models.load_model(custom_model.h5)
I've tried loading with absolute and relative paths and even without .h5
I think the problem is building a class for the model and custom loss function outside the class and i can't use .add_loss function and this happens for metrics too
here is a part of my class implemetation
class custom_Model():
def __init__(self, inputs):
self.inputs = inputs
def __call__(self):
self.build_model()
def build_model(self):
......
......
self.model = Model(inputs = inputs, outputs = x7, name=req_cust_model)
def save_model(self,filepath,overwrite=True,
include_optimizer=True,save_format=None,
signatures=None,options=None,save_traces=True):
return tf.keras.models.save_model(self.model,filepath,overwrite=overwrite,
include_optimizer=include_optimizer,
save_format=save_format,
signatures=signatures,options=options,
save_traces=save_traces)
custom loss function
class custom_loss(Loss):
def __init__(self,alpha=0.25,beta=0.75):
super().__init__()
self.alpha=alpha
self.beta=beta
def call(self,y, y_pred):
l1=tf.keras.losses.MeanSquaredError()
l2=tf.keras.losses.MeanSquaredLogarithmicError()
return self.alpha * l1 + self.beta * l2
any idea how i can link a custom loss to a custom model implemented as shown above
Topic machine-learning-model keras tensorflow loss-function deep-learning
Category Data Science