转置卷积(反卷积)算法
Transpose convolution (deconvolution) arithmetic
我正在使用tensorflow构建一个卷积神经网络。给定一个形状为 (none, 16, 16, 4, 192) 的张量,我想执行一个转置卷积,得到形状 (none, 32, 32, 7, 192)。
过滤器尺寸 [2,2,4,192,192] 和步幅 [2,2,1,1,1] 会产生我想要的输出形状吗?
是的,你几乎是对的。
一个小的更正是 tf.nn.conv3d_transpose
需要 NCDHW
或 NDHWC
输入格式(你的看起来是 NHWDC
)并且过滤器形状应该是 [depth, height, width, output_channels, in_channels]
。这会影响 filter
和 stride
:
中的维度顺序
# Original format: NHWDC.
original = tf.placeholder(dtype=tf.float32, shape=[None, 16, 16, 4, 192])
print original.shape
# Convert to NDHWC format.
input = tf.reshape(original, shape=[-1, 4, 16, 16, 192])
print input.shape
# input shape: [batch, depth, height, width, in_channels].
# filter shape: [depth, height, width, output_channels, in_channels].
# output shape: [batch, depth, height, width, output_channels].
filter = tf.get_variable('filter', shape=[4, 2, 2, 192, 192], dtype=tf.float32)
conv = tf.nn.conv3d_transpose(input,
filter=filter,
output_shape=[-1, 7, 32, 32, 192],
strides=[1, 1, 2, 2, 1],
padding='SAME')
print conv.shape
final = tf.reshape(conv, shape=[-1, 32, 32, 7, 192])
print final.shape
输出:
(?, 16, 16, 4, 192)
(?, 4, 16, 16, 192)
(?, 7, 32, 32, 192)
(?, 32, 32, 7, 192)
我正在使用tensorflow构建一个卷积神经网络。给定一个形状为 (none, 16, 16, 4, 192) 的张量,我想执行一个转置卷积,得到形状 (none, 32, 32, 7, 192)。
过滤器尺寸 [2,2,4,192,192] 和步幅 [2,2,1,1,1] 会产生我想要的输出形状吗?
是的,你几乎是对的。
一个小的更正是 tf.nn.conv3d_transpose
需要 NCDHW
或 NDHWC
输入格式(你的看起来是 NHWDC
)并且过滤器形状应该是 [depth, height, width, output_channels, in_channels]
。这会影响 filter
和 stride
:
# Original format: NHWDC.
original = tf.placeholder(dtype=tf.float32, shape=[None, 16, 16, 4, 192])
print original.shape
# Convert to NDHWC format.
input = tf.reshape(original, shape=[-1, 4, 16, 16, 192])
print input.shape
# input shape: [batch, depth, height, width, in_channels].
# filter shape: [depth, height, width, output_channels, in_channels].
# output shape: [batch, depth, height, width, output_channels].
filter = tf.get_variable('filter', shape=[4, 2, 2, 192, 192], dtype=tf.float32)
conv = tf.nn.conv3d_transpose(input,
filter=filter,
output_shape=[-1, 7, 32, 32, 192],
strides=[1, 1, 2, 2, 1],
padding='SAME')
print conv.shape
final = tf.reshape(conv, shape=[-1, 32, 32, 7, 192])
print final.shape
输出:
(?, 16, 16, 4, 192)
(?, 4, 16, 16, 192)
(?, 7, 32, 32, 192)
(?, 32, 32, 7, 192)