我想要一个带过滤器的2d卷积,这取决于张量流中的小批量样本。任何想法如何做到这一点,特别是如果每个小批量的样品数量未知?
具体而言,我输入了[{
"name": "shubham"
}, {
"name": "shubham",
"surname": "xaio"
}]
表单的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])
不能拥有小批量维度。知道如何解决这个问题吗?
答案 0 :(得分:3)
我认为提议的技巧实际上是不对的。 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)
如果MB
未知,应该可以使用tf.shape()
(我认为)
答案 1 :(得分:3)
接受的答案在如何处理维度方面略有错误,因为它们是通过填充=" VALID"来改变的。 (他把它们视为填充="相同")。因此,在一般情况下,由于这种不匹配,代码将崩溃。我附上他更正的代码,两种情况都得到了正确的处理。
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_r, [1, H, W, MB*channels_img])
padding = "VALID" #or "SAME"
out = tf.nn.depthwise_conv2d(
inp_r,
filter=F,
strides=[1, 1, 1, 1],
padding=padding) # here no requirement about padding being 'VALID', use whatever you want.
# Now out shape is (1, H-fh+1, W-fw+1, MB*channels*out_channels), because we used "VALID"
if padding == "SAME":
out = tf.reshape(out, [H, W, MB, channels, out_channels)
if padding == "VALID":
out = tf.reshape(out, [H-fh+1, W-fw+1, MB, channels, out_channels)
out = tf.transpose(out, [2, 0, 1, 3, 4])
out = tf.reduce_sum(out, axis=3)
# out shape is now (MB, H-fh+1, W-fw+1, out_channels)
答案 2 :(得分:2)
他们绕过它的方法是使用
添加额外的维度tf.expand_dims(inp, 0)
创建一个假冒的'批量大小。然后使用
tf.nn.conv3d()
过滤器深度与批量大小匹配的操作。这将导致每个过滤器在每批中仅与一个样本卷积。
可悲的是,你不会用这种方式解决变量批量问题,只能解决卷积问题。
答案 3 :(得分:1)
您可以使用tf.map_fn
,如下所示:
inp = tf.placeholder(tf.float32, [None, h, w, c_in])
def single_conv(tupl):
x, kernel = tupl
return tf.nn.conv2d(x, kernel, strides=(1, 1, 1, 1), padding='VALID')
# Assume kernels shape is [tf.shape(inp)[0], fh, fw, c_in, c_out]
batch_wise_conv = tf.squeeze(tf.map_fn(
single_conv, (tf.expand_dims(inp, 1), kernels), dtype=tf.float32),
axis=1
)
为dtype
指定map_fn
非常重要。基本上,此解决方案定义了batch_dim_size
2D卷积运算。