映象問題:pytorch 自定義卷積核進行卷積--卷積核訂製
有時想用tensorflow的conv2d的卷積操作,實現一些特定的濾波操作,如patch求和、計算梯度等,這時可以通過設計特定的卷積核來實現功能。
先看tf.nn.conv2d的各個引數:
主要需要注意的是:input,filter,strides的shape及各維度的含義。尤其filter各維度的含義與input是不同的。stride是4個維度。tf.nn.conv2d (input, filter, strides, padding, use_cudnn_on_gpu=none, data_format=none, name=none)
引數: input : tf張量,shape=[n,h,w,c] ,dtype=tf.float32(最好)
filter: 卷積核,tf張量,dtype=tf.float32(與input保持一致),
shape=[filter_height,filter_weight,input_channel=c,output_channels ],
其中 filter_height 為卷積核高度,
filter_weight 為卷積核寬度,
in_channel 是影象通道數 ,和 input 的 in_channel 要保持一致,
out_channel 是卷積核的個數,代表輸出的feature_map的通道數。
strides: 卷積時在影象每一維的步長,這是乙個一維的向量,[ 1, strides, strides, 1],
第一位和最後一位固定必須是1,最後乙個維度步長必須是1,是因為卷積是逐個通道進行的。
padding: string型別,值為「same」 和 「valid」,表示的是卷積的形式,是否考慮邊界。
"same"是考慮邊界,不足的時候用0去填充周圍,
"valid"則不考慮
use_cudnn_on_gpu: bool型別,是否使用cudnn加速,預設為true
示例:
返回結果:"""
指令碼功能:將256*256的影象不重疊的劃分為64*64的patch,計算每個patch中的畫素值之和
實現:設計64*64的全1卷積核,以stride=64移動,不新增0 padding
"""feature_map = tf.ones([1,256,256,1],dtype=tf.float32)
filter = tf.ones((64,64,1,1),dtype=tf.float32)
padding = "valid"
result = tf.nn.conv2d(feature_map, filter, strides=(1,64,64,1), padding=padding)
print(result.shape)
with tf.session() as sess:
print(sess.run(result))
(1, 4, 4, 1)
[[[[4096.]
[4096.]
[4096.]
[4096.]]
[[4096.]
[4096.]
[4096.]
[4096.]]
[[4096.]
[4096.]
[4096.]
[4096.]]
[[4096.]
[4096.]
[4096.]
[4096.]]]]
Tensorflow卷積介面總結
這個介面用了這麼久,每次都有點迷惑,這裡做下總結。filter 卷積核引數,shape為 kernel height,kernel width,in channels,out channels 第三維與input的第四維對應,第四維決定了卷積核的個數 stride 步長,一維向量,4個值,其中 0 ...
tensorflow實現卷積與反卷積自編碼框架
從dcgan中了解到了反卷積的操作,所以我本來打算能通過卷積操作作為編碼器將一幀影象轉換為乙個20維的向量,而後再通過反卷積實現解碼功能從而達到影象恢復效果,先把程式貼上,後續有空再調整網路層數和引數吧 from tensorflow.examples.tutorials.mnist import ...
Tensorflow卷積神經網路
卷積神經網路 convolutional neural network,cnn 是一種前饋神經網路,在計算機視覺等領域被廣泛應用.本文將簡單介紹其原理並分析tensorflow官方提供的示例.關於神經網路與誤差反向傳播的原理可以參考作者的另一篇博文bp神經網路與python實現.卷積是影象處理中一種...