text stringlengths 1 93.6k |
|---|
def _dice_loss(self, score, target):
|
target = target.float()
|
smooth = 1e-5
|
intersect = torch.sum(score * target)
|
y_sum = torch.sum(target * target)
|
z_sum = torch.sum(score * score)
|
loss = (2 * intersect + smooth) / (z_sum + y_sum + smooth)
|
loss = 1 - loss
|
return loss
|
def forward(self, inputs, target, weight=None, softmax=False):
|
if softmax:
|
inputs = torch.softmax(inputs, dim=1)
|
target = self._one_hot_encoder(target)
|
if weight is None:
|
weight = [1] * self.n_classes
|
assert inputs.size() == target.size(), 'predict {} & target {} shape do not match'.format(inputs.size(), target.size())
|
class_wise_dice = []
|
loss = 0.0
|
for i in range(0, self.n_classes):
|
dice = self._dice_loss(inputs[:, i], target[:, i])
|
class_wise_dice.append(1.0 - dice.item())
|
loss += dice * weight[i]
|
return loss / self.n_classes
|
class CeDiceLoss(nn.Module):
|
def __init__(self, num_classes, loss_weight=[0.4, 0.6]):
|
super(CeDiceLoss, self).__init__()
|
self.celoss = nn.CrossEntropyLoss()
|
self.diceloss = nDiceLoss(num_classes)
|
self.loss_weight = loss_weight
|
def forward(self, pred, target):
|
loss_ce = self.celoss(pred, target[:].long())
|
loss_dice = self.diceloss(pred, target, softmax=True)
|
loss = self.loss_weight[0] * loss_ce + self.loss_weight[1] * loss_dice
|
return loss
|
class BceDiceLoss(nn.Module):
|
def __init__(self, wb=1, wd=1):
|
super(BceDiceLoss, self).__init__()
|
self.bce = BCELoss()
|
self.dice = DiceLoss()
|
self.wb = wb
|
self.wd = wd
|
def forward(self, pred, target):
|
bceloss = self.bce(pred, target)
|
diceloss = self.dice(pred, target)
|
loss = self.wd * diceloss + self.wb * bceloss
|
return loss
|
class GT_BceDiceLoss(nn.Module):
|
def __init__(self, wb=1, wd=1):
|
super(GT_BceDiceLoss, self).__init__()
|
self.bcedice = BceDiceLoss(wb, wd)
|
def forward(self, gt_pre, out, target):
|
bcediceloss = self.bcedice(out, target)
|
gt_pre5, gt_pre4, gt_pre3, gt_pre2, gt_pre1 = gt_pre
|
gt_loss = self.bcedice(gt_pre5, target) * 0.1 + self.bcedice(gt_pre4, target) * 0.2 + self.bcedice(gt_pre3, target) * 0.3 + self.bcedice(gt_pre2, target) * 0.4 + self.bcedice(gt_pre1, target) * 0.5
|
return bcediceloss + gt_loss
|
class myToTensor:
|
def __init__(self):
|
pass
|
def __call__(self, data):
|
image, mask = data
|
return torch.tensor(image).permute(2,0,1), torch.tensor(mask).permute(2,0,1)
|
class myResize:
|
def __init__(self, size_h=256, size_w=256):
|
self.size_h = size_h
|
self.size_w = size_w
|
def __call__(self, data):
|
image, mask = data
|
return TF.resize(image, [self.size_h, self.size_w]), TF.resize(mask, [self.size_h, self.size_w])
|
class myRandomHorizontalFlip:
|
def __init__(self, p=0.5):
|
self.p = p
|
def __call__(self, data):
|
image, mask = data
|
if random.random() < self.p: return TF.hflip(image), TF.hflip(mask)
|
else: return image, mask
|
class myRandomVerticalFlip:
|
def __init__(self, p=0.5):
|
self.p = p
|
def __call__(self, data):
|
Subsets and Splits
No community queries yet
The top public SQL queries from the community will appear here once available.