Search code examples
pythontensorflowkeraskeras-layerkeras-2

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


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'

Solution

  • 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.