How to import keras from tf.keras in Tensorflow? - python

import tensorflow as tf
import tensorflow
from tensorflow import keras
from keras.layers import Dense
I am getting the below error
from keras.layers import Input, Dense
Traceback (most recent call last):
File "<ipython-input-6-b5da44e251a5>", line 1, in <module>
from keras.layers import Input, Dense
ModuleNotFoundError: No module named 'keras'
How do I solve this?
Note: I am using Tensorflow version 1.4

Use the keras module from tensorflow like this:
import tensorflow as tf
Import classes
from tensorflow.python.keras.layers import Input, Dense
or use directly
dense = tf.keras.layers.Dense(...)
EDIT Tensorflow 2
from tensorflow.keras.layers import Input, Dense
and the rest stays the same.

Try from tensorflow.python import keras
with this, you can easily change keras dependent code to tensorflow in one line change.
You can also try from tensorflow.contrib import keras. This works on tensorflow 1.3
Edited: for tensorflow 1.10 and above you can use import tensorflow.keras as keras to get keras in tensorflow.

To make it simple I will take the two versions of the code in keras and tf.keras. The example here is a simple Neural Network Model with different layers in it.
In Keras (v2.1.5)
from keras.models import Sequential
from keras.layers import Dense
def get_model(n_x, n_h1, n_h2):
model = Sequential()
model.add(Dense(n_h1, input_dim=n_x, activation='relu'))
model.add(Dense(n_h2, activation='relu'))
model.add(Dropout(0.5))
model.add(Dense(4, activation='softmax'))
model.compile(loss='binary_crossentropy', optimizer='adam', metrics=['accuracy'])
print(model.summary())
return model
In tf.keras (v1.9)
import tensorflow as tf
def get_model(n_x, n_h1, n_h2):
model = tf.keras.Sequential()
model.add(tf.keras.layers.Dense(n_h1, input_dim=n_x, activation='relu'))
model.add(tf.keras.layers.Dense(n_h2, activation='relu'))
model.add(tf.keras.layers.Dropout(0.5))
model.add(tf.keras.layers.Dense(4, activation='softmax'))
model.compile(loss='categorical_crossentropy', optimizer='adam', metrics=['accuracy'])
print(model.summary())
return model
or it can be imported the following way instead of the above-mentioned way
from tensorflow.keras.layers import Dense
The official documentation of tf.keras
Note: TensorFlow Version is 1.9

Its not quite fine to downgrade everytime, you may need to make following changes as shown below:
Tensorflow
import tensorflow as tf
#Keras
from tensorflow.keras.models import Sequential, Model, load_model, save_model
from tensorflow.keras.callbacks import ModelCheckpoint
from tensorflow.keras.layers import Dense, Activation, Dropout, Input, Masking, TimeDistributed, LSTM, Conv1D, Embedding
from tensorflow.keras.layers import GRU, Bidirectional, BatchNormalization, Reshape
from tensorflow.keras.optimizers import Adam
from tensorflow.keras.layers import Reshape, Dropout, Dense,Multiply, Dot, Concatenate,Embedding
from tensorflow.keras import optimizers
from tensorflow.keras.callbacks import ModelCheckpoint
The point is that instead of using
from keras.layers import Reshape, Dropout, Dense,Multiply, Dot, Concatenate,Embedding
you need to add
from tensorflow.keras.layers import Reshape, Dropout, Dense,Multiply, Dot, Concatenate,Embedding

