12

I am trying to run a script, but I struggle already at the imports. This import

from keras.preprocessing.image import save_img

raises the following error:

AttributeError: module 'tensorflow' has no attribute 'name_scope'.

I am using the following packages.

Keras                     2.2.2,                     
Keras-Applications        1.0.4,                   
Keras-Preprocessing       1.0.2,                   
tensorflow                1.9.0,                     
tensorflow-gpu            1.9.0                

7 Answers 7

15

I was unable to reproduce with the same versions of the keras and tensorflow, reinstalling keras and tensorflow, may solve the issue, please use commands below:

pip install --upgrade pip setuptools wheel
pip install -I tensorflow
pip install -I keras

NOTE: The -I parameter stands for ignore installed package.

Sign up to request clarification or add additional context in comments.

5 Comments

Just to add to this answer, name_scope should indeed be included with tf 1.9.0 so it seems that there is something fishy with the installation.
@VegardKT thanks, yes, it's available for sure, I bet it's installation issues to.
But Why? I was using this environment for a week now to build models in keras. Maybe because I installed tensorflow serving today?
I get the error "ImportError: Keras requires TensorFlow 2.2 or higher. Install TensorFlow via pip install tensorflow" when following these instructions.
@user3367130 I think you have installed older tensorflow version at some point, you can check versions with pip freeze
4

For everyone who use Tensorflow 2.0 and stumble upon this question with the same error, like I did: I solved it by changing the imports from keras.xxx to tensorflow.keras.xxx

Comments

1

I also encountered this same problem when I stopped my IDE while executing. Restarting my IDE works for me. Just save your program and restart IDE. Hope it will work for you as well.

Comments

0

As Andriy Ivaneyko mentioned above, reinstalling tensorflow helps. I'm not sure why, but installing tensorflow-serving-api breaks something somewhere along the way. We solved this by running:

pip install --force-reinstall tensorflow

Note that this applies to both tensorflow and tensorflow-gpu installations. Specifically, the above command will fix this problem in situations where you're specifically using tensorlfow-gpu. tensorflow-serving-api installs regular tensorflow if it's not already installed.

Comments

0

I encountered this same bug and reinstalling tensorflow made no difference, and this caused me some headscratching.

Eventually I noticed that my IDE autocomplete had added the following line to my code:

from tensorflow_core.python.keras.callbacks import EarlyStopping

It seems that directly referencing tensorflow_core.python will break tensorflow.

Replacing this with the normal tensorflow import solved the problem!

from tensorflow.keras.callbacks import EarlyStopping

Comments

0

My IDE offered me two different import paths

keras

or

tensorflow_core.python.keras

In my example I could either import like this:

from keras.layers import Dense, Dropout, LSTM, Input, Activation
from keras import optimizers, Model

or like that:

from tensorflow_core.python.keras import Input, Model, optimizers
from tensorflow_core.python.keras.layers import LSTM, Dropout, Dense

Mixing up tensorflow_core.python.keras and plain keras led to the problem in my case. After I imported everything directly from keras and keras.layers, it worked for me.

Comments

0

My tensorflow version is 2.1, and I found my tensorflow-estimator version is 2.2

My fix is to downgrade the estimator to the same version

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.