I am encountering an error when trying to load a Keras model with custom objects. The error message is:
AttributeError: 'function' object has no attribute 'get_shape'
This occurs in the following context:
custom_objects={'Self_Attention': Self_Attention, 'conditional_BCE': conditional_BCE})
Here is the relevant code snippet where the error occurs:
from keras.models import load_model
# Load the model with custom objects model =
load_model('path_to_your_model.h5', custom_objects={'Self_Attention': Self_Attention, 'conditional_BCE': conditional_BCE})
Custom Object Code:
One of my custom objects is a custom loss function:
from keras import backend as K
def conditional_BCE(input_mask, flag):
def conditional_BCE2(y_true, y_pred):
loss = flag * K.binary_crossentropy(y_true, y_pred) * input_mask
return K.sum(loss) / K.sum(input_mask)
return conditional_BCE2
Keras and TensorFlow Versions:
Keras: 2.0.8
TensorFlow: 1.2.1
Python: 2.7
What I Expected vs. What Happened:
Expected:
- The model should load successfully with the custom objects, and I should be able to use it for inference or further training.
Actual:
- I receive an
AttributeErrorindicating that a 'function' object has no attribute 'get_shape'.
Steps Taken:
I have checked the custom objects code and believe it is correct.
I suspect this issue might be related to the TensorFlow or Keras version incompatibility.
Can someone help identify whether this issue is due to version incompatibility, an error in the custom objects, or something else? Any guidance on how to resolve this issue or workarounds would be greatly appreciated.