tensorflow - You must feed a value for placeholder tensor 'c_1' with dtype float and shape [1] -


i read issue same problem, looks issue different. want freeze graph , use it.

here simple example how this. first, create session , save both checkpoint , graphdef:

a = tf.variable(tf.constant(1.), name='a') b = tf.variable(tf.constant(2.), name='b') c = tf.placeholder(tf.float32, shape =[1], name="c") add = tf.add(a, b, 'sum') add2 = tf.add(add, c, 'sum2')  dir_path = "<full_path>/simple_store"  tf.session() sess:     tf.initialize_all_variables().run()     sess.run([add2], feed_dict={c:[7.]})     tf.train.saver().save(sess, dir_path + "/" + 'simple.ckpt')     tf.train.write_graph(graph_def=sess.graph.as_graph_def(), logdir=dir_path, name='simple_as_text.pb') 

then use bazel tool freezing such way:

../tensorflow/bazel-bin/tensorflow/python/tools/freeze_graph --input_graph=simple_store/simple_as_text.pb --input_checkpoint=simple_store/simple.ckpt --output_graph=simple_store/freeze_out.pb --output_node_names=sum2 

then load freeze_out.pb in python , try run:

import tensorflow tf tensorflow.core.framework import graph_pb2, cost_graph_pb2  graph_def = graph_pb2.graphdef() d = none  c = tf.placeholder(tf.float32, shape=[1], name="c") feed_dict = {c: [5.]}  tf.session() session:     print("load graph")     open("<somepath>/simple_store/freeze_out.pb", "rb") f:         graph_def.parsefromstring(f.read())         d = tf.import_graph_def(graph_def, return_elements=["sum2:0"], name='')     print(session.run([d[0]], feed_dict=feed_dict)) 

and following error:

--------------------------------------------------------------------------- invalidargumenterror                      traceback (most recent call last) <ipython-input-3-6345b17fba3b> in <module>()       8         d = tf.import_graph_def(graph_def, return_elements=["sum2:0"], name='')       9     tf.initialize_all_variables().run() ---> 10     print(session.run([d[0]], feed_dict=feed_dict))  /usr/local/lib/python2.7/dist-packages/tensorflow/python/client/session.pyc in run(self, fetches, feed_dict, options, run_metadata)     370     try:     371       result = self._run(none, fetches, feed_dict, options_ptr, --> 372                          run_metadata_ptr)     373       if run_metadata:     374         proto_data = tf_session.tf_getbuffer(run_metadata_ptr)  /usr/local/lib/python2.7/dist-packages/tensorflow/python/client/session.pyc in _run(self, handle, fetches, feed_dict, options, run_metadata)     634     try:     635       results = self._do_run(handle, target_list, unique_fetches, --> 636                              feed_dict_string, options, run_metadata)     637     finally:     638       # movers no longer used. delete them.  /usr/local/lib/python2.7/dist-packages/tensorflow/python/client/session.pyc in _do_run(self, handle, target_list, fetch_list, feed_dict, options, run_metadata)     706     if handle none:     707       return self._do_call(_run_fn, self._session, feed_dict, fetch_list, --> 708                            target_list, options, run_metadata)     709     else:     710       return self._do_call(_prun_fn, self._session, handle, feed_dict,  /usr/local/lib/python2.7/dist-packages/tensorflow/python/client/session.pyc in _do_call(self, fn, *args)     726         except keyerror:     727           pass --> 728       raise type(e)(node_def, op, message)     729      730   def _extend_graph(self):  invalidargumenterror: must feed value placeholder tensor 'c_1' dtype float , shape [1]      [[node: c_1 = placeholder[dtype=dt_float, shape=[1], _device="/job:localhost/replica:0/task:0/cpu:0"]()]] caused op u'c_1', defined at:   file "/usr/lib/python2.7/runpy.py", line 174, in _run_module_as_main     "__main__", fname, loader, pkg_name)   file "/usr/lib/python2.7/runpy.py", line 72, in _run_code     exec code in run_globals   file "/home/artem/.local/lib/python2.7/site-packages/ipykernel/__main__.py", line 3, in <module>     app.launch_new_instance()   file "/home/artem/.local/lib/python2.7/site-packages/traitlets/config/application.py", line 596, in launch_instance     app.start()   file "/home/artem/.local/lib/python2.7/site-packages/ipykernel/kernelapp.py", line 442, in start     ioloop.ioloop.instance().start()   file "/home/artem/.local/lib/python2.7/site-packages/zmq/eventloop/ioloop.py", line 162, in start     super(zmqioloop, self).start()   file "/home/artem/.local/lib/python2.7/site-packages/tornado/ioloop.py", line 887, in start     handler_func(fd_obj, events)   file "/home/artem/.local/lib/python2.7/site-packages/tornado/stack_context.py", line 275, in null_wrapper     return fn(*args, **kwargs)   file "/home/artem/.local/lib/python2.7/site-packages/zmq/eventloop/zmqstream.py", line 440, in _handle_events     self._handle_recv()   file "/home/artem/.local/lib/python2.7/site-packages/zmq/eventloop/zmqstream.py", line 472, in _handle_recv     self._run_callback(callback, msg)   file "/home/artem/.local/lib/python2.7/site-packages/zmq/eventloop/zmqstream.py", line 414, in _run_callback     callback(*args, **kwargs)   file "/home/artem/.local/lib/python2.7/site-packages/tornado/stack_context.py", line 275, in null_wrapper     return fn(*args, **kwargs)   file "/home/artem/.local/lib/python2.7/site-packages/ipykernel/kernelbase.py", line 276, in dispatcher     return self.dispatch_shell(stream, msg)   file "/home/artem/.local/lib/python2.7/site-packages/ipykernel/kernelbase.py", line 228, in dispatch_shell     handler(stream, idents, msg)   file "/home/artem/.local/lib/python2.7/site-packages/ipykernel/kernelbase.py", line 391, in execute_request     user_expressions, allow_stdin)   file "/home/artem/.local/lib/python2.7/site-packages/ipykernel/ipkernel.py", line 199, in do_execute     shell.run_cell(code, store_history=store_history, silent=silent)   file "/home/artem/.local/lib/python2.7/site-packages/ipython/core/interactiveshell.py", line 2705, in run_cell     interactivity=interactivity, compiler=compiler, result=result)   file "/home/artem/.local/lib/python2.7/site-packages/ipython/core/interactiveshell.py", line 2809, in run_ast_nodes     if self.run_code(code, result):   file "/home/artem/.local/lib/python2.7/site-packages/ipython/core/interactiveshell.py", line 2869, in run_code     exec(code_obj, self.user_global_ns, self.user_ns)   file "<ipython-input-3-6345b17fba3b>", line 8, in <module>     d = tf.import_graph_def(graph_def, return_elements=["sum2:0"], name='')   file "/usr/local/lib/python2.7/dist-packages/tensorflow/python/framework/importer.py", line 274, in import_graph_def     op_def=op_def)   file "/usr/local/lib/python2.7/dist-packages/tensorflow/python/framework/ops.py", line 2260, in create_op     original_op=self._default_original_op, op_def=op_def)   file "/usr/local/lib/python2.7/dist-packages/tensorflow/python/framework/ops.py", line 1230, in __init__     self._traceback = _extract_stack() 

