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

17,466

Solution 1

Change

Import keras.<something>.<something>

to

Import tensorflow.keras.<something>.<something>

where "something" refers to the module you want to import. It worked for me.

Solution 2

Another cause due to which this is happening is that in tensorflow_backend.py

located in : lib/python3.6/site-packages/keras/backend/

uses tf.compat.v1.get_default_graph for obtaining graph

instead of tf.get_default_graph.

By replacing this in the directory this problem can be solved successfully.

Solution 3

Keras integrated into TensorFlow 2.0

The article below cleared this up for me. Key points are:

  1. Keras is included in the TensorFlow 2.0 package
  2. So no need to install the stand-alone Keras package in your environment
  3. And now the fore-mentioned solutions of using "from tensorflow.keras…" make sense.

After recognizing that and making the change, my code samples work with some minor changes here and there. https://www.pyimagesearch.com/2019/10/21/keras-vs-tf-keras-whats-the-difference-in-tensorflow-2-0/

Solution 4

I fixed this problem by replacing tensorflow.keras.* to tensorflow.python.keras.*

Working example:

from tensorflow.python.keras.models import Sequential
Share:
17,466
Ayush Kushwaha
Author by

Ayush Kushwaha

currently pursuing a Computer Science and Engineering from National Institute of Technology Allahabad. Newbie but familiar with networking, operating system, software, database, programming languages,data structure and algorithms and Interested in machine learning. Objective: To become a Data Engineer

Updated on June 21, 2022

Comments

  • Ayush Kushwaha
    Ayush Kushwaha almost 2 years

    I am doing some task related to image captioning and I have loaded the weights of inception model like this

    model = InceptionV3(weights='imagenet')
    

    But I am getting error like this:

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

    What should I do? Please help. Here is the full output of above code.

    1 . --------------------------------------------------------------------------- AttributeError Traceback (most recent call last) in () 1 # Load the inception v3 model ----> 2 model = InceptionV3(include_top=True,weights='imagenet') 3 # InceptionV3(weights='imagenet')

    ~/anaconda3/lib/python3.6/site-packages/keras/applications/__init__.py
    in wrapper(*args, **kwargs)
         26             kwargs['models'] = models
         27             kwargs['utils'] = utils
    ---> 28         return base_fun(*args, **kwargs)
         29 
         30     return wrapper
    
    ~/anaconda3/lib/python3.6/site-packages/keras/applications/inception_v3.py
    in InceptionV3(*args, **kwargs)
          9 @keras_modules_injection
         10 def InceptionV3(*args, **kwargs):
    ---> 11     return inception_v3.InceptionV3(*args, **kwargs)
         12 
         13 
    
    ~/anaconda3/lib/python3.6/site-packages/keras_applications/inception_v3.py
    in InceptionV3(include_top, weights, input_tensor, input_shape,
    pooling, classes, **kwargs)
        155 
        156     if input_tensor is None:
    --> 157         img_input = layers.Input(shape=input_shape)
        158     else:
        159         if not backend.is_keras_tensor(input_tensor):
    
    ~/anaconda3/lib/python3.6/site-packages/keras/engine/input_layer.py
    in Input(shape, batch_shape, name, dtype, sparse, tensor)
        176                              name=name, dtype=dtype,
        177                              sparse=sparse,
    --> 178                              input_tensor=tensor)
        179     # Return tensor including _keras_shape and _keras_history.
        180     # Note that in this case train_output and test_output are the same pointer.
    
    ~/anaconda3/lib/python3.6/site-packages/keras/legacy/interfaces.py
    in wrapper(*args, **kwargs)
         89                 warnings.warn('Update your `' + object_name + '` call to the ' +
         90                               'Keras 2 API: ' + signature, stacklevel=2)
    ---> 91             return func(*args, **kwargs)
         92         wrapper._original_function = func
         93         return wrapper
    
    ~/anaconda3/lib/python3.6/site-packages/keras/engine/input_layer.py
    in __init__(self, input_shape, batch_size, batch_input_shape, dtype,
    input_tensor, sparse, name)
         37         if not name:
         38             prefix = 'input'
    ---> 39             name = prefix + '_' + str(K.get_uid(prefix))
         40         super(InputLayer, self).__init__(dtype=dtype, name=name)
         41 
    
    ~/anaconda3/lib/python3.6/site-packages/keras/backend/tensorflow_backend.py
    in get_uid(prefix)
         72     """
         73     global _GRAPH_UID_DICTS
    ---> 74     graph = tf.get_default_graph()
         75     if graph not in _GRAPH_UID_DICTS:
         76         _GRAPH_UID_DICTS[graph] = defaultdict(int)
    
    AttributeError: module 'tensorflow' has no attribute
    'get_default_graph'