學習筆記TF013:卷積、跨度、邊界填充、卷積核

卷積運算,兩個輸入張量(輸入數據和卷積核)進行卷積,輸出代表來自每個輸入的信息張量。tf.nn.conv2d完成卷積運算。卷積核(kernel),權值、濾波器、卷積矩陣或模版,filter。權值訓練習得。卷積核(filter參數)權值數量決定需要學習卷積核數量。通道,計算機器視覺,描述輸出向量。RGB圖像,3個代表秩1張量[red,green,blue]通道。輸出與input_batch同秩張量,與卷積核維數相同。兩個張量卷積生成特徵圖(feature map)。特徵圖為輸出添加新層代表張量卷積。訪問輸入批數據和特徵圖元素用相同索引,可了解輸入與kernel卷積運算值變化。層,輸出新維度。

計算機視覺卷積價值,修改卷積核strides(跨度)參數實現輸入降維。strides參數使卷積核無需遍歷每個輸入元素,跳過部分圖像像素。kernel在input_batch滑動,跨過部分元素,每次移動以input_batch一個元素為中心。位置重疊值相乘,乘積相加,得卷積結果。逐點相乘,整合兩個輸入。設置跨度,調整輸入張量維數。降維減少運算量,避免重疊感受域。strides參數格式與輸入向量相同(image_batch_size_stride、image_height_stride、image_width_stride、image_channels_stride)。

邊界填充,卷積核與圖像尺寸不匹配,填充圖像缺失區域。TensorFlow用0填充。padding參數控制conv2d零填充數或錯誤狀態。SAME:卷積輸出輸入尺寸相同,不考慮濾波器尺寸,缺失像素填充0,卷積核掃像素數大於圖像實際像素數。VALID:考慮濾波器尺寸。盡量不越過圖像邊界,也可能邊界被填充。

data_format修改數據格式。NHWC指定輸入輸出數據格式,[batch_size(批數據張量數)、in_height(批數據張量高度)、in_width(批數據張量寬度)、in_channels(批數據張量通道數)]。NCHW指定輸入輸出數據格式,[batch_size、in_channels、in_height、in_width]。

TensorFlow濾波器參數指定輸入卷積運算卷積核。濾波器使用特定模式突出圖像中感興趣特徵。圖像與邊緣檢測卷積核的卷積輸出是所有檢測邊緣區域。tf.minimum和tf.nn.relu使卷積值保持在RGB顏色值合法範圍[0,255]內。卷積核初值隨機設定,訓練迭代,值由CNN學習層自動調整,訓練一迭代,接收圖像,與卷積核卷積,預測結果與圖像真實標籤是否一致,調整卷積核。

