8

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'
4
  • Do you have a file called tensorflow.py in your codebase? How did you install tensorflow? Commented Mar 19, 2019 at 9:07
  • Are you using tensorflow 2.0? Are you in eager mode? Commented Mar 19, 2019 at 10:07
  • 1
    @Vlad thanks for the reply but i resolved this issue after updating keras,tensorflow,and conda 's all packages . Commented Mar 19, 2019 at 21:13
  • @MatiasValdenegro thanks for your reply but i resolved this issue after updating keras,tensorflow,and conda 's all packages and i don't have file called tensorflow.py Commented Mar 19, 2019 at 21:15

6 Answers 6

13

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.

Sign up to request clarification or add additional context in comments.

2 Comments

i don't know above answer work or not but i updated my Environment and it worked i think there was an issue related to compatibility.
Worked for me, using tensorflow 2
3

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.

Comments

1

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/

Comments

1

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

Working example:

from tensorflow.python.keras.models import Sequential

Comments

1

Two Steps solved this issue for me in Google Colabs

First Step

Change

from keras.something import something

To

from tensorflow.keras.something import something

Second Step

Use

tf.compat.v1.get_default_graph

instead of tf.get_default_graph

Reason

https://www.pyimagesearch.com/2019/10/21/keras-vs-tf-keras-whats-the-difference-in-tensorflow-2-0/

Comments

0

In my case, replacing

from keras.models import models

with:

from tensorflow.keras.models import models

in my script, fixed this problem.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.