Search code examples
tensorflowkerasdistributed-computingmulti-gpukeras-2

Can not save model using model.save following multi_gpu_model in Keras


Following the upgrade to Keras 2.0.9, I have been using the multi_gpu_model utility but I can't save my models or best weights using

model.save('path')

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?


Solution

  • To be honest, the easiest approach to this is to actually examine the multi gpu parallel model using

     parallel_model.summary()
    

    (The parallel model is simply the model after applying the multi_gpu function). This clearly highlights the actual model (in I think the penultimate layer - I am not at my computer right now). Then you can use the name of this layer to save the model.

     model = parallel_model.get_layer('sequential_1)
    

    Often its called sequential_1 but if you are using a published architecture, it may be 'googlenet' or 'alexnet'. You will see the name of the layer from the summary.

    Then its simple to just save

     model.save()
    

    Maxims approach works, but its overkill I think.

    Rem: you will need to compile both the model, and the parallel model.