生活随笔
收集整理的這篇文章主要介紹了
用TensorFlow可视化卷积层的方法
小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
深度學習中對于卷積層的可視化可以幫助理解卷積層的工作原理與訓練狀態,然而卷積層可視化的方法不只一種。最簡單的方法即直接輸出卷積核和卷積后的filter通道,成為圖片。然而也有一些方法試圖通過反卷積(轉置卷積)了解卷積層究竟看到了什么。
在TensorFlow中,即使是最簡單的直接輸出卷積層的方法,網上的講解也參差不齊,David 9 今天要把可運行的方法告訴大家,以免大家受到誤導。
廢話少說,最簡單的方法在此:
如果你有一個卷積層,我們以Tensorflow自帶的cifar-10訓練為例子:
with tf.variable_scope('conv1') as scope: kernel = _variable_with_weight_decay('weights', shape=[5, 5, 3, 64], stddev=5e-2, wd=0.0) conv = tf.nn.conv2d(images, kernel, [1, 1, 1, 1], padding='SAME') biases = _variable_on_cpu('biases', [64], tf.constant_initializer(0.0)) pre_activation = tf.nn.bias_add(conv, biases) conv1 = tf.nn.relu(pre_activation, name=scope.name) _activation_summary(conv1)
不出所料的話你一定會有以上代碼,這是第一層卷積層conv1的TensorFlow流圖定義。顯然這里conv1對象是卷積層的激活輸出。我們要做的就是直接可視化輸出。在這個scope中加上如下代碼:
with tf.variable_scope('visualization'): # scale weights to [0 1], type is still float x_min = tf.reduce_min(kernel) x_max = tf.reduce_max(kernel) kernel_0_to_1 = (kernel - x_min) / (x_max - x_min) # to tf.image_summary format [batch_size, height, width, channels] kernel_transposed = tf.transpose (kernel_0_to_1, [3, 0, 1, 2]) # this will display random 3 filters from the 64 in conv1 tf.summary.image('conv1/filters', kernel_transposed, max_outputs=3) layer1_image1 = conv1[0:1, :, :, 0:16] layer1_image1 = tf.transpose(layer1_image1, perm=[3,1,2,0]) tf.summary.image("filtered_images_layer1", layer1_image1, max_outputs=16)
即總體變為:
with tf.variable_scope('conv1') as scope: kernel = _variable_with_weight_decay('weights', shape=[5, 5, 3, 64], stddev=5e-2, wd=0.0) conv = tf.nn.conv2d(images, kernel, [1, 1, 1, 1], padding='SAME') biases = _variable_on_cpu('biases', [64], tf.constant_initializer(0.0)) pre_activation = tf.nn.bias_add(conv, biases) conv1 = tf.nn.relu(pre_activation, name=scope.name) _activation_summary(conv1) with tf.variable_scope('visualization'): # scale weights to [0 1], type is still float x_min = tf.reduce_min(kernel) x_max = tf.reduce_max(kernel) kernel_0_to_1 = (kernel - x_min) / (x_max - x_min) # to tf.image_summary format [batch_size, height, width, channels] kernel_transposed = tf.transpose (kernel_0_to_1, [3, 0, 1, 2]) # this will display random 3 filters from the 64 in conv1 tf.summary.image('conv1/filters', kernel_transposed, max_outputs=3) layer1_image1 = conv1[0:1, :, :, 0:16] layer1_image1 = tf.transpose(layer1_image1, perm=[3,1,2,0]) tf.summary.image("filtered_images_layer1", layer1_image1, max_outputs=16)
加入的功能是在TensorBoard中隨機顯示3張卷積核,并且,顯示16張卷積后的輸出filter通道。
知道講解的是,這里的tf.transpose()方法,是轉置方法。
tf.transpose(layer1_image1, perm=[3,1,2,0])
這句代碼表示把第0維和第3維調換,因為圖片輸出函數
tf.summary.image()
需要輸入維度的格式是(batch數,長,寬,彩色通道),而剛才卷積輸出得到的是(batch數,長,寬,卷積通道), 現在的彩色通道是應該是空,現在batch數應該是剛才卷積輸出的彩色通道數。
總之加了以上visualization 的scope之后,就能實時跑了。親測可用。輸出樣例如下:
?
參考文獻:
http://stackoverflow.com/questions/35759220/how-to-visualize-learned-filters-on-tensorflow https://github.com/tensorflow/tensorflow/issues/842 https://github.com/yosinski/deep-visualization-toolbox https://github.com/tensorflow/tensorflow/issues/908 https://medium.com/@awjuliani/visualizing-neural-network-layer-activation-tensorflow-tutorial-d45f8bf7bbc4 https://gist.github.com/kukuruza/03731dc494603ceab0c5
source:?http://nooverfit.com/wp/%E7%94%A8tensorflow%E5%8F%AF%E8%A7%86%E5%8C%96%E5%8D%B7%E7%A7%AF%E5%B1%82%E7%9A%84%E6%96%B9%E6%B3%95/#comment-900
與50位技術專家面對面20年技術見證,附贈技術全景圖
總結
以上是生活随笔為你收集整理的用TensorFlow可视化卷积层的方法的全部內容,希望文章能夠幫你解決所遇到的問題。
如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。