Active-Contour-Loss
Active-Contour-Loss copied to clipboard
Loss is not minimizing after 2nd epoch.
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.
I am using the NIH dataset for training (https://wiki.cancerimagingarchive.net/display/Public/Pancreas-CT). Since the data format is different (8, 256, 256, 1), I changed the indexes.
My code is the following:
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
However, the output of the training does not go well:
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
Any ideas what I am doing wrong?
Thanks in advance!
I also just reshaped my data, so that I don't have to do any other changes in the original code:
y_true = tf.transpose(y_true2, [0, 3, 1, 2])
y_pred = tf.transpose(y_pred2, [0, 3, 1, 2])
but unfortunately the training output is exactly the same.
Hello, I also encountered the same problem, have you solved it?
Hello!
I haven't solved the problem yet. I contacted the author and he said that the loss is sensitive to unbalanced classes problems on diiferent datasets.
So, he suggested the following things:
- Try to find two another well-choosed hyperparameters a and b: a* regionin + b * regionout in the region term according to your dataset.
- Try to train with a pre-trained model (no need converged too much) to tackle the initialisation problem of model and the AC model.
Good luck! Please update if you solve it, I wll do the same :)
Hi there, I solved this problem by crop my data. I think the loss is sensitive to unbalanced categories. And the learning rate is 0.0005, keep the y_pred is a probability map between [0,1]. No other code changes.
Thank you very much! I can't test it for the next few weeks, but I will try and also update.