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

Keras 如何管理活动轮廓丢失的多标签分割图?,keras,deep-learning,computer-vision,image-segmentation,unity3d-unet,Keras,Deep Learning,Computer Vision,Image Segmentation,Unity3d Unet,我使用的活动轮廓损失()如下所示: 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[:,:

我使用的活动轮廓损失()如下所示:

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
但是,当我将其用于U-net模型时。我在编译时遇到以下错误

    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].
InvalidArgumentError回溯(最近一次调用上次)
/usr/local/lib/python3.6/dist-packages/tensorflow_core/python/framework/ops.py in_create_c_op(图形、节点定义、输入、控制输入)
1606尝试:
->1607 c_op=c_api.TF_FinishOperation(op_desc)
1608错误除外。InvalidArgumentError为e:
InvalidArgumentError:维度必须相等,但对于输入形状为[?,256,4],?,256256]的“丢失/激活”\u 57\u丢失/多个1”(op:“多个”),维度为4和256。
在处理上述异常期间,发生了另一个异常:
ValueError回溯(最近一次调用上次)
12帧
在()
50#model.compile(优化器='adam',loss='binary\u crossentropy',metrics=['accurity'])
51
--->52 model.compile(优化器='adam',loss=Active\u Contour\u loss,metrics=['accurity']))
/编译中的usr/local/lib/python3.6/dist-packages/keras/engine/training.py(self、optimizer、loss、metrics、loss\u weights、sample\u weight\u mode、weighted\u metrics、target\u tensors、**kwargs)
343带有K.name_作用域(self.output_names[i]+''u loss'):
344输出损耗=加权损耗,
-->345样品(重量,面罩)
346如果透镜(自输出)>1:
347自度量张量追加(输出损失)
/usr/local/lib/python3.6/dist-packages/keras/engine/training\u utils.py in weighted(y_true,y_pred,weights,mask)
426         """
427#score_数组的ndim>=2
-->428分数数组=fn(y_真,y_pred)
429如果掩码不是无:
430#将遮罩投射到floatX,以避免float64向上投射
在活动轮廓损失中(y_真,y_pred)
28 C_2=np.零((256,256))
29
--->本文中的30个区域=K.abs(K.sum(y_pred[:,0,:,:]*((y_true[:,0,:,::]-C_1)**2))等于(12)
本文中的31个区域=K.abs(K.sum((1-y_pred[:,0,:,:])*((y_true[:,0,:,:]-C_2)**2))#等式(12)
32
/二进制op_包装中的usr/local/lib/python3.6/dist-packages/tensorflow_core/python/ops/math_ops.py(x,y)
897操作名称\范围(无,操作名称,[x,y])作为名称:
898如果isinstance(x,运算张量)和isinstance(y,运算张量):
-->899返回函数(x,y,name=name)
900 elif不存在(y,稀疏张量稀疏传感器):
901尝试:
/usr/local/lib/python3.6/dist-packages/tensorflow\u core/python/ops/math\u ops.py in\u mul\u dispatch(x,y,name)
1204是张量(y,运算张量)
1205如果是张量:
->1206返回gen_math_ops.mul(x,y,name=name)
1207其他:
1208断言存在(y,稀疏张量。稀疏传感器)#情况:稠密*稀疏。
/mul中的usr/local/lib/python3.6/dist-packages/tensorflow\u core/python/ops/gen\u math\u ops.py(x,y,name)
6699#将节点添加到TensorFlow图。
6700 u,u,_op=_op_def_lib._apply_op_helper(
->6701“Mul”,x=x,y=y,name=name)
6702 _结果=_运算输出[:]
6703 _输入_平坦=_操作输入
/usr/local/lib/python3.6/dist-packages/tensorflow_core/python/framework/op_def_library.py in_apply_op_helper(self,op_type_name,name,**关键字)
792 op=g.create_op(op_type_name,inputs,dtypes=None,name=scope,
793输入类型=输入类型,属性=属性协议,
-->794 op_def=op_def)
795
796#有条件地调用tfdbg v2的op回调。
/新函数中的usr/local/lib/python3.6/dist-packages/tensorflow_core/python/util/deprecation.py(*args,**kwargs)
505“在未来版本中”如果日期不是其他日期(“在%s“%date”之后),
506(说明)
-->507返回函数(*args,**kwargs)
508
509 doc=\u添加\u不推荐的\u参数\u通知\u到\u docstring(
/create_op中的usr/local/lib/python3.6/dist-packages/tensorflow_core/python/framework/ops.py(***解析参数失败***)
3355 raise TypeError(“输入#%d不是张量:%s”%(idx,a))
3356返回自我。创建操作内部(操作类型、输入、数据类型、输入类型、名称、,
->3357属性、运算定义、计算设备)
3358
3359定义创建操作内部(
/usr/local/lib/python3.6/dist-packages/tensorflow\u core/python/framework/ops.py in\u create\u op\u internal(self、op\u类型、输入、数据类型、输入类型、名称、属性、op\u def、计算设备)
3424输入类型=输入类型,
3425原始值=自身值。默认值原始值,
->3426 op_def=op_def)
3427 self.\u create\u op\u helper(ret,compute\u device=compute\u device)
3428返回ret
/usr/local/lib/python3.6/dist-packages/tensorflow_core/python/framework/ops.py in_uuu___init_u_(self、node_def、g、input、output_type、control_input、input_type、original_op、op_def)
1768操作定义,输入,节点定义属性)
1769 self._c_op=_create_c_op(self._图形、节点定义、分组输入、,
->1770控制(输入操作)
1771#pylint:enable=受保护访问
1772
/usr/local/lib/python3.6/dist-packages/tensorflow_core/python/framework/ops.py in_create_c_op(图形、节点定义、输入、控制输入)
1608错误除外。InvalidArgumentError为e:
1609#转换为ValueError以实现向后兼容性。
->1610提升值错误(str(e))
1611
1612返回c_op
ValueError:维度必须相等,但对于输入形状为[?,256,4],?,256256]的“丢失/激活\u 2/丢失/多个1”(op:“多个”)而言,维度为4和256。

我试图用形状(256256,4)指定C_1和C_2。但是,分割掩码没有生成。我是否遗漏了什么?

您为活动轮廓共享的源代码似乎很好。Pr
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))