All trainers are selected by class name with --trainer <ClassName> when running main.py (launched with torchrun, see BaseTrainer). Available names:
--trainer |
File | Base class | Typical dataset / model |
|---|---|---|---|
Trainer_3D |
trainer_3D.py | BaseTrainer |
ATLAS, BraTS3D / UNet3D |
Trainer_2Dsliced |
trainer_2Dsliced.py | BaseTrainer |
BraTS2D / UNet2D |
Trainer_2D |
trainer_2D.py | BaseTrainer |
QaTaCov / UNet2D |
Trainer_2D_TextGuided |
trainer_2D_text_guided.py | BaseTrainerText |
QaTaCovTextEmb / UNet2D + CombinedSegSigLoss |
Trainer_3DText |
trainer_3D_text_guided.py | BaseTrainer |
BraTS3DText / UNet3DText |
🏋🏼♂️ trainer_3D.py:
Trainer for 3D volume segmentation. Supports mixed precision (--mixed_precision), clips gradients (norm 2) and calls train_sampler.set_epoch(epoch) for DDP shuffling.
Key Methods:
_train_epoch(epoch): Iterates through patch-based training loader, computes loss, backpropagates, updates metricseval_epoch(epoch, phase): Performs patch-based inference on full volumes using GridSampler/GridAggregator (hannoverlap mode), computes validation/test metrics_inference_sampler(sample): Creates GridSampler for dense patch-based inference (usesdataset.patch_sizeanddataset.grid_overlap; volumes smaller than the patch are padded withCropOrPad)_results_dict(phase, epoch): Retrieves metrics for the epoch
🏋🏼♂️ trainer_2Dsliced.py:
Trainer for 2D slice-based segmentation of 3D volumes (datasets inheriting from BaseDataset2DSliced). At evaluation, predictions are argmax'ed before computing metrics.
🏋🏼♂️ trainer_2D.py:
Trainer for full 2D image segmentation (e.g., QaTaCov chest X-rays) without patch-based inference.
🏋🏼♂️ trainer_2D_text_guided.py:
Trainer_2D_TextGuided: report-guided 2D trainer built on BaseTrainerText.
- Training: calls
model(image, return_bottleneck=True), projects bottleneck and the sample'stext_emb(precomputed embedding,[B, 768]) with the guidance head, and computes the loss withimg_emb/txt_emb(e.g.CombinedSegSigLoss). AKeyErroris raised if a training sample has notext_emb. Withuse_text_guidance=Falseit behaves likeTrainer_2D - Evaluation: segmentation only (no text needed), so val/test samples can come without embeddings
- Logs parameter counts and a gradient-flow summary at the first epoch/iteration; clips gradients of model and head (norm 1)
- Fine-tuning: set
pretrained_unet_pathin the config (see BaseTrainerText)
🏋🏼♂️ trainer_3D_text_guided.py:
Trainer_3DText: report-guided 3D trainer, to be used with BraTS3DText and UNet3DText.
- Constructor: extra
pretrained_pathargument (frommain.py --pretrained_path); if set, the weights (rawstate_dictor checkpoint with a"model"key) are loaded withstrict=Falseafter initialization - Datasets:
_init_dataset()builds a training loader of subjects without reports (get_loader('train')) and a second one of subjects with reports (get_loader('train', report=True)) - Report interleaving: after every 2 segmentation-only training iterations, one extra step is done on a batch from the (cycled) report loader, calling
model(image, report)→(prediction, z_img, z_txt)andloss(prediction, label, z_img, z_txt) - Evaluation: same patch-based GridSampler/GridAggregator inference as
Trainer_3D, segmentation only - Supports mixed precision (
--mixed_precision)
- Create a new file in
trainer/with your trainer class that inherits fromBaseTrainer(orBaseTrainerTextfor report guidance) - Implement
_train_epoch(epoch): Define training loop, iterate through batches, compute loss, backpropagate, and return metrics dict - Implement
eval_epoch(epoch, phase): Define validation/test loop, compute predictions and metrics, and return metrics dict. The returned dict must containresults[<first metric key>]['<key>_<eval_metric_type>'], which is used for best-model selection - Add your trainer class to
__init__.pyin thetrainer/folder:from .YourTrainer import YourTrainer
- Use it by specifying
--trainer YourTrainerwhen runningmain.py.