我正在tensorflow中执行图像分割任务。
代码:
height = 1024
width = 1024
channels = 1
# input place holders
X = tf.placeholder(tf.float32, [None, height, width, channels], name = 'image')
Y = tf.placeholder(tf.float32, [None, height, width, channels], name = 'annotation')
# variable learning rate
lr = tf.placeholder(tf.float32, name = 'lr')
W1 = tf.Variable(tf.truncated_normal([3, 3, 1, 2], stddev=0.1))
B1 = tf.Variable(tf.ones([2])/(2*2))
W2 = tf.Variable(tf.truncated_normal([3, 3, 2, 1], stddev=0.1))
B2 = tf.Variable(tf.ones([1])/(1*1))
W3 = tf.Variable(tf.truncated_normal([2, 2, 1, 1], stddev=0.1))
B3 = tf.Variable(tf.ones([1])/(1*1))
stride = 1
Y1 = tf.nn.relu(tf.nn.conv2d(X, W1, strides=[1, stride, stride, 1], padding= 'VALID') + B1)
stride = 1
Y2 = tf.nn.relu(tf.nn.conv2d(Y1, W2, strides=[1, stride, stride, 1], padding='VALID') + B2)
Ylogits = tf.nn.conv2d_transpose(Y2, W3, output_shape = [1, 1024, 1024, 1], strides = [1, 2, 2, 1])除了我运行下面这行代码之外,一切都很好:
train_step = tf.train.GradientDescentOptimizer(learning_rate=lr).minimize(cross_entropy_sum)
回溯:
train_step = tf.train.GradientDescentOptimizer(learning_rate=lr).minimize(cross_entropy_sum)
File "/home/anaconda/lib/python2.7/site-packages/tensorflow/python/training/optimizer.py", line 315, in minimize
grad_loss=grad_loss)
File "/home/anaconda/lib/python2.7/site-packages/tensorflow/python/training/optimizer.py", line 386, in compute_gradients
colocate_gradients_with_ops=colocate_gradients_with_ops)
File "/home/anaconda/lib/python2.7/site-packages/tensorflow/python/ops/gradients_impl.py", line 580, in gradients
in_grad.set_shape(t_in.get_shape())
File "/home/anaconda/lib/python2.7/site-packages/tensorflow/python/framework/ops.py", line 413, in set_shape
self._shape = self._shape.merge_with(shape)
File "/home/anaconda/lib/python2.7/site-packages/tensorflow/python/framework/tensor_shape.py", line 564, in merge_with
(self, other))
ValueError: Shapes (1, 512, 512, 1) and (?, 1020, 1020, 1) are not compatible发布于 2017-07-06 00:28:54
因为您的conv2d_transpose操作的步幅是[1, 2, 2, 1]的,所以它将导致输出的高度和宽度是输入的两倍。有关为什么会发生这种情况的可视化信息,请参阅this Data Science answer和page it links here。
您设置output_shape = [1, 1024, 1024, 1],反卷积层需要输入高度和宽度的一半,以及相同的批量大小和通道数,这意味着它需要的形状为1,512,512,1。
您为其提供的输入Y2的形状为None,1020,1020,1,因为两个conv2d层各自围绕数据的边缘修剪了一点,但是因为步幅为1,所以它们不会显著减小宽度或高度。批处理大小是None,因为它在这些层中保持不变。
要解决此问题,一种选择是在卷积层之一上使用步长1,2,2,1,这将使宽度和高度大致减半(要准确地减半,请使用padding='SAME'。这将防止边缘被稍微修剪)。您还必须将反卷积层的output_shape更改为None,1024,1024,1。
https://stackoverflow.com/questions/44931192
复制相似问题