Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Unable to Load Custom Objectives from an H5 Model File #3977

Closed
StuartFarmer opened this issue Oct 5, 2016 · 16 comments
Closed

Unable to Load Custom Objectives from an H5 Model File #3977

StuartFarmer opened this issue Oct 5, 2016 · 16 comments

Comments

@StuartFarmer
Copy link

Traceback (most recent call last):
File "stoxnet-tester-0.0.2.py", line 75, in
model = load_model(filename)
File "C:\Users\stuart\Anaconda2\lib\site-packages\keras\models.py", line 155, in load_model
sample_weight_mode=sample_weight_mode)
File "C:\Users\stuart\Anaconda2\lib\site-packages\keras\models.py", line 547, in compile
**kwargs)
File "C:\Users\stuart\Anaconda2\lib\site-packages\keras\engine\training.py", line 537, in compile
loss_function = objectives.get(loss)
File "C:\Users\stuart\Anaconda2\lib\site-packages\keras\objectives.py", line 77, in get
return get_from_module(identifier, globals(), 'objective')
File "C:\Users\stuart\Anaconda2\lib\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.

@Neltherion
Copy link

Neltherion commented Oct 14, 2016

@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})

@mw66
Copy link

mw66 commented Oct 30, 2016

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})

@danmackinlay
Copy link

This may change with #5012; if not, we should submit a documentation pull request.

@staywithme23
Copy link

@Neltherion Neltherion Thx. You save my day

@libphy
Copy link

libphy commented Mar 13, 2017

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"?

@jkquijas
Copy link

jkquijas commented Mar 14, 2017

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

@CheRaissi
Copy link

@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})

@libphy
Copy link

libphy commented Mar 24, 2017

Thanks CheRaissi! I tested and it works :)
I'm adding my little findings:

  • The model doesn't save the custom function dice_coef (used in my metric) in the model object.
  • But it can load it without a problem as long as I define it before I load it.

@stale
Copy link

stale bot commented Jun 22, 2017

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.

@Demetrio92
Copy link

load_model(modelPath, custom_objects={'mean_squared_abs_error': mean_squared_abs_error})
this should be added into docs

@sayZeeel
Copy link

sayZeeel commented Mar 5, 2019

@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?

@cememreakbas
Copy link

cememreakbas commented May 27, 2019

@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?

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?

@cememreakbas
Copy link

I just found the solution here: #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.

@eliethesaiyan
Copy link

@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

@sumannelli-Ib
Copy link

@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?

Facing the same issue. Can anyone please help me on this.
Thanks

@Vijju203
Copy link

@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?

Hi I have same issue. Please help

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests