Hi,
I've just upgraded Keras/Theano to the last cutting edge versions.
Unfortunately, I cannot load my models anymore, I get this error:
Traceback (most recent call last):
File "Convolutional4.py", line 488, in
model = LoadModel("./Results/Model_4x1_Architecture.json", "./Results/Model_4x1_Weights.h5")
File "Convolutional4.py", line 209, in LoadModel
model = model_from_json(open(ModelArchitecturePath).read())
File "/eppec/storage/sw/conda/envs/tensorflow/lib/python2.7/site-packages/keras/models.py", line 30, in model_from_json
return layer_from_config(config, custom_objects=custom_objects)
File "/eppec/storage/sw/conda/envs/tensorflow/lib/python2.7/site-packages/keras/utils/layer_utils.py", line 24, in layer_from_config
class_name = config['class_name']
KeyError: 'class_name'
And here is the little:
def LoadModel(ModelArchitecturePath, ModelWeightsPath):
model = model_from_json(open(ModelArchitecturePath).read())
model.load_weights(ModelWeightsPath)
return model
This code was perfectly working before, but since the last upgrade, it does not anymore.
My last upgrade was 5 weeks ago, so not such a long time.
The last update was a major version update (1.0) so it probably broke a few things.
One thing you could try is to recreate your model in the code from the ground up try loading your weights into that. As long as the basic architecture is the same it should work fine.
Thanks for your answer.
What do you mean by "recreate your model in the code from the ground up try loading your weights into that"?
Oh sorry I think I a few words there. Just write out the steps to build the same model you had before, like:
model = Sequential()
model.add(...)
model.load_weights(my_weights_file)
Same error here -
KeyError Traceback (most recent call last)
<ipython-input-8-48bfb4120d6a> in <module>()
34 modelPath = './SegmentationModels/'
35 modelName = 'Arch_1_40'
---> 36 model = model_from_json(open(modelPath + modelName + '.json').read())
37 model.compile(loss='categorical_crossentropy', optimizer=optim_sgd)
38 model.load_weights(str(modelPath + 'weights.70-0.74.hdf5'))
/usr/local/lib/python2.7/dist-packages/keras/models.pyc in model_from_json(json_string, custom_objects)
28 from keras.utils.layer_utils import layer_from_config
29 config = json.loads(json_string)
---> 30 return layer_from_config(config, custom_objects=custom_objects)
31
32
/usr/local/lib/python2.7/dist-packages/keras/utils/layer_utils.pyc in layer_from_config(config, custom_objects)
22 globals()[cls_key] = custom_objects[cls_key]
23
---> 24 class_name = config['class_name']
25
26 if class_name == 'Sequential':
KeyError: 'class_name'
I have to recreate the model using the original code. Loading weights doesn't work either in the sense that when I try to retrain the model, it starts as if its doing it the first time. It doesn't continue the training process from where I left it the other day. I have opened my issue here - [https://github.com/fchollet/keras/issues/2378]
Thanks, I am going to test it, answer tomorrow.
But it seems that trane has an error doing it :-(
The json format of saved models has changed significantly (why?!). All my old models are broken too. The only way to fix this is to manually correct your old json. Save new model as json and see the difference in the format.
also the saved model does not include the optimizer and loss function anymore.
From to_yaml of pre 1.0:
loss: categorical_crossentropy
optimizer: {beta_1: 0.8999999761581421, beta_2: 0.9990000128746033, epsilon: 1.0e-08,
lr: 0.0010000000474974513, name: Adam}
now the FAQs state that after model_from_yaml() you should call model.compile() manually so that users would need to save their loss and optimizer parameters elsewhere.
This makes the model save and load operation less self contained. I think most users would prefer if this could be reverted to the original pre 1.0 way.
I also have the problem that I cannot load my old json files, after updating keras. Is there already a fix for this? I really need to use my old files.
MartijnL,
Keras 1.0 sort of breaks the functionality of loading older JSON models,
since it changed the way models are exported as JSON. A simple fix is to
use the original code for generating your model, compile it, and load the
weights from your saved h5 file using load_weights().
As a side note, this issue is about not able yo train a loaded model,
instead of model load failure.
On Monday 2 May 2016, MartijnL [email protected] wrote:
I also have the problem that I cannot load my old json files, after
updating keras. Is there already a fix for this? I really need to use my
old files.—
You are receiving this because you commented.
Reply to this email directly or view it on GitHub
https://github.com/fchollet/keras/issues/2386#issuecomment-216247601
Thanks for your quick reply. I will try your suggestion.
I was under the impression that my problem was actually the same as the problem in the first post, since I also used the same code to load my models and subsequently get the same error.
Anyway, I will see if it works by only loading the weights.
EDIT: Loading the weights this way indeed works, tyvm!
I also dit it to reconstruct my models:
Most helpful comment
also the saved model does not include the optimizer and loss function anymore.
From to_yaml of pre 1.0:
now the FAQs state that after
model_from_yaml()you should callmodel.compile()manually so that users would need to save their loss and optimizer parameters elsewhere.This makes the model save and load operation less self contained. I think most users would prefer if this could be reverted to the original pre 1.0 way.