使用卷积转置层时Tensorflow梯度形状不兼容

使用卷积转置层时Tensorflow梯度形状不兼容,tensorflow,neural-network,conv-neural-network,tensorflow-gpu,Tensorflow,Neural Network,Conv Neural Network,Tensorflow Gpu,我在尝试创建卷积-反卷积网络时遇到了一个问题。原始图像尺寸是565*584,我正在尝试生成565*584的分段 虽然我以前在1024*1024图像的网络中没有问题,但我在这些维度上遇到了一些问题。我在计算梯度时遇到了这个问题: segmentation_result.shape: (?, 565, 584, 1), targets.shape: (?, 565, 584, 1) Process Process-1: Traceback (most recent call last): \Pyt

我在尝试创建卷积-反卷积网络时遇到了一个问题。原始图像尺寸是
565*584
,我正在尝试生成
565*584
的分段

虽然我以前在1024*1024图像的网络中没有问题,但我在这些维度上遇到了一些问题。我在计算梯度时遇到了这个问题:

segmentation_result.shape: (?, 565, 584, 1), targets.shape: (?, 565, 584, 1)
Process Process-1:
Traceback (most recent call last):

\Python\Python35\lib\site-packages\tensorflow\python\framework\tensor_shape.py", line 558, in merge_with


    self.assert_is_compatible_with(other)

\Python\Python35\lib\site-packages\tensorflow\python\framework\tensor_shape.py", line 106, in assert_is_compatible_with
    other))
ValueError: Dimensions 565 and 566 are not compatible
在处理上述异常期间,发生了另一个异常:

Traceback (most recent call last):

\Python\Python35\lib\multiprocessing\process.py", line 249, in _bootstrap
    self.run()

\Python\Python35\lib\multiprocessing\process.py", line 93, in run
    self._target(*self._args, **self._kwargs)

.py", line 418, in train
    network = Network(net_id = count, weight=pos_weight)

.py", line 199, in __init__
    self.train_op = tf.train.AdamOptimizer().minimize(self.cost)

\Python\Python35\lib\site-packages\tensorflow\python\training\optimizer.py", line 315, in minimize
    grad_loss=grad_loss)

\Python\Python35\lib\site-packages\tensorflow\python\training\optimizer.py", line 386, in compute_gradients

    colocate_gradients_with_ops=colocate_gradients_with_ops)

\Python\Python35\lib\site-packages\tensorflow\python\ops\gradients_impl.py", line 560, in gradients
    in_grad.set_shape(t_in.get_shape())

\Python\Python35\lib\site-packages\tensorflow\python\framework\ops.py", line 443, in set_shape
    self._shape = self._shape.merge_with(shape)

\Python\Python35\lib\site-packages\tensorflow\python\framework\tensor_shape.py", line 561, in merge_with
    raise ValueError("Shapes %s and %s are not compatible" % (self, other))

ValueError: Shapes (?, 565, 584, 64) and (?, 566, 584, 64) are not compatible
整个网络有10个卷积层和10个反卷积层。每个反褶积层都是前向层的反向版本。下面是生成卷积层的代码示例:

def create_layer_reversed(self, input, prev_layer=None):
    net_id = self.net_id
    print(net_id)

    with tf.variable_scope('conv', reuse=False):
        W = tf.get_variable('W{}_{}_'.format(self.name[-3:], net_id),
                            shape=(self.kernel_size, self.kernel_size, self.input_shape[3], self.output_channels))
        b = tf.Variable(tf.zeros([W.get_shape().as_list()[2]]))

    output = tf.nn.conv2d_transpose(
        input, W,
        tf.stack([tf.shape(input)[0], self.input_shape[1], self.input_shape[2], self.input_shape[3]]),
        strides=[1,1,1,1], padding='SAME')

    Conv2d.layer_index += 1
    output.set_shape([None, self.input_shape[1], self.input_shape[2], self.input_shape[3]])

    output = lrelu(tf.add(tf.contrib.layers.batch_norm(output), b))

    return output

我发现其中一个问题是,经过几层最大池后,输出形状变得太小;这似乎解决了这个特定的bug。我发现其中一个问题是,经过几层最大池后,输出形状变得太小;这似乎解决了这个特定的错误。