Mask_rcnn: Tensor is not an element in the graph.

Created on 25 May 2018  路  10Comments  路  Source: matterport/Mask_RCNN

When I ran the demo.ipynb, I got an error which said "ValueError: Tensor Tensor("mrcnn_detection/Reshape_1:0", shape=(1, 100, 6), dtype=float32) is not an element of this graph"

Most helpful comment

I don't know what the demo does, but I've come across this error too. I've been able to prevent it in one case by calling clear_session().
from keras.backend import clear_session
then in you code
## Code where you train or use a model ##
clear_session()
Hope it works for you but I am curious to know what is the root cause of this. I've got to load weights and create another model each time I do inference because of that error.
If anyone who knows better, go look at line 3668 of ops.py in tensorflow/python/framework, that's where the conditions is invoked. The graph isn't itself.

EDIT:
I found joaospinto's answer and it worked for me here : https://github.com/keras-team/keras/issues/6462
It seems that you got to call model._make_predict_function() before using model.predict()

All 10 comments

Hi, I ran into the same problem. Have you found a way around this?

I don't know what the demo does, but I've come across this error too. I've been able to prevent it in one case by calling clear_session().
from keras.backend import clear_session
then in you code
## Code where you train or use a model ##
clear_session()
Hope it works for you but I am curious to know what is the root cause of this. I've got to load weights and create another model each time I do inference because of that error.
If anyone who knows better, go look at line 3668 of ops.py in tensorflow/python/framework, that's where the conditions is invoked. The graph isn't itself.

EDIT:
I found joaospinto's answer and it worked for me here : https://github.com/keras-team/keras/issues/6462
It seems that you got to call model._make_predict_function() before using model.predict()

@ItsTehStory
Thank you for sharing your fix.

Strangely, the same error persists despite calling model._make_predict_function()before model.predict(). The model in both function calls are assumed to be from keras.

May I confirm the following?

Using the context of this repository, should we include the line:
self.keras_model._make_predict_function() above
self.keras_model.predict([molded_images, image_metas, anchors], verbose=0) in line 2532 of Mask_RCNN/mrcnn/model.py

@cardboardcode
That's weird.... I could think of one thing that could influence: keras version. When I wrote my answer 4 months ago, keras for my Ubuntu station was v2.1.6 I think. This is a very long shot... but I did have some problems with my keras version at some point during the summer.
My code looked something like that too:

# Create model object in inference mode.
model = modellib.MaskRCNN(mode="inference", model_dir=MODEL_DIR, config=config)

# Load weights
model.load_weights(WEIGHT_PATH, by_name=True)
model.keras_model._make_predict_function()

#Then in a method defined (let's say foo here)
def foo:
    # Run detection
    results = model.detect([image], verbose=1)
    # Where image is a numpy array that was read from an image

Anyway, hope that can help you in any way!

@ItsTehStory
Thank you for the prompt response.

Running pip freeze | grep Keras reveals:

Keras==2.2.2

This may be the issue but I will need to investigate. I will update again once I have done so.

https://github.com/tensorflow/tensorflow/issues/14356, solve the issue.
def load_model():
global model
model = ResNet50(weights="imagenet")
# this is key : save the graph after loading the model
global graph
graph = tf.get_default_graph()
While predicting, use the same graph

with graph.as_default():
preds = model.predict(image)
#... etc

I had tried both clear_session() and global graph two ways, but I still met Tensor Tensor("mrcnn_detection/Reshape_1:0", shape=(1, 100, 6), dtype=float32, device=/device:GPU:0) is not an element of this graph., my keras version is 2.2.4, Anybody could help me? pls.

tensorflow/tensorflow#14356, solve the issue.
def load_model():
global model
model = ResNet50(weights="imagenet")

this is key : save the graph after loading the model

global graph
graph = tf.get_default_graph()
While predicting, use the same graph

with graph.as_default():
preds = model.predict(image)
#... etc

This worked! 馃槀

tensorflow/tensorflow#14356, solve the issue.
def load_model():
global model
model = ResNet50(weights="imagenet")

this is key : save the graph after loading the model

global graph
graph = tf.get_default_graph()
While predicting, use the same graph

with graph.as_default():
preds = model.predict(image)
#... etc

hi can you please help in code that you have provides should I write a new function load_weight because I am not able to find that function in model.py
Thanks in advance

@cardboardcode
That's weird.... I could think of one thing that could influence: keras version. When I wrote my answer 4 months ago, keras for my Ubuntu station was v2.1.6 I think. This is a very long shot... but I did have some problems with my keras version at some point during the summer.
My code looked something like that too:

# Create model object in inference mode.
model = modellib.MaskRCNN(mode="inference", model_dir=MODEL_DIR, config=config)

# Load weights
model.load_weights(WEIGHT_PATH, by_name=True)
model.keras_model._make_predict_function()

#Then in a method defined (let's say foo here)
def foo:
    # Run detection
    results = model.detect([image], verbose=1)
    # Where image is a numpy array that was read from an image

Anyway, hope that can help you in any way!

This worked for me. Thanks a lot. Keep on posting..!!

Was this page helpful?
0 / 5 - 0 ratings