使用tf.data.Datasets冻结Tensorflow图时确定输入节点

时间:2018-06-20 18:48:31

标签: python tensorflow

我将Tensorflow tf.data.Dataset API用作输入管道,如下所示:

train_dataset = tf.data.Dataset.from_tensor_slices((trn_X,trn_y))
train_dataset = 
train_dataset.map(_trn_parse_function,num_parallel_calls=12)
train_dataset = 
train_dataset.shuffle(buffer_size=1000).repeat(args.num_epochs)# 
.batch(args.batch_size)
train_dataset = train_dataset.apply(tf.contrib.data.batch_and_drop_remainder(args.batch_size))
train_dataset = train_dataset.prefetch(buffer_size=600)



val_dataset = tf.data.Dataset.from_tensor_slices((val_X,val_y))
val_dataset = val_dataset.map(_val_parse_function,num_parallel_calls=4)
val_dataset = val_dataset.repeat(1)
val_dataset = val_dataset.apply(tf.contrib.data.batch_and_drop_remainder(args.batch_size))
val_dataset = val_dataset.prefetch(buffer_size=200)


handle = tf.placeholder(tf.string, shape=[])
iterator = tf.data.Iterator.from_string_handle(
        handle, train_dataset.output_types, 
train_dataset.output_shapes)

images,labels = iterator.get_next()


train_iter = train_dataset.make_initializable_iterator()
val_iter = val_dataset.make_initializable_iterator()

然后使用此代码在训练和验证数据集之间切换:

# Define training and validation handlers
training_handle = sess.run(train_iter.string_handle())
validation_handle = sess.run(val_iter.string_handle())
sess.run(train_iter.initializer)
sess.run(val_iter.initializer)

...
loss = sess.run([train_op],feed_dict={handle:training_handle, 
is_training:True})

训练后,我保存权重,然后从保存的检查点((.meta))将图形冻结为.pb格式。随后,运行tensorflow存储库中提供的optimize_for_inference.py工具。脚本要求定义input_nodes_names。我无法确定哪个是图形的正确输入节点。以下是我的图形的节点:

['Variable/initial_value',
'Variable',
'Variable/Assign',
'Variable/read',
'increment_global_step/value',
'increment_global_step',
'Placeholder',
'is_training',
'tensors/component_0',
'tensors/component_1',
'num_parallel_calls',
'batch_size',
'count',
'buffer_size',
'OneShotIterator',
'IteratorToStringHandle',
'IteratorGetNext',
....
....
'output/Softmax]

可以轻松确定输出节点,而不是输入节点。

1 个答案:

答案 0 :(得分:0)

handle = tf.placeholder(tf.string,shape = [])是您的输入,因此张量很可能是'Placeholder:0'。

但是写起来更有意义:

handle = tf.placeholder(tf.string, shape=[], name="input_placeholder")

那你肯定知道。