Should I have to compile the model after loading the model from JSON and weight from hdf5 in Keras 1.0

4,930 views
Skip to first unread message

cycha...@gmail.com

unread,
Apr 25, 2016, 12:53:16 AM4/25/16
to Keras-users
Hi,

I've been using Keras from version 0.3.1.
I save the model to JSON and the weight to hdf5 after training.
When using version 0.3.1, I can just start to predict after loading the model from JSON and weight from hdf5.
But in version 1.0.1, I need to load the JSON/hdf5 and "compile the model" before prediction.

It means I need to record the optimizer and loss parameter somewhere else than JSON/hdf5 to rebuild the model.
Is there any better way that I can use my pre-trained model to do predict only with saved JSON/hdf5 files?

Thank you very much.

fire...@gmail.com

unread,
May 9, 2016, 5:08:53 AM5/9/16
to Keras-users, cycha...@gmail.com
Hi,
I have exactly the same problem. My issue being that I've not recorded the optimizer used, ans its parameters :-(
How did you solve it?
Regards,

Kyle McDonald

unread,
May 9, 2016, 11:35:06 AM5/9/16
to Keras-users, cycha...@gmail.com, fire...@gmail.com
since four days ago it's no longer necessary to compile in order to predict.

to get the latest code run:

pip install git+git://github.com/fchollet/keras.git --upgrade --no-deps


françois changed this behavior in this commit:


after i described a similar problem in this issue:

fire...@gmail.com

unread,
May 9, 2016, 7:00:21 PM5/9/16
to Keras-users, cycha...@gmail.com
If we don't compile, don't we loose performances?

fire...@gmail.com

unread,
May 9, 2016, 8:30:48 PM5/9/16
to Keras-users, cycha...@gmail.com, fire...@gmail.com
I've upgraded both Theano and Keras (1.0.2), but I still get the same error:

Traceback (most recent call last):

  File "Convolutional5.py", line 577, in <module>

    TestModelOnImage(model, "../../storage/150717_150727_Helios_4165t2_Crop1/", 31, 1, "../../storage/NN_Results_Crop1_1/")

  File "Convolutional5.py", line 446, in TestModelOnImage

    probas = model.predict_proba(imtest, batch_size=SizeX-2*ws2, verbose=0)

  File "/eppec/storage/sw/conda/envs/tensorflow/lib/python2.7/site-packages/keras/models.py", line 535, in predict_proba

    raise Exception('The model needs to be compiled before being used.')

Exception: The model needs to be compiled before being used.


Here is my little function to load a model:

def LoadModel(ModelArchitecturePath, ModelWeightsPath):

model = model_from_json(open(ModelArchitecturePath).read())

model.load_weights(ModelWeightsPath)

return model


Any idea?


Kyle McDonald

unread,
May 9, 2016, 9:30:03 PM5/9/16
to fire...@gmail.com, Keras-users, cycha...@gmail.com
it seems like you had a problem upgrading. the error says "line 535, in predict_proba", but it's only keras 1.0.1 that has that exception on line 535. in the code where this works, like 535 is a comment.

also, upgrading to 1.0.2 won't be enough. you need to use the latest from github as i wrote previously, since there isn't a tag associated with this change yet.

--
You received this message because you are subscribed to a topic in the Google Groups "Keras-users" group.
To unsubscribe from this topic, visit https://groups.google.com/d/topic/keras-users/d4fRjfu23aU/unsubscribe.
To unsubscribe from this group and all its topics, send an email to keras-users...@googlegroups.com.
To view this discussion on the web, visit https://groups.google.com/d/msgid/keras-users/60fe072a-71c7-439e-ac6f-d0effb2e48c2%40googlegroups.com.

For more options, visit https://groups.google.com/d/optout.

fire...@gmail.com

unread,
May 10, 2016, 4:25:42 AM5/10/16
to Keras-users, fire...@gmail.com, cycha...@gmail.com
Thanks, it worked!

Reply all
Reply to author
Forward
0 new messages