张量流的语义分割 - 损失函数中的ValueError(稀疏 - softmax)

msh*_*hiv 6 python image-segmentation tensorflow softmax

所以,我正在建立一个完全卷积网络(FCN),基于Marvin Teichmann的tensorflow-fcn

我的输入图像数据暂时是750x750x3 RGB图像.在通过网络运行后,我使用shape [batch_size,750,750,2]的logits进行损失计算.

这是一个二进制分类 - 我这里有两个类,[0,1]在我的标签中(形状[batch_sizex750x750].这些进入损失函数,如下:

def loss(logits, labels, num_classes):
with tf.name_scope('loss mine'):
    logits = tf.to_float(tf.reshape(logits, [-1, num_classes]))

    #CHANGE labels type to int, for sparse_softmax...
    labels = tf.to_int64(tf.reshape(labels, [-1]))

    print ('shape of logits: %s' % str(logits.get_shape()))
    print ('shape of labels: %s' % str(labels.get_shape()))

    cross_entropy = tf.nn.sparse_softmax_cross_entropy_with_logits(logits, labels, name='Cross_Entropy')
    tf.add_to_collection('losses', cross_entropy)

    loss = tf.add_n(tf.get_collection('losses'), name='total_loss')
return loss
Run Code Online (Sandbox Code Playgroud)

这些是重塑后的logits和标签的形状:

shape of logits: (562500, 2)
shape of labels: (562500,)
Run Code Online (Sandbox Code Playgroud)

在这里,它抛出一个ValueError说明:

Shapes () and (562500,) are not compatible
Run Code Online (Sandbox Code Playgroud)

完整追溯如下:

  File "train.py", line 89, in <module>
loss_train = loss.loss(logits, data.train.labels, 2)
File "/tensorflow-fcn/loss.py", line 86, in loss
loss = tf.add_n(tf.get_collection('losses'), name='total_loss')
File "/tensorflow/lib/python2.7/site-packages/tensorflow/python/ops/gen_math_ops.py", line 88, in add_n
result = _op_def_lib.apply_op("AddN", inputs=inputs, name=name)
File "/tensorflow/lib/python2.7/site-packages/tensorflow/python/ops/op_def_library.py", line 704, in apply_op
op_def=op_def)
File "/tensorflow/lib/python2.7/site-packages/tensorflow/python/framework/ops.py", line 2262, in create_op
set_shapes_for_outputs(ret)
File "/tensorflow/lib/python2.7/site-packages/tensorflow/python/framework/ops.py", line 1702, in      set_shapes_for_outputs
shapes = shape_func(op)
File "/tensorflow/lib/python2.7/site-packages/tensorflow/python/ops/math_ops.py", line 1557, in _AddNShape
merged_shape = merged_shape.merge_with(input_.get_shape())
File "/tensorflow/lib/python2.7/site-packages/tensorflow/python/framework/tensor_shape.py", line 570, in merge_with
(self, other))
ValueError: Shapes () and (562500,) are not compatible
Run Code Online (Sandbox Code Playgroud)

建议?我执行tf.add_to_collection('losses', cross_entropy)错了吗?

更新:

我尝试运行这个没有像素的总和(或者我认为),通过cross_entropy直接返回上面的代码,作为损失.

它似乎有效.(它现在ValueError从训练优化器函数中抛出一个,说明:No gradients provided for any variable.假设这与我的权重初始化和正则化有关,而不是其他任何事情.

更新2:

以上(关于由于没有梯度而导致的ValueError)是微不足道的.如所提到的在这里,当存在任何定义的tf.Variable对象和损耗张量正在被最小化之间没有路径,通常会遇到此消息.

tf.add_n尽管使用持久性的最初问题.我假设它与Graph集合在TensorFlow中的工作方式有关.初始化我的变量后,错误现在显示为:

Shapes () and (?,) are not compatible
Run Code Online (Sandbox Code Playgroud)

msh*_*hiv 3

结束。事实证明,损失函数中的代码缺少均值求和。对于面临此问题的其他人,请按如下所示修改损失函数,它应该可以正常工作。

    cross_entropy = tf.nn.sparse_softmax_cross_entropy_with_logits(logits, labels, name='Cross_Entropy')
    cross_entropy_mean = tf.reduce_mean(cross_entropy, name='xentropy_mean')
    tf.add_to_collection('losses', cross_entropy_mean)

    loss = tf.add_n(tf.get_collection('losses'), name='total_loss')
return loss 
Run Code Online (Sandbox Code Playgroud)