Traceback (most recent call last):
File "stoxnet-tester-0.0.2.py", line 75, in
model = load_model(filename)
File "C:Users\stuart\Anaconda2lib\site-packages\keras\models.py", line 155, in load_model
sample_weight_mode=sample_weight_mode)
File "C:Users\stuart\Anaconda2lib\site-packages\keras\models.py", line 547, in compile
**kwargs)
File "C:Users\stuart\Anaconda2lib\site-packages\keras\engine\training.py", line 537, in compile
loss_function = objectives.get(loss)
File "C:Users\stuart\Anaconda2lib\site-packages\keras\objectives.py", line 77, in get
return get_from_module(identifier, globals(), 'objective')
File "C:Users\stuart\Anaconda2lib\site-packages\keras\utils\generic_utils.py", line 16, in get_from_module
str(identifier))
Exception: Invalid objective: mean_squared_abs_error
def mean_squared_abs_error: as it exists in my original training script exists exactly as it does in the tester script.
@StuartFarmer did you find a solution to your problem?
all I had to do was to use load_model
like below:
load_model(modelPath, custom_objects={'mean_squared_abs_error': mean_squared_abs_error})
I think this information should be add to the doc:
https://keras.io/models/model/
To Load Custom Objectives from an H5 Model File, e.g.
load_model(modelPath, custom_objects={'mean_squared_abs_error': mean_squared_abs_error})
This may change with #5012; if not, we should submit a documentation pull request.
@Neltherion Neltherion Thx. You save my day
It did not work for me.
I defined a custom function "dice_coef", then I also defined a custom objectives "dice_coef_loss" which is essentially -1* dice_coef. Then I compiled my model for training:
model.compile(optimizer=Adam(lr=1e-6), loss=dice_coef_loss, metrics=[dice_coef])
After training and saving the model, it fails to load when I use above approach:
model = load_model('mymodel.h5', custom_objects={'dice_coef_loss': dice_coef_loss})
It gives me an error:
ValueError: Invalid metric: dice_coef
Is there an option to also specify something like "custom_metric" in addition to "custom_objects"?
I too have the same issue, but with a custom activation function. I tried the custom_objects={'my_activation' : my_activation} approach, but fails. It seems as if this function object is never even read or processed, since the same error message pops up when I do/don't include the custom_objects parameter using load_model.
I also tried
import keras.activations
keras.activations.custom_activation = my_activation
model = load_model('path/to/model.h5', custom_objects={'my_activation' : keras.activations.custom_activation})
but nothing... I get the error
"str(identifier))
ValueError: Invalid activation function: my_activation"
Any help will be highly appreciated! :D
@jkquijas @libphy You need to specify the metrics in the custom_objects dictionary:
model = load_model("mymodel.h5", custom_objects={'dice_coef_loss': dice_coef_loss, 'dice_coef': dice_coef})
Thanks CheRaissi! I tested and it works :)
I'm adding my little findings:
This issue has been automatically marked as stale because it has not had recent activity. It will be closed after 30 days if no further activity occurs, but feel free to re-open a closed issue if needed.
load_model(modelPath, custom_objects={'mean_squared_abs_error': mean_squared_abs_error})
this should be added into docs
@CheRaissi,
My loss function is like this.
def custom_loss_function(inputs):
def custom_loss(y_true, y_pred):
print (inputs.shape, y_true.shape, y_pred.shape)
x = k.exp(inputs[:,12,:])
y_t = k.log(k.exp(y_true)x)
y_p = k.log(k.exp(y_pred)x)
# y_p[y_p==0] = 1e-6
l = y_t - y_p
return k.square(l)
return custom_loss
I am getting the following error
model=load_model('lstm_fftm_custom_loss.h5', custom_objects={'custom_loss':custom_loss, 'custom_loss_function':custom_loss_function})
NameError: name 'custom_loss' is not defined
Can you please help?
@CheRaissi,
My loss function is like this.def custom_loss_function(inputs):
def custom_loss(y_true, y_pred):
print (inputs.shape, y_true.shape, y_pred.shape)
x = k.exp(inputs[:,12,:])
y_t = k.log(k.exp(y_true)x)
y_p = k.log(k.exp(y_pred)x)y_p[y_p==0] = 1e-6
l = y_t - y_p
return k.square(l)return custom_loss
I am getting the following error
model=load_model('lstm_fftm_custom_loss.h5', custom_objects={'custom_loss':custom_loss, 'custom_loss_function':custom_loss_function})
NameError: name 'custom_loss' is not definedCan you please help?
I am experiencing exactly the same issue. I use a nested loss function like this because I have three inputs.
To resolve, I made the inner loss function global as follows:
def custom_loss_function(inputs):
global custom_loss
def custom_loss(y_true, y_pred):
...
return <some_value>
return custom_loss
However, I received the following error:
ValueError: Unknown loss function:custom_loss
@CheRaissi any idea how to resolve this issue?
I just found the solution here: https://github.com/keras-team/keras/issues/5916
The model has to be loaded with the following parameters:
model=load_model('model.h5', compile=False)
This solution works perfectly only if you're loading the model for prediction.
@cememreakbas , that is a decorator problem, you can just custom_loss instead of custom_loss_function but you have to make sure that they share arguments, it would work, I am not sure if preventing your model to compile is a healthy thing
@CheRaissi,
My loss function is like this.def custom_loss_function(inputs):
def custom_loss(y_true, y_pred):
print (inputs.shape, y_true.shape, y_pred.shape)
x = k.exp(inputs[:,12,:])
y_t = k.log(k.exp(y_true)x)
y_p = k.log(k.exp(y_pred)x)y_p[y_p==0] = 1e-6
l = y_t - y_p
return k.square(l)return custom_loss
I am getting the following error
model=load_model('lstm_fftm_custom_loss.h5', custom_objects={'custom_loss':custom_loss, 'custom_loss_function':custom_loss_function})
NameError: name 'custom_loss' is not definedCan you please help?
Facing the same issue. Can anyone please help me on this.
Thanks
@CheRaissi,
My loss function is like this.def custom_loss_function(inputs):
def custom_loss(y_true, y_pred):
print (inputs.shape, y_true.shape, y_pred.shape)
x = k.exp(inputs[:,12,:])
y_t = k.log(k.exp(y_true)x)
y_p = k.log(k.exp(y_pred)x)y_p[y_p==0] = 1e-6
l = y_t - y_p
return k.square(l)return custom_loss
I am getting the following error
model=load_model('lstm_fftm_custom_loss.h5', custom_objects={'custom_loss':custom_loss, 'custom_loss_function':custom_loss_function})
NameError: name 'custom_loss' is not definedCan you please help?
Hi I have same issue. Please help
Most helpful comment
@StuartFarmer did you find a solution to your problem?
all I had to do was to use
load_model
like below:load_model(modelPath, custom_objects={'mean_squared_abs_error': mean_squared_abs_error})