what did wrong? how should correct this?

the tf.import_graph_def() function maintains structure of imported graph, unless pass input_map argument.

in original graph passed freeze_graph, tensor named "sum2:0" depends on placeholder operation called "c" in same graph. when import frozen graph, tensorflow first imports node named "c"

when import frozen graph using tf.import_graph_def(), tensorflow first imports node named "c" in freeze_out.pb. however, because have created node named "c" in second program, imported node renamed "c_1" (an automatically generated unique name) , imported version of "sum_2" rewritten depend on "c_1". notably, not depend on placeholder feeding (which named "c").

there 2 solutions. more straightforward solution extract created placeholder imported graph, rather creating new one. can adding "c:0" list of return_elements:

graph_def = tf.graphdef()  open("<somepath>/simple_store/freeze_out.pb", "rb") f:     graph_def.parsefromstring(f.read())  # extract placeholder "c" imported graph. c, d = tf.import_graph_def(graph_def, return_elements=["c:0", "sum2:0"])  tf.session() session:     print(session.run([d[0]], feed_dict={c: [5.]})) 

alternatively, can remap placeholder in imported graph use placeholder in new graph. (there not point in doing substitution, can useful when new graph more complex , includes new preprocessing, example.) uses input_map argument tf.import_graph_def():

graph_def = tf.graphdef()  # create new placeholder map imported graph. # (n.b. has no advantage, useful if `c` more interesting # function.) c = tf.placeholder(tf.float32, shape=[1], name="c") feed_dict = {c: [5.]}  open("<somepath>/simple_store/freeze_out.pb", "rb") f:     graph_def.parsefromstring(f.read())  # remap placeholder in imported graph use placeholder created # above. notice syntax feed_dict, performs static  # remapping of 1 tensor @ graph construction time. d = tf.import_graph_def(graph_def, input_map={"c:0": c}, return_elements=["sum2:0"])  tf.session() session:     print(session.run([d[0]], feed_dict={c: [5.]})) 

Comments

Popular posts from this blog

jOOQ update returning clause with Oracle -

java - Warning equals/hashCode on @Data annotation lombok with inheritance -

java - BasicPathUsageException: Cannot join to attribute of basic type -