When I want to use Keras with TensorFlow 2 I got this error:
AttributeError: module 'tensorflow' has no attribute
'get_default_graph'
The Keras API (https://keras.io/) has multiple implementations, including the original and reference implementation (https://github.com/keras-team/keras), but also various other implementations, including tf.keras, which is part of TensorFlow.
So there are two ways you can use Keras with TensorFlow:
Using the reference implementation with the TensorFlow backend. However, this implementation has not been updated to support TensorFlow 2 yet (as of June 2019).
Using TensorFlow's implementation, tf.keras. This one works fine with TF 2.
To use tf.keras, you must make sure to use the correct imports:
from tensorflow import keras
# NOT: import keras
Similarly, use:
from tensorflow.keras.layers import Dense
# Not from keras.layers import Dense
Hope this helps.
Since TensorFlow 2 defaults to eager execution Keras will need some changes in order to be compatible with it, but until then a previous version of TensorFlow is required.
Related
I was using TensorFlow 1.13 and Keras for my research projects. Nowadays, due to some future warnings, I installed TensorFlow 2.0 and tried to use it.
Instead of using Keras as I did before, I used tf.keras and built the same RNN model. i.e.
from keras.layers import Dense (I used before)
v.s.
from tf.keras.layers import Dense (I tried now)
All other codes are the same. However, I get some worse results for using import from tf.keras.layers one. And I am pretty sure it's not a coincidence, I tried cross-validation and run the models many times.
Does anyone have some ideas about why it happens? Are there any differences from the tf.keras.layers and keras.layers? If so, how can we be careful in case we got some "wrose" results?
tf.keras is tensorflow's implementation of the keras api. Ideally, using tf.keras should not provide you worse results. However, there might be a mismatch in the versions of both the keras which may/may not give you different results. You can check the version using tf.keras.version function and see if that is the same version of keras that you had used before.
For more details refer:
https://www.tensorflow.org/guide/keras/overview
I've been using the keras module from tensorflow 1.12.0 for training and saving models. I recently came across a seemingly useful library for visualization of the weights/outputs, but they require the models be loaded as a Keras model. I'm running into an error trying to load my tf.keras models using keras, and was hoping someone could provide a solution. Python version 3.5.2, Keras version 2.2.4.
I've defined the custom object for the GlorotUniform since keras doesn't recognize that initializer. Afterwards, when I try to load the model, I get a TypeError.
# This works
model = tf.keras.models.load_model('./densenet_model.h5')
# This does not work
model = keras.models.load_model('./densenet_model.h5', custom_objects={"GlorotUniform": tf.keras.initializers.glorot_uniform})
# This is the error that happens
TypeError: tuple indices must be integers or slices, not list
In summary, I was wondering if there was a simple way to convert a model created with tf.keras to a keras model.
I figured out a workaround. I just load the model with tf.keras.load_model, then save_weights. Then I build the same model with Keras and just use load_weights. I verified that the weights were loaded appropriately by checking the output with my validation dataset.
Instead of from keras.models import load_model I used from tensorflow.python.keras.models import load_model. The problem is solved.
I want to use mobileNetV2 with tf.keras.
If look on the tensorflow website for keras applications
I find
mobilenet = tf.keras.applications.MobileNetV2()
If I try to import MobileNetV2
from tensorflow.keras.applications import MobileNetV2
I get an error:
ImportError: cannot import name 'MobileNetV2'
If I check the Keras2 webside I do find only a handful of applications. The mobileNetV2 (or V1) is not one of them. But the V1 model can be loaded and used.
If I follow the link on the tensorflow.keras website, it brings me to the classic keras webside which in my opinion is Keras1 not keras2, am I wrong? Also stating MobileNetV2, which apparently is not implemented. So I guess the link is wrong.
This is all confusing to me. Probably, this is all due to due to the switch to tf.keras, or am mixing things up?
To formulate my question more concrete: Is there a predefined, usable MobileNetV2 application with tf.keras or do I have to implement it by hand?
Thanks
edit: TF version 1.10.
You are using this link for your reference for MobileNetV2 but that is documented for tensorflow version 1.13. And you are using tensorflow version 1.10. In this you can only find MobileNet not MobileNetV2.
For tensorflow version 1.10, you can import like this,
from tensorflow.keras.applications.mobilenet import MobileNet
or
model = tf.keras.applications.MobileNet()
If you want to check what are the model are included in tf.keras.applications, you can check github repo with appropriate tensorflow version.
If you want to use MobileNetV2, please upgrade your tensorflow version and you can use as it mentioned in the documentation.
For Google Colab and latest version of tensorflow,
Use:
!pip install keras_applications
.. will install keras-applications >= 1.0.8
For tensorflow version >= 2.5.0, use
from keras.applications.mobilenet_v2 import MobileNetV2
I'm learning TensorFlow and Keras. I'd like to try https://www.amazon.com/Deep-Learning-Python-Francois-Chollet/dp/1617294438/, and it seems to be written in Keras.
Would it be fairly straightforward to convert code to tf.keras?
I'm not more interested in the portability of the code, rather than the true difference between the two.
The difference between tf.keras and keras is the Tensorflow specific enhancement to the framework.
keras is an API specification that describes how a Deep Learning framework should implement certain part, related to the model definition and training.
Is framework agnostic and supports different backends (Theano, Tensorflow, ...)
tf.keras is the Tensorflow specific implementation of the Keras API specification. It adds the framework the support for many Tensorflow specific features like: perfect support for tf.data.Dataset as input objects, support for eager execution, ...
In Tensorflow 2.0 tf.keras will be the default and I highly recommend to start working using tf.keras
At this point tensorflow has pretty much entirely adopted the keras API and for a good reason - it's simple, easy to use and easy to learn, whereas "pure" tensorflow comes with a lot of boilerplate code. And yes, you can use tf.keras without any issues, though you might have to re-work your imports in the code. For instance
from keras.layers.pooling import MaxPooling2D
Would turn into:
from tensorflow.keras.layers import MaxPooling2D
The history of Keras Vs tf.keras is long and twisted.
Keras: Keras is a high-level (easy to use) API, built by Google AI Developer/Researcher, Francois Chollet. Written in Python and capable of running on top of backend engines like TensorFlow, CNTK, or Theano.
TensorFlow: A library, also developed by Google, for the Deep Learning developer Community, for making deep learning applications accessible and usable to public. Open Sourced and available on GitHub.
With the release of Keras v1.1.0, Tensorflow was made default backend engine. That meant: if you installed Keras on your system, you were also installing TensorFlow.
Later, with TensorFlow v1.10.0, for the first time tf.keras submodule was introduced in Tensorflow. The first step in integrating Keras within TensorFlow
With the release of Keras 2.3.0,
first release of Keras in sync with tf.keras
Last major release to support other multi-backend engines
And most importantly, going forward, recommend switching the code from keras to Tensorflow2.0 and tf.keras packages.
Refer this tweet from François Chollet to use tf.keras.
That means,
Change Everywhere
From
from keras.models import Sequential
from keras.models import load_model
To
from tensorflow.keras.models import Sequential
from tensorflow.keras.models import load_model
And In requirements.txt,
tensorflow==2.3.0
*Disclaimer: it might give conflicts if you were using an older version of Keras. Do pip uninstall keras in that case.
I am working in a single jupyter notebook. I create and train a very simple CNN with keras. It compiles, fits, and predicts fine. I save it with:
model.save("mymodel.hd5")
Model is a keras.models.Sequential.
I then read that back in with:
reload_keras_model = keras.models.load_model("mymodel.hd5")
That also works fine. However if I try to read the model in using tensorflow via:
from tensorflow.keras.models import load_model
reload_tf_mmodel = load_model("mymodel.hd5")
That fails with:
ValueError: Unknown layer:layers
Most of the threads I've read on github say "update your model" or comments about custom objects (I'm not using any). My target platform is the rpi zero and I've been able to install tf but unable to install keras, and that's why I want to load via tf. Why would keras and tf.keras handle this model differently and what do I need to update/change to read it in with tf.keras?
While keras (can) use TF as Backend, it does not guarantee that the saved model is readable in TF as well.
Note that you can use keras with both theano and tf, thus reload_keras_model = keras.models.load_model("mymodel.hd5") will work good with both backends, as the saving/loading is done in the "keras" part, and not using the backend.
You can use this tool: keras_to_tensorflow
Or something similar.