fadamsyah/pytorch_brain_mri_segmentation

Questions about the code

Opened this issue · 2 comments

Thanks for sharing your work. It is really cool.
In my opinion, there are some minor misktakes in your code about DiceLoss and Dicesccore.
As for the DiceLoss, i think the DiceLoss should be calculated by averaging the loss in the batch,which is more consistent with the definition of the DIceloss.
So i code this in the way as follows.
class Diceloss(nn.Module):
def init(self):
super().init()
def forward(self,prediction,target,smooth=1e-12):
assert prediction.shape==target.shape,'the dimensions of prediction and target do not match/diceloss'
batch_size=prediction.shape[0]
intersection=(prediction.view(batch_size,-1) * target.view(batch_size,-1)).sum(dim=1)
union=prediction.view(batch_size,-1).sum(dim=1)+target.view(batch_size,-1).sum(dim=1)
dice=(2*intersection+smooth)/(union+smooth)
dice_loss = 1 - dice
dice_loss=torch.mean(dice_loss)
return dice_loss

class Diceloss(nn.Module):
def init(self):
super().init()
def forward(self,prediction,target,smooth=1e-12):
assert prediction.shape==target.shape,'the dimensions of prediction and target do not match/diceloss'
batch_size=prediction.shape[0]
intersection=(prediction.view(batch_size,-1) * target.view(batch_size,-1)).sum(dim=1)
union=prediction.view(batch_size,-1).sum(dim=1)+target.view(batch_size,-1).sum(dim=1)
dice=(2*intersection+smooth)/(union+smooth)
dice_loss = 1 - dice
dice_loss=torch.mean(dice_loss)
return dice_loss

As for the Dicescore, i think you might attempt to average the dicescore in the batch.
However, the code(np.array(list(map(dice_score, y_true, y_pred, [self.smoothing for _ in range(y_pred.shape[0])])))) will return a floating point number instead of an array.