0

我已经为反卷积层编写了代码,

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)

收到此错误,

文件“u-net.py”,第 84 行,在 obj.computation() 文件“u-net.py”,第 41 行,计算中 deconvolution1 = deconv2d(x=cnn_layer10, W=[2,2,512,1024], stride=2) 文件“/home/shuvo/u-net/architecture.py”,第 35 行,在 deconv2d decon = tf.nn.conv2d_transpose(x, W, output_shape, strides=[1, stride, stride, 1] , padding='SAME') 文件“/usr/local/lib/python3.5/dist-packages/tensorflow/python/ops/nn_ops.py”,第 1019 行,在 conv2d_transpose 如果不是 value.get_shape()[axis] .is_compatible_with(filter.get_shape()[3]):
文件“/usr/local/lib/python3.5/dist-packages/tensorflow/python/framework/tensor_shape.py”,第500行,在getitem中 返回self._dims [key] IndexError:列表索引超出范围

我的意图是在形状应该是 [batch_size, 36,36,1024]=>[batch_size,72,72,512] 的地方制作一个反卷积层。请帮我解决这个错误,

4

1 回答 1

1

输入参数filtertf.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_transposeAPI,其中filtersarg 是过滤器大小,权重初始化发生在其中。

于 2018-05-12T15:24:07.787 回答