YangZeyu95 / unofficial-implement-of-openpose

Implement of Openpose use Tensorflow
272 stars 79 forks source link

Checkpoint file to .pb file conversion #7

Open AdnanMuhib opened 5 years ago

AdnanMuhib commented 5 years ago

Can I have the .pb file of the checkpoint file? I tried the code from the answer at Stackoverflow

import tensorflow as tf

meta_path = 'model-59000.ckpt.meta' # Your .meta file

with tf.Session() as sess:

    # Restore the graph
    saver = tf.train.import_meta_graph(meta_path)

    # Load weights
    saver.restore(sess,tf.train.latest_checkpoint('.'))

    # Output nodes
    output_node_names =[n.name for n in tf.get_default_graph().as_graph_def().node]

    # Freeze the graph
    frozen_graph_def = tf.graph_util.convert_variables_to_constants(
        sess,
        sess.graph_def,
        output_node_names)

    # Save the frozen graph
    with open('output_graph.pb', 'wb') as f:
      f.write(frozen_graph_def.SerializeToString())

But that produced the following errors

(tensorflow) H:\OpenPose\unofficial-implement-of-openpose\checkpoints\train\2018-12-13-16-56-49>python toPb.py
C:\Program Files\Anaconda2\envs\tensorflow\lib\site-packages\h5py\__init__.py:36: FutureWarning: Conversion of the second argument of issubdtype from `float` to `np.floating` is deprecated. In future, it will be treated as `np.float64 == np.dtype(float).type`.
  from ._conv import register_converters as _register_converters
2018-12-31 17:05:27.908516: I C:\tf_jenkins\workspace\rel-win\M\windows\PY\35\tensorflow\core\platform\cpu_feature_guard.cc:137] Your CPU supports instructions that this TensorFlow binary was not compiled to use: AVX AVX2
Traceback (most recent call last):
  File "C:\Program Files\Anaconda2\envs\tensorflow\lib\site-packages\tensorflow\python\client\session.py", line 1350, in _do_call
    return fn(*args)
  File "C:\Program Files\Anaconda2\envs\tensorflow\lib\site-packages\tensorflow\python\client\session.py", line 1329, in _run_fn
    status, run_metadata)
  File "C:\Program Files\Anaconda2\envs\tensorflow\lib\site-packages\tensorflow\python\framework\errors_impl.py", line 473, in __exit__
    c_api.TF_GetCode(self.status.status))
tensorflow.python.framework.errors_impl.FailedPreconditionError: Attempting to use uninitialized value global_step
         [[Node: _retval_global_step_0_0 = _Retval[T=DT_INT32, index=0, _device="/job:localhost/replica:0/task:0/device:CPU:0"](global_step)]]

During handling of the above exception, another exception occurred:

Traceback (most recent call last):
  File "toPb.py", line 20, in <module>
    output_node_names)
  File "C:\Program Files\Anaconda2\envs\tensorflow\lib\site-packages\tensorflow\python\framework\graph_util_impl.py", line 243, in convert_variables_to_constants
    returned_variables = sess.run(variable_names)
  File "C:\Program Files\Anaconda2\envs\tensorflow\lib\site-packages\tensorflow\python\client\session.py", line 895, in run
    run_metadata_ptr)
  File "C:\Program Files\Anaconda2\envs\tensorflow\lib\site-packages\tensorflow\python\client\session.py", line 1128, in _run
    feed_dict_tensor, options, run_metadata)
  File "C:\Program Files\Anaconda2\envs\tensorflow\lib\site-packages\tensorflow\python\client\session.py", line 1344, in _do_run
    options, run_metadata)
  File "C:\Program Files\Anaconda2\envs\tensorflow\lib\site-packages\tensorflow\python\client\session.py", line 1363, in _do_call
    raise type(e)(node_def, op, message)
tensorflow.python.framework.errors_impl.FailedPreconditionError: Attempting to use uninitialized value global_step
         [[Node: _retval_global_step_0_0 = _Retval[T=DT_INT32, index=0, _device="/job:localhost/replica:0/task:0/device:CPU:0"](global_step)]]
YangZeyu95 commented 5 years ago

That's because .meta file contain variables that were not saved in .data file(eg. globel_step). Add sess.run(tf.global_variables_initializer()) below saver = tf.train.import_meta_graph(meta_path) to initialize those variables. btw, do you know why variables in .mate and .data are not same?

AdnanMuhib commented 5 years ago

I am a beginner to tensorflow and don't know much about details and even I couldn't understand the answer of yours.
But for my current experiment, I found the graph file from the Models Folder.
Still, it would be great if you can post the .pb version of model or the script for that for future usage. Maybe some script from https://github.com/ildoonet/tf-pose-estimation might be helpful. btw, Thanks for your time.

gouyinchao commented 5 years ago

hi, I cann't find the file of 'heatpaf1.pkl' in estimator.py,how can i get that file ?Can you please help?

YangZeyu95 commented 5 years ago

@gouyinchao estimator.py is from https://github.com/ildoonet/tf-pose-estimation and please open another issue.

gouyinchao commented 5 years ago

When I run the estimator.py ,I will get the error as follow :File Not Found Error: [Errno 2] No such file or directory: './etcs/heatpaf1.pkl',How can I get the file of 'heatpaf1.pkl' ?Thanks for your time again