0
私は学習したTensorflowモデルをフリーズしようとしています。これは、チュートリアルDeep MNIST for Expertsから撮影されています。そして、私は私のモデルTensorFlow:モデルをフリーズすると、出力ノードのみが格納されているようです。
今from tensorflow.python.tools import freeze_graph
freeze_graph.freeze_graph(input_graph=output_graph_name,
input_saver="",
input_binary=True,
input_checkpoint="my_model",
output_node_names="y_",
restore_op_name="save/restore_all",
filename_tensor_name="save/Const:0",
output_graph="frozen_graph.pb",
clear_devices=True,
initializer_nodes="")
を凍結しよう
def weight_variable(shape):
initial = tf.truncated_normal(shape, stddev=0.1)
return tf.Variable(initial)
def bias_variable(shape):
initial = tf.constant(0.1, shape=shape)
return tf.Variable(initial)
def conv2d(x_vector, w_matrix):
return tf.nn.conv2d(x_vector, w_matrix, strides=[1, 1, 1, 1], padding='SAME')
def max_pool_2x2(x_vector):
return tf.nn.max_pool(x_vector, ksize=[1, 2, 2, 1], strides=[1, 2, 2, 1], padding='SAME')
output_graph_name = 'my_graph.pb'
# Create model
label_count = 12
x = tf.placeholder(tf.float32, shape=[None, 1024], name="x")
y_ = tf.placeholder(tf.float32, shape=[None, label_count], name="y_")
w_conv1 = weight_variable([5, 5, 1, 32])
b_conv1 = bias_variable([32])
x_image = tf.reshape(x, [-1, 32, 32, 1])
h_conv1 = tf.nn.relu(conv2d(x_image, w_conv1) + b_conv1)
h_pool1 = max_pool_2x2(h_conv1)
w_conv2 = weight_variable([5, 5, 32, 64])
b_conv2 = bias_variable([64])
h_conv2 = tf.nn.relu(conv2d(h_pool1, w_conv2) + b_conv2)
h_pool2 = max_pool_2x2(h_conv2)
w_fc1 = weight_variable([8 * 8 * 64, 1024])
b_fc1 = bias_variable([1024])
h_pool2_flat = tf.reshape(h_pool2, [-1, 8 * 8 * 64])
h_fc1 = tf.nn.relu(tf.matmul(h_pool2_flat, w_fc1) + b_fc1)
keep_prob = tf.placeholder(tf.float32)
h_fc1_drop = tf.nn.dropout(h_fc1, keep_prob)
w_fc2 = weight_variable([1024, label_count])
b_fc2 = bias_variable([label_count])
y_conv = tf.matmul(h_fc1_drop, w_fc2) + b_fc2
cross_entropy = tf.reduce_mean(tf.nn.softmax_cross_entropy_with_logits(labels=y_, logits=y_conv))
train_step = tf.train.AdamOptimizer(1e-4).minimize(cross_entropy)
correct_prediction = tf.equal(tf.argmax(y_conv, 1), tf.argmax(y_, 1))
accuracy = tf.reduce_mean(tf.cast(correct_prediction, tf.float32))
# Add ops to save and restore all the variables.
saver = tf.train.Saver()
with tf.Session() as sess:
sess.run(tf.global_variables_initializer())
# ... train, etc etc ...
# train_step.run(feed_dict={x: train_set[0],
# y_: train_set[1],
# keep_prob: 0.5})
# Save the variables to disk.
save_path = saver.save(sess, "my_model.ckpt")
# Save graph
tf.train.write_graph(sess.graph_def, '.', output_graph_name, as_text=False)
、凍結されたグラフはy_
プレースホルダ、および全体ではなく、関連するネットワークが含まれています。 graph_util.extract_sub_graph
は、y_
のみを抽出します。なぜこれが起こるのですか?どうすればネットワーク全体をフリーズできますか? y_
の代わりにconv_y
を使用する必要がありますか? y_
が実際に出力ノードではないことを改めてあなたを説得するには
です。freeze_graphはtf関数ではありません。これ、どこから来たの? 'tf.identity(y_conv、name = 'my_output')'を追加し、この広告をoutput_node_nameとして使用してください。ほとんどのグラフフリーズは、グラフの必要な部分のみを抽出します。そして、 'y_'から' y_'に行くことはあなたの部分グラフのようです。 – Patwie
'from tensorflow.python.tools import freeze_graph'です。 2番目の部分は、 'y_'だけがどのように関連しているのか理解していません。私は[グラフ全体](https://www.tensorflow.org/images/mnist_deep.png)が 'x'(入力ノード)から' y_'(出力ノード)に来ることを期待しています – Maarten
'y_'は入力ノードとトレーニング中の計算の開始を示します。予測y_convとコストノードcross_entropyは出力ノードである。だから私の答えは同じです:tf.identity(y_conv、name = 'my_output')を追加し、 'output_node_names = 'my_output'を使用します。 – Patwie