在 Tensorflow 中使用 3d 转置卷积进行上采样

Upsampling using 3d transpose convolutions in Tensorflow

我在Tensorflow中定义一个3D转置卷积如下:

def weights(shape):
    return tf.Variable(tf.truncated_normal(shape, mean = 0.0, stddev=0.1))

def biases(shape):
    return tf.Variable(tf.constant(value = 0.1, shape = shape))

def trans_conv3d(x, W, output_shape, strides, padding):
    return tf.nn.conv3d_transpose(x, W, output_shape, strides, padding)

def transconv3d_layer(x, shape, out_shape, strides, padding):
   # shape: [depth, height, width, output_channels, in_channels].
   # output_shape: [batch, depth, height, width, output_channels]
    W = weights(shape)
    b = biases([shape[4]]) 
    return tf.nn.elu(trans_conv3d(x, W, out_shape, strides, padding) + b)

假设我有一个来自前一层的 4D 张量 x,形状为 [2, 1, 1, 1, 10],其中 batch = 2depth = 1height = 1width = 1,以及in_channels = 10,如here

我如何使用 transconv3d_layer 对层序列 x 进行上采样 ,以获得最终形状,例如[2, 100, 100, 100, 10] 或类似的东西? 我不清楚如何通过转置层来跟踪张量的形状。

使用方法如下:

input = tf.random_normal(shape=[2, 1, 1, 1, 10])
deconv1 = transconv3d_layer(input,
                            shape=[2, 3, 3, 10, 10],
                            out_shape=[2, 50, 50, 50, 10],
                            strides=[1, 1, 1, 1, 1],
                            padding='SAME')
deconv2 = transconv3d_layer(deconv1,
                            shape=[2, 3, 3, 10, 10],
                            out_shape=[2, 100, 100, 100, 10],
                            strides=[1, 1, 1, 1, 1],
                            padding='SAME')
# deconv3 ...

print(deconv1)  # Tensor("Elu:0", shape=(2, 50, 50, 50, 10), dtype=float32)
print(deconv2)  # Tensor("Elu_1:0", shape=(2, 100, 100, 100, 10), dtype=float32)

基本上,您应该将每个 out_shape 指定为您想要上采样的对象:(2, 50, 50, 50, 10)(2, 100, 100, 100, 10)、...

为清楚起见,以下是上述不同张量中维度的含义:

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]