ultrasound-nerve-segmentation icon indicating copy to clipboard operation
ultrasound-nerve-segmentation copied to clipboard

How can I make my own loss defination

Open Kaido0 opened this issue 7 years ago • 5 comments

I have my own training sets,but the background are too large. Also,I know the background is black(pixel=0). When I am calculating the dice,I want to 'remove' the background(Don't calculate it into dice). I try this:for i in y_true_f.eval(): But it didn't work~

Kaido0 avatar Mar 22 '17 08:03 Kaido0

In current implementation "black" pixels are not calculated into the Dice score, only "white" ones.

jocicmarko avatar Apr 05 '17 14:04 jocicmarko

@jocicmarko Oh!You are so great~ I saw the new code,but I find Dice code not changed.Where did you add code to judge the pixel? Is it caculated by Keras backend? Thank you so much!I learn keras all by your code.Haha~

Kaido0 avatar Apr 06 '17 02:04 Kaido0

@Kaido0

Where did you add code to judge the pixel?

If you take a look at the Dice loss function, you will notice that part of the code intersection = K.sum(y_true_f * y_pred_f) does multiplication of true pixels with predicted pixels. Since 1s are for mask 0s are for background, some of the predicted values are multiplied by 0s in true masks, thus "ignoring" the background! Hope this makes it clearer.

jocicmarko avatar Apr 11 '17 09:04 jocicmarko

@jocicmarko Thanks~I got it. I made a mistake,I want to divide the image into four categories(Include background).So I need to make "0" pixels are not calculated into the Dice score, only '1','0.25','0.75' .I want to use'softmax' and conv10 = Convolution2D(4, 1, 1, activation='softmax')(conv9),but I think it is not clearly,Would you give me some suggestion,thank you very much

Kaido0 avatar Apr 14 '17 02:04 Kaido0

@jocicmarko Thanks for your code, it helped me quite a lot to get my unet architecture. @Kaido0 I run into the same issue with the dice computation and this is what I tried: def dice_coef(y_true, y_pred): y_true_f = K.flatten(y_true[...,1:])
y_pred_f = K.flatten(y_pred[...,1:]) intersection = K.sum(y_true_f * y_pred_f) return (2. * intersection + smooth) / (K.sum(y_true_f) + K.sum(y_pred_f) + smooth)

The network runs, but I got weird dice now. Basically I have 4 classes being (background, and 3 more within the image), so my y_true should look like (nb_samples, img_size, 4). I am doing a quick test to see whether this DICE is correct, basically using only one image volume for training and then I predict using the exact same volume so the DICE for every class should be the same. Well, it is not even close. Anyway, hope this helps you. If you figure that out, I would appreciate a feedback on that.

EloyRoura avatar Jul 20 '17 08:07 EloyRoura