Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

please share a total example from training in python and prediction in C++. #615

Closed
leftstone2015 opened this issue Dec 25, 2015 · 15 comments
Labels
type:docs-bug Document issues

Comments

@leftstone2015
Copy link

Hi, TFs

Current I'm trying prediction in C++ following the tutorial in tensorflow/tensorflow/g3doc/tutorials/image_recognition/index.md.

I want to train the mode in python, using interface tf.train.write_graph() to write the graph to a file, and then loading it in the C++ for predition, just like the above tutorial. Because there is no python codes in the tutorial and no guides to create the file of "tensorflow_inception_graph.pb".

I've tried several times, but failed. Could you share a total example in both sides in pyhton and c++?

@leftstone2015
Copy link
Author

I tried to evaluate the graph file which is generated by tf.train.write_graph() in python, fed the required data, then run in session, finally encountered the followng error, the error shows use not initialized value. How to write the initialized value to file by tf.train.write_graph() ?

W tensorflow/core/common_runtime/executor.cc:1076] 0x2672cc0 Compute status: Failed precondition: Attempting to use uninitialized value embedding/W
[[Node: embedding/W/read = IdentityT=DT_FLOAT, _device="/job:localhost/replica:0/task:0/cpu:0"]]
[[Node: _recv_dropout_keep_prob_0 = _Recvclient_terminated=true, recv_device="/job:localhost/replica:0/task:0/cpu:0", send_device="/job:localhost/replica:0/task:0/cpu:0", send_device_incarnation=4326657343305820101, tensor_name="dropout_keep_prob:0", tensor_type=DT_FLOAT, _device="/job:localhost/replica:0/task:0/cpu:0"]]
Traceback (most recent call last):
File "/data/wmjiang/tf_workspace/tf/test/20_news_load.py", line 38, in
predictions = sess.run(output, {input_x: data, input_y:label,dropout_prob:1.0})
File "/usr/local/lib/python2.7/dist-packages/tensorflow/python/client/session.py", line 368, in run
results = self._do_run(target_list, unique_fetch_targets, feed_dict_string)
File "/usr/local/lib/python2.7/dist-packages/tensorflow/python/client/session.py", line 444, in _do_run
e.code)
tensorflow.python.framework.errors.FailedPreconditionError: Attempting to use uninitialized value embedding/W
[[Node: embedding/W/read = IdentityT=DT_FLOAT, _device="/job:localhost/replica:0/task:0/cpu:0"]]
Caused by op u'embedding/W/read', defined at:
File "/data/wmjiang/tf_workspace/tf/test/20_news_load.py", line 29, in
create_graph()
File "/data/wmjiang/tf_workspace/tf/test/20_news_load.py", line 26, in create_graph
_ = tf.import_graph_def(graph_def, name='')
File "/usr/local/lib/python2.7/dist-packages/tensorflow/python/framework/importer.py", line 232, in import_graph_def
compute_shapes=False)
File "/usr/local/lib/python2.7/dist-packages/tensorflow/python/framework/ops.py", line 1834, 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 1043, in init
self._traceback = _extract_stack()

@koth
Copy link

koth commented Jan 4, 2016

same problem, please someone tell me how to restore weights from the checkpoint file via c++

@martinwicke martinwicke assigned mrry and martinwicke and unassigned mrry Jan 4, 2016
@martinwicke
Copy link
Member

This is probably a question better suited for stackoverflow. Short version: write_graph only writes the graph, not the variable content. You need a Saver, see for instance: http://stackoverflow.com/questions/33689598/how-to-pause-resume-training-in-tensorflow/33690809#33690809

@HamedMP
Copy link

HamedMP commented Feb 14, 2016

@martinwicke I run the loader.cc example and it's okay,
But I have one question which enables me to use c++ with my own model.

After I load the graph and run the session with inputs I get following error:

Attempting to use uninitialized value Variable_1
     [[Node: Variable_1/read = Identity[T=DT_FLOAT, _device="/job:localhost/replica:0/task:0/cpu:0"](Variable_1)]]

And the number of variable is changing, 1, 2, 3,...
I used

status = session->Run(inputs, {"prediction"}, {"init_all_vars_op"}, &outputs);

As the tutorial I wrote the graph and have the file, but in your previous comment you are saying that the write_graph only saves the architecture, but not the trained model, so:

  1. If this is the case, so how your Inception model tutorial works?? There you only load the graph to the model + labels and the test image. Where is the saved wights there?
  2. If not and you should load the model, then how?

@martinwicke
Copy link
Member

Take a look at python/tools/freeze_graph.py, which takes the contents of
variables and saves them to the graph as constants. Then when the graph is
loaded it comes with all the data.

On Sun, Feb 14, 2016 at 10:00 AM Hamed notifications@github.com wrote:

@martinwicke https://github.com/martinwicke I run the loader.cc example
and it's okay,
But I have one question which enables me to use c++ with my own model.

