简体   繁体   English

如何管理活动轮廓损失的多标签分割图?

[英]How to manage multilabel segmentation map for Active contour loss?

I am using Active contour loss ( https://github.com/xuuuuuuchen/Active-Contour-Loss/blob/master/Active-Contour-Loss.py ) which is as follows:我正在使用主动轮廓损失( https://github.com/xuuuuuuchen/Active-Contour-Loss/blob/master/Active-Contour-Loss.py ),如下所示:

from keras import backend as K
import numpy as np
def Active_Contour_Loss(y_true, y_pred): 

#y_pred = K.cast(y_pred, dtype = 'float64')


x = y_pred[:,:,1:,:] - y_pred[:,:,:-1,:] # horizontal and vertical directions 
y = y_pred[:,:,:,1:] - y_pred[:,:,:,:-1]

delta_x = x[:,:,1:,:-2]**2
delta_y = y[:,:,:-2,1:]**2
delta_u = K.abs(delta_x + delta_y) 

epsilon = 0.00000001 # where is a parameter to avoid square root is zero in practice.
w = 1
lenth = w * K.sum(K.sqrt(delta_u + epsilon)) # equ.(11) in the paper

"""
region term
"""

C_1 = np.ones((256, 256))
C_2 = np.zeros((256, 256))

region_in = K.abs(K.sum( y_pred[:,0,:,:] * ((y_true[:,0,:,:] - C_1)**2) ) ) # equ.(12) in the paper
region_out = K.abs(K.sum( (1-y_pred[:,0,:,:]) * ((y_true[:,0,:,:] - C_2)**2) )) # equ.(12) in the paper

lambdaP = 1 # lambda parameter could be various.

loss =  lenth + lambdaP * (region_in + region_out) 

return loss

However, when I use it for the U-net model.但是,当我将它用于 U-net 模型时。 I am getting below error while compiling.编译时出现以下错误。

    InvalidArgumentError                      Traceback (most recent call last)
/usr/local/lib/python3.6/dist-packages/tensorflow_core/python/framework/ops.py in _create_c_op(graph, node_def, inputs, control_inputs)
   1606   try:
-> 1607     c_op = c_api.TF_FinishOperation(op_desc)
   1608   except errors.InvalidArgumentError as e:

InvalidArgumentError: Dimensions must be equal, but are 4 and 256 for 'loss_2/activation_57_loss/mul_1' (op: 'Mul') with input shapes: [?,256,4], [?,256,256].

During handling of the above exception, another exception occurred:

ValueError                                Traceback (most recent call last)
12 frames
<ipython-input-33-b98b233ef3b2> in <module>()
     50 # model.compile(optimizer='adam', loss='binary_crossentropy', metrics=['accuracy'])
     51 
---> 52 model.compile(optimizer='adam', loss=Active_Contour_Loss, metrics=['accuracy'])

/usr/local/lib/python3.6/dist-packages/keras/engine/training.py in compile(self, optimizer, loss, metrics, loss_weights, sample_weight_mode, weighted_metrics, target_tensors, **kwargs)
    343                 with K.name_scope(self.output_names[i] + '_loss'):
    344                     output_loss = weighted_loss(y_true, y_pred,
--> 345                                                 sample_weight, mask)
    346                 if len(self.outputs) > 1:
    347                     self.metrics_tensors.append(output_loss)

/usr/local/lib/python3.6/dist-packages/keras/engine/training_utils.py in weighted(y_true, y_pred, weights, mask)
    426         """
    427         # score_array has ndim >= 2
--> 428         score_array = fn(y_true, y_pred)
    429         if mask is not None:
    430             # Cast the mask to floatX to avoid float64 upcasting in Theano

<ipython-input-32-b273672af934> in Active_Contour_Loss(y_true, y_pred)
     28         C_2 = np.zeros((256, 256))
     29 
---> 30         region_in = K.abs(K.sum( y_pred[:,0,:,:] * ((y_true[:,0,:,:] - C_1)**2) ) ) # equ.(12) in the paper
     31         region_out = K.abs(K.sum( (1-y_pred[:,0,:,:]) * ((y_true[:,0,:,:] - C_2)**2) )) # equ.(12) in the paper
     32 

/usr/local/lib/python3.6/dist-packages/tensorflow_core/python/ops/math_ops.py in binary_op_wrapper(x, y)
    897     with ops.name_scope(None, op_name, [x, y]) as name:
    898       if isinstance(x, ops.Tensor) and isinstance(y, ops.Tensor):
--> 899         return func(x, y, name=name)
    900       elif not isinstance(y, sparse_tensor.SparseTensor):
    901         try:

/usr/local/lib/python3.6/dist-packages/tensorflow_core/python/ops/math_ops.py in _mul_dispatch(x, y, name)
   1204   is_tensor_y = isinstance(y, ops.Tensor)
   1205   if is_tensor_y:
-> 1206     return gen_math_ops.mul(x, y, name=name)
   1207   else:
   1208     assert isinstance(y, sparse_tensor.SparseTensor)  # Case: Dense * Sparse.

/usr/local/lib/python3.6/dist-packages/tensorflow_core/python/ops/gen_math_ops.py in mul(x, y, name)
   6699   # Add nodes to the TensorFlow graph.
   6700   _, _, _op = _op_def_lib._apply_op_helper(
-> 6701         "Mul", x=x, y=y, name=name)
   6702   _result = _op.outputs[:]
   6703   _inputs_flat = _op.inputs

/usr/local/lib/python3.6/dist-packages/tensorflow_core/python/framework/op_def_library.py in _apply_op_helper(self, op_type_name, name, **keywords)
    792         op = g.create_op(op_type_name, inputs, dtypes=None, name=scope,
    793                          input_types=input_types, attrs=attr_protos,
--> 794                          op_def=op_def)
    795 
    796       # Conditionally invoke tfdbg v2's op callback(s).

/usr/local/lib/python3.6/dist-packages/tensorflow_core/python/util/deprecation.py in new_func(*args, **kwargs)
    505                 'in a future version' if date is None else ('after %s' % date),
    506                 instructions)
--> 507       return func(*args, **kwargs)
    508 
    509     doc = _add_deprecated_arg_notice_to_docstring(

/usr/local/lib/python3.6/dist-packages/tensorflow_core/python/framework/ops.py in create_op(***failed resolving arguments***)
   3355         raise TypeError("Input #%d is not a tensor: %s" % (idx, a))
   3356     return self._create_op_internal(op_type, inputs, dtypes, input_types, name,
-> 3357                                     attrs, op_def, compute_device)
   3358 
   3359   def _create_op_internal(

/usr/local/lib/python3.6/dist-packages/tensorflow_core/python/framework/ops.py in _create_op_internal(self, op_type, inputs, dtypes, input_types, name, attrs, op_def, compute_device)
   3424           input_types=input_types,
   3425           original_op=self._default_original_op,
-> 3426           op_def=op_def)
   3427       self._create_op_helper(ret, compute_device=compute_device)
   3428     return ret

/usr/local/lib/python3.6/dist-packages/tensorflow_core/python/framework/ops.py in __init__(self, node_def, g, inputs, output_types, control_inputs, input_types, original_op, op_def)
   1768           op_def, inputs, node_def.attr)
   1769       self._c_op = _create_c_op(self._graph, node_def, grouped_inputs,
-> 1770                                 control_input_ops)
   1771     # pylint: enable=protected-access
   1772 

/usr/local/lib/python3.6/dist-packages/tensorflow_core/python/framework/ops.py in _create_c_op(graph, node_def, inputs, control_inputs)
   1608   except errors.InvalidArgumentError as e:
   1609     # Convert to ValueError for backwards compatibility.
-> 1610     raise ValueError(str(e))
   1611 
   1612   return c_op

ValueError: Dimensions must be equal, but are 4 and 256 for 'loss_2/activation_57_loss/mul_1' (op: 'Mul') with input shapes: [?,256,4], [?,256,256].

I tried to assign C_1 and C_2 with shape (256,256,4).我试图为 C_1 和 C_2 分配形状 (256,256,4)。 But, the segmentation mask is not generated.但是,不会生成分割掩码。 Am I missing something?我错过了什么吗?

The source code you shared for Active Contours seems fine.您为 Active Contours 共享的源代码看起来不错。 Probably, you are feeding the loss function with wrong parameter size.可能,您正在使用错误的参数大小提供损失函数。

Since the code is working with tensors, you should feed the function 4-D.由于代码使用张量,您应该输入函数 4-D。 Try something like that:尝试这样的事情:

a0 = np.zeros([1,256,256,1], dtype='float64')
a0[0,...] = np.expand_dims(C_1, -1).astype(np.float64)
a1 = np.zeros([1,256,256,1], dtype='float64')
a1[0,...] = np.expand_dims(C_2, -1).astype(np.float64)
los_ac = Active_Contour_Loss(a0, a1)
print(sess.run(los_ac))

I wrote this code quickly but I suppose it'll work.我很快就写了这段代码,但我想它会起作用。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 Keras语义分割加权损失像素图 - Keras Semantic Segmentation Weighted Loss Pixel Map 如何应用损失度量来惩罚在多标签分类问题中预测全零? - How to apply a loss metric that will penalize predicting all zeros in multilabel classification problem? Tensorflow - keras - 多标签分类的形状和损失 - Tensorflow - keras - shapes and loss for multilabel classification 如何在 Tensorflow 分割模型中实现 Dice 损失? - How do I implementing Dice loss in a Tensorflow segmentation model? 如何在 Tensorflow 2.0/Keras 中正确使用 CategoricalCrossentropy loss 进行图像分割? - How to properly use CategoricalCrossentropy loss for image segmentation in Tensorflow 2.0/Keras? Tensorflow-多标签分类损失减少缓慢/完全没有变化 - Tensorflow - Multilabel classification loss decreasing slowly/not changing at all 尝试在多标签分类问题中应用自定义损失时出现 TypeError - TypeError when trying to apply custom loss in a multilabel classification problem 一个序列的多标签分类,怎么办? - Multilabel classification of a sequence, how to do it? 如何在具有不同基本事实和 output 通道的多 class 分割问题中计算损失? (UNET,骰子损失) - How the loss is computed in multi class segmentation problem which has different ground truth and output channels? (UNET, dice loss) 如何为 KERAS 多标签问题提供 DataGenerator? - how to feed DataGenerator for KERAS multilabel issue?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM