question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

load_model not working - NameError: global name 'batch_size' is not defined????

See original GitHub issue

I had to restart my ipython interpreter so I saved the model with model.save() but loading isnt working

batch_size is of course set. 50. The model is a deeper deconv vae from the examples, nothing fancy. vae_loss is the same with the example.

from keras.models import load_model
vae = load_model('Image_VAECNN04-256_100.h5', custom_objects={'vae_loss':vae_loss})
---------------------------------------------------------------------------
NameError                                 Traceback (most recent call last)
<ipython-input-38-ea67b750c140> in <module>()
      1 from keras.models import load_model
----> 2 vae = load_model('Image_VAECNN04-256_100.h5', custom_objects={'vae_loss':vae_loss})

/opt/conda/lib/python2.7/site-packages/keras/models.pyc in load_model(filepath, custom_objects)
    126         raise ValueError('No model found in config file.')
    127     model_config = json.loads(model_config.decode('utf-8'))
--> 128     model = model_from_config(model_config, custom_objects=custom_objects)
    129 
    130     # set weights

/opt/conda/lib/python2.7/site-packages/keras/models.pyc in model_from_config(config, custom_objects)
    175         raise Exception('`model_fom_config` expects a dictionary, not a list. '
    176                         'Maybe you meant to use `Sequential.from_config(config)`?')
--> 177     return layer_from_config(config, custom_objects=custom_objects)
    178 
    179 

/opt/conda/lib/python2.7/site-packages/keras/utils/layer_utils.pyc in layer_from_config(config, custom_objects)
     34         layer_class = get_from_module(class_name, globals(), 'layer',
     35                                       instantiate=False)
---> 36     return layer_class.from_config(config['config'])
     37 
     38 

/opt/conda/lib/python2.7/site-packages/keras/engine/topology.pyc in from_config(cls, config, custom_objects)
   2373 
   2374         for layer_data in config['layers']:
-> 2375             process_layer(layer_data)
   2376 
   2377         name = config.get('name')

/opt/conda/lib/python2.7/site-packages/keras/engine/topology.pyc in process_layer(layer_data)
   2370                         layer(input_tensors[0])
   2371                     else:
-> 2372                         layer(input_tensors)
   2373 
   2374         for layer_data in config['layers']:

/opt/conda/lib/python2.7/site-packages/keras/engine/topology.pyc in __call__(self, x, mask)
    512         if inbound_layers:
    513             # this will call layer.build() if necessary
--> 514             self.add_inbound_node(inbound_layers, node_indices, tensor_indices)
    515             input_added = True
    516 

/opt/conda/lib/python2.7/site-packages/keras/engine/topology.pyc in add_inbound_node(self, inbound_layers, node_indices, tensor_indices)
    570         # creating the node automatically updates self.inbound_nodes
    571         # as well as outbound_nodes on inbound layers.
--> 572         Node.create_node(self, inbound_layers, node_indices, tensor_indices)
    573 
    574     def get_output_shape_for(self, input_shape):

/opt/conda/lib/python2.7/site-packages/keras/engine/topology.pyc in create_node(cls, outbound_layer, inbound_layers, node_indices, tensor_indices)
    152             output_shapes = to_list(outbound_layer.get_output_shape_for(input_shapes[0]))
    153         else:
--> 154             output_tensors = to_list(outbound_layer.call(input_tensors, mask=input_masks))
    155             output_masks = to_list(outbound_layer.compute_mask(input_tensors, input_masks))
    156             output_shapes = to_list(outbound_layer.get_output_shape_for(input_shapes))

/opt/conda/lib/python2.7/site-packages/keras/layers/core.pyc in call(self, x, mask)
    554         if 'mask' in arg_spec.args:
    555             arguments['mask'] = mask
--> 556         return self.function(x, **arguments)
    557 
    558     def get_config(self):

/opt/conda/lib/python2.7/site-packages/keras/layers/core.pyc in sampling(args)

NameError: global name 'batch_size' is not defined

Issue Analytics

  • State:closed
  • Created 7 years ago
  • Reactions:1
  • Comments:10 (1 by maintainers)

github_iconTop GitHub Comments

1reaction
GeorgesAlkhouricommented, Oct 18, 2016

Hey,

I have the same issue. As workaround I created the Model and called model.compile. After that I loaded the weights manually with model.load_weights(...)

0reactions
MarcelRobitaillecommented, Dec 7, 2021

I opened a new issue with a reproducible minimal code snippet that I believe is the same issue: #15759

Read more comments on GitHub >

github_iconTop Results From Across the Web

How to fix"NameError: name 'load_model' is not defined"
I am reading the book"Deep Learning with Python" and I have come across a problem. I have used ...
Read more >
NameError: name 'BatchLoggerCallback' is not defined - Help
NameError : name 'BatchLoggerCallback' is not defined ... this controls the batch size, or you can manipulate the tf.data.
Read more >
Trainer — PyTorch Lightning 1.8.5.post0 documentation
Runs n if set to n (int) else 1 if set to True batch(es) to ensure your code will execute without errors. This...
Read more >
Image Classification - | notebook.community
The CIFAR-10 dataset consists of 5 batches, named data_batch_1 ... Set batch size if not already set try: if batch_size: pass except NameError:...
Read more >
Errors in saving a Keras model structure - Google Groups
Does anyone have the same problem? ... NameError: global name 'batch_size' is not defined ... seems to me, that you simply have to...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found