As the tutorial
https://www.tensorflow.org/versions/v0.6.0/api_docs/cc/index.html I
wrote the graph and have the file, but in your previous comment you are
saying that the write_graph only saves the architecture, but not the
trained model, so:

If this is the case, so how your Inception model
https://www.tensorflow.org/versions/v0.6.0/tutorials/image_recognition/index.html
tutorial works?? There you only load the graph to the model + labels and
the test image. Where is the saved wights there?
2.

If not and you should load the model, then how?


Reply to this email directly or view it on GitHub
#615 (comment)
.

@HamedMP
Copy link

HamedMP commented Feb 18, 2016

Thank you very much @martinwicke

After looking into documentations, I found that there are few similar methods, can you tell what is the difference between freeze_graph() and:
tf.train.export_meta_graph as it has similar parameters, but it seems it can also be used for importing models to C++ (I just guess the difference is that for using the file output by this method you can only use import_graph_def() or it's something else?)

Also one question about how to use write_graph():
In documentations the graph_def is given by sess.graph_def but in examples in freeze_graph() it is sess.graph.as_graph_def(). What is the difference between these two?

Thank you!

@martinwicke
Copy link
Member

I think we should take this discussion to stackoverflow. Can you post there and add a link here? We shouldn't abuse the issue tracker for questions. I'll leave this issue open as a feature request for a tutorial on graph import/export.

@martinwicke martinwicke added the type:docs-bug Document issues label Feb 18, 2016
@martinwicke martinwicke removed their assignment Feb 18, 2016
@HamedMP
Copy link

HamedMP commented Feb 19, 2016

Yes, of course, here you are @martinwicke : http://stackoverflow.com/questions/35508866/tensorflow-different-ways-to-export-and-run-graph-in-c

Btw, I want to inform you that the freeze_graph() didn't work because of lacking graph_util.convert_variables_to_constants() method in the wheel installation by pip for Mac. I managed to run it by cloning and building binary. May be I need to open a new issue but I think you will update the wheel soon so I didn't pile a new issue on top of them now :)

@martinwicke
Copy link
Member

I think a new issue would be good, I don't think we have a fix for this yet.

On Fri, Feb 19, 2016 at 7:30 AM Hamed notifications@github.com wrote:

Yes, of course, here you are @martinwicke https://github.com/martinwicke
:
http://stackoverflow.com/questions/35508866/tensorflow-different-ways-to-export-and-run-graph-in-c

Btw, I want to inform you that the freeze_graph() didn't work because of
lacking graph_util.convert_variables_to_constants() method in the wheel
installation by pip for Mac. I managed to run it by cloning and building
binary. May be I need to open a new issue but I think you will update the
wheel soon so I didn't pile a new issue on top of them now :)


Reply to this email directly or view it on GitHub
#615 (comment)
.

@HamedMP
Copy link

HamedMP commented Feb 20, 2016

Which issue you mean?
As @vrv said in #1199 he will look for the graph_util.convert_variables_to_constants().
The other problem about the difference I think it's only a matter of documentation.

@martinwicke
Copy link
Member

👍
On Sat, Feb 20, 2016 at 03:17 Hamed notifications@github.com wrote:

Which issue you mean?
As @vrv https://github.com/vrv said in #1199
#1199 he will look for
the graph_util.convert_variables_to_constants().
The other problem about the difference I think it's only a matter of
documentation.


Reply to this email directly or view it on GitHub
#615 (comment)
.

@peci1
Copy link

peci1 commented Apr 26, 2017

I've written a (hopefully) self-contained example today: http://stackoverflow.com/a/43639305/1076564

@xumengwei
Copy link

Hi @peci1, thanks for your helpful post on stackoverflow. However, I tried your solution but it complains

Error creating graph: Invalid argument: No OpKernel was registered to support Op 'Const' with these attrs.  Registered devices: [CPU], Registered kernels:
  <no registered kernels>

	 [[Node: save/RestoreV2_8/shape_and_slices = Const[_output_shapes=[[1]], dtype=DT_STRING, value=Tensor<type: string shape: [1] values: >]()]]

How you compile all necessary ops and kernels into the lib?

@peci1
Copy link

peci1 commented Dec 30, 2017

@xumengwei Check out my other answer :) https://stackoverflow.com/a/43526252/1076564 . And I'm not sure if it still works with TF 1.4, I had some problems with it. But 1.3 is working for me.

@sathyarr
Copy link

sathyarr commented Feb 5, 2019

any recent(improved) methods to train models in Python and make predictions with C++? as now, what is the best method to achieve this? @martinwicke @HamedMP

darkbuck pushed a commit to darkbuck/tensorflow that referenced this issue Jan 23, 2020
update nccl_manager_test.cc, Participant now takes nccl_stream
mahmoud-abuzaina added a commit to Intel-tensorflow/tensorflow that referenced this issue Oct 4, 2023
…8-onednn-v3

Enabling oneDNN v3.0 in QuantizedConv ops/fusions
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
type:docs-bug Document issues
Projects
None yet
Development

No branches or pull requests

8 participants