title: Tensorflow : Restoring a tensorflow model and pass a single image to examine gradients
body: <p>I want to restore a checkpoint and pass a single image and do guided backpropogation. </p>
  
  <p>This is the code I'm using right now.</p>
  
  <pre><code>import tensorflow as tf
  import numpy as np
  import PIL.Image
  from matplotlib import pylab as P
  import pickle
  import os
  import cv2
  import matplotlib.pyplot as plt
  from net import network
  
  graph = tf.Graph()
  with graph.as_default():
      images = tf.placeholder(tf.float32, shape=(None ,1, 418400,3))
  
      sess = tf.Session(graph=graph)
      saver = tf.train.import_meta_graph('flat-3000.meta')
      saver.restore(sess, tf.train.latest_checkpoint('./checkpoint'))
  
      logits = graph.get_tensor_by_name('Logits/Squeeze:0')
      neuron_selector = tf.placeholder(tf.int32)
      y = logits[0][neuron_selector]
      prediction = tf.argmax(logits, 1)
  im = (((cv2.imread('./images/\Image_ALL_11.png', 1)) / 255) * 2 )- 1
  im_flat = np.reshape(im, (1,418400,3), order = 'F')
  prediction_class = sess.run(prediction, feed_dict = {images: [im_flat]})[0]
  </code></pre>
  
  <p>This is the error stack trace.</p>
  
  <hr>
  
  <pre><code>InvalidArgumentError                      Traceback (most recent call last)
  ~/anaconda3/envs/tensorflow/lib/python3.6/site-packages/tensorflow/python/client/session.py in _do_call(self, fn, *args)
     1322     try:
  -&gt; 1323       return fn(*args)
     1324     except errors.OpError as e:
  
  ~/anaconda3/envs/tensorflow/lib/python3.6/site-packages/tensorflow/python/client/session.py in _run_fn(session, feed_dict, fetch_list, target_list, options, run_metadata)
     1301                                    feed_dict, fetch_list, target_list,
  -&gt; 1302                                    status, run_metadata)
     1303 
  
  ~/anaconda3/envs/tensorflow/lib/python3.6/site-packages/tensorflow/python/framework/errors_impl.py in __exit__(self, type_arg, value_arg, traceback_arg)
      472             compat.as_text(c_api.TF_Message(self.status.status)),
  --&gt; 473             c_api.TF_GetCode(self.status.status))
      474     # Delete the underlying status object from memory otherwise it stays alive
  
  InvalidArgumentError: You must feed a value for placeholder tensor 'Input/Input' with dtype float and shape [?,1,418400,3]
       [[Node: Input/Input = Placeholder[dtype=DT_FLOAT, shape=[?,1,418400,3], _device="/job:localhost/replica:0/task:0/device:GPU:0"]()]]
       [[Node: ArgMax/_121 = _Recv[client_terminated=false, recv_device="/job:localhost/replica:0/task:0/device:CPU:0", send_device="/job:localhost/replica:0/task:0/device:GPU:0", send_device_incarnation=1, tensor_name="edge_292_ArgMax", tensor_type=DT_INT64, _device="/job:localhost/replica:0/task:0/device:CPU:0"]()]]
  
  During handling of the above exception, another exception occurred:
  
  InvalidArgumentError                      Traceback (most recent call last)
  &lt;ipython-input-15-9df4a7dc7f9b&gt; in &lt;module&gt;()
  ----&gt; 1 prediction_class = sess.run(prediction, feed_dict = {images: [im_flat]})[0]
  
  ~/anaconda3/envs/tensorflow/lib/python3.6/site-packages/tensorflow/python/client/session.py in run(self, fetches, feed_dict, options, run_metadata)
      887     try:
      888       result = self._run(None, fetches, feed_dict, options_ptr,
  --&gt; 889                          run_metadata_ptr)
      890       if run_metadata:
      891         proto_data = tf_session.TF_GetBuffer(run_metadata_ptr)
  
  ~/anaconda3/envs/tensorflow/lib/python3.6/site-packages/tensorflow/python/client/session.py in _run(self, handle, fetches, feed_dict, options, run_metadata)
     1118     if final_fetches or final_targets or (handle and feed_dict_tensor):
     1119       results = self._do_run(handle, final_targets, final_fetches,
  -&gt; 1120                              feed_dict_tensor, options, run_metadata)
     1121     else:
     1122       results = []
  
  ~/anaconda3/envs/tensorflow/lib/python3.6/site-packages/tensorflow/python/client/session.py in _do_run(self, handle, target_list, fetch_list, feed_dict, options, run_metadata)
     1315     if handle is None:
     1316       return self._do_call(_run_fn, self._session, feeds, fetches, targets,
  -&gt; 1317                            options, run_metadata)
     1318     else:
     1319       return self._do_call(_prun_fn, self._session, handle, feeds, fetches)
  
  ~/anaconda3/envs/tensorflow/lib/python3.6/site-packages/tensorflow/python/client/session.py in _do_call(self, fn, *args)
     1334         except KeyError:
     1335           pass
  -&gt; 1336       raise type(e)(node_def, op, message)
     1337 
     1338   def _extend_graph(self):
  
  InvalidArgumentError: You must feed a value for placeholder tensor 'Input/Input' with dtype float and shape [?,1,418400,3]
       [[Node: Input/Input = Placeholder[dtype=DT_FLOAT, shape=[?,1,418400,3], _device="/job:localhost/replica:0/task:0/device:GPU:0"]()]]
       [[Node: ArgMax/_121 = _Recv[client_terminated=false, recv_device="/job:localhost/replica:0/task:0/device:CPU:0", send_device="/job:localhost/replica:0/task:0/device:GPU:0", send_device_incarnation=1, tensor_name="edge_292_ArgMax", tensor_type=DT_INT64, _device="/job:localhost/replica:0/task:0/device:CPU:0"]()]]
  
  Caused by op 'Input/Input', defined at:
    File "/home/vkv/anaconda3/envs/tensorflow/lib/python3.6/runpy.py", line 193, in _run_module_as_main
      "__main__", mod_spec)
    File "/home/vkv/anaconda3/envs/tensorflow/lib/python3.6/runpy.py", line 85, in _run_code
      exec(code, run_globals)
    File "/home/vkv/anaconda3/envs/tensorflow/lib/python3.6/site-packages/ipykernel_launcher.py", line 16, in &lt;module&gt;
      app.launch_new_instance()
    File "/home/vkv/anaconda3/envs/tensorflow/lib/python3.6/site-packages/traitlets/config/application.py", line 658, in launch_instance
      app.start()
    File "/home/vkv/anaconda3/envs/tensorflow/lib/python3.6/site-packages/ipykernel/kernelapp.py", line 486, in start
      self.io_loop.start()
    File "/home/vkv/anaconda3/envs/tensorflow/lib/python3.6/site-packages/tornado/platform/asyncio.py", line 112, in start
      self.asyncio_loop.run_forever()
    File "/home/vkv/anaconda3/envs/tensorflow/lib/python3.6/asyncio/base_events.py", line 421, in run_forever
      self._run_once()
    File "/home/vkv/anaconda3/envs/tensorflow/lib/python3.6/asyncio/base_events.py", line 1431, in _run_once
      handle._run()
    File "/home/vkv/anaconda3/envs/tensorflow/lib/python3.6/asyncio/events.py", line 145, in _run
      self._callback(*self._args)
    File "/home/vkv/anaconda3/envs/tensorflow/lib/python3.6/site-packages/tornado/platform/asyncio.py", line 102, in _handle_events
      handler_func(fileobj, events)
    File "/home/vkv/anaconda3/envs/tensorflow/lib/python3.6/site-packages/tornado/stack_context.py", line 276, in null_wrapper
      return fn(*args, **kwargs)
    File "/home/vkv/anaconda3/envs/tensorflow/lib/python3.6/site-packages/zmq/eventloop/zmqstream.py", line 450, in _handle_events
      self._handle_recv()
    File "/home/vkv/anaconda3/envs/tensorflow/lib/python3.6/site-packages/zmq/eventloop/zmqstream.py", line 480, in _handle_recv
      self._run_callback(callback, msg)
    File "/home/vkv/anaconda3/envs/tensorflow/lib/python3.6/site-packages/zmq/eventloop/zmqstream.py", line 432, in _run_callback
      callback(*args, **kwargs)
    File "/home/vkv/anaconda3/envs/tensorflow/lib/python3.6/site-packages/tornado/stack_context.py", line 276, in null_wrapper
      return fn(*args, **kwargs)
    File "/home/vkv/anaconda3/envs/tensorflow/lib/python3.6/site-packages/ipykernel/kernelbase.py", line 283, in dispatcher
      return self.dispatch_shell(stream, msg)
    File "/home/vkv/anaconda3/envs/tensorflow/lib/python3.6/site-packages/ipykernel/kernelbase.py", line 233, in dispatch_shell
      handler(stream, idents, msg)
    File "/home/vkv/anaconda3/envs/tensorflow/lib/python3.6/site-packages/ipykernel/kernelbase.py", line 399, in execute_request
      user_expressions, allow_stdin)
    File "/home/vkv/anaconda3/envs/tensorflow/lib/python3.6/site-packages/ipykernel/ipkernel.py", line 208, in do_execute
      res = shell.run_cell(code, store_history=store_history, silent=silent)
    File "/home/vkv/anaconda3/envs/tensorflow/lib/python3.6/site-packages/ipykernel/zmqshell.py", line 537, in run_cell
      return super(ZMQInteractiveShell, self).run_cell(*args, **kwargs)
    File "/home/vkv/anaconda3/envs/tensorflow/lib/python3.6/site-packages/IPython/core/interactiveshell.py", line 2728, in run_cell
      interactivity=interactivity, compiler=compiler, result=result)
    File "/home/vkv/anaconda3/envs/tensorflow/lib/python3.6/site-packages/IPython/core/interactiveshell.py", line 2850, in run_ast_nodes
      if self.run_code(code, result):
    File "/home/vkv/anaconda3/envs/tensorflow/lib/python3.6/site-packages/IPython/core/interactiveshell.py", line 2910, in run_code
      exec(code_obj, self.user_global_ns, self.user_ns)
    File "&lt;ipython-input-13-1fc4f0d15c89&gt;", line 7, in &lt;module&gt;
      saver = tf.train.import_meta_graph('flat-3000.meta')
    File "/home/vkv/anaconda3/envs/tensorflow/lib/python3.6/site-packages/tensorflow/python/training/saver.py", line 1810, in import_meta_graph
      **kwargs)
    File "/home/vkv/anaconda3/envs/tensorflow/lib/python3.6/site-packages/tensorflow/python/framework/meta_graph.py", line 660, in import_scoped_meta_graph
      producer_op_list=producer_op_list)
    File "/home/vkv/anaconda3/envs/tensorflow/lib/python3.6/site-packages/tensorflow/python/framework/importer.py", line 313, in import_graph_def
      op_def=op_def)
    File "/home/vkv/anaconda3/envs/tensorflow/lib/python3.6/site-packages/tensorflow/python/framework/ops.py", line 2956, in create_op
      op_def=op_def)
    File "/home/vkv/anaconda3/envs/tensorflow/lib/python3.6/site-packages/tensorflow/python/framework/ops.py", line 1470, in __init__
      self._traceback = self._graph._extract_stack()  # pylint: disable=protected-access
  
  InvalidArgumentError (see above for traceback): You must feed a value for placeholder tensor 'Input/Input' with dtype float and shape [?,1,418400,3]
       [[Node: Input/Input = Placeholder[dtype=DT_FLOAT, shape=[?,1,418400,3], _device="/job:localhost/replica:0/task:0/device:GPU:0"]()]]
       [[Node: ArgMax/_121 = _Recv[client_terminated=false, recv_device="/job:localhost/replica:0/task:0/device:CPU:0", send_device="/job:localhost/replica:0/task:0/device:GPU:0", send_device_incarnation=1, tensor_name="edge_292_ArgMax", tensor_type=DT_INT64, _device="/job:localhost/replica:0/task:0/device:CPU:0"]()]]
  </code></pre>
  
  <p>I'm using tensorflow v1.4.1.
  The error traces of tensorflow is very confusing, so I don't understand why the error says that I don't feed anything to the Input place holder.</p>
  
  <p>This is the network I'm using. </p>
  
  <pre><code>class network():
  
      def __init__(self):
          self.initializer = tf.contrib.layers.xavier_initializer()
          self.isTrain = True
          self.epochs = 100
          self.batch_size = 10
          self.data_size = 2503
          self.restore = False
          self.decay = 0.99
          self.model_name = 'model'
          self.ckpt_dir = './' + self.model_name + "/checkpoint"
          self.val_fraction = 0.1
          self.test_fraction = 0.1
          self.drop = 0.5
  
  
      def layer(self, input_, output_ch, kernel, stride, drop_rate, pad = "VALID", name = "Layer"):
  
          with tf.variable_scope(name) as scope:
  
              x = Conv(input_, output_ch, self.initializer, kernel = kernel, stride = stride, name = 'Conv', pad = "VALID")
              x = batchnorm(x, isTrain = self.isTrain, decay = self.decay)
              x = tf.nn.relu(x)
              x = dropout(x, rate = drop_rate, training = self.isTrain)
  
          return x
  
      def model(self):
  
          if not os.path.exists(self.model_name):
              os.mkdir(self.model_name)
  
          with tf.variable_scope('Input') as scope:   
              self.input_ = tf.placeholder(name = "Input", shape = [None, 1, 418400, 3], dtype = tf.float32)
              self.labels = tf.placeholder(name = "Labels", shape = [None, 5], dtype = tf.float32)
              self.isTrain = tf.placeholder(name = "isTrain", shape = None, dtype = tf.bool)
              self.step = tf.train.get_or_create_global_step()
              self.lr = tf.placeholder(name = "LR", shape = None, dtype = tf.float32)
  
          print('Output Shapes:')
          x = self.layer(self.input_, output_ch = 64, kernel = (1,50), stride = 15, drop_rate = self.drop, pad = "VALID", name = "Layer_1")
          print('Layer_1:',x.get_shape())
  
          x = self.layer(x, output_ch = 128, kernel = (1,50), stride = 10, drop_rate = self.drop, pad = "VALID", name = "Layer_2")
          print('Layer_2:',x.get_shape())
  
          x = self.layer(x, output_ch = 256, kernel = (1,10), stride = 5, drop_rate = self.drop, pad = "VALID", name = "Layer_3")
          print('Layer_3:',x.get_shape())
  
          x = self.layer(x, output_ch = 512, kernel = (1,10), stride = 5, drop_rate = self.drop, pad = "VALID", name = "Layer_4")
          print('Layer_4:',x.get_shape())
  
          x = self.layer(x, output_ch = 5, kernel = (1,10), stride = 5, drop_rate = self.drop, pad = "VALID", name = "Layer_5")
          print('Layer_5:',x.get_shape())
  
          x = AvgPool(x, kernel = (1,21), stride = 1, name = "AvgPool_1", pad = 'VALID')
          print('AvgPool_1:',x.get_shape())
  
          with tf.variable_scope("Logits") as scope:
              self.logits = tf.squeeze(x, [1,2], name = "Squeeze")
              self.logits = tf.nn.tanh(self.logits)
              print('Logits:',self.logits.get_shape())
  
          with tf.variable_scope("Loss") as scope:
  
              self.loss = tf.reduce_mean(tf.nn.softmax_cross_entropy_with_logits(logits = self.logits, labels = self.labels))
  
          with tf.variable_scope("Accuracy") as scope:
  
              self.logits_max_args = tf.argmax(self.logits, axis = 1)
              self.labels_max_args = tf.argmax(self.labels, axis = 1)
              self.equal = tf.reduce_sum(tf.cast(tf.equal(self.logits_max_args, self.labels_max_args), tf.float32))
              self.batch_acc = tf.divide(self.equal, tf.cast(tf.shape(self.logits)[0], tf.float32))
  
          with tf.variable_scope("Optimizer") as scope:
  
              update_ops = tf.get_collection(tf.GraphKeys.UPDATE_OPS)
              with tf.control_dependencies(update_ops):
                  self.optimizer = tf.train.AdamOptimizer(self.lr).minimize(self.loss, self.step)
              self.saver = tf.train.Saver(max_to_keep = 3)
  
          with tf.variable_scope("Summary") as scope:
  
              loss_sum = tf.summary.scalar('Loss', self.loss)
              acc_sum = tf.summary.scalar('Batch Accuracy', self.batch_acc)
              self.summary = tf.summary.merge([loss_sum, acc_sum])
  </code></pre>