Keras’s model defines metric or loss,
there is no problem when saving to H5, but when using load_ When importing the model, an error will be reported:
unknown metric function: HammingScore. Please ensure this object is passed to the
custom_ objects
argument.
This is because the custom parameters are not passed in. There are two solutions:
-
-
-
-
- if you only need to predict and no longer train, you can add
compile = false directly
-
-
-
model = keras.models.load_model('model.h5', compile = False)
If you need further training or modification, add your own metrics code and compile it again
model.compile(loss='binary_crossentropy',
optimizer=Ada,
metrics=[HammingScore]) # 这里HammingScore是我自定义的metric
-
-
- when importing, the user-defined metric/loss is passed into
-
Custom as a key value_ objects
-
-
- :
-
model = keras.models.load_model('model.h5', custom_objects={'HammingScore': HammingScore} )
Note that the key value should be consistent
Read More:
- [Solved] Python Keras Error: AttributeError: ‘Sequential‘ object has no attribute ‘predict_classes‘
- How to Solve keras load_model() Error
- [Solved] RuntimeError: Cannot clone object <keras.wrappers.scikit_learn.KerasClassifier object…
- Keras import package error: importerror: cannot import name ‘get_ config‘
- `Model.XXX` is not supported when the `Model` instance was constructed with eager mode enabled
- To solve the problem that the loss of verification set of resnet50 pre-training model remains unchanged
- Python custom convolution kernel weight parameters
- from keras.preprocessing.text import Tokenizer error: AttributeError: module ‘tensorflow.compat.v2‘ has..
- pytorch RuntimeError: Error(s) in loading state_ Dict for dataparall… Import model error solution
- [Solved] Operator Not Allowed In Graph Error & Attribute Error Tensor object has no attribute numpy
- Plt.acorr() Function Error: ValueError: object too deep for desired array
- Python error: ‘int’ object is not callable
- [Solved] AttributeError: ‘_IncompatibleKeys‘ object has no attribute ‘parameters‘
- [Solved] AttributeError: ‘DataParallel‘ object has no attribute ‘save‘
- [Solved] AttributeError: ‘_IncompatibleKeys’ object has no attribute
- Pytorch Loading model error: RuntimeError: Error(s) in loading state_dict for Model: Missing key(s) in state_dict
- [Solved] Pytorch-transformers Error: AttributeError: ‘str‘ object has no attribute ‘shape‘
- Tensorflow import Error: ImportError: libcuda.so.1: cannot open shared object file: No such file or dire
- [Solved] Import cv2 Error: ImportError: libGL.so.1: cannot open shared object file: No such file or directory
- [Solved] TypeError: Object of type ‘bytes’ is not JSON serializable