Python 在Tensorflow中使用3d转置卷积进行上采样
我在Tensorflow中定义了3D转置卷积,如下所示:Python 在Tensorflow中使用3d转置卷积进行上采样,python,tensorflow,computer-vision,deep-learning,convolution,Python,Tensorflow,Computer Vision,Deep Learning,Convolution,我在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, stri
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,10]
,其中批次=2
,深度=1
,高度=1
,宽度=1
,以及,如图所示
我如何使用transconv3d\u层
在一系列层上增加采样x
,以获得最终形状,如[21000100100,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\u形状指定为您希望向上采样的形状:(2,50,50,50,10)
,(2,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]
多谢各位。但是,如果我想在一系列转置卷积层上,一个接一个地得到[21001001010]的形状,该怎么办?我不确定我是否理解这个设计。通常,该层从上一层获取输入,因此您将有一个DECONV-DECONV。。。图案这就是你的意思吗?或者你的意思是来自同一输入的多个DECONV层?我不确定这是前一个还是后一个,但它看起来是这样的:x=tf.random\u normal(shape=[2,1,1,1,10])
transconv1=transconv3d\u层(x,shape=s1,out\u shape=o1,strips=st1,padding=pd1)
transconv2=transconv3d\u层(transconv1,shape=s2,out\u shape=o2,跨步=st2,padding=pd2)
其中transconv2的形状应该是[21000100100,10]
,这更有意义。但是请注意,在第一个conv层之后,形状已经是[21000100100,10]
。因此,向上采样到相同的形状是没有意义的。如果您愿意,您应该应用一个正常的卷积,但是如果我想使用几个反卷积层来达到[210001001010]
的最终形状,那么上采样是渐进的,而不是一次性的呢?例如,transconv1
可能有一个形状[2,50,50,10]
,然后transconv2
将有形状[2,100,100,10]
,因为它是最后一层。