我的数据格式为NHWC:100 x 64 x 64 x 3
。我想分别将拉普拉斯滤波器应用于每个通道。我希望输出为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])
我尝试了这个,但这会引发尺寸错误。它期望3个频道作为输入。 output = tf.abs(tf.nn.conv2d(input,k,strides = [1,1,1,1],padding ='SAME'))
我修改了k = tf.reshape(tf.constant([[0, -1, 0], [-1, 4, -1], [0, 1, 0]]*3, tf.float32), [3, 3, 3, 1])
,但这只输出了1个要素图100 x 64 x 64 x 1
。
`
我尝试使用tf.nn.depthwise_conv2d
,但它抛出同样的错误。我如何实际实现它?
output = tf.abs(tf.nn.depthwise_conv2d(input, k, strides=[1, 1, 1, 1], padding='SAME'))
答案 0 :(得分:2)
这是tf.nn.depthwise_conv2d
的作用。但是,它更通用,实际上让你选择一个或多个卷积内核每个通道。
如果要为所有通道使用相同的内核,则需要复制内核以匹配通道数。 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')