2017-08-24 124 views
3

對此問題的更準確描述是,當is_training未明確設置爲true時,MobileNet表現不佳。 我指的是TensorFlow在其模型庫https://github.com/tensorflow/models/blob/master/slim/nets/mobilenet_v1.py中提供的MobileNet。設置is_training爲false時,MobileNet不可用

這是我如何創建網(phase_train = TRUE):

with slim.arg_scope(mobilenet_v1.mobilenet_v1_arg_scope(is_training=phase_train)): 
     features, endpoints = mobilenet_v1.mobilenet_v1(
      inputs=images_placeholder, features_layer_size=features_layer_size, dropout_keep_prob=dropout_keep_prob, 
      is_training=phase_train) 

我培養了識別網絡和訓練時我測試的LFW。我在訓練過程中得到的結果隨着時間的推移而逐漸改善,並獲得了很好的準確性。

部署之前我凍結了圖形。如果我使用is_training = True來凍結圖形,那麼我在LFW上得到的結果與訓練期間的結果相同。 但是,如果我設置is_training = False我得到的結果像網絡沒有訓練...

這種行爲實際上發生在像其他網絡,如先知。

我傾向於相信,我錯過了一些非常基本的這裏,這是不是在TensorFlow一個bug ......

任何幫助,將不勝感激。

添加更多的代碼...

這是我的訓練做準備:

images_placeholder = tf.placeholder(tf.float32, shape=(None, image_size, image_size, 1), name='input') 
labels_placeholder = tf.placeholder(tf.int32, shape=(None)) 
dropout_placeholder = tf.placeholder_with_default(1.0, shape=(), name='dropout_keep_prob') 
phase_train_placeholder = tf.Variable(True, name='phase_train') 

global_step = tf.Variable(0, name='global_step', trainable=False) 

# build graph 

with slim.arg_scope(mobilenet_v1.mobilenet_v1_arg_scope(is_training=phase_train_placeholder)): 
    features, endpoints = mobilenet_v1.mobilenet_v1(
     inputs=images_placeholder, features_layer_size=512, dropout_keep_prob=1.0, 
     is_training=phase_train_placeholder) 

# loss 

logits = slim.fully_connected(inputs=features, num_outputs=train_data.get_class_count(), activation_fn=None, 
           weights_initializer=tf.truncated_normal_initializer(stddev=0.1), 
           weights_regularizer=slim.l2_regularizer(scale=0.00005), 
           scope='Logits', reuse=False) 

tf.losses.sparse_softmax_cross_entropy(labels=labels_placeholder, logits=logits, 
             reduction=tf.losses.Reduction.MEAN) 

loss = tf.losses.get_total_loss() 

# normalize output for inference 

embeddings = tf.nn.l2_normalize(features, 1, 1e-10, name='embeddings') 

# optimizer 

optimizer = tf.train.AdamOptimizer() 
train_op = optimizer.minimize(loss, global_step=global_step) 

這是我的火車步:

batch_data, batch_labels = train_data.next_batch() 
feed_dict = { 
    images_placeholder: batch_data, 
    labels_placeholder: batch_labels, 
    dropout_placeholder: dropout_keep_prob 
} 
_, loss_value = sess.run([train_op, loss], feed_dict=feed_dict) 

我可以添加代碼我如何凍結圖表,但並不是真的有必要。用is_train = false構建圖表就足夠了,加載最新的檢查點並在LWF上運行評估來重現問題。

更新...

我發現問題是在批標準化層。將此圖層設置爲is_training = false以重現問題就足​​夠了。

引用,我發現這個後發現:

http://ruishu.io/2016/12/27/batchnorm/

https://github.com/tensorflow/tensorflow/issues/10118

Batch Normalization - Tensorflow

將與解決方案更新一次我有一個測試之一。

回答

2

所以我找到了解決方案。 主要是利用這個參考:http://ruishu.io/2016/12/27/batchnorm/

從鏈接:

注意:當is_training是真正的moving_mean和moving_variance需要更新,默認情況下update_ops放在tf.GraphKeys.UPDATE_OPS所以他們需要添加爲train_op的依賴項,例如:

update_ops = tf.get_collection(tf.GraphKeys.UPDATE_OPS)if update_ops:updates = tf.group(* update_ops)total_loss = control_flow_ops.with_dependencies([updates] ,total_loss)

而且要開門見山的 而不是創建優化像這樣:

optimizer = tf.train.AdamOptimizer(learning_rate=learning_rate) 
train_op = optimizer.minimize(total_loss, global_step=global_step) 

做這樣的:

update_ops = tf.get_collection(tf.GraphKeys.UPDATE_OPS) 
with tf.control_dependencies(update_ops): 
    optimizer = tf.train.AdamOptimizer(learning_rate=learning_rate) 
    train_op = optimizer.minimize(total_loss, global_step=global_step) 

這將解決這一問題。

0

is_training不應該有這種效果。我需要查看更多的代碼才能瞭解發生了什麼,但是當您將is_training設置爲false時,變量名稱不匹配,可能是因爲變量範圍重用問題。

+0

增加了更多的代碼。謝謝 –

相關問題