標籤:

TensorFlow保留中間層的Feature Map

【1】在inference裡面(每個卷積層後面),寫:

#卷積層nconv1 = conv_layer_with_bn(norm1, [7, 7, images.get_shape().as_list()[3], 64], phase_train, name="conv1")n#保存特徵圖nshow_feature_map(layer = conv1, layer_name="conv1", num_or_size_splits=64, axis=3, max_outputs=3)n

其中,show_feature_map()的定義如下:

def show_feature_map(layer, layer_name, num_or_size_splits, axis, max_outputs):n split = tf.split(layer, num_or_size_splits=num_or_size_splits, axis=axis)n for i in range(num_or_size_splits):n tf.summary.image(layer_name + "-" + str(i), split[i], max_outputs)n

【2】在「with tf.Graph().as_default():」的最後,寫:

summary_op = tf.summary.merge_all()n

【3】在「with tf.Session() as sess:」裡面,讀取完數據之後,寫:

summary_writer = tf.summary.FileWriter(train_dir, tf.get_default_graph())n

【4】在要保存summary的時候,寫:

summary_str = sess.run(summary_op, feed_dict=feed_dict)nsummary_writer.add_summary(summary_str, step)n

【5】用TensorBoard查看,如下圖所示:

【6】怎樣將這些圖片批量下載到本地?我寫了一個python腳本:

from urllib import requestnnndef get_feature_maps(layer_num, feature_map_num, layer_name="conv", save_dir="G:Deep_Learning_ExperimentsSegNet-TensorFlow-SerialsTensorflow-Feature-Map-SegNet-deconv-TSD-Lane-Train-200000-20171207Feature-Map"):n url = http://127.0.0.1:6006/data/plugin/images/individualImage?ts=1512829497.9180086&sample=0&index=0&tag= + str(layer_name) + str(n layer_num) + - + str(feature_map_num) + %2Fimage%2F0&run=.n save_path = save_dir + "" + layer_name + str(layer_name) + str(layer_num) + - + str(feature_map_num) + ".png"n with request.urlopen(url) as web:n with open(save_path, wb) as outfile:n outfile.write(web.read())nndef main():n for i in range(1,5):n for j in range(64):n get_feature_maps(i, j)nnif __name__ == "__main__":n main()n

推薦閱讀:

Tensorflow 2018學習筆記-04.TensorBoard可視化
譯文 | 簡明 TensorFlow 教程:混合模型
實現屬於自己的TensorFlow(二) - 梯度計算與反向傳播
Deep Learning的IR「之爭」
深度學習對話系統實戰篇--老版本tf.contrib.legacy_seq2seq API介紹和源碼解析

TAG:TensorFlow |