Unable to save Keras model in SavedModel format: KeyError: 'inputs

243 views
Skip to first unread message

geantbrun

unread,
Feb 12, 2021, 9:52:13 AM2/12/21
to Keras-users
Hello,
Having trained a retinanet model with Keras (https://github.com/fizyr/keras-retinanet),  I'm now trying to save it under SavedModel format. My command is :

prediction_model.save('my_model')

If anyone can help, it. would be greatly appreciated. I created an issue here but I got no answer.
 
The error I get is  "KeyError: 'inputs' ", full trace below.
Thank you for your help.


~/.virtualenvs/tripanalysis_env/lib/python3.7/site-packages/tensorflow/python/keras/engine/training.py in save(self, filepath, overwrite, include_optimizer, save_format, signatures, options) 1977 """ 1978 save.save_model(self, filepath, overwrite, include_optimizer, save_format, -> 1979 signatures, options) 1980 1981 def save_weights(self, ~/.virtualenvs/tripanalysis_env/lib/python3.7/site-packages/tensorflow/python/keras/saving/save.py in save_model(model, filepath, overwrite, include_optimizer, save_format, signatures, options) 132 else: 133 saved_model_save.save(model, filepath, overwrite, include_optimizer, --> 134 signatures, options) 135 136 ~/.virtualenvs/tripanalysis_env/lib/python3.7/site-packages/tensorflow/python/keras/saving/saved_model/save.py in save(model, filepath, overwrite, include_optimizer, signatures, options) 78 # we use the default replica context here. 79 with distribution_strategy_context._get_default_replica_context(): # pylint: disable=protected-access ---> 80 save_lib.save(model, filepath, signatures, options) 81 82 if not include_optimizer: ~/.virtualenvs/tripanalysis_env/lib/python3.7/site-packages/tensorflow/python/saved_model/save.py in save(obj, export_dir, signatures, options) 974 975 _, exported_graph, object_saver, asset_info = _build_meta_graph( --> 976 obj, export_dir, signatures, options, meta_graph_def) 977 saved_model.saved_model_schema_version = constants.SAVED_MODEL_SCHEMA_VERSION 978 ~/.virtualenvs/tripanalysis_env/lib/python3.7/site-packages/tensorflow/python/saved_model/save.py in _build_meta_graph(obj, export_dir, signatures, options, meta_graph_def) 1045 if signatures is None: 1046 signatures = signature_serialization.find_function_to_export( -> 1047 checkpoint_graph_view) 1048 1049 signatures, wrapped_functions = ( ~/.virtualenvs/tripanalysis_env/lib/python3.7/site-packages/tensorflow/python/saved_model/signature_serialization.py in find_function_to_export(saveable_view) 73 # If the user did not specify signatures, check the root object for a function 74 # that can be made into a signature. ---> 75 functions = saveable_view.list_functions(saveable_view.root) 76 signature = functions.get(DEFAULT_SIGNATURE_ATTR, None) 77 if signature is not None: ~/.virtualenvs/tripanalysis_env/lib/python3.7/site-packages/tensorflow/python/saved_model/save.py in list_functions(self, obj, extra_functions) 143 if obj_functions is None: 144 obj_functions = obj._list_functions_for_serialization( # pylint: disable=protected-access --> 145 self._serialization_cache) 146 self._functions[obj] = obj_functions 147 if extra_functions: ~/.virtualenvs/tripanalysis_env/lib/python3.7/site-packages/tensorflow/python/keras/engine/training.py in _list_functions_for_serialization(self, serialization_cache) 2588 self.predict_function = None 2589 functions = super( -> 2590 Model, self)._list_functions_for_serialization(serialization_cache) 2591 self.train_function = train_function 2592 self.test_function = test_function ~/.virtualenvs/tripanalysis_env/lib/python3.7/site-packages/tensorflow/python/keras/engine/base_layer.py in _list_functions_for_serialization(self, serialization_cache) 3017 def _list_functions_for_serialization(self, serialization_cache): 3018 return (self._trackable_saved_model_saver -> 3019 .list_functions_for_serialization(serialization_cache)) 3020 3021 def __getstate__(self): ~/.virtualenvs/tripanalysis_env/lib/python3.7/site-packages/tensorflow/python/keras/saving/saved_model/base_serialization.py in list_functions_for_serialization(self, serialization_cache) 85 `ConcreteFunction`. 86 """ ---> 87 fns = self.functions_to_serialize(serialization_cache) 88 89 # The parent AutoTrackable class saves all user-defined tf.functions, and ~/.virtualenvs/tripanalysis_env/lib/python3.7/site-packages/tensorflow/python/keras/saving/saved_model/layer_serialization.py in functions_to_serialize(self, serialization_cache) 77 def functions_to_serialize(self, serialization_cache): 78 return (self._get_serialized_attributes( ---> 79 serialization_cache).functions_to_serialize) 80 81 def _get_serialized_attributes(self, serialization_cache): ~/.virtualenvs/tripanalysis_env/lib/python3.7/site-packages/tensorflow/python/keras/saving/saved_model/layer_serialization.py in _get_serialized_attributes(self, serialization_cache) 93 94 object_dict, function_dict = self._get_serialized_attributes_internal( ---> 95 serialization_cache) 96 97 serialized_attr.set_and_validate_objects(object_dict) ~/.virtualenvs/tripanalysis_env/lib/python3.7/site-packages/tensorflow/python/keras/saving/saved_model/model_serialization.py in _get_serialized_attributes_internal(self, serialization_cache) 55 objects, functions = ( 56 super(ModelSavedModelSaver, self)._get_serialized_attributes_internal( ---> 57 serialization_cache)) 58 functions['_default_save_signature'] = default_signature 59 return objects, functions ~/.virtualenvs/tripanalysis_env/lib/python3.7/site-packages/tensorflow/python/keras/saving/saved_model/layer_serialization.py in _get_serialized_attributes_internal(self, serialization_cache) 102 """Returns dictionary of serialized attributes.""" 103 objects = save_impl.wrap_layer_objects(self.obj, serialization_cache) --> 104 functions = save_impl.wrap_layer_functions(self.obj, serialization_cache) 105 # Attribute validator requires that the default save signature is added to 106 # function dict, even if the value is None. ~/.virtualenvs/tripanalysis_env/lib/python3.7/site-packages/tensorflow/python/keras/saving/saved_model/save_impl.py in wrap_layer_functions(layer, serialization_cache) 153 # Reset the losses of the layer and its children. The call function in each 154 # child layer is replaced with tf.functions. --> 155 original_fns = _replace_child_layer_functions(layer, serialization_cache) 156 original_losses = _reset_layer_losses(layer) 157 ~/.virtualenvs/tripanalysis_env/lib/python3.7/site-packages/tensorflow/python/keras/saving/saved_model/save_impl.py in _replace_child_layer_functions(layer, serialization_cache) 272 serialized_functions = ( 273 child_layer._trackable_saved_model_saver._get_serialized_attributes( --> 274 serialization_cache).functions) 275 else: 276 serialized_functions = ( ~/.virtualenvs/tripanalysis_env/lib/python3.7/site-packages/tensorflow/python/keras/saving/saved_model/layer_serialization.py in _get_serialized_attributes(self, serialization_cache) 93 94 object_dict, function_dict = self._get_serialized_attributes_internal( ---> 95 serialization_cache) 96 97 serialized_attr.set_and_validate_objects(object_dict) ~/.virtualenvs/tripanalysis_env/lib/python3.7/site-packages/tensorflow/python/keras/saving/saved_model/layer_serialization.py in _get_serialized_attributes_internal(self, serialization_cache) 102 """Returns dictionary of serialized attributes.""" 103 objects = save_impl.wrap_layer_objects(self.obj, serialization_cache) --> 104 functions = save_impl.wrap_layer_functions(self.obj, serialization_cache) 105 # Attribute validator requires that the default save signature is added to 106 # function dict, even if the value is None. ~/.virtualenvs/tripanalysis_env/lib/python3.7/site-packages/tensorflow/python/keras/saving/saved_model/save_impl.py in wrap_layer_functions(layer, serialization_cache) 163 call_fn_with_losses = call_collection.add_function( 164 _wrap_call_and_conditional_losses(layer), --> 165 '{}_layer_call_and_return_conditional_losses'.format(layer.name)) 166 call_fn = call_collection.add_function( 167 _extract_outputs_from_fn(layer, call_fn_with_losses), ~/.virtualenvs/tripanalysis_env/lib/python3.7/site-packages/tensorflow/python/keras/saving/saved_model/save_impl.py in add_function(self, call_fn, name) 503 # Manually add traces for layers that have keyword arguments and have 504 # a fully defined input signature. --> 505 self.add_trace(*self._input_signature) 506 return fn 507 ~/.virtualenvs/tripanalysis_env/lib/python3.7/site-packages/tensorflow/python/keras/saving/saved_model/save_impl.py in add_trace(self, *args, **kwargs) 418 fn.get_concrete_function(*args, **kwargs) 419 --> 420 trace_with_training(True) 421 trace_with_training(False) 422 else: ~/.virtualenvs/tripanalysis_env/lib/python3.7/site-packages/tensorflow/python/keras/saving/saved_model/save_impl.py in trace_with_training(value, fn) 416 utils.set_training_arg(value, self._training_arg_index, args, kwargs) 417 with K.deprecated_internal_learning_phase_scope(value): --> 418 fn.get_concrete_function(*args, **kwargs) 419 420 trace_with_training(True) ~/.virtualenvs/tripanalysis_env/lib/python3.7/site-packages/tensorflow/python/keras/saving/saved_model/save_impl.py in get_concrete_function(self, *args, **kwargs) 547 if not self.call_collection.tracing: 548 self.call_collection.add_trace(*args, **kwargs) --> 549 return super(LayerCall, self).get_concrete_function(*args, **kwargs) 550 551 ~/.virtualenvs/tripanalysis_env/lib/python3.7/site-packages/tensorflow/python/eager/def_function.py in get_concrete_function(self, *args, **kwargs) 1165 ValueError: if this object has not yet been called on concrete values. 1166 """ -> 1167 concrete = self._get_concrete_function_garbage_collected(*args, **kwargs) 1168 concrete._garbage_collector.release() # pylint: disable=protected-access 1169 return concrete ~/.virtualenvs/tripanalysis_env/lib/python3.7/site-packages/tensorflow/python/eager/def_function.py in _get_concrete_function_garbage_collected(self, *args, **kwargs) 1071 if self._stateful_fn is None: 1072 initializers = [] -> 1073 self._initialize(args, kwargs, add_initializers_to=initializers) 1074 self._initialize_uninitialized_variables(initializers) 1075 ~/.virtualenvs/tripanalysis_env/lib/python3.7/site-packages/tensorflow/python/eager/def_function.py in _initialize(self, args, kwds, add_initializers_to) 695 self._concrete_stateful_fn = ( 696 self._stateful_fn._get_concrete_function_internal_garbage_collected( # pylint: disable=protected-access --> 697 *args, **kwds)) 698 699 def invalid_creator_scope(*unused_args, **unused_kwds): ~/.virtualenvs/tripanalysis_env/lib/python3.7/site-packages/tensorflow/python/eager/function.py in _get_concrete_function_internal_garbage_collected(self, *args, **kwargs) 2853 args, kwargs = None, None 2854 with self._lock: -> 2855 graph_function, _, _ = self._maybe_define_function(args, kwargs) 2856 return graph_function 2857 ~/.virtualenvs/tripanalysis_env/lib/python3.7/site-packages/tensorflow/python/eager/function.py in _maybe_define_function(self, args, kwargs) 3211 3212 self._function_cache.missed.add(call_context_key) -> 3213 graph_function = self._create_graph_function(args, kwargs) 3214 self._function_cache.primary[cache_key] = graph_function 3215 return graph_function, args, kwargs ~/.virtualenvs/tripanalysis_env/lib/python3.7/site-packages/tensorflow/python/eager/function.py in _create_graph_function(self, args, kwargs, override_flat_arg_shapes) 3073 arg_names=arg_names, 3074 override_flat_arg_shapes=override_flat_arg_shapes, -> 3075 capture_by_value=self._capture_by_value), 3076 self._function_attributes, 3077 function_spec=self.function_spec, ~/.virtualenvs/tripanalysis_env/lib/python3.7/site-packages/tensorflow/python/framework/func_graph.py in func_graph_from_py_func(name, python_func, args, kwargs, signature, func_graph, autograph, autograph_options, add_control_dependencies, arg_names, op_return_value, collections, capture_by_value, override_flat_arg_shapes) 984 _, original_func = tf_decorator.unwrap(python_func) 985 --> 986 func_outputs = python_func(*func_args, **func_kwargs) 987 988 # invariant: `func_outputs` contains only Tensors, CompositeTensors, ~/.virtualenvs/tripanalysis_env/lib/python3.7/site-packages/tensorflow/python/eager/def_function.py in wrapped_fn(*args, **kwds) 598 # __wrapped__ allows AutoGraph to swap in a converted function. We give 599 # the function a weak reference to itself to avoid a reference cycle. --> 600 return weak_wrapped_fn().__wrapped__(*args, **kwds) 601 weak_wrapped_fn = weakref.ref(wrapped_fn) 602 ~/.virtualenvs/tripanalysis_env/lib/python3.7/site-packages/tensorflow/python/keras/saving/saved_model/save_impl.py in wrapper(*args, **kwargs) 513 layer = call_collection.layer 514 training = None --> 515 inputs = call_collection.get_input_arg_value(args, kwargs) 516 # pylint: disable=protected-access 517 if (args or kwargs) and call_collection.training_arg_was_passed( ~/.virtualenvs/tripanalysis_env/lib/python3.7/site-packages/tensorflow/python/keras/saving/saved_model/save_impl.py in get_input_arg_value(self, args, kwargs) 453 def get_input_arg_value(self, args, kwargs): 454 return self.layer._get_call_arg_value( # pylint: disable=protected-access --> 455 self._input_arg_name, args, kwargs, inputs_in_args=True) 456 457 def _maybe_wrap_with_training_arg(self, call_fn): ~/.virtualenvs/tripanalysis_env/lib/python3.7/site-packages/tensorflow/python/keras/engine/base_layer.py in _get_call_arg_value(self, arg_name, args, kwargs, inputs_in_args) 2534 call_fn_args = call_fn_args[1:] 2535 args_dict = dict(zip(call_fn_args, args)) -> 2536 return args_dict[arg_name] 2537 2538 

def _set_call_arg_value( KeyError: 'inputs'

Sayak Paul

unread,
Feb 12, 2021, 10:53:24 AM2/12/21
to geantbrun, Keras-users
I think it might be better to raise this issue on the official Keras RetinaNet repository. On the other hand, Keras has a pretty neat official example on RetinaNet that you could use - https://keras.io/examples/vision/retinanet
Sayak Paul | sayak.dev



--
You received this message because you are subscribed to the Google Groups "Keras-users" group.
To unsubscribe from this group and stop receiving emails from it, send an email to keras-users...@googlegroups.com.
To view this discussion on the web, visit https://groups.google.com/d/msgid/keras-users/defc5b45-a1de-4158-8cf2-d1a913c104aen%40googlegroups.com.

geantbrun

unread,
Feb 16, 2021, 4:27:00 PM2/16/21
to Keras-users
Thank you for your help Sayak. After experimenting a bit, it seems it's not related to specific retinanet repo that I'm using. I reproduced my error for a minimal model:

from tensorflow import keras
import keras_resnet
import keras_resnet.models

inputs = keras.layers.Input(shape=(None, None, 3))
resnet = keras_resnet.models.ResNet50(inputs, include_top=False, freeze_bn=True)

resnet.save("my-model")

and I get the same error: "KeyError: 'inputs'"

Is there something I'm missing with the save command?
Regards,
Patrick

geantbrun

unread,
Feb 16, 2021, 5:53:08 PM2/16/21
to Keras-users
I've realized that it's related to the resnet50 specific implementation. I thought at the beginning that it was the official keras implementation. I have tested the official implementation from keras.applications.resnet and it's working. Thanks again.
Reply all
Reply to author
Forward
0 new messages