Keras: AttributeError: module 'tensorflow' has no attribute 'get_default_session'

Created on 15 Oct 2019  路  10Comments  路  Source: keras-team/keras

System information

  • Have I written custom code (as opposed to using example directory): no
  • OS Platform and Distribution: 8.04.3 LTS (Bionic Beaver)
  • TensorFlow backend (yes / no): yes
  • TensorFlow version: 2.0.0 (gpu)
  • Keras version: 2.2.5
  • Python version: 3.6.8
  • CUDA/cuDNN version: 10.1
  • GPU model and memory: Tesla K80 (memory limit 11330115994).

Describe the current behavior

I get the following error

AttributeError                            Traceback (most recent call last)
<ipython-input-17-3d00d838479b> in <module>()
      1 from keras import backend as K
----> 2 K.tensorflow_backend._get_available_gpus()

1 frames
/usr/local/lib/python3.6/dist-packages/keras/backend/tensorflow_backend.py in get_session()
    188     global _SESSION
    189 
--> 190     default_session = tf.get_default_session()
    191 
    192     if default_session is not None:

AttributeError: module 'tensorflow' has no attribute 'get_default_session'

when executing the following code

from keras import backend as K
K.tensorflow_backend._get_available_gpus()

or

from tensorflow.keras import backend
backend._get_available_gpus()

AFAIK, sessions should still be supported in TF 2.0. Even though _get_available_gpus() is considered a private method, given the initial _, it does fail with TF 2.0, so I think this is a bug.

Describe the expected behavior

No error and I get the available GPUs. In general, I would like to make sure Keras uses GPU (with TF 2.0). Any reliable method to achieve this?

Code to reproduce the issue

See above.

Other info / logs

See above.

tensorflow support

All 10 comments

I think the following code will solve the problem
pip install --force-reinstall tensorflow==1.15

@burakyldrm Of course, I don't want to use any other TensorFlow version other than 2.0.0, as I said above.

I was having the same problem, but if you modify "tf.get_default_session" to "tf.compat.v1.get_default_session", it should work, at least until the next TensorFlow update.

@Rogan-Clark Have you read my post above? The only lines of code I had were

from keras import backend as K
K.tensorflow_backend._get_available_gpus()

You have to go into the /usr/local/lib/python3.6/dist-packages/keras/backend/tensorflow_backend.py file and edit it in that, I think.

@Rogan-Clark Well, I would like to use TensorFlow and Keras out of the box. I don't want to mess with their implementation.

Hi nbro, did you solve your issue? (I have exactly the same one)
thanks

I also run into the same problem. It would be appreciated if the solving method could be informed.
Thanks

Same issue here, have also tried to go into the source files with no luck. But I can't find "tf_keras_backend" defined anywhere.

`"""Get a list of available gpu devices (formatted as strings).

# Returns
    A list of available GPU devices.
"""
global _LOCAL_DEVICES
if _LOCAL_DEVICES is None:
    if _is_tf_1():
        **devices = get_session().list_devices()**
        #Source of error^^^
        _LOCAL_DEVICES = [x.name for x in devices]
    else:
        _LOCAL_DEVICES = tf.config.experimental_list_devices()
return [x for x in _LOCAL_DEVICES if 'device:gpu' in x.lower()]

`

You have to go into the /usr/local/lib/python3.6/dist-packages/keras/backend/tensorflow_backend.py file and edit it in that, I think.
^^See above @Rogan-Clark

I have the same issue while running

# freeze graph frozen_graph = freeze_session(K.get_session(), output_names=[out.op.name for out in model.outputs])

I imported the correct library I guess and load an h5 model I previously made using Keras and TF2

`from keras import backend as K

from tensorflow.keras.models import load_model

load model

model = load_model('C:/Python36/face-mask-detector/mask_detector.model')`

haha T_T

Was this page helpful?
0 / 5 - 0 ratings

Related issues

LuCeHe picture LuCeHe  路  3Comments

farizrahman4u picture farizrahman4u  路  3Comments

snakeztc picture snakeztc  路  3Comments

nryant picture nryant  路  3Comments

kylemcdonald picture kylemcdonald  路  3Comments