Ped*_*nte 2 convolution tensorflow
我在由2d图像组成的数据样本上使用卷积层.滤波器形状的一个选项是1x2,它作用于两个相邻像素的1x2连续块.如果我想要一个也可以作用于2个像素的滤镜,但是它们之间的另一个像素被分开了怎么办?是否有可能在神经网络中编码这样的滤波器用于卷积?
下面是一些示例代码,它定义了Conv2d的内核和5x5掩码,它只允许中心和外部值通过.
import tensorflow as tf
import numpy as np
image = np.array( range(25) ).reshape([1,5,5,1] ).astype( float )
image = tf.stop_gradient( tf.constant( image , dtype=tf.float32 ) )
kern = tf.Variable( tf.ones( [5,5,1,1] , dtype=tf.float32) )
mask = np.array([[ 1., 1., 1., 1., 1.],
[ 1., 0., 0., 0., 1.],
[ 1., 0., 1., 0., 1.],
[ 1., 0., 0., 0., 1.],
[ 1., 1., 1., 1., 1.]]).reshape( [5,5,1,1] )
mask_variable = tf.Variable( mask , dtype=tf.float32 )
mask = tf.stop_gradient( mask_variable )
output = tf.nn.conv2d( image , kern , strides=[1,1,1,1] , padding="VALID" )
output_with_mask = tf.nn.conv2d( image , kern * mask , strides=[1,1,1,1] , padding="VALID" )
sess = tf.Session()
sess.run(tf.global_variables_initializer())
print "Using whole kernal :",sess.run( output )
print "Using kernal with a mask :",sess.run( output_with_mask )
Run Code Online (Sandbox Code Playgroud)
和输出
Using whole kernal : [[[[ 300.]]]]
Using kernal with a mask : [[[[ 204.]]]]
Run Code Online (Sandbox Code Playgroud)
此外,backprop不会更改掩码,因为掩码包含在tf.stop_gradient中.
是的,有可能像@ vijay-m所说的那样做,不,你可能不想这样做@ Salvador-Dali说.当然,有理由说明为什么你希望能够做到这一点.
干杯
| 归档时间: |
|
| 查看次数: |
947 次 |
| 最近记录: |