当冻结带有局部变量的图时,freeze_graph出现错误,指出“尝试使用未初始化的值...”。有问题的局部变量是通过以下方式初始化的:
with tf.variable_scope(tf.get_variable_scope(),reuse=tf.AUTO_REUSE):
b_init = tf.constant(10.0, shape=[2, 1], dtype="float32",name = 'bi')
b = tf.get_variable('b',initializer=b_init,collections=[tf.GraphKeys.LOCAL_VARIABLES])
我能够创建一个保存的模型并运行该保存的模型。但是,我试图冻结另一个图进行优化。如果删除“ LOCAL_VARIABLES”标志,此错误将消失。但是,此变量然后变为全局变量,这会导致重新加载我的检查点时出现问题(Tensorflow无法在检查点中找到该变量)。
通常,我希望Frozen_graph使用'b_init'初始化'b'。
重现该问题的代码:
import os, sys, json
import tensorflow as tf
from tensorflow.python.lib.io import file_io
from tensorflow.core.framework import variable_pb2
from tensorflow.python.framework import ops
from tensorflow.python.ops import variables
from tensorflow.python.framework.ops import register_proto_function
from tensorflow.python.saved_model import tag_constants
from tensorflow.python.tools import freeze_graph
from tensorflow.python import ops
from tensorflow.tools.graph_transforms import TransformGraph
#flags
tf.app.flags.DEFINE_integer('model_version',1,'Models version number.')
tf.app.flags.DEFINE_string('export_model_dir','../model_batch/versions', 'Directory where model will be exported to')
FLAGS = tf.app.flags.FLAGS
def main(_):
''' main function'''
a = tf.placeholder(dtype = tf.float32, shape = [2,1])
with tf.variable_scope(tf.get_variable_scope(),reuse=tf.AUTO_REUSE):
b_init = tf.constant(10.0, shape=[2, 1], dtype="float32",name = 'bi')
b = tf.get_variable('b',initializer=b_init,collections=[tf.GraphKeys.LOCAL_VARIABLES])
b = tf.assign(b,a)
c = []
for d in range(5):
b = b * 1.1
c.append(b)
c = tf.identity(c,name = 'c')
init = tf.group(tf.global_variables_initializer(),
tf.local_variables_initializer())
with tf.Session() as sess:
#init
sess.run(init)
print(tf.get_default_graph().get_collection(tf.GraphKeys.LOCAL_VARIABLES))
#create saved model builder class
export_path_base = FLAGS.export_model_dir
export_path = os.path.join(
tf.compat.as_bytes(export_path_base),
tf.compat.as_bytes(str(FLAGS.model_version)))
if tf.gfile.Exists(export_path):
print ('Removing previous artifacts')
tf.gfile.DeleteRecursively(export_path)
#inputs
tensor_info_a = tf.saved_model.utils.build_tensor_info(a)
#outputs
tensor_info_c = tf.saved_model.utils.build_tensor_info(c)
print('Exporting trained model to', export_path)
builder = tf.saved_model.builder.SavedModelBuilder(export_path)
#define signatures
prediction_signature = (
tf.saved_model.signature_def_utils.build_signature_def(
inputs={'cameras': tensor_info_a},
outputs = {'depthmap' : tensor_info_c},
method_name=tf.saved_model.signature_constants.PREDICT_METHOD_NAME))
builder.add_meta_graph_and_variables(
sess, [tf.saved_model.tag_constants.SERVING],
signature_def_map = {'predict_batch': prediction_signature})
#export model
builder.save(as_text=True)
writer = tf.summary.FileWriter("output_batch", sess.graph)
writer.close()
#load graph from saved model
print ('Freezing graph')
initializer_nodes = ''
output_node_names = 'c'
saved_model_dir = os.path.join(FLAGS.export_model_dir,str(FLAGS.model_version))
output_graph_filename = os.path.join(saved_model_dir,'frozen_graph.pb')
freeze_graph.freeze_graph(
input_saved_model_dir=saved_model_dir,
output_graph=output_graph_filename,
saved_model_tags = tag_constants.SERVING,
output_node_names=output_node_names,
initializer_nodes=initializer_nodes,
input_graph=None,
input_saver=False,
input_binary=False,
input_checkpoint=None,
restore_op_name=None,
filename_tensor_name=None,
clear_devices=False)
if __name__ == '__main__':
tf.app.run()
答案 0 :(得分:0)
我无法在冻结的图形中包含local_variables,但是我确实提出了解决方案。
最初的问题是我的检查点是从包含local_variables的图形创建的。不幸的是,冻结图形会产生错误:
Attempting to use uninitialized value...
要解决此问题,我所做的就是将局部变量更改为不可训练的全局变量。然后,使用以下解决方案过滤掉不在检查点中的全局变量:
https://stackoverflow.com/a/39142780/6693924
我能够创建一个saveModel并冻结其图形。