hamidriasat / BASNet

Code for Boundary-Aware Segmentation Network for Mobile and Web Applications
MIT License
20 stars 1 forks source link

Why the model return 7 outputs but loss have inputs for 2 only #1

Closed alqurri77 closed 2 years ago

alqurri77 commented 2 years ago

Hi;

First, thank you very much for implementing this in Tensorflow. I just have a confusion, I notice the return for the model have 7 outputs as expected:

model = models.Model(inputs=[x_in], outputs=[d_stage_1, d_stage_2, d_stage_3, d_stage_4, d_stage_5, d_stage_6, bridge]) However, the loss have input for 2, I was expecting also 7: def ssim_loss(y_true, y_pred):

Because in Basenet implementation in Pytorch:

def muti_bce_loss_fusion(d0, d1, d2, d3, d4, d5, d6, d7, labels_v):

    loss0 = bce_ssim_loss(d0,labels_v)
    loss1 = bce_ssim_loss(d1,labels_v)
    loss2 = bce_ssim_loss(d2,labels_v)
    loss3 = bce_ssim_loss(d3,labels_v)
    loss4 = bce_ssim_loss(d4,labels_v)
    loss5 = bce_ssim_loss(d5,labels_v)
    loss6 = bce_ssim_loss(d6,labels_v)
    loss7 = bce_ssim_loss(d7,labels_v)
    #ssim0 = 1 - ssim_loss(d0,labels_v)

    # iou0 = iou_loss(d0,labels_v)
    #loss = torch.pow(torch.mean(torch.abs(labels_v-d0)),2)*(5.0*loss0 + loss1 + loss2 + loss3 + loss4 + loss5) #+ 5.0*lossa
    loss = loss0 + loss1 + loss2 + loss3 + loss4 + loss5 + loss6 + loss7#+ 5.0*lossa
    print("l0: %3f, l1: %3f, l2: %3f, l3: %3f, l4: %3f, l5: %3f, l6: %3f\n"%(loss0.data[0],loss1.data[0],loss2.data[0],loss3.data[0],loss4.data[0],loss5.data[0],loss6.data[0]))
    # print("BCE: l1:%3f, l2:%3f, l3:%3f, l4:%3f, l5:%3f, la:%3f, all:%3f\n"%(loss1.data[0],loss2.data[0],loss3.data[0],loss4.data[0],loss5.data[0],lossa.data[0],loss.data[0]))

    return loss0, loss

I think I'm missing something here.

hamidriasat commented 2 years ago

Hi,

Thanks for the acknowledgement.

There is nothing wrong here, In TensorFlow/Keras we can apply multiple loss functions to multiple outputs.

Basically what happens here is that each loss function(loss=[iou_loss, bce_loss, ssim_loss] line 288) is gonna be applied to each model output.

Search how can we apply multiple loss functions to multiple outputs, then you can understand the flow.

alqurri77 commented 2 years ago

Thank you.... just more question ... is there a difference between this and averaging the loss in the model like this: nestnet_output_all = keras.layers.Average()([d1, d2, d3, d4, e5])

hamidriasat commented 2 years ago

Assuming d1 is the loss calculated on stage 1 and so on... This is still different because in Keras it takes the sum of all loss functions, you are taking average so that will be different.

alqurri77 commented 2 years ago

Thank you ... but does it practically makes a difference, because in both cases it will just minimize the output.

hamidriasat commented 2 years ago

It does, it will change the magnitude of loss value which then propagated into network using back propagation. That's why researcher use different weight's value to increase or decrease the effects of some particular loss value.