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

前端 未结 5 672
一生所求
一生所求 2021-01-05 11:26

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

model = InceptionV3(weights=\'imagenet\')


        
相关标签:
5条回答
  • 2021-01-05 11:35

    In my case, replacing

    from keras.models import models

    with:

    from tensorflow.keras.models import models

    in my script, fixed this problem.

    0 讨论(0)
  • 2021-01-05 11:36

    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.

    0 讨论(0)
  • 2021-01-05 11:47

    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/

    0 讨论(0)
  • 2021-01-05 11:48

    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.

    0 讨论(0)
  • 2021-01-05 11:57

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

    Working example:

    from tensorflow.python.keras.models import Sequential
    
    0 讨论(0)
提交回复
热议问题