Reputation: 515
I have data in the format NHWC: 100 x 64 x 64 x 3
. I want to apply the laplacian filter to each channel separately. I want the output as 100 x 64 x 64 x 3
.
k = tf.reshape(tf.constant([[0, -1, 0], [-1, 4, -1], [0, -1, 0]], tf.float32), [3, 3, 1, 1])
I tried this, but this throws an error of dimensions. It expects 3 channels as input. output = tf.abs(tf.nn.conv2d(input, k, strides=[1, 1, 1, 1], padding='SAME'))
I modified k = tf.reshape(tf.constant([[0, -1, 0], [-1, 4, -1], [0, 1, 0]]*3, tf.float32), [3, 3, 3, 1])
, but this just outputs 1 feature map 100 x 64 x 64 x 1
.
`
I tried using tf.nn.depthwise_conv2d
but its throwing the same error. How do I actually implement it?
output = tf.abs(tf.nn.depthwise_conv2d(input, k, strides=[1, 1, 1, 1], padding='SAME'))
Upvotes: 2
Views: 223
Reputation: 24581
This is what tf.nn.depthwise_conv2d
does. However, it is more general than that and actually let you choose one or more convolution kernels per channel.
If you want to have the same kernel for all channels, you need to duplicate the kernel to match the number of channels. E.g.
# my 2D conv kernel
k = tf.constant([[0, -1, 0], [-1, 4, -1], [0, 1, 0]], tf.float32)
# duplicate my kernel channel_in times
k = tf.tile(k[...,tf.newaxis], [1, 1, channel_in])[...,tf.newaxis]
# apply conv
tf.nn.depthwise_conv2d(input, k, strides=[1, 1, 1, 1], padding='SAME')
Upvotes: 2