apt45
apt45

Reputation: 433

ImportError: cannot import name 'backend'

I encounter an ImportError when importing backend from keras

from keras import backend

The output is

Using TensorFlow backend.
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "/Library/Frameworks/Python.framework/Versions/3.5/lib/python3.5/site-packages/keras/__init__.py", line 3, in <module>
    from . import utils
  File "/Library/Frameworks/Python.framework/Versions/3.5/lib/python3.5/site-packages/keras/utils/__init__.py", line 6, in <module>
    from . import conv_utils
  File "/Library/Frameworks/Python.framework/Versions/3.5/lib/python3.5/site-packages/keras/utils/conv_utils.py", line 9, in <module>
    from .. import backend as K
  File "/Library/Frameworks/Python.framework/Versions/3.5/lib/python3.5/site-packages/keras/backend/__init__.py", line 89, in <module>
    from .tensorflow_backend import *
  File "/Library/Frameworks/Python.framework/Versions/3.5/lib/python3.5/site-packages/keras/backend/tensorflow_backend.py", line 5, in <module>
    import tensorflow as tf
  File "/Library/Frameworks/Python.framework/Versions/3.5/lib/python3.5/site-packages/tensorflow/__init__.py", line 24, in <module>
    from tensorflow.python import pywrap_tensorflow  # pylint: disable=unused-import
  File "/Library/Frameworks/Python.framework/Versions/3.5/lib/python3.5/site-packages/tensorflow/python/__init__.py", line 88, in <module>
    from tensorflow.python import keras
  File "/Library/Frameworks/Python.framework/Versions/3.5/lib/python3.5/site-packages/tensorflow/python/keras/__init__.py", line 25, in <module>
    from tensorflow.python.keras import applications
  File "/Library/Frameworks/Python.framework/Versions/3.5/lib/python3.5/site-packages/tensorflow/python/keras/applications/__init__.py", line 74, in <module>
    from tensorflow.python.keras.applications.densenet import DenseNet121
  File "/Library/Frameworks/Python.framework/Versions/3.5/lib/python3.5/site-packages/tensorflow/python/keras/applications/densenet/__init__.py", line 21, in <module>
    from tensorflow.python.keras._impl.keras.applications.densenet import decode_predictions
  File "/Library/Frameworks/Python.framework/Versions/3.5/lib/python3.5/site-packages/tensorflow/python/keras/_impl/keras/applications/densenet.py", line 30, in     module>
    from tensorflow.python.keras._impl.keras import backend as K
ImportError: cannot import name 'backend'

I don't understand which may be the problem.

Upvotes: 3

Views: 20711

Answers (2)

mouserat
mouserat

Reputation: 1985

import tensorflow
from tensorflow.compat.v1.keras.backend import set_session

this worked in tensorflow==2.2

Upvotes: 1

apt45
apt45

Reputation: 433

I have solved by uninstalling keras and tensorflow, removing their directories in the site-packages folder and then installing them again.

pip3 uninstall tensorflow
pip3 uninstall keras
rm -r /Library/Frameworks/Python.framework/Versions/3.5/lib/python3.5/site-packages/keras*
rm -r /Library/Frameworks/Python.framework/Versions/3.5/lib/python3.5/site-packages/Keras*
rm -r /Library/Frameworks/Python.framework/Versions/3.5/lib/python3.5/site-packages/tensorflow*
pip3 install tensorflow
pip3 install keras

Upvotes: 6

Related Questions