Hello everyone! I tried to implement the AC loss function on a U-Net model.
I am using the the AC loss as loss function and the Dice Coefficient as metric.
def Active_Contour_Loss(y_true, y_pred):
y_true = K.cast(y_true, dtype = 'float64')
y_pred = K.cast(y_pred, dtype = 'float64')
"""
lenth term
"""
#Reordered indexes, because data format is different.
x = y_pred[:,1:,:,:] - y_pred[:,:-1,:,:] #y_true shape is: (8, 256, 256, 1)
y = y_pred[:,:,1:,:] - y_pred[:,:,:-1,:] #y_pred shape is: (8, 256, 256, 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))
#Reordered indexes, because data format is different.
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) # equ. (8) in the paper
return loss
Epoch 1/20
18750/18750 - 3202s - loss: 3657.6096 - dice_coef: 0.0187 - val_loss: 909.7355 - val_dice_coef: 0.5784
Epoch 2/20
18750/18750 - 3150s - loss: 716.8154 - dice_coef: 0.0240 - val_loss: 909.7353 - val_dice_coef: 0.5785
Epoch 3/20
18750/18750 - 3158s - loss: 717.3530 - dice_coef: 0.0230 - val_loss: 909.7353 - val_dice_coef: 0.5785
Epoch 4/20
18750/18750 - 3161s - loss: 718.0414 - dice_coef: 0.0221 - val_loss: 909.7353 - val_dice_coef: 0.5785
Epoch 5/20
18750/18750 - 3126s - loss: 717.6503 - dice_coef: 0.0222 - val_loss: 909.7353 - val_dice_coef: 0.5785
Epoch 6/20
18750/18750 - 3133s - loss: 718.0792 - dice_coef: 0.0229 - val_loss: 909.7353 - val_dice_coef: 0.5785
Epoch 7/20
18750/18750 - 3092s - loss: 717.6919 - dice_coef: 0.0229 - val_loss: 909.7353 - val_dice_coef: 0.5785