Starting from TensorFlow 2.0, only PyCharm versions > 2019.3 are able to recognise tensorflow and keras inside tensorflow (tensorflow.keras). Francois Chollet himself (author of Keras) recommends that everybody switches to tensorflow.keras in place of plain keras.
There is also one important mentioning here:
IMPORTANT NOTE FOR TF >= 2.0
There (this) is an ongoing issue with JetBrains (in fact from TensorFlow side), it seems that this error comes up from time to time (https://youtrack.jetbrains.com/issue/PY-53599).
It sometimes happens that PyCharm is not able to correctly import/recognize keras inside tensorflow or other imports.
Depending on Python + TF + PyCharm versions, you may have to alternate between the following import types:
from tensorflow.keras.models import Model
OR
from tensorflow.python.keras.models import Model

this worked for me in tensorflow==1.4.0
from tensorflow.python import keras

I have a similar problem importing those libs. I am using Anaconda Navigator 1.8.2 with Spyder 3.2.8.
My code is the following:
import matplotlib.pyplot as plt
import tensorflow as tf
import numpy as np
import math
#from tf.keras.models import Sequential # This does not work!
from tensorflow.python.keras.models import Sequential
from tensorflow.python.keras.layers import InputLayer, Input
from tensorflow.python.keras.layers import Reshape, MaxPooling2D
from tensorflow.python.keras.layers import Conv2D, Dense, Flatten
I get the following error:
from tensorflow.python.keras.models import Sequential
ModuleNotFoundError: No module named 'tensorflow.python.keras'
I solve this erasing tensorflow.python
With this code I solve the error:
import matplotlib.pyplot as plt
import tensorflow as tf
import numpy as np
import math
#from tf.keras.models import Sequential # This does not work!
from keras.models import Sequential
from keras.layers import InputLayer, Input
from keras.layers import Reshape, MaxPooling2D
from keras.layers import Conv2D, Dense, Flatten

I had the same problem with Tensorflow 2.0.0 in PyCharm. PyCharm did not recognize tensorflow.keras; I updated my PyCharm and the problem was resolved!

Related

How to solve "model has not yet been built" error in Keras?

I got this error while working with CNN:
This model has not yet been built. Build the model first by calling `build()` or by calling the model on a batch of data.
My code is below :
from tensorflow.keras.models import Sequential
from tensorflow.keras.layers import Dense,Dropout
from tensorflow.keras.layers import Conv2D
from tensorflow.keras.layers import Flatten
from tensorflow.keras.layers import MaxPooling2D
from tensorflow.keras.callbacks import TensorBoard
model=Sequential()
model.add(Conv2D(32,5,input_shape=(1,28,28),activation='relu',data_format='channels_last'))
model.add(MaxPooling2D(pool_size=(2,2),strides=2))
model.add(Flatten())
model.add(Conv2D(64,5,input_shape=(1,28,28),activation='relu',data_format='channels_last'))
model.add(MaxPooling2D(pool_size=2,strides=2))
model.add(Flatten())
model.add(Dense(units=32,activation='relu'))
model.add(Dropout(0.4))
model.add(Dense(units=1,activation='softmax'))
model.summary()
According to the error, run:
input_shape=(1,28,28)
model.build(input_shape)
model.summary()

Unable to import SGD and Adam from 'tensorflow.python.keras.optimizers'

Trying to run---
import tensorflow as tf
from tensorflow import keras
from tensorflow.python.keras.models import Sequential
from tensorflow.python.keras.layers import Flatten, Dense
from tensorflow.python.keras.optimizers import SGD, Adam
import numpy as np
print(tf.__version__)
I get this error---
---------------------------------------------------------------------------
ImportError Traceback (most recent call last)
<ipython-input-8-f05f8f753c47> in <module>()
4 from tensorflow.python.keras.models import Sequential
5 from tensorflow.python.keras.layers import Flatten, Dense
----> 6 from tensorflow.python.keras.optimizers import SGD, Adam
7
8 import numpy as np
ImportError: cannot import name 'SGD' from 'tensorflow.python.keras.optimizers' (/usr/local/lib/python3.7/dist-packages/tensorflow/python/keras/optimizers.py)
---------------------------------------------------------------------------
NOTE: If your import is failing due to a missing package, you can
manually install dependencies using either !pip or !apt.
---------------------------------------------------------------------------
I'm studying machine learning in Google Colab.
I pasted the example code and run it, and get error message.
I could find similar errors in Google, but I couldn't find anything to solve this problem.
I tried 'from tensorflow.keras.optimizers import SGD, Adam', 'from tf.keras.optimizers import SGD, Adam', and 'from keras.optimizers import SGD, Adam'.
But everything didn't work.
try this:
from tensorflow.python.keras.optimizer_v1 import SGD
You need to mention the exact updated alias name while importing the model(Sequential),layers (Flatten, Dense) and optimizers (SGD, Adam).
Please try again using the below code in new Google Colab notebook.
import tensorflow as tf
from tensorflow import keras
from tensorflow.keras.models import Sequential #removed python from each layer
from tensorflow.keras.layers import Flatten, Dense
from tensorflow.keras.optimizers import SGD, Adam
import numpy as np
print(tf.__version__)
Output:
2.8.2

'Adam' object has no attribute 'Adam'

This is how I imported the modules
from keras.layers import Conv2D, BatchNormalization, Activation
from keras.models import Model, Input
from keras import optimizer_v2
from keras.optimizer_v2 import adam
import keras.backend as K
But I'm getting this Error
I tried using adam = adam(learning_rate= 0.00001) but it doesn't work either
I tried the following steps and it worked
import keras
adam = keras.optimizer_v2.adam.Adam(learning_rate= 0.00001)
deblur_CNN.compile(optimizer= adam, loss= 'mean_squared_error')
deblur_CNN.load_weights('/content/drive/My Drive/Colab Notebooks/FDmodels/deblur_cnn_weights.h5')
after specifying the exact path to the adam.Adam module it worked fine.
Still, if anyone wants to add something can add.

AttributeError: 'Node' object has no attribute 'output_masks'

I use Keras pretrained model VGG16. The problem is that after configuring tensorflow to use the GPU I get an error that I didn't have before when using the CPU.
The error is the following one:
Traceback (most recent call last):
File "/home/guillaume/Documents/Allianz/ConstatOrNotConstatv3/train_network.py", line 109, in <module>
model = LeNet.build(width=100, height=100, depth=3, classes=5)
File "/home/guillaume/Documents/Allianz/ConstatOrNotConstatv3/lenet.py", line 39, in build
output = model(pretrainedOutput)
File "/usr/local/lib/python3.6/dist-packages/keras/engine/base_layer.py", line 443, in __call__
previous_mask = _collect_previous_mask(inputs)
File "/usr/local/lib/python3.6/dist-packages/keras/engine/base_layer.py", line 1311, in _collect_previous_mask
mask = node.output_masks[tensor_index]
AttributeError: 'Node' object has no attribute 'output_masks'
I get it after executing this code :
pretrained_model = VGG16(
include_top=False,
input_shape=(height, width, depth),
weights='imagenet'
)
for layer in pretrained_model.layers:
layer.trainable = False
model = Sequential()
# first (and only) set of FC => RELU layers
model.add(Flatten())
model.add(Dense(200, activation='relu'))
model.add(Dropout(0.5))
model.add(BatchNormalization())
model.add(Dense(400, activation='relu'))
model.add(Dropout(0.5))
model.add(BatchNormalization())
# softmax classifier
model.add(Dense(classes,activation='softmax'))
pretrainedInput = pretrained_model.input
pretrainedOutput = pretrained_model.output
output = model(pretrainedOutput)
model = Model(pretrainedInput, output)
EDIT1 : I've got keras (2.2.2) and tensorflow(1.10.0rc1). I've also tried on keras 2.2.0 and same error. The thing is that the python environment I use works on others non-pretrained NN.
EDIT2 : I'm able to connect two homemade models. It's only whith the pretrained ones there is a problem and not only VGG16.
You're likely importing tf.keras.layers or tf.keras.applications or other keras modules from tensorflow.keras, and mixing these objects with objects from the "pure" keras package, which is not compatible, based upon version, etc.
I recommend seeing if you can import and run everything from the "pure" keras modules; don't use tf.keras while debugging, as they're not necessarily compatible. I had the same problem, and this solution is working for me.
I had the same error when I import keras and tenerflow.keras simultaneously:
from tensorflow.keras.optimizers import Adam
from keras.utils import multi_gpu_model
I solved this problem after changing the code into:
from tensorflow.keras.optimizers import Adam
from tensorflow.keras.utils import multi_gpu_model
I had a similar issue, but with different architecture. As people suggested, it's important not to mix keras with tensorflow.keras, so try swapping code like:
from keras.preprocessing import image
from keras.models import Model
from keras.layers import Dense, GlobalAveragePooling2D
from keras import backend as K
to:
from tensorflow.keras.preprocessing import image
from tensorflow.keras.models import Model
from tensorflow.keras.layers import Dense, GlobalAveragePooling2D
from tensorflow.keras import backend as K
Also make sure, you don't use keras.something inside your code (not only imports) as well, hope it helps : )
Also, I used Keras 2.2.4 with tensorflow 1.10.0
if you are importing VGG16 from tensorflow.keras.applications.vgg16 then import all models from tensorflow
from tensorflow.keras.applications.vgg16 import VGG16
from tensorflow.keras.models import Sequential
from tensorflow.keras.layers import Dense, Dropout, Flatten
elif you are importing from keras like:
from keras.applications.vgg16 import VGG16
then use model import from keras
from keras.models import Sequential
from keras.layers import Dense

How to import keras.engine.topology in Tensorflow?

I want to import keras.engine.topology in Tensorflow.
I used to add the word tensorflow at the beginning of every Keras import if I want to use the Tensorflow version of Keras.
For example: instead of writing:
from keras.layers import Dense, Dropout, Input
I just write the following code and it works fine :
from tensorflow.keras.layers import Dense, Dropout, Input
But that's not the case for this specific import:
from tensorflow.keras.engine.topology import Layer, InputSpec
And I m getting the following error message:
No module named 'tensorflow.keras.engine'
You can import Layer and InputSpec from TensorFlow as follows:
from tensorflow.python.keras.layers import Layer, InputSpec
UPDATE: 30/10/2019
from tensorflow.keras.layers import Layer, InputSpec
In the keras_vggface/models.py file, change the import from:
from keras.engine.topology import get_source_inputs
to:
from keras.utils.layer_utils import get_source_inputs
In order to import keras.engine you may try using:
import tensorflow.python.keras.engine
Note: But from tensorflow.python.keras.engine you cannot import topology
I solved this issue by changing the import from from keras.engine.topology import get_source_inputs to from keras.utils.layer_utils import get_source_inputs

Categories