keras-team / keras

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

name 'y_pred_graph' is not defined when keras.models.load_model( h5 file.) #14065

Closed ooksang closed 3 years ago

ooksang commented 4 years ago

I'm trying to h5 to pb to use tflite. But I got error. Original code is from https://github.com/xuannianz/keras-GaussianYOLOv3

Below is my code from tensorflow import keras model_path = "../checkpoints/pascal_21_9.4463_12.8289_0.8334_0.8535.h5" model = keras.models.load_model(model_path, compile=False) model.save("../checkpoints/", save_format="tf")

Error messages It can not load custom defined module when load model.

NameError Traceback (most recent call last)

in () 1 # freeze.py 2 from tensorflow import keras ----> 3 model = keras.models.load_model(model_path, compile=False) 4 model.save("../checkpoints/", save_format="tf") /home/ubuntu/anaconda3/envs/tensorflow_p36/lib/python3.6/site-packages/tensorflow_core/python/keras/saving/save.py in load_model(filepath, custom_objects, compile) 141 if (h5py is not None and ( 142 isinstance(filepath, h5py.File) or h5py.is_hdf5(filepath))): --> 143 return hdf5_format.load_model_from_hdf5(filepath, custom_objects, compile) 144 145 if isinstance(filepath, six.string_types): /home/ubuntu/anaconda3/envs/tensorflow_p36/lib/python3.6/site-packages/tensorflow_core/python/keras/saving/hdf5_format.py in load_model_from_hdf5(filepath, custom_objects, compile) 160 model_config = json.loads(model_config.decode('utf-8')) 161 model = model_config_lib.model_from_config(model_config, --> 162 custom_objects=custom_objects) 163 164 # set weights /home/ubuntu/anaconda3/envs/tensorflow_p36/lib/python3.6/site-packages/tensorflow_core/python/keras/saving/model_config.py in model_from_config(config, custom_objects) 53 '`Sequential.from_config(config)`?') 54 from tensorflow.python.keras.layers import deserialize # pylint: disable=g-import-not-at-top ---> 55 return deserialize(config, custom_objects=custom_objects) 56 57 /home/ubuntu/anaconda3/envs/tensorflow_p36/lib/python3.6/site-packages/tensorflow_core/python/keras/layers/serialization.py in deserialize(config, custom_objects) 103 module_objects=globs, 104 custom_objects=custom_objects, --> 105 printable_module_name='layer') /home/ubuntu/anaconda3/envs/tensorflow_p36/lib/python3.6/site-packages/tensorflow_core/python/keras/utils/generic_utils.py in deserialize_keras_object(identifier, module_objects, custom_objects, printable_module_name) 189 custom_objects=dict( 190 list(_GLOBAL_CUSTOM_OBJECTS.items()) + --> 191 list(custom_objects.items()))) 192 with CustomObjectScope(custom_objects): 193 return cls.from_config(cls_config) /home/ubuntu/anaconda3/envs/tensorflow_p36/lib/python3.6/site-packages/tensorflow_core/python/keras/engine/network.py in from_config(cls, config, custom_objects) 1079 if layer in unprocessed_nodes: 1080 for node_data in unprocessed_nodes.pop(layer): -> 1081 process_node(layer, node_data) 1082 1083 name = config.get('name') /home/ubuntu/anaconda3/envs/tensorflow_p36/lib/python3.6/site-packages/tensorflow_core/python/keras/engine/network.py in process_node(layer, node_data) 1037 if not isinstance(input_tensors, dict) and len(flat_input_tensors) == 1: 1038 input_tensors = flat_input_tensors[0] -> 1039 layer(input_tensors, **kwargs) 1040 1041 def process_layer(layer_data): /home/ubuntu/anaconda3/envs/tensorflow_p36/lib/python3.6/site-packages/tensorflow_core/python/keras/engine/base_layer.py in __call__(self, inputs, *args, **kwargs) 852 outputs = base_layer_utils.mark_as_return(outputs, acd) 853 else: --> 854 outputs = call_fn(cast_inputs, *args, **kwargs) 855 856 except errors.OperatorNotAllowedInGraphError as e: /home/ubuntu/anaconda3/envs/tensorflow_p36/lib/python3.6/site-packages/tensorflow_core/python/keras/layers/core.py in call(self, inputs, mask, training) 787 arguments['training'] = training 788 with variable_scope.variable_creator_scope(self._variable_creator): --> 789 return self.function(inputs, **arguments) 790 791 def _variable_creator(self, next_creator, **kwargs): /home/ubuntu/anaconda3/envs/tensorflow_p36/lib/python3.6/site-packages/tensorflow_core/python/keras/layers/core.py in yolo_loss(args, anchors, num_anchors_per_layer, num_classes, ignore_thresh, print_loss) 73 - Set `x[:, 3, :] = 0.` and `x[:, 5, :] = 0.` 74 - Insert a `Masking` layer with `mask_value=0.` before the LSTM layer: ---> 75 76 ```python 77 model = Sequential() NameError: name 'y_pred_graph' is not defined ------------------------------------------------------------------------------------------------------------- At the last line in the error " name 'y_pred_graph' is defined" when called yolo_loss. But "yolo_loss" is also define in loss.py and "y_pred_graph" is also defined in "util_graphs.py" When I checked save options in "train.py", It doesn't use save_only_weights whose default value is False. I copied to "y_pred_graph " function into loss.py but didn't work. and I wrote "sys.path.insert" in loss.py to append "util_graphs.py" path but didn't work. My directories are like this -------------------------------------------------------------------------------------------------------------- GaussianYOLO_tf2_upgrade -- utils (folder) -- checkpoints (folder) -- *.h5 -- eval (folder) -- models.py -- loss.py -- util_graph.py -- train.py --------------------------------------------------------------------------------------------------------------
jvishnuvardhan commented 3 years ago

@ooksang This is a stale issue. Sorry for late response.

Is this still an issue for you? Can you please share a simple standalone code to reproduce the issue? Also, check your code with the recent TF versions (2.6 and tf-nightly) and let us know whether the issue persists with recent TF versions. Thanks!

google-ml-butler[bot] commented 3 years ago

This issue has been automatically marked as stale because it has no recent activity. It will be closed if no further activity occurs. Thank you.

google-ml-butler[bot] commented 3 years ago

Closing as stale. Please reopen if you'd like to work on this further.