-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathnnUNetTrainerSensitivityDCLoss-ProbabilityOutput.py
51 lines (41 loc) · 2.63 KB
/
nnUNetTrainerSensitivityDCLoss-ProbabilityOutput.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
import numpy as np
import torch
from nnunetv2.training.loss.sensitivity import MemoryEfficientSoftDiceLossAndTPR
from nnunetv2.training.loss.deep_supervision import DeepSupervisionWrapper
from nnunetv2.training.nnUNetTrainer.nnUNetTrainer import nnUNetTrainer
from nnunetv2.utilities.helpers import softmax_helper_dim1
class nnUNetTrainerSensitivityDCLoss(nnUNetTrainer):
def _build_loss(self):
loss = MemoryEfficientSoftDiceLossAndTPR(**{'batch_dice': self.configuration_manager.batch_dice,
'do_bg': self.label_manager.has_regions, 'smooth': 1e-5, 'ddp': self.is_ddp},
apply_nonlin=torch.sigmoid if self.label_manager.has_regions else softmax_helper_dim1)
deep_supervision_scales = self._get_deep_supervision_scales()
# we give each output a weight which decreases exponentially (division by 2) as the resolution decreases
# this gives higher resolution outputs more weight in the loss
weights = np.array([1 / (2 ** i) for i in range(len(deep_supervision_scales))])
weights[-1] = 0
# we don't use the lowest 2 outputs. Normalize weights so that they sum to 1
weights = weights / weights.sum()
# now wrap the loss
loss = DeepSupervisionWrapper(loss, weights)
return loss
class nnUNetTrainerSEN_DCLoss_short(nnUNetTrainer):
def __init__(self, plans: dict, configuration: str, fold: int, dataset_json: dict, unpack_dataset: bool = True,
device: torch.device = torch.device('cuda')):
"""used for debugging plans etc"""
super().__init__(plans, configuration, fold, dataset_json, unpack_dataset, device)
self.num_epochs = 10
def _build_loss(self):
loss = MemoryEfficientSoftDiceLossAndTPR(**{'batch_dice': self.configuration_manager.batch_dice,
'do_bg': self.label_manager.has_regions, 'smooth': 1e-5, 'ddp': self.is_ddp},
apply_nonlin=torch.sigmoid if self.label_manager.has_regions else softmax_helper_dim1)
deep_supervision_scales = self._get_deep_supervision_scales()
# we give each output a weight which decreases exponentially (division by 2) as the resolution decreases
# this gives higher resolution outputs more weight in the loss
weights = np.array([1 / (2 ** i) for i in range(len(deep_supervision_scales))])
weights[-1] = 0
# we don't use the lowest 2 outputs. Normalize weights so that they sum to 1
weights = weights / weights.sum()
# now wrap the loss
loss = DeepSupervisionWrapper(loss, weights)
return loss