Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to fix ' module 'keras.backend.tensorflow_backend' has no attribute '_is_tf_1''

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'
like image 562
Vergilben Avatar asked Sep 22 '19 08:09

Vergilben


People also ask

Is it possible to integrate keras with TensorFlow?

Note: From TF 2.0, onwards Keras are integrated with Tensorflow as tf.keras, please use this module tf.keras.backend Thanks for contributing an answer to Stack Overflow!

How to choose a backend for keras?

When it comes to choosing a backend for Keras you need to consider a few aspects. The first is the popularity and therefore the probability that a given library will continue to be updated and supported in the future.

Does keras run on the GPU or CPU?

Although the code runs when I try to run it using Keras backend without using the TensorFlow, it only runs on the CPU, not GPU. Join ResearchGate to ask questions, get input, and advance your work. Can you help by adding an answer?

How to choose between TensorFlow and Theano?

Secondly, you need to consider the functionality of a given library. While Theano is just as easy to use as TensorFlow out-of-the-box (in terms of Keras backends), TensorFlow allows for a more architecture agnostic deployment.


Video Answer


2 Answers

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
like image 190
cho alan Avatar answered Sep 20 '22 16:09

cho alan


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.

like image 23
SHEP AI Avatar answered Sep 16 '22 16:09

SHEP AI