Как управлять картой многослойной сегментации для потери активного контура? - PullRequest
0 голосов
/ 21 января 2020

Я использую активную потерю контура (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

Однако, когда я использую ее для модели 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].

Я пытался назначить C_1 и C_2 с формой (256,256,4). Но маска сегментации не генерируется. Я что-то упустил?

1 Ответ

0 голосов
/ 08 апреля 2020

Исходный код, которым вы поделились для Active Contours, выглядит нормально. Возможно, вы используете функцию потерь с неправильным размером параметра.

Поскольку код работает с тензорами, вы должны передать функцию 4-D. Попробуйте что-то вроде этого:

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))

Я написал этот код быстро, но я думаю, он будет работать.

...