gpt4 book ai didi

python - 将 batchnorm(TensorFlow) 的 is_training 变为 False

转载 作者:太空宇宙 更新时间:2023-11-04 00:28:59 26 4
gpt4 key购买 nike

我想在训练后将模型的 is_training 状态变为 False,我该怎么做?

net = tf.layers.conv2d(inputs = features, filters = 64, kernel_size = [3, 3], strides = (2, 2), padding = 'same')
net = tf.contrib.layers.batch_norm(net, is_training = True)
net = tf.nn.relu(net)
net = tf.reshape(net, [-1, 64 * 7 * 7]) #
net = tf.layers.dense(inputs = net, units = class_num, kernel_initializer = tf.contrib.layers.xavier_initializer(), name = 'regression_output')

#......
#after training

saver = tf.train.Saver()
saver.save(sess, 'reshape_final.ckpt')
tf.train.write_graph(sess.graph.as_graph_def(), "", 'graph_final.pb')

batchnorm的is_training保存后如何变成False

我尝试了 tensorflow batchnorm turn of trainingtensorflow change state 等关键字,但找不到如何操作。

编辑 1:

感谢@Maxim 的解决方案,它可以工作,但是当我尝试卡住图形时,出现了另一个问题。

命令:

python3 ~/.keras2/lib/python3.5/site-packages/tensorflow/python/tools/freeze_graph.py --input_graph=graph_final.pb --input_checkpoint=reshape_final.ckpt --output_graph=frozen_graph.pb --output_node_names=regression_output/BiasAdd

python3 ~/.keras2/lib/python3.5/site-packages/tensorflow/python/tools/optimize_for_inference.py --input frozen_graph.pb --output opt_graph.pb --frozen_graph True --input_names input --output_names regression_output/BiasAdd

~/Qt/3rdLibs/tensorflow/bazel-bin/tensorflow/tools/graph_transforms/transform_graph --in_graph=opt_graph.pb --out_graph=fused_graph.pb --inputs=input --outputs=regression_output/BiasAdd --transforms="fold_constants sort_by_execution_order fold_batch_norms fold_old_batch_norms"

执行transform_graph后,弹出错误信息

“您必须使用 dtype bool 为占位符张量‘训练’提供一个值”

我通过以下代码保存图形:

sess.run(loss, feed_dict={features : train_imgs, x : real_delta, training : False})
saver = tf.train.Saver()
saver.save(sess, 'reshape_final.ckpt')
tf.train.write_graph(sess.graph.as_graph_def(), "", 'graph_final.pb')

编辑 2:

将占位符更改为变量有效,但转换后的图形无法通过 opencv dnn 加载。

改变

training = tf.placeholder(tf.bool, name='training')

training = tf.Variable(False, name='training', trainable=False)

最佳答案

您应该为模式定义一个placeholder 变量(它可以是 bool 值或字符串),并在训练和测试期间将不同的值传递给session.run。示例代码:

x = tf.placeholder('float32', (None, 784), name='x')
y = tf.placeholder('float32', (None, 10), name='y')
phase = tf.placeholder(tf.bool, name='phase')
...

# training (phase = 1)
sess.run([loss, accuracy],
feed_dict={'x:0': mnist.train.images,
'y:0': mnist.train.labels,
'phase:0': 1})
...

# testing (phase = 0)
sess.run([loss, accuracy],
feed_dict={'x:0': mnist.test.images,
'y:0': mnist.test.labels,
'phase:0': 0})

您可以在 this post 中找到完整的代码.

关于python - 将 batchnorm(TensorFlow) 的 is_training 变为 False,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46498332/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com