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

时间:2017-11-25 21:48:59

标签: 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):

\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

0 个答案:

没有答案