0

I tried to convert TensorFlow 1 CycleGAN non-frozen model from .ckpt to .pb.

enter image description here

I had referred to this question but in vain.

I ran the following code.

import os
import tensorflow as tf

export_dir = 'export_dir' 
trained_checkpoint_prefix = 'HFpEF_LV_256/cyclegan.model-2002'
graph = tf.Graph()
loader = tf.train.import_meta_graph(trained_checkpoint_prefix + ".meta" )
sess = tf.Session()
loader.restore(sess,trained_checkpoint_prefix)
builder = tf.saved_model.builder.SavedModelBuilder(export_dir)
builder.add_meta_graph_and_variables(sess, [tf.saved_model.tag_constants.TRAINING, tf.saved_model.tag_constants.SERVING], strip_default_attrs=True)
builder.save()

But got the error message below.

Traceback (most recent call last):
  File "C:\Users\john0\AppData\Roaming\Python\Python36\site-packages\tensorflow_core\python\client\session.py", line 1365, in _do_call
    return fn(*args)
  File "C:\Users\john0\AppData\Roaming\Python\Python36\site-packages\tensorflow_core\python\client\session.py", line 1350, in _run_fn
    target_list, run_metadata)
  File "C:\Users\john0\AppData\Roaming\Python\Python36\site-packages\tensorflow_core\python\client\session.py", line 1443, in _call_tf_sessionrun
    run_metadata)
tensorflow.python.framework.errors_impl.FailedPreconditionError: Attempting to use uninitialized value beta1_power
         [[{{node save/SaveV2_1}}]]

During handling of the above exception, another exception occurred:

Traceback (most recent call last):
  File "ckpt2pb.py", line 11, in <module>
    builder.add_meta_graph_and_variables(sess, [tf.saved_model.tag_constants.TRAINING, tf.saved_model.tag_constants.SERVING], strip_default_attrs=True)
  File "C:\Users\john0\AppData\Roaming\Python\Python36\site-packages\tensorflow_core\python\util\deprecation.py", line 507, in new_func
    return func(*args, **kwargs)
  File "C:\Users\john0\AppData\Roaming\Python\Python36\site-packages\tensorflow_core\python\saved_model\builder_impl.py", line 595, in add_meta_graph_and_variables
    saver.save(sess, variables_path, write_meta_graph=False, write_state=False)
  File "C:\Users\john0\AppData\Roaming\Python\Python36\site-packages\tensorflow_core\python\training\saver.py", line 1193, in save
    raise exc
  File "C:\Users\john0\AppData\Roaming\Python\Python36\site-packages\tensorflow_core\python\training\saver.py", line 1176, in save
    {self.saver_def.filename_tensor_name: checkpoint_file})
  File "C:\Users\john0\AppData\Roaming\Python\Python36\site-packages\tensorflow_core\python\client\session.py", line 956, in run
    run_metadata_ptr)
  File "C:\Users\john0\AppData\Roaming\Python\Python36\site-packages\tensorflow_core\python\client\session.py", line 1180, in _run
    feed_dict_tensor, options, run_metadata)
  File "C:\Users\john0\AppData\Roaming\Python\Python36\site-packages\tensorflow_core\python\client\session.py", line 1359, in _do_run
    run_metadata)
  File "C:\Users\john0\AppData\Roaming\Python\Python36\site-packages\tensorflow_core\python\client\session.py", line 1384, in _do_call
    raise type(e)(node_def, op, message)
tensorflow.python.framework.errors_impl.FailedPreconditionError: Attempting to use uninitialized value beta1_power
         [[node save/SaveV2_1 (defined at C:\Users\john0\AppData\Roaming\Python\Python36\site-packages\tensorflow_core\python\framework\ops.py:1748) ]]

Original stack trace for 'save/SaveV2_1':
  File "ckpt2pb.py", line 11, in <module>
    builder.add_meta_graph_and_variables(sess, [tf.saved_model.tag_constants.TRAINING, tf.saved_model.tag_constants.SERVING], strip_default_attrs=True)
  File "C:\Users\john0\AppData\Roaming\Python\Python36\site-packages\tensorflow_core\python\util\deprecation.py", line 507, in new_func
    return func(*args, **kwargs)
  File "C:\Users\john0\AppData\Roaming\Python\Python36\site-packages\tensorflow_core\python\saved_model\builder_impl.py", line 589, in add_meta_graph_and_variables
    saver = self._maybe_create_saver(saver)
  File "C:\Users\john0\AppData\Roaming\Python\Python36\site-packages\tensorflow_core\python\saved_model\builder_impl.py", line 227, in _maybe_create_saver
    allow_empty=True)
  File "C:\Users\john0\AppData\Roaming\Python\Python36\site-packages\tensorflow_core\python\training\saver.py", line 828, in __init__
    self.build()
  File "C:\Users\john0\AppData\Roaming\Python\Python36\site-packages\tensorflow_core\python\training\saver.py", line 840, in build
    self._build(self._filename, build_save=True, build_restore=True)
  File "C:\Users\john0\AppData\Roaming\Python\Python36\site-packages\tensorflow_core\python\training\saver.py", line 878, in _build
    build_restore=build_restore)
  File "C:\Users\john0\AppData\Roaming\Python\Python36\site-packages\tensorflow_core\python\training\saver.py", line 499, in _build_internal
    save_tensor = self._AddShardedSaveOps(filename_tensor, per_device)
  File "C:\Users\john0\AppData\Roaming\Python\Python36\site-packages\tensorflow_core\python\training\saver.py", line 291, in _AddShardedSaveOps
    return self._AddShardedSaveOpsForV2(filename_tensor, per_device)
  File "C:\Users\john0\AppData\Roaming\Python\Python36\site-packages\tensorflow_core\python\training\saver.py", line 265, in _AddShardedSaveOpsForV2
    sharded_saves.append(self._AddSaveOps(sharded_filename, saveables))
  File "C:\Users\john0\AppData\Roaming\Python\Python36\site-packages\tensorflow_core\python\training\saver.py", line 206, in _AddSaveOps
    save = self.save_op(filename_tensor, saveables)
  File "C:\Users\john0\AppData\Roaming\Python\Python36\site-packages\tensorflow_core\python\training\saver.py", line 122, in save_op
    tensors)
  File "C:\Users\john0\AppData\Roaming\Python\Python36\site-packages\tensorflow_core\python\ops\gen_io_ops.py", line 1945, in save_v2
    name=name)
  File "C:\Users\john0\AppData\Roaming\Python\Python36\site-packages\tensorflow_core\python\framework\op_def_library.py", line 794, in _apply_op_helper
    op_def=op_def)
  File "C:\Users\john0\AppData\Roaming\Python\Python36\site-packages\tensorflow_core\python\util\deprecation.py", line 507, in new_func
    return func(*args, **kwargs)
  File "C:\Users\john0\AppData\Roaming\Python\Python36\site-packages\tensorflow_core\python\framework\ops.py", line 3357, in create_op
    attrs, op_def, compute_device)
  File "C:\Users\john0\AppData\Roaming\Python\Python36\site-packages\tensorflow_core\python\framework\ops.py", line 3426, in _create_op_internal
    op_def=op_def)
  File "C:\Users\john0\AppData\Roaming\Python\Python36\site-packages\tensorflow_core\python\framework\ops.py", line 1748, in __init__
    self._traceback = tf_stack.extract_stack()

How could I deal with the .ckpt files properly?

Should I need to initialize all variables and reload the weights first?

Here is my non-frozen model files.

Please help, thanks!

TseHsien
  • 73
  • 8

0 Answers0