Hi there,
start a new keras model and try to convert to tf Estimator
get error valueerror: expected model argument to be a model instance, got xxxxxxxxxxxxxxx
any suggestion? thanks
here is simple example
# set model
model = Sequential()
model.add(Dense(8, input_dim=4))
model.add(Activation('relu'))
model.add(Dense(3, input_dim=8))
model.add(Activation('softmax'))
model.compile(optimizer='SGD', loss='categorical_crossentropy', metrics=['accuracy'])
model_dir = os.path.join(os.getcwd(), "models//123").replace("//", "\\")
estimator_model = tf.keras.estimator.model_to_estimator(keras_model=model,model_dir=model_dir)
I used the Keras Functional API and I encountered the same issue.
The to_estimator does a type check on the Model object. Unfortunately, the tensorflow version of the Model object comes from deep within the bowels of TF in the version I am using (1.4.0). So a default keras Model object will be rejected.
I changed my model import to the following:
from tensorflow.python.keras._impl.keras.models import Model
That fixed my issue.
I believe that all the structures have been changed in 1.5, so I would be careful when upgrading.
Hi @x831617 @dheepanr
thanks
yes, not use keras. Using everything from tf such as tf.keras.layers, tf.keras.layers.Conv1D ,..
model = tf.keras.models.Model(inp,out_final). to define your model should be fine.
so many tricks. Cost much time... but solved finally. thanks all.
I have the same issue when using an Inception-V3 net I modified, with code similar to the example (Keras 2.1.4, TF 1.5.0 on CPU):
# Instantiate a Keras inception v3 model.
inception_v3_net = inception.InceptionV3(
input_shape=input_shape,
classes=y_classes,
)
# Compile model with the optimizer, loss, and metrics you'd like to train with.
inception_v3_net.compile(
optimizer=tf.train.AdamOptimizer(),
loss='categorical_crossentropy',
metric='accuracy')
# Create an Estimator from the compiled Keras model. Note the initial model
# state of the keras model is preserved in the created Estimator.
estimator = tf.keras.estimator.model_to_estimator(
keras_model=inception_v3_net,
model_dir=None)
Traceback (most recent call last):
File "load_test.py", line 79, in <module>
tf.app.run(main=run_network)
File "/home/purohit/.local/lib/python3.6/site-packages/tensorflow/python/platform/app.py", line 124, in run
_sys.exit(main(argv))
File "load_test.py", line 73, in run_network
model_dir=None)
File "/home/purohit/.local/lib/python3.6/site-packages/tensorflow/python/keras/_impl/keras/estimator.py", line 292, in model_to_estimator
_save_first_checkpoint(keras_model, est, custom_objects, keras_weights)
File "/home/purohit/.local/lib/python3.6/site-packages/tensorflow/python/keras/_impl/keras/estimator.py", line 218, in _save_first_checkpoint
custom_objects)
File "/home/purohit/.local/lib/python3.6/site-packages/tensorflow/python/keras/_impl/keras/estimator.py", line 102, in _clone_and_build_model
model = models.clone_model(keras_model, input_tensors=input_tensors)
File "/home/purohit/.local/lib/python3.6/site-packages/tensorflow/python/keras/_impl/keras/models.py", line 1557, in clone_model
return _clone_functional_model(model, input_tensors=input_tensors)
File "/home/purohit/.local/lib/python3.6/site-packages/tensorflow/python/keras/_impl/keras/models.py", line 1361, in _clone_functional_model
'to be a `Model` instance, got ', model)
ValueError: ('Expected `model` argument to be a `Model` instance, got ', <keras.engine.training.Model object at 0x7f76223f6d68>)
Try importing the tensorflow implementation of Keras instead of the standard?
from tensorflow.python.keras._impl import keras
Then, keras.models.Model will in fact be the same class that the instanceof check is looking for.
That makes sense. What is the plan for this -- is that recommended practice now if we want to use Keras with TensorFlow?
Hello calpeyser,
Using tensorflow's implementation of keras did not completely solve the problem, because I use a model with custom layers. I observed that tensorflow's keras version does not call the compute_output_shape function of my custom layers, even though this is crucial for my unpooling layer.
For me, it worked to simply cast the keras model to the tensorflow one:
myKerasModel = build_keras_model(...) # put your keras model here
tfCompatibleMod = tf.keras.models.Model(myKerasModel) # cast to type tensorflow expect
tfCompatibleMod.compile(...) # compile
estimator = tf.keras.estimator.model_to_estimator(keras_model=tfCompatibleMod)
I hope to help anyone who finds it here.
Edit: I encountered bugs when trying to train the network with a casted keras model, for which no solution could be found. According to other reported issues, there is a problem with some cache introduced in the more recent Keras versions, which stores the network and introduces incompatiblities with tensorflow. I will stop using Keras at this point.
@schulz0r Thanks for the casting tip. In TF 1.8, the module is now tf.keras.Model However, as observed at your end, casting the keras model as a tf.keras.Model is then resulting in AttributeError: 'Model' object has no attribute '_output_tensor_cache', with the traceback:
Traceback (most recent call last):
File "mnist_lenet_keras.py", line 44, in <module>
main()
File "mnist_lenet_keras.py", line 40, in main
keras_estimator.run(model, X, Y_oh, nb_epochs=5)
File "/home/pratyush/workspace/DL/dl_utils/tf/estimators/keras_estimator.py", line 43, in run
model_est.train(input_fn=train_input_fn, steps=nb_batches)
File "/home/pratyush/workspace/virtualenv/tf/lib/python3.6/site-packages/tensorflow/python/estimator/estimator.py", line 363, in train
loss = self._train_model(input_fn, hooks, saving_listeners)
File "/home/pratyush/workspace/virtualenv/tf/lib/python3.6/site-packages/tensorflow/python/estimator/estimator.py", line 843, in _train_model
return self._train_model_default(input_fn, hooks, saving_listeners)
File "/home/pratyush/workspace/virtualenv/tf/lib/python3.6/site-packages/tensorflow/python/estimator/estimator.py", line 856, in _train_model_default
features, labels, model_fn_lib.ModeKeys.TRAIN, self.config)
File "/home/pratyush/workspace/virtualenv/tf/lib/python3.6/site-packages/tensorflow/python/estimator/estimator.py", line 831, in _call_model_fn
model_fn_results = self._model_fn(features=features, **kwargs)
File "/home/pratyush/workspace/virtualenv/tf/lib/python3.6/site-packages/tensorflow/python/keras/_impl/keras/estimator.py", line 330, in model_fn
labels)
File "/home/pratyush/workspace/virtualenv/tf/lib/python3.6/site-packages/tensorflow/python/keras/_impl/keras/estimator.py", line 291, in _clone_and_build_model
model._set_inputs(input_tensors)
File "/home/pratyush/workspace/virtualenv/tf/lib/python3.6/site-packages/tensorflow/python/keras/_impl/keras/engine/training.py", line 873, in _set_inputs
self._symbolic_set_inputs(inputs, training=training)
File "/home/pratyush/workspace/virtualenv/tf/lib/python3.6/site-packages/tensorflow/python/keras/_impl/keras/engine/training.py", line 992, in _symbolic_set_inputs
outputs = self.call(self.inputs[0], training=training)
File "/home/pratyush/workspace/virtualenv/tf/lib/python3.6/site-packages/tensorflow/python/keras/_impl/keras/engine/network.py", line 628, in call
if cache_key in self._output_tensor_cache:
AttributeError: 'Model' object has no attribute '_output_tensor_cache'
so how should we solve this problem @aspratyush ?
there are something interesting things I wanna share about this issue
I'm using a StackAutoEncoderClassifier implementation like this
in a nutshell:
AutoEncoder(Sequential) -> StackedAutoEncoder(Sequential) -> StackedAutoEncoderClassifier(Sequential)
when I try to train the classifier, the '_output_tensor_cache' happened, but no error happened when training each AE respectively or training the SAE inside the SAEC
I use pdb to check each model, SAE has _output_tensor_cache, but none for SAEC, it's pretty weird.......
Using Tensorflow backend and keras is the appropriate way to allow converting the keras model to an Estimator. If you find a model implemented in keras , it's easy to make some ajustements and build the blocks using tf.keras.
@schulz0r Hi I'm trying to prune some layers of a custom model too (YOLOv3). May I know if you solved the issue?
@zmlim No, I simply stopped using Keras.
@zmlim No, I simply stopped using Keras.
I'm curious, what do you use now?
@mayowaosibodu I used TF estimators.
Most helpful comment
Hello calpeyser,
Using tensorflow's implementation of keras did not completely solve the problem, because I use a model with custom layers. I observed that tensorflow's keras version does not call the compute_output_shape function of my custom layers, even though this is crucial for my unpooling layer.
For me, it worked to simply cast the keras model to the tensorflow one:
I hope to help anyone who finds it here.
Edit: I encountered bugs when trying to train the network with a casted keras model, for which no solution could be found. According to other reported issues, there is a problem with some cache introduced in the more recent Keras versions, which stores the network and introduces incompatiblities with tensorflow. I will stop using Keras at this point.