如何在张量流中制作反卷积层?
How to make a de-convolution layer in tensorflow?
我写了一个反卷积层的代码,
def deconv2d(x, W,stride):
x_shape = tf.shape(x)
output_shape = tf.stack([x_shape[0], x_shape[1]*2, x_shape[2]*2, x_shape[3]//2])
decon = tf.nn.conv2d_transpose(x, W, output_shape, strides=[1, stride, stride, 1], padding='SAME')
layer_shape = get_layer_shape(decon)
print('DECONV Shape : ', layer_shape)
return decon
上面的函数我是这样调用的,
deconvolution1 = deconv2d(x=cnn_layer10, W=[2,2,512,1024], stride=2)
收到此错误,
File "u-net.py", line 84, in
obj.computation() File "u-net.py", line 41, in computation
deconvolution1 = deconv2d(x=cnn_layer10, W=[2,2,512,1024], stride=2) File "/home/shuvo/u-net/architecture.py", line 35, in
deconv2d
decon = tf.nn.conv2d_transpose(x, W, output_shape, strides=[1, stride, stride, 1], padding='SAME') File
"/usr/local/lib/python3.5/dist-packages/tensorflow/python/ops/nn_ops.py",
line 1019, in conv2d_transpose
if not value.get_shape()[axis].is_compatible_with(filter.get_shape()[3]):
File
"/usr/local/lib/python3.5/dist-packages/tensorflow/python/framework/tensor_shape.py",
line 500, in getitem
return self._dims[key] IndexError: list index out of range
我的目的是在形状应该是的地方做一个反卷积层,
[batch_size, 36,36,1024]=>[batch_size,72,72,512]。
请帮我解决这个错误,
输入参数 filter
到 tf.nn.conv2d_transpose
是权重矩阵本身,而不仅仅是过滤器的大小。
修复上述问题的修改代码如下所示:
cnn_layer10 = tf.placeholder(tf.float32, (10, 36, 36, 1024))
def deconv2d(x, W,stride):
x_shape = tf.shape(x)
weights = tf.Variable(tf.random_normal(W))
output_shape = tf.stack([x_shape[0], x_shape[1]*2, x_shape[2]*2, x_shape[3]//2])
decon = tf.nn.conv2d_transpose(x, weights, output_shape, strides=[1, stride, stride, 1], padding='SAME')
return decon
deconvolution1 = deconv2d(x=cnn_layer10, W=[2,2,512,1024], stride=2)
with tf.Session() as sess:
sess.run(tf.global_variables_initializer())
print('Decon Shape:',sess.run(deconvolution1, {cnn_layer10: np.random.random((10, 36,36,1024))}).shape)
#Output
#Decon Shape: (10, 72, 72, 512)
注意:最好使用 tf.layers.conv2d_transpose
API,其中 filters
arg 是过滤器大小,权重初始化发生在其中。
我写了一个反卷积层的代码,
def deconv2d(x, W,stride):
x_shape = tf.shape(x)
output_shape = tf.stack([x_shape[0], x_shape[1]*2, x_shape[2]*2, x_shape[3]//2])
decon = tf.nn.conv2d_transpose(x, W, output_shape, strides=[1, stride, stride, 1], padding='SAME')
layer_shape = get_layer_shape(decon)
print('DECONV Shape : ', layer_shape)
return decon
上面的函数我是这样调用的,
deconvolution1 = deconv2d(x=cnn_layer10, W=[2,2,512,1024], stride=2)
收到此错误,
File "u-net.py", line 84, in obj.computation() File "u-net.py", line 41, in computation deconvolution1 = deconv2d(x=cnn_layer10, W=[2,2,512,1024], stride=2) File "/home/shuvo/u-net/architecture.py", line 35, in deconv2d decon = tf.nn.conv2d_transpose(x, W, output_shape, strides=[1, stride, stride, 1], padding='SAME') File "/usr/local/lib/python3.5/dist-packages/tensorflow/python/ops/nn_ops.py", line 1019, in conv2d_transpose if not value.get_shape()[axis].is_compatible_with(filter.get_shape()[3]):
File "/usr/local/lib/python3.5/dist-packages/tensorflow/python/framework/tensor_shape.py", line 500, in getitem return self._dims[key] IndexError: list index out of range
我的目的是在形状应该是的地方做一个反卷积层, [batch_size, 36,36,1024]=>[batch_size,72,72,512]。 请帮我解决这个错误,
输入参数 filter
到 tf.nn.conv2d_transpose
是权重矩阵本身,而不仅仅是过滤器的大小。
修复上述问题的修改代码如下所示:
cnn_layer10 = tf.placeholder(tf.float32, (10, 36, 36, 1024))
def deconv2d(x, W,stride):
x_shape = tf.shape(x)
weights = tf.Variable(tf.random_normal(W))
output_shape = tf.stack([x_shape[0], x_shape[1]*2, x_shape[2]*2, x_shape[3]//2])
decon = tf.nn.conv2d_transpose(x, weights, output_shape, strides=[1, stride, stride, 1], padding='SAME')
return decon
deconvolution1 = deconv2d(x=cnn_layer10, W=[2,2,512,1024], stride=2)
with tf.Session() as sess:
sess.run(tf.global_variables_initializer())
print('Decon Shape:',sess.run(deconvolution1, {cnn_layer10: np.random.random((10, 36,36,1024))}).shape)
#Output
#Decon Shape: (10, 72, 72, 512)
注意:最好使用 tf.layers.conv2d_transpose
API,其中 filters
arg 是过滤器大小,权重初始化发生在其中。