title: Tensor Tensor("predictions/Softmax:0", shape=(?, 1000), dtype=float32) is not an element of this graph
body: <p>I am trying to follow a <a href="https://machinelearningmastery.com/use-pre-trained-vgg-model-classify-objects-photographs/" rel="nofollow noreferrer">simple tutorial</a> on how to use a pre-trained VGG model for image classification. The code which I have: </p>
  
  <pre><code>from keras.applications.vgg16 import VGG16
  from keras.preprocessing.image import load_img, img_to_array
  from keras.applications.vgg16 import preprocess_input, decode_predictions
  
  import numpy as np
  
  class KerasModel(object):
      def __init__(self):
          self.model = VGG16()
      def evaluate(self):
          image = load_img('mug.jpg', target_size=(224,224))
          image = img_to_array(image)
          image = image.reshape((1, image.shape[0], image.shape[1], image.shape[2]))
          image = preprocess_input(image)
          yhat = self.model.predict(image)
          label = decode_predictions(yhat)
          label = label[0][0]
          return ('%s (%.2f%%)' % (label[1]), label[2]*100)
  </code></pre>
  
  <p>This gives the error: Tensor Tensor("predictions/Softmax:0", shape=(?, 1000), dtype=float32) is not an element of this graph.</p>
  
  <p>After some searching for this error I got to this code: </p>
  
  <pre><code>from keras.applications.vgg16 import VGG16
  from keras.preprocessing.image import load_img, img_to_array
  from keras.applications.vgg16 import preprocess_input, decode_predictions
  
  import numpy as np
  
  import tensorflow as tf
  graph = tf.get_default_graph()
  
  
  class KerasModel(object):
      def __init__(self):
          self.model = VGG16()
      def evaluate(self):
          image = load_img('mug.jpg', target_size=(224,224))
          image = img_to_array(image)
          image = image.reshape((1, image.shape[0], image.shape[1], image.shape[2]))
          image = preprocess_input(image)
          with graph.as_default():
              yhat = self.model.predict(image)
          label = decode_predictions(yhat)
          label = label[0][0]
          return ('%s (%.2f%%)' % (label[1]), label[2]*100)
  </code></pre>
  
  <p>But this still results in the same error. Could someone please help me out? I don't understand what I am doing wrong because the tutorial seems to work for everyone. </p>
  
  <p>Model summary:</p>
  
  <pre><code> _________________________________________________________________
  xvision | Layer (type)                 Output Shape              Param #   
  xvision | =================================================================
  xvision | input_1 (InputLayer)         (None, 224, 224, 3)       0         
  xvision | _________________________________________________________________
  xvision | block1_conv1 (Conv2D)        (None, 224, 224, 64)      1792      
  xvision | _________________________________________________________________
  xvision | block1_conv2 (Conv2D)        (None, 224, 224, 64)      36928     
  xvision | _________________________________________________________________
  xvision | block1_pool (MaxPooling2D)   (None, 112, 112, 64)      0         
  xvision | _________________________________________________________________
  xvision | block2_conv1 (Conv2D)        (None, 112, 112, 128)     73856     
  xvision | _________________________________________________________________
  xvision | block2_conv2 (Conv2D)        (None, 112, 112, 128)     147584    
  xvision | _________________________________________________________________
  xvision | block2_pool (MaxPooling2D)   (None, 56, 56, 128)       0         
  xvision | _________________________________________________________________
  xvision | block3_conv1 (Conv2D)        (None, 56, 56, 256)       295168    
  xvision | _________________________________________________________________
  xvision | block3_conv2 (Conv2D)        (None, 56, 56, 256)       590080    
  xvision | _________________________________________________________________
  xvision | block3_conv3 (Conv2D)        (None, 56, 56, 256)       590080    
  xvision | _________________________________________________________________
  xvision | block3_pool (MaxPooling2D)   (None, 28, 28, 256)       0         
  xvision | _________________________________________________________________
  xvision | block4_conv1 (Conv2D)        (None, 28, 28, 512)       1180160   
  xvision | _________________________________________________________________
  xvision | block4_conv2 (Conv2D)        (None, 28, 28, 512)       2359808   
  xvision | _________________________________________________________________
  xvision | block4_conv3 (Conv2D)        (None, 28, 28, 512)       2359808   
  xvision | _________________________________________________________________
  xvision | block4_pool (MaxPooling2D)   (None, 14, 14, 512)       0         
  xvision | _________________________________________________________________
  xvision | block5_conv1 (Conv2D)        (None, 14, 14, 512)       2359808   
  xvision | _________________________________________________________________
  xvision | block5_conv2 (Conv2D)        (None, 14, 14, 512)       2359808   
  xvision | _________________________________________________________________
  xvision | block5_conv3 (Conv2D)        (None, 14, 14, 512)       2359808   
  xvision | _________________________________________________________________
  xvision | block5_pool (MaxPooling2D)   (None, 7, 7, 512)         0         
  xvision | _________________________________________________________________
  xvision | flatten (Flatten)            (None, 25088)             0         
  xvision | _________________________________________________________________
  xvision | fc1 (Dense)                  (None, 4096)              102764544 
  xvision | _________________________________________________________________
  xvision | fc2 (Dense)                  (None, 4096)              16781312  
  xvision | _________________________________________________________________
  xvision | predictions (Dense)          (None, 1000)              4097000   
  xvision | =================================================================
  xvision | Total params: 138,357,544
  xvision | Trainable params: 138,357,544
  xvision | Non-trainable params: 0
  xvision | _________________________________________________________________
  xvision | None
  </code></pre>