keras-team / keras

Deep Learning for humans
http://keras.io/
Apache License 2.0
61.98k stars 19.46k forks source link

Lambda Load Model Error #5524

Closed zgbkdlm closed 7 years ago

zgbkdlm commented 7 years ago

I was saving the encoder of the example : "examples/variational_autoencoder.py"

def sampling(args):
    z_mean, z_log_var = args
    epsilon = K.random_normal(shape=(batch_size, latent_dim), mean=0.,
                              std=1.0)
    return z_mean + K.exp(z_log_var / 2) * epsilon

z = Lambda(sampling, output_shape=(latent_dim,))([z_mean, z_log_var])
......
encoder = Model(input=vae_input, output=z)
encoder.save('Compiled/variationalAutoencoders_encoder.h5')

But when I load it, the error ocurrs, batch_size not defined. It doesn't work even I set a global batch_size.

from keras.models import Model, load_model
from keras import backend as K
import numpy as np
import matplotlib.pyplot as plt

encoding_dim = 64
batch_size = 100

autoencoder = load_model('Compiled/variationalAutoencoders_autoencoder.h5')
Traceback (most recent call last):
  File "F:/Uppsala/SVAE/probe_script.py", line 9, in <module>
    autoencoder = load_model('Compiled/variationalAutoencoders_autoencoder.h5')
  File "C:\Users\Administrator\AppData\Local\Programs\Python\Python35\lib\site-packages\keras\models.py", line 142, in load_model
    model = model_from_config(model_config, custom_objects=custom_objects)
  File "C:\Users\Administrator\AppData\Local\Programs\Python\Python35\lib\site-packages\keras\models.py", line 193, in model_from_config
    return layer_from_config(config, custom_objects=custom_objects)
  File "C:\Users\Administrator\AppData\Local\Programs\Python\Python35\lib\site-packages\keras\utils\layer_utils.py", line 40, in layer_from_config
    custom_objects=custom_objects)
  File "C:\Users\Administrator\AppData\Local\Programs\Python\Python35\lib\site-packages\keras\engine\topology.py", line 2582, in from_config
    process_layer(layer_data)
  File "C:\Users\Administrator\AppData\Local\Programs\Python\Python35\lib\site-packages\keras\engine\topology.py", line 2579, in process_layer
    layer(input_tensors)
  File "C:\Users\Administrator\AppData\Local\Programs\Python\Python35\lib\site-packages\keras\engine\topology.py", line 572, in __call__
    self.add_inbound_node(inbound_layers, node_indices, tensor_indices)
  File "C:\Users\Administrator\AppData\Local\Programs\Python\Python35\lib\site-packages\keras\engine\topology.py", line 635, in add_inbound_node
    Node.create_node(self, inbound_layers, node_indices, tensor_indices)
  File "C:\Users\Administrator\AppData\Local\Programs\Python\Python35\lib\site-packages\keras\engine\topology.py", line 172, in create_node
    output_tensors = to_list(outbound_layer.call(input_tensors, mask=input_masks))
  File "C:\Users\Administrator\AppData\Local\Programs\Python\Python35\lib\site-packages\keras\layers\core.py", line 641, in call
    return self.function(x, **arguments)
  File "F:/Uppsala/SVAE/variationalAutoencoders.py", line 30, in sampling
    epsilon = K.random_normal(shape=(batch_size, latent_dim), mean=0.,
NameError: name 'batch_size' is not defined
Chandrak1907 commented 7 years ago

Hi, I also faced similar problem. I referred to this, I guess, If we only save the weights, then model architecture need always need to be known or documented properly. So, I wanted to save the model with lambda function in Keras.

I tried two approaches:

Approach-1: I did not try to change the vgg_mean function

Mean of each channel as provided by VGG researchers

 vgg_mean = np.array([123.68, 116.779, 103.939]).reshape((3,1,1))

  def vgg_preprocess(x):
      x = x - vgg_mean     # subtract mean
     return x[:, ::-1]    # reverse axis bgr->rgb '

And, then I was able to save the model. But, when I reload the saved model

  new_model = load_model('my_model_copy2.h5',custom_objects={'vgg_mean': vgg_mean})

I get error mentioning 'global name vgg_mean is not defined". Gist file for this working is located here.

Approach 2:

I referred to this: and changed vgg_preprocess function as below:

  def vgg_preprocess(x, vgg_mean):  
     x = x - vgg_mean     # subtract mean
    return x[:, ::-1] 

I get below error, when I try to save the model:

ValueError: can only convert an array of size 1 to a Python scalar Gist file for this working is here.

How to save the model both architecture and weights with lambda function?

stale[bot] commented 7 years ago

This issue has been automatically marked as stale because it has not had recent activity. It will be closed after 30 days if no further activity occurs, but feel free to re-open a closed issue if needed.