Skip to content

Can not save model using model.save following multi_gpu_model #8446

Closed
@SimonWalsh1000

Description

@SimonWalsh1000

Please make sure that the boxes below are checked before you submit your issue. If your issue is an implementation question, please ask your question on StackOverflow or join the Keras Slack channel and ask there instead of filing a GitHub issue.

Thank you!

  • Check that you are up-to-date with the master branch of Keras. You can update with:
    pip install git+git://github.com/fchollet/keras.git --upgrade --no-deps

    If running on TensorFlow, check that you are up-to-date with the latest version. The installation instructions can be found here.

This is a short and simple issue. Following upgrading to Keras 2.0.9 I have been using the multi_gpu_model utility but I can seem to save my models or best weights using model.save.

The error I get is

TypeError: can’t pickle module objects

I suspect there is some problem gaining access to the model object. Is there a work around this issue?

Activity

datumbox

datumbox commented on Nov 10, 2017

@datumbox
Contributor

Are you trying to save directly the model which is parallelized? That can be the problem. Could you provide a snippet that shows the steps you are following?

SimonWalsh1000

SimonWalsh1000 commented on Nov 10, 2017

@SimonWalsh1000
Author

Yes - thats exactly what I am doing - I'm building the model normally, then calling the multi gpu model method function on it and after training, trying to save it with model.save. Does this need to be converted back to a non-parallelized version of the model? If so, how is this achieved?

fchollet

fchollet commented on Nov 10, 2017

@fchollet
Collaborator

For now we recommend saving the original (template) model instead of the parallel model. I.e. call save on the model you passed to multi_gpu_model, not the model returned by it.

Both models share the same weights.

SimonWalsh1000

SimonWalsh1000 commented on Nov 10, 2017

@SimonWalsh1000
Author
SimonWalsh1000

SimonWalsh1000 commented on Nov 10, 2017

@SimonWalsh1000
Author

Just to clarify - do you mean call model.save or model.save_weights on the template model at the end of training?

fchollet

fchollet commented on Nov 10, 2017

@fchollet
Collaborator

Yes. Either method should work fine.

DingkunLiu

DingkunLiu commented on Jan 9, 2018

@DingkunLiu

But how can I save the optimizer state by just saving the template model?

s-zk

s-zk commented on Jan 25, 2018

@s-zk

I am on keras 2.1.2 and encountered same problem. I tried follow this answer on StackOverflow, and it works for me. Hope it helps.

wt-huang

wt-huang commented on Nov 13, 2018

@wt-huang

Closing as this is resolved

taalbrecht

taalbrecht commented on Nov 28, 2018

@taalbrecht

Closing as this is resolved

Has this been resolved by a commit and can models and weights be saved as expected when using multi_gpu_model?

Heisenberg0391

Heisenberg0391 commented on Dec 8, 2018

@Heisenberg0391

So, as mentioned above, i should train with parallel_model but save the origin model. But what if i want save weights on every epoch as checkpoints using a callback, what should i do?

jashshah

jashshah commented on Jun 19, 2019

@jashshah

Are there any updates regarding this either on keras or on tf.keras?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

      Development

      No branches or pull requests

        Participants

        @fchollet@datumbox@jashshah@taalbrecht@s-zk

        Issue actions

          Can not save model using model.save following multi_gpu_model · Issue #8446 · keras-team/keras