Tensorflow:对小批量中的每个样本使用不同过滤器进行卷积

2023-11-23

我想要一个带有过滤器的二维卷积,该过滤器取决于张量流中小批量中的样本。有什么想法可以做到这一点,特别是在每个小批量的样本数量未知的情况下?

具体来说,我有输入数据inp形式的MB x H x W x Channels,我有过滤器F形式的MB x fh x fw x Channels x OutChannels.

假设

inp = tf.placeholder('float', [None, H, W, channels_img], name='img_input').

我想要做tf.nn.conv2d(inp, F, strides = [1,1,1,1]),但是这是不允许的,因为F不能有小批量维度。知道如何解决这个问题吗?


我认为所提出的技巧实际上是不对的。会发生什么情况tf.conv3d()层是输入在深度(=实际批量)维度上进行卷积,然后沿着结果特征图求和。和padding='SAME'结果输出的数量恰好与批量大小相同,所以有人被愚弄了!

编辑:我认为对不同的小批量元素使用不同的过滤器进行卷积的一种可能方法涉及“破解”深度卷积。假设批量大小MB已知:

inp = tf.placeholder(tf.float32, [MB, H, W, channels_img])

# F has shape (MB, fh, fw, channels, out_channels)
# REM: with the notation in the question, we need: channels_img==channels

F = tf.transpose(F, [1, 2, 0, 3, 4])
F = tf.reshape(F, [fh, fw, channels*MB, out_channels)

inp_r = tf.transpose(inp, [1, 2, 0, 3]) # shape (H, W, MB, channels_img)
inp_r = tf.reshape(inp, [1, H, W, MB*channels_img])

out = tf.nn.depthwise_conv2d(
          inp_r,
          filter=F,
          strides=[1, 1, 1, 1],
          padding='VALID') # here no requirement about padding being 'VALID', use whatever you want. 
# Now out shape is (1, H, W, MB*channels*out_channels)

out = tf.reshape(out, [H, W, MB, channels, out_channels) # careful about the order of depthwise conv out_channels!
out = tf.transpose(out, [2, 0, 1, 3, 4])
out = tf.reduce_sum(out, axis=3)

# out shape is now (MB, H, W, out_channels)

In case MB未知,应该可以使用动态确定它tf.shape()(我认为)

本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

Tensorflow:对小批量中的每个样本使用不同过滤器进行卷积 的相关文章

随机推荐