import tensorflow as tfnn input_batch = tf.constant([nn [#第1個輸入nn [[0.0],[1.0]],nn [[2.0],[3.0]]nn ],nn [#第2個輸入nn [[2.0],[4.0]],nn [[6.0],[8.0]]nn ]nn ])nn print input_batchnn kernel = tf.constant([nn [nn [[1.0, 2.0]]nn ]nn ])nn print kernelnn conv2d = tf.nn.conv2d(input_batch, kernel, strides=[1, 1, 1, 1], padding=SAME)#conv2d卷積運算nn print conv2dnn sess = tf.Session()nn print sess.run(conv2d)nn lower_right_image_pixel = sess.run(input_batch)[0][1][1]nn lower_right_kernel_pixel = sess.run(conv2d)[0][1][1]nn print lower_right_image_pixel, lower_right_kernel_pixelnn input_batch2 = tf.constant([nn [#第1個輸入(6x6x1)nn [[0.0],[1.0],[2.0],[3.0],[4.0],[5.0]],nn [[0.1],[1.1],[2.1],[3.1],[4.1],[5.1]],nn [[0.2],[1.2],[2.2],[3.2],[4.2],[5.2]],nn [[0.3],[1.3],[2.3],[3.3],[4.3],[5.3]],nn [[0.4],[1.4],[2.4],[3.4],[4.4],[5.4]],nn [[0.5],[1.5],[2.5],[3.5],[4.5],[5.5]]nn ]nn ])nn print input_batch2nn kernel2 = tf.constant([#卷積核(3x3x1)nn [[[0.0]], [[0.5]], [[0.0]]],nn [[[0.0]], [[1.0]], [[0.0]]],nn [[[0.0]], [[0.5]], [[0.0]]]nn ])nn print kernel2nn conv2d2 = tf.nn.conv2d(input_batch2, kernel2, strides=[1, 3, 3, 1], padding=SAME)nn print conv2d2nn print sess.run(conv2d2)nn lower_right_image_pixel2 = sess.run(input_batch2)[0][1][1]nn lower_right_kernel_pixel2 = sess.run(conv2d2)[0][1][1]nn print lower_right_image_pixel2, lower_right_kernel_pixel2nn input_batch3 = tf.constant([nn [#第1個輸入(6x6x1)nn [[0.0,1.0,2.0],[1.0,2.0,3.0]],nn [[0.1,1.1,2.1],[1.1,2.1,3.1]],nn [[0.2,1.2,2.2],[1.2,2.2,3.2]],nn [[0.3,1.3,2.3],[1.3,2.3,3.3]],nn [[0.4,1.4,2.4],[1.4,2.4,3.4]],nn [[0.5,1.5,2.5],[1.5,2.5,3.5]]nn ]nn ])nn print input_batch3nn kernel3 = tf.constant([nn [nn [[-1., 0., 0.],[0., -1., 0.],[0., 0., -1.]],nn [[-1., 0., 0.],[0., -1., 0.],[0., 0., -1.]],nn [[-1., 0., 0.],[0., -1., 0.],[0., 0., -1.]]nn ],nn [nn [[-1., 0., 0.],[0., -1., 0.],[0., 0., -1.]],nn [[8., 0., 0.],[0., 8., 0.],[0., 0., 8.]],nn [[-1., 0., 0.],[0., -1., 0.],[0., 0., -1.]]nn ],nn [nn [[-1., 0., 0.],[0., -1., 0.],[0., 0., -1.]],nn [[-1., 0., 0.],[0., -1., 0.],[0., 0., -1.]],nn [[-1., 0., 0.],[0., -1., 0.],[0., 0., -1.]]nn ]nn ])nn print kernel3nn conv2d3 = tf.nn.conv2d(input_batch3, kernel3, strides=[1, 1, 1, 1], padding=SAME)nn print conv2d3nn activation_map3 = sess.run(tf.minimum(tf.nn.relu(conv2d3), 255))nn print activation_map3nn lower_right_image_pixel3 = sess.run(input_batch3)[0][1][1]nn lower_right_kernel_pixel3 = sess.run(conv2d3)[0][1][1]nn print lower_right_image_pixel3, lower_right_kernel_pixel3nn kernel4 = tf.constant([nn [nn [[0., 0., 0.],[0., 0., 0.],[0., 0., 0.]],nn [[-1., 0., 0.],[0., -1., 0.],[0., 0., -1.]],nn [[0., 0., 0.],[0., 0., 0.],[0., 0., 0.]]nn ],nn [nn [[-1., 0., 0.],[0., -1., 0.],[0., 0., -1.]],nn [[5., 0., 0.],[0., 5., 0.],[0., 0., 5.]],nn [[-1., 0., 0.],[0., -1., 0.],[0., 0., -1.]]nn ],nn [nn [[0., 0., 0.],[0., 0., 0.],[0., 0., 0.]],nn [[-1., 0., 0.],[0., -1., 0.],[0., 0., -1.]],nn [[0., 0., 0.],[0., 0., 0.],[0., 0., 0.]]nn ]nn ])nn print kernel4nn conv2d4 = tf.nn.conv2d(input_batch3, kernel4, strides=[1, 1, 1, 1], padding=SAME)nn print conv2d4nn activation_map4 = sess.run(tf.minimum(tf.nn.relu(conv2d4), 255))nn print activation_map4nn lower_right_image_pixel4 = sess.run(input_batch3)[0][1][1]nn lower_right_kernel_pixel4 = sess.run(conv2d4)[0][1][1]nn print lower_right_image_pixel4, lower_right_kernel_pixel4n

參考資料:

《面向機器智能的TensorFlow實踐》

歡迎加我微信交流:qingxingfengzi

我的微信公眾號:qingxingfengzigz

我老婆張幸清的微信公眾號:qingqingfeifangz

推薦閱讀:

某熊周刊系列:一周推薦外文技術資料(2.5)
機器學習系列-Logistic回歸:我看你像誰 (下篇)
深度學習一行一行敲faster rcnn-keras版(3.3,calc_rpn函數)
大數據與深度學習區別?
Python · 樸素貝葉斯(二)· MultinomialNB

TAG:TensorFlow | 机器学习 | 深度学习DeepLearning |