I am with Tensorflow 1.7 and Keras 2.1.6.
My model was trained on Model with a Sequential built-in, I wrote a min case to reproduce the error:
import keras
from keras.models import Sequential, Model, load_model
from keras.layers import Input,Conv2D, BatchNormalization
from keras.losses import binary_crossentropy
from keras.utils.vis_utils import plot_model
seq = Sequential([
Conv2D(64, (3, 3), activation='relu', input_shape=(240,320,1), padding='same',name='conv'),
BatchNormalization()], name='conv_seq')
_input = Input((240,320,1), name='input')
model = Model(inputs=_input, outputs=seq(_input))
model.compile(optimizer="adam",loss={'conv_seq': binary_crossentropy})
plot_model(model,to_file='seq_plot.png',show_shapes=True, show_layer_names=True)
model.save('seq.hd5')
After training, while I tried to load_model:
_model = load_model('seq.hd5')
it throws error:
/usr/local/lib/python2.7/site-packages/keras/models.pyc in load_model(filepath, custom_objects, compile)
299 metrics=metrics,
300 loss_weights=loss_weights,
--> 301 sample_weight_mode=sample_weight_mode)
302
303 # Set optimizer weights.
/usr/local/lib/python2.7/site-packages/keras/engine/training.pyc in compile(self, optimizer, loss, metrics, loss_weights, sample_weight_mode, weighted_metrics, target_tensors, **kwargs)
614 'dictionary: "' + name + '". '
615 'Only expected the following keys: ' +
--> 616 str(self.output_names))
617 loss_functions = []
618 for name in self.output_names:
Unknown entry in loss dictionary: "conv_seq". Only expected the following keys: ['sequential_1']
It seems my so named conv_seq sequential layer is not recognized when reloading, it is named as sequential_1 instead. Is this a bug?
Hello folks, I've checked the Keras source and discovered when performing load_model or model_from_config, the name however is not passed to Sequential class which caused such error. Thus, I added some code to mitigate this.
# keras/models.py, L1404
@classmethod
def from_config(cls, config, custom_objects=None,name=None):
if 'class_name' not in config[0] or config[0]['class_name'] == 'Merge':
return cls.legacy_from_config(config)
model = cls(name=name)
...
and in
# keras/utils/generic_utils.py, Line 143
if 'name' in config:
return cls.from_config(config['config'],
custom_objects=dict(list(_GLOBAL_CUSTOM_OBJECTS.items()) +
list(custom_objects.items())),
name=config['name'])
try naming all the layers, it should work fine
Most helpful comment
Hello folks, I've checked the Keras source and discovered when performing
load_modelormodel_from_config, thenamehowever is not passed toSequentialclass which caused such error. Thus, I added some code to mitigate this.and in