torch_em.trainer.hybrid_unetr_trainer
1import torch 2 3from .default_trainer import DefaultTrainer 4 5 6class HybridUNETRDeepSupervisionTrainer(DefaultTrainer): 7 """Train HybridUNETR with the three auxiliary heads of the `cagd_3d` decoder. 8 9 Import this class from `torch_em.trainer.hybrid_unetr_trainer` and pass it as 10 `trainer_class` to `torch_em.default_segmentation_trainer`. The same loss and 11 target are used for each prediction. Set `deep_supervision=True` in the model's 12 `decoder_kwargs`. Validation uses the main prediction. 13 """ 14 15 aux_weights = (0.4, 0.3, 0.2) # MedCAGD weights, deepest head first. 16 17 def _forward_and_loss(self, x, y): 18 if not self.model.training: 19 return super()._forward_and_loss(x, y) 20 21 outputs = self.model(x) 22 if isinstance(outputs, torch.Tensor): 23 raise ValueError("HybridUNETRDeepSupervisionTrainer requires deep_supervision=True.") 24 prediction, auxiliary_outputs = outputs 25 if len(auxiliary_outputs) != len(self.aux_weights): 26 raise ValueError("Expected one loss weight per auxiliary prediction.") 27 28 if self._iteration % self.log_image_interval == 0 and prediction.requires_grad: 29 prediction.retain_grad() 30 31 loss = self.loss(prediction, y) 32 for weight, auxiliary in zip(self.aux_weights, auxiliary_outputs): 33 loss = loss + weight * self.loss(auxiliary, y) 34 return prediction, loss
7class HybridUNETRDeepSupervisionTrainer(DefaultTrainer): 8 """Train HybridUNETR with the three auxiliary heads of the `cagd_3d` decoder. 9 10 Import this class from `torch_em.trainer.hybrid_unetr_trainer` and pass it as 11 `trainer_class` to `torch_em.default_segmentation_trainer`. The same loss and 12 target are used for each prediction. Set `deep_supervision=True` in the model's 13 `decoder_kwargs`. Validation uses the main prediction. 14 """ 15 16 aux_weights = (0.4, 0.3, 0.2) # MedCAGD weights, deepest head first. 17 18 def _forward_and_loss(self, x, y): 19 if not self.model.training: 20 return super()._forward_and_loss(x, y) 21 22 outputs = self.model(x) 23 if isinstance(outputs, torch.Tensor): 24 raise ValueError("HybridUNETRDeepSupervisionTrainer requires deep_supervision=True.") 25 prediction, auxiliary_outputs = outputs 26 if len(auxiliary_outputs) != len(self.aux_weights): 27 raise ValueError("Expected one loss weight per auxiliary prediction.") 28 29 if self._iteration % self.log_image_interval == 0 and prediction.requires_grad: 30 prediction.retain_grad() 31 32 loss = self.loss(prediction, y) 33 for weight, auxiliary in zip(self.aux_weights, auxiliary_outputs): 34 loss = loss + weight * self.loss(auxiliary, y) 35 return prediction, loss
Train HybridUNETR with the three auxiliary heads of the cagd_3d decoder.
Import this class from torch_em.trainer.hybrid_unetr_trainer and pass it as
trainer_class to torch_em.default_segmentation_trainer. The same loss and
target are used for each prediction. Set deep_supervision=True in the model's
decoder_kwargs. Validation uses the main prediction.
Inherited Members
- torch_em.trainer.default_trainer.DefaultTrainer
- DefaultTrainer
- name
- id_
- train_loader
- val_loader
- model
- loss
- optimizer
- metric
- device
- lr_scheduler
- log_image_interval
- save_root
- compile_model
- rank
- best_metric
- mixed_precision
- mixed_precision_dtype
- early_stopping
- train_time
- logger_class
- logger_kwargs
- checkpoint_folder
- iteration
- epoch
- Deserializer
- Serializer
- fit