0
votes

I have trained a model with keras layers and weight_normalization layer from tensorflow_addons. This is the model I trained and saved in tensorflow file format:

import tensorflow as tf
import tensorflow.keras as tk
import tensorflow_addons as tfa

model = tf.keras.Sequential([
    tf.keras.layers.Input((X_train.shape[1]-1,)),
    tf.keras.layers.BatchNormalization(),
    tf.keras.layers.Dropout(0.2),
    tfa.layers.WeightNormalization(tf.keras.layers.Dense(2048, activation="relu")),
    tf.keras.layers.BatchNormalization(),
    tf.keras.layers.Dropout(0.5),
    tfa.layers.WeightNormalization(tf.keras.layers.Dense(1048, activation="relu")),
    tf.keras.layers.BatchNormalization(),
    tf.keras.layers.Dropout(0.5),
    tfa.layers.WeightNormalization(tf.keras.layers.Dense(206, activation="sigmoid")),
    ])

(and it has no custom metrics)

from keras.callbacks import ModelCheckpoint, EarlyStopping

# autosave best Model
best_model = ModelCheckpoint("model", monitor='val_accuracy', mode='max',verbose=0, save_best_only=True)

earlystop = EarlyStopping(monitor = 'val_accuracy',
                          patience = 15,
                          mode = 'max',
                          verbose = 1,
                          restore_best_weights = True)

callbacks = [best_model, earlystop]

model.compile(loss= 'binary_crossentropy',optimizer= 'Adam',metrics= ['accuracy'])
history = model.fit(X_res, y_res, epochs=100, verbose= 2, validation_data=(X_val[X_val.columns[1:]],y_val[y_val.columns[1:]]), callbacks=callbacks)

But when I load the model it returns an error:

model = tk.models.load_model("../input/model")

--------------------------------------------------------------------------- KeyError Traceback (most recent call last) in 2 return 3 ----> 4 model = tk.models.load_model("../input/model-custom", custom_objects={'__inference_dense_layer_call_fn_1126407':f1})

/opt/conda/lib/python3.7/site-packages/tensorflow/python/keras/saving/save.py in load_model(filepath, custom_objects, compile, options) 185 if isinstance(filepath, six.string_types): 186 loader_impl.parse_saved_model(filepath) --> 187 return saved_model_load.load(filepath, compile, options) 188 189 raise IOError(

/opt/conda/lib/python3.7/site-packages/tensorflow/python/keras/saving/saved_model/load.py in load(path, compile, options) 119 120 model = tf_load.load_internal( --> 121 path, options=options, loader_cls=KerasObjectLoader) 122 123 # pylint: disable=protected-access

/opt/conda/lib/python3.7/site-packages/tensorflow/python/saved_model/load.py in load_internal(export_dir, tags, options, loader_cls) 631 try: 632 loader = loader_cls(object_graph_proto, saved_model_proto, export_dir, --> 633 ckpt_options) 634 except errors.NotFoundError as err: 635 raise FileNotFoundError(

/opt/conda/lib/python3.7/site-packages/tensorflow/python/keras/saving/saved_model/load.py in init(self, *args, **kwargs) 192 self._models_to_reconstruct = [] 193 --> 194 super(KerasObjectLoader, self).init(*args, **kwargs) 195 196 # Now that the node object has been fully loaded, and the checkpoint has

/opt/conda/lib/python3.7/site-packages/tensorflow/python/saved_model/load.py in init(self, object_graph_proto, saved_model_proto, export_dir, ckpt_options) 128 self._concrete_functions[name] = _WrapperFunction(concrete_function) 129 --> 130 self._load_all() 131 self._restore_checkpoint() 132

/opt/conda/lib/python3.7/site-packages/tensorflow/python/keras/saving/saved_model/load.py in _load_all(self) 216 217 # Load all other nodes and functions. --> 218 super(KerasObjectLoader, self)._load_all() 219 220 # Finish setting up layers and models. See function docstring for more info.

/opt/conda/lib/python3.7/site-packages/tensorflow/python/saved_model/load.py in _load_all(self) 139 def _load_all(self): 140 """Loads all nodes and functions from the SavedModel and their edges.""" --> 141 self._load_nodes() 142 self._load_edges() 143 # TODO(b/124045874): There are limitations with functions whose captures

/opt/conda/lib/python3.7/site-packages/tensorflow/python/saved_model/load.py in _load_nodes(self) 281 # interface. 282 continue --> 283 node, setter = self._recreate(proto, node_id) 284 nodes[node_id] = node 285 node_setters[node_id] = setter

/opt/conda/lib/python3.7/site-packages/tensorflow/python/keras/saving/saved_model/load.py in _recreate(self, proto, node_id) 237 obj._handle_name = proto.variable.name + ':0' # pylint: disable=protected-access 238 else: --> 239 obj, setter = super(KerasObjectLoader, self)._recreate(proto, node_id) 240 return obj, setter 241

/opt/conda/lib/python3.7/site-packages/tensorflow/python/saved_model/load.py in _recreate(self, proto, node_id) 391 if kind not in factory: 392 raise ValueError("Unknown SavedObject type: %r" % kind) --> 393 return factorykind 394 395 def _recreate_user_object(self, proto, node_id):

/opt/conda/lib/python3.7/site-packages/tensorflow/python/saved_model/load.py in () 380 lambda: self._recreate_user_object(proto.user_object, node_id)), 381 "asset": lambda: self._recreate_asset(proto.asset), --> 382 "function": lambda: self._recreate_function(proto.function), 383 "bare_concrete_function": functools.partial( 384 self._recreate_bare_concrete_function,

/opt/conda/lib/python3.7/site-packages/tensorflow/python/saved_model/load.py in _recreate_function(self, proto) 419 def _recreate_function(self, proto): 420 return function_deserialization.recreate_function( --> 421 proto, self._concrete_functions), setattr 422 423 def _recreate_bare_concrete_function(self, proto):

/opt/conda/lib/python3.7/site-packages/tensorflow/python/saved_model/function_deserialization.py in recreate_function(saved_function, concrete_functions) 259 concrete_function_objects = [] 260 for concrete_function_name in saved_function.concrete_functions: --> 261 concrete_function_objects.append(concrete_functions[concrete_function_name]) 262 263 for cf in concrete_function_objects:

KeyError: '__inference_dense_layer_call_fn_1126407'

Can you please help me load the model correctly.. Thanks

1
Please show us how you're saving and loading your model.rayryeng
Yes I have added the codes for that tooTimothy Alex John
@rayryeng here is the model I was talking about kaggle.com/timothyalexjohn/moa-imbalanced-multi-label please download the model and try loading the model to tf.keras you will also get an error... Please help me understand the error and rectify it...Thank YouTimothy Alex John

1 Answers

1
votes

I suspect that you have both keras and tensorflow installed separately; I have worked with tfa and never had problems with regard to such a loading matter;

In fact, here you import everything via tensorflow:

import tensorflow as tf
import tensorflow.keras as tk
import tensorflow_addons as tfa

But here you load the callbacks via plain keras:

from keras.callbacks import ModelCheckpoint, EarlyStopping

In order to first ensure that you do have a loading model problem situation, please make sure that every import is done via tensorflow.keras (I expect the problem to disappear altogether once you do this).

Replace

from keras.callbacks import ModelCheckpoint, EarlyStopping

with:

from tensorflow.keras.callbacks import ModelCheckpoint, EarlyStopping

To sum up, retrain from scratch with the new imports (all from tensorflow.keras) and then check if the problem is reproduced.