Search code examples
tensorflowkerascoremlcoremltools

Keras + Tensorflow model convert to coreml exits NameError: global name ... is not defined


I've adapted the VAE example from the keras site to train on my data, and everything runs fine. But I'm unable to convert to coreml. The error is:

NameError: global name `batch_size' is not defined

Since batch_size clearly is defined in the python source, I'm guessing it has to do with how the conversion tool captures variable names. Does anyone know how I can fix it (or whether it is, indeed, possible to fix)?

Many thanks,

J.


Solution

  • I ran into a similar message when using parameters to construct the neural net. This should work:

    from keras import models
    
    batch_size = 50    
    
    model = models.load_model(filename, custom_objects={'batch_size': batch_size})
    

    See also documentation: https://keras.io/getting-started/faq/#how-can-i-save-a-keras-model