17

While training the yolov3 framework, there's always this module error

I have tried reinstalling keras and tensorflow, and the version of keras is 2.3.0 and the version of tensorflow is 1.14.0.

Traceback (most recent call last):
  File "train.py", line 6, in <module>
    import keras.backend as K
  File "F:\Anacoda\lib\site-packages\keras\__init__.py", line 3, in <module>
    from . import utils
  File "F:\Anacoda\lib\site-packages\keras\utils\__init__.py", line 27, in <module>
    from .multi_gpu_utils import multi_gpu_model
  File "F:\Anacoda\lib\site-packages\keras\utils\multi_gpu_utils.py", line 7, in <module>
    from ..layers.merge import concatenate
  File "F:\Anacoda\lib\site-packages\keras\layers\__init__.py", line 4, in <module>
    from ..engine.base_layer import Layer
  File "F:\Anacoda\lib\site-packages\keras\engine\__init__.py", line 8, in <module>
    from .training import Model
  File "F:\Anacoda\lib\site-packages\keras\engine\training.py", line 21, in <module>
    from . import training_arrays
  File "F:\Anacoda\lib\site-packages\keras\engine\training_arrays.py", line 14, in <module>
    from .. import callbacks as cbks
  File "F:\Anacoda\lib\site-packages\keras\callbacks\__init__.py", line 19, in <module>
    if K.backend() == 'tensorflow' and not K.tensorflow_backend._is_tf_1():
AttributeError: module 'keras.backend.tensorflow_backend' has no attribute '_is_tf_1'
Vergilben
  • 173
  • 1
  • 1
  • 4

6 Answers6

13

I fix this problem by replacing keras.XXX to tensorflow.keras.XXX

try replace

import keras.backend as K

to

import tensorflow.keras.backend as K
cho alan
  • 131
  • 3
  • Hi! Thank u for answering my question! I really want to know if you're running code in windows or linux? And...what's the versions of your keras and tensorflow? Thanks~ – Vergilben Sep 27 '19 at 08:14
  • worked for me on Linux(Ubuntu), but has to be OS independent I guess. – Omid S. Oct 15 '20 at 16:07
  • same problem: AttributeError: module 'tensorflow.keras.backend' has no attribute 'tensorflow_backend' – skan Jan 28 '23 at 17:42
9

import this:

import tensorflow as tf

then use tf.compat.v1.keras.backend. as prefix of your desired attribute

Kaveh
  • 4,618
  • 2
  • 20
  • 33
Hridoy_089
  • 318
  • 3
  • 11
3

I had the same error and tried installing and uninstalling. In the end, I found that the library was not actually installed correctly.

I went through each library in my:

C:\Users\MyName\AppData\Local\Packages\PythonSoftwareFoundation.Python.3.8_qbz5n2kfra8p0\LocalCache\local-packages\Python38\site-packages\

I tracked down the file in within the site-packages in Keras, which was calling from the Tensorflow library, which was calling from another folder. I found the final folder had the get_session(), but this was not being called in. When I checked the directory, I found that get_session wasn't being loaded in. Within the file directory /tensorflow/keras/backend.py it was importing everything, but missed out the get_session.

To fix this I added this line:

from tensorflow.python.keras.backend import get_session

Then saved it. The next time I ran my code it was fine. I would suggest doing similar with your files, locating where the attribute is called in and making sure it is being imported.

SHEP AI
  • 51
  • 4
2
pip3 uninstall keras
pip3 install keras --upgrade

https://github.com/keras-team/keras/issues/13352

RobC
  • 22,977
  • 20
  • 73
  • 80
0

installing TensorFlow 1.14.0 + Keras 2.2.5 on Python 3.6 fix this

-3

make sure your keras version is right. if your backbend is tensorflow,you can

import tensorflow as tf
    print(tf.VERSION)
    print(tf.keras.__version__)

get the right version of keras,then install this version,I fix this problem by this way,i hope my answer can help you.

SOTA
  • 1
  • 1
    `AttributeError: module 'tensorflow' has no attribute 'VERSION'`, use `print(tf.__version__)` instead – Lucas Jan 18 '20 at 14:29