synapse_net.training.semisupervised_training

  1import os
  2from typing import Optional, Tuple
  3
  4from .domain_adaptation import mean_teacher_adaptation
  5from .supervised_training import supervised_training
  6
  7
  8def semisupervised_training(
  9    name: str,
 10    unsupervised_train_paths: Tuple[str],
 11    unsupervised_val_paths: Tuple[str],
 12    patch_shape: Tuple[int, int, int],
 13    save_root: str,
 14    supervised_train_paths: Tuple[str],
 15    supervised_val_paths: Tuple[str],
 16    label_key: str,
 17    source_checkpoint=None,
 18    confidence_threshold: float = 0.9,
 19    raw_key: str = "raw",
 20    batch_size: int = 1,
 21    lr: float = 1e-4,
 22    n_iterations: int = int(1e5),
 23    teacher_warmup_iterations: int = int(1e4),
 24    n_samples_train: Optional[int] = None,
 25    n_samples_val: Optional[int] = None,
 26    train_mask_paths: Optional[Tuple[str]] = None,
 27    val_mask_paths: Optional[Tuple[str]] = None,
 28    sample_mask_key: Optional[str] = None,
 29    unsupervised_sampler: Optional[callable] = None,
 30    supervised_sampler: Optional[callable] = None,
 31    backbone: Optional[str] = None,
 32    model_type: Optional[str] = None,
 33    encoder_checkpoint: Optional[str] = None,
 34    aug_dict: Optional[dict] = None,
 35    separate_backward: bool = False,
 36    check: bool = False,
 37):
 38    """Run semisupervised segmentation training.
 39
 40    This proceeds in two phases:
 41
 42        1. If no `source_checkpoint` is given, run supervised training to
 43            warmup the teacher.
 44        2. Run semisupervised training using mean teacher setup with invertible
 45            augmentations, using the warmup checkpoint to initialize the teacher.
 46
 47    Args:
 48        name: The name for the checkpoint to be trained. The warmup checkpoint is saved
 49            under the name "{name}-warmup".
 50        unsupervised_train_paths: Filepaths to the hdf5 files for the unsupervised
 51            training data. This data does not require labels.
 52        unsupervised_val_paths: Filepaths to the hdf5 files for the unsupervised
 53            validation data. This data does not require labels.
 54        patch_shape: The patch shape used for a training example.
 55            In order to run 2d training pass a patch shape with a singleton in the z-axis,
 56            e.g. 'patch_shape = [1, 512, 512]'.
 57        save_root: Folder where the checkpoints will be saved.
 58        supervised_train_paths: Filepaths to the hdf5 files for the supervised training
 59            data, requires labels. Used for both the teacher warmup and the semi-supervised loss.
 60        supervised_val_paths: Filepaths to the hdf5 files for the supervised validation data,
 61            requires labels.
 62        label_key: The key that holds the labels inside of the hdf5 files.
 63        source_checkpoint: Warmup checkpoint used to initialize the teacher model. If not provided,
 64            run supervised training for `teacher_warmup_iterations`.
 65        confidence_threshold: The threshold for filtering data in the unsupervised loss.
 66            The label filtering is done based on the uncertainty of network predictions, and only
 67            the data with higher certainty than this threshold is used for training.
 68        raw_key: The key that holds the raw data inside of the hdf5 files.
 69        batch_size: The batch size for training.
 70        lr: The initial learning rate.
 71        n_iterations: The number of mean-teacher training iterations.
 72            Set to 0 to stop after the teacher warmup.
 73        teacher_warmup_iterations: The number of iterations for the supervised teacher warmup,
 74            only used if no warmup checkpoint exists yet and no `source_checkpoint` is given.
 75        n_samples_train: The number of train samples per epoch. By default this will be estimated
 76            based on the patch_shape and size of the volumes used for training.
 77        n_samples_val: The number of val samples per epoch. By default this will be estimated
 78            based on the patch_shape and size of the volumes used for validation.
 79        train_mask_paths: Sample masks used by the unsupervised sampler to accept or reject patches for training.
 80        val_mask_paths: Sample masks used by the unsupervised sampler to accept or reject patches for validation.
 81        sample_mask_key: The key to the sample mask dataset inside each file.
 82        unsupervised_sampler: Sampler to accept or reject patches for the unsupervised data stream.
 83        supervised_sampler: Sampler to accept or reject patches for the supervised data stream.
 84        backbone: The pretrained encoder. Options: "sam", "sam2", "dinov2", "dinov3" (ViT encoders of a UNETR),
 85            or "cryosiam" (3D CNN encoder of a CryoZooUNet).
 86            The ViT backbones must be set together with `model_type`.
 87        model_type: Model type for the selected ViT `backbone` model family, for example "vit_b" or
 88            "vit_b_em_organelles" for "sam".
 89        encoder_checkpoint: Path to the pretrained encoder weights for `backbone`.
 90            By default they are fetched for `backbone` and `model_type`.
 91        aug_dict: The augmentations for the teacher and the student, with keys "intensity" and "geometrical".
 92            Defaults to Gaussian blur and noise, plus random flips and 90-degree rotations.
 93        separate_backward: Whether to backpropagate each loss term separately to reduce peak memory.
 94        check: Whether to check the training and validation loaders instead of running training.
 95    """
 96    if check:
 97        mean_teacher_adaptation(
 98            name=name,
 99            unsupervised_train_paths=unsupervised_train_paths,
100            unsupervised_val_paths=unsupervised_val_paths,
101            patch_shape=patch_shape,
102            save_root=save_root,
103            source_checkpoint=source_checkpoint,
104            supervised_train_paths=supervised_train_paths,
105            supervised_val_paths=supervised_val_paths,
106            confidence_threshold=confidence_threshold,
107            raw_key=raw_key,
108            raw_key_supervised=raw_key,
109            label_key=label_key,
110            batch_size=batch_size,
111            lr=lr,
112            n_iterations=n_iterations,
113            n_samples_train=n_samples_train,
114            n_samples_val=n_samples_val,
115            train_mask_paths=train_mask_paths,
116            val_mask_paths=val_mask_paths,
117            sample_mask_key=sample_mask_key,
118            unsupervised_sampler=unsupervised_sampler,
119            supervised_sampler=supervised_sampler,
120            backbone=backbone,
121            model_type=model_type,
122            encoder_checkpoint=encoder_checkpoint,
123            aug_dict=aug_dict,
124            separate_backward=separate_backward,
125            check=True,
126        )
127        return
128
129    warmup_name = f"{name}-warmup"
130    if source_checkpoint is None:
131        warmup_checkpoint = os.path.join(save_root, "checkpoints", warmup_name, "best.pt")
132
133        if not os.path.exists(warmup_checkpoint):
134            print(
135                "No warmup checkpoint was found, initiating supervised warmup for teacher model "
136                f"with {teacher_warmup_iterations} iterations."
137            )
138
139            supervised_training(
140                name=warmup_name,
141                train_paths=supervised_train_paths,
142                val_paths=supervised_val_paths,
143                label_key=label_key,
144                patch_shape=patch_shape,
145                save_root=save_root,
146                raw_key=raw_key,
147                batch_size=batch_size,
148                lr=lr,
149                n_iterations=teacher_warmup_iterations,
150                sampler=supervised_sampler,
151                n_samples_train=n_samples_train,
152                n_samples_val=n_samples_val,
153                backbone=backbone,
154                model_type=model_type,
155                encoder_checkpoint=encoder_checkpoint,
156                check=False,
157            )
158        source_checkpoint = os.path.dirname(warmup_checkpoint)
159
160    if n_iterations == 0:
161        return
162
163    mean_teacher_adaptation(
164        name=name,
165        unsupervised_train_paths=unsupervised_train_paths,
166        unsupervised_val_paths=unsupervised_val_paths,
167        patch_shape=patch_shape,
168        save_root=save_root,
169        source_checkpoint=source_checkpoint,
170        supervised_train_paths=supervised_train_paths,
171        supervised_val_paths=supervised_val_paths,
172        confidence_threshold=confidence_threshold,
173        raw_key=raw_key,
174        raw_key_supervised=raw_key,
175        label_key=label_key,
176        batch_size=batch_size,
177        lr=lr,
178        n_iterations=n_iterations,
179        n_samples_train=n_samples_train,
180        n_samples_val=n_samples_val,
181        train_mask_paths=train_mask_paths,
182        val_mask_paths=val_mask_paths,
183        sample_mask_key=sample_mask_key,
184        unsupervised_sampler=unsupervised_sampler,
185        supervised_sampler=supervised_sampler,
186        backbone=backbone,
187        model_type=model_type,
188        encoder_checkpoint=encoder_checkpoint,
189        aug_dict=aug_dict,
190        separate_backward=separate_backward,
191        check=False,
192    )
def semisupervised_training( name: str, unsupervised_train_paths: Tuple[str], unsupervised_val_paths: Tuple[str], patch_shape: Tuple[int, int, int], save_root: str, supervised_train_paths: Tuple[str], supervised_val_paths: Tuple[str], label_key: str, source_checkpoint=None, confidence_threshold: float = 0.9, raw_key: str = 'raw', batch_size: int = 1, lr: float = 0.0001, n_iterations: int = 100000, teacher_warmup_iterations: int = 10000, n_samples_train: Optional[int] = None, n_samples_val: Optional[int] = None, train_mask_paths: Optional[Tuple[str]] = None, val_mask_paths: Optional[Tuple[str]] = None, sample_mask_key: Optional[str] = None, unsupervised_sampler: Optional[<built-in function callable>] = None, supervised_sampler: Optional[<built-in function callable>] = None, backbone: Optional[str] = None, model_type: Optional[str] = None, encoder_checkpoint: Optional[str] = None, aug_dict: Optional[dict] = None, separate_backward: bool = False, check: bool = False):
  9def semisupervised_training(
 10    name: str,
 11    unsupervised_train_paths: Tuple[str],
 12    unsupervised_val_paths: Tuple[str],
 13    patch_shape: Tuple[int, int, int],
 14    save_root: str,
 15    supervised_train_paths: Tuple[str],
 16    supervised_val_paths: Tuple[str],
 17    label_key: str,
 18    source_checkpoint=None,
 19    confidence_threshold: float = 0.9,
 20    raw_key: str = "raw",
 21    batch_size: int = 1,
 22    lr: float = 1e-4,
 23    n_iterations: int = int(1e5),
 24    teacher_warmup_iterations: int = int(1e4),
 25    n_samples_train: Optional[int] = None,
 26    n_samples_val: Optional[int] = None,
 27    train_mask_paths: Optional[Tuple[str]] = None,
 28    val_mask_paths: Optional[Tuple[str]] = None,
 29    sample_mask_key: Optional[str] = None,
 30    unsupervised_sampler: Optional[callable] = None,
 31    supervised_sampler: Optional[callable] = None,
 32    backbone: Optional[str] = None,
 33    model_type: Optional[str] = None,
 34    encoder_checkpoint: Optional[str] = None,
 35    aug_dict: Optional[dict] = None,
 36    separate_backward: bool = False,
 37    check: bool = False,
 38):
 39    """Run semisupervised segmentation training.
 40
 41    This proceeds in two phases:
 42
 43        1. If no `source_checkpoint` is given, run supervised training to
 44            warmup the teacher.
 45        2. Run semisupervised training using mean teacher setup with invertible
 46            augmentations, using the warmup checkpoint to initialize the teacher.
 47
 48    Args:
 49        name: The name for the checkpoint to be trained. The warmup checkpoint is saved
 50            under the name "{name}-warmup".
 51        unsupervised_train_paths: Filepaths to the hdf5 files for the unsupervised
 52            training data. This data does not require labels.
 53        unsupervised_val_paths: Filepaths to the hdf5 files for the unsupervised
 54            validation data. This data does not require labels.
 55        patch_shape: The patch shape used for a training example.
 56            In order to run 2d training pass a patch shape with a singleton in the z-axis,
 57            e.g. 'patch_shape = [1, 512, 512]'.
 58        save_root: Folder where the checkpoints will be saved.
 59        supervised_train_paths: Filepaths to the hdf5 files for the supervised training
 60            data, requires labels. Used for both the teacher warmup and the semi-supervised loss.
 61        supervised_val_paths: Filepaths to the hdf5 files for the supervised validation data,
 62            requires labels.
 63        label_key: The key that holds the labels inside of the hdf5 files.
 64        source_checkpoint: Warmup checkpoint used to initialize the teacher model. If not provided,
 65            run supervised training for `teacher_warmup_iterations`.
 66        confidence_threshold: The threshold for filtering data in the unsupervised loss.
 67            The label filtering is done based on the uncertainty of network predictions, and only
 68            the data with higher certainty than this threshold is used for training.
 69        raw_key: The key that holds the raw data inside of the hdf5 files.
 70        batch_size: The batch size for training.
 71        lr: The initial learning rate.
 72        n_iterations: The number of mean-teacher training iterations.
 73            Set to 0 to stop after the teacher warmup.
 74        teacher_warmup_iterations: The number of iterations for the supervised teacher warmup,
 75            only used if no warmup checkpoint exists yet and no `source_checkpoint` is given.
 76        n_samples_train: The number of train samples per epoch. By default this will be estimated
 77            based on the patch_shape and size of the volumes used for training.
 78        n_samples_val: The number of val samples per epoch. By default this will be estimated
 79            based on the patch_shape and size of the volumes used for validation.
 80        train_mask_paths: Sample masks used by the unsupervised sampler to accept or reject patches for training.
 81        val_mask_paths: Sample masks used by the unsupervised sampler to accept or reject patches for validation.
 82        sample_mask_key: The key to the sample mask dataset inside each file.
 83        unsupervised_sampler: Sampler to accept or reject patches for the unsupervised data stream.
 84        supervised_sampler: Sampler to accept or reject patches for the supervised data stream.
 85        backbone: The pretrained encoder. Options: "sam", "sam2", "dinov2", "dinov3" (ViT encoders of a UNETR),
 86            or "cryosiam" (3D CNN encoder of a CryoZooUNet).
 87            The ViT backbones must be set together with `model_type`.
 88        model_type: Model type for the selected ViT `backbone` model family, for example "vit_b" or
 89            "vit_b_em_organelles" for "sam".
 90        encoder_checkpoint: Path to the pretrained encoder weights for `backbone`.
 91            By default they are fetched for `backbone` and `model_type`.
 92        aug_dict: The augmentations for the teacher and the student, with keys "intensity" and "geometrical".
 93            Defaults to Gaussian blur and noise, plus random flips and 90-degree rotations.
 94        separate_backward: Whether to backpropagate each loss term separately to reduce peak memory.
 95        check: Whether to check the training and validation loaders instead of running training.
 96    """
 97    if check:
 98        mean_teacher_adaptation(
 99            name=name,
100            unsupervised_train_paths=unsupervised_train_paths,
101            unsupervised_val_paths=unsupervised_val_paths,
102            patch_shape=patch_shape,
103            save_root=save_root,
104            source_checkpoint=source_checkpoint,
105            supervised_train_paths=supervised_train_paths,
106            supervised_val_paths=supervised_val_paths,
107            confidence_threshold=confidence_threshold,
108            raw_key=raw_key,
109            raw_key_supervised=raw_key,
110            label_key=label_key,
111            batch_size=batch_size,
112            lr=lr,
113            n_iterations=n_iterations,
114            n_samples_train=n_samples_train,
115            n_samples_val=n_samples_val,
116            train_mask_paths=train_mask_paths,
117            val_mask_paths=val_mask_paths,
118            sample_mask_key=sample_mask_key,
119            unsupervised_sampler=unsupervised_sampler,
120            supervised_sampler=supervised_sampler,
121            backbone=backbone,
122            model_type=model_type,
123            encoder_checkpoint=encoder_checkpoint,
124            aug_dict=aug_dict,
125            separate_backward=separate_backward,
126            check=True,
127        )
128        return
129
130    warmup_name = f"{name}-warmup"
131    if source_checkpoint is None:
132        warmup_checkpoint = os.path.join(save_root, "checkpoints", warmup_name, "best.pt")
133
134        if not os.path.exists(warmup_checkpoint):
135            print(
136                "No warmup checkpoint was found, initiating supervised warmup for teacher model "
137                f"with {teacher_warmup_iterations} iterations."
138            )
139
140            supervised_training(
141                name=warmup_name,
142                train_paths=supervised_train_paths,
143                val_paths=supervised_val_paths,
144                label_key=label_key,
145                patch_shape=patch_shape,
146                save_root=save_root,
147                raw_key=raw_key,
148                batch_size=batch_size,
149                lr=lr,
150                n_iterations=teacher_warmup_iterations,
151                sampler=supervised_sampler,
152                n_samples_train=n_samples_train,
153                n_samples_val=n_samples_val,
154                backbone=backbone,
155                model_type=model_type,
156                encoder_checkpoint=encoder_checkpoint,
157                check=False,
158            )
159        source_checkpoint = os.path.dirname(warmup_checkpoint)
160
161    if n_iterations == 0:
162        return
163
164    mean_teacher_adaptation(
165        name=name,
166        unsupervised_train_paths=unsupervised_train_paths,
167        unsupervised_val_paths=unsupervised_val_paths,
168        patch_shape=patch_shape,
169        save_root=save_root,
170        source_checkpoint=source_checkpoint,
171        supervised_train_paths=supervised_train_paths,
172        supervised_val_paths=supervised_val_paths,
173        confidence_threshold=confidence_threshold,
174        raw_key=raw_key,
175        raw_key_supervised=raw_key,
176        label_key=label_key,
177        batch_size=batch_size,
178        lr=lr,
179        n_iterations=n_iterations,
180        n_samples_train=n_samples_train,
181        n_samples_val=n_samples_val,
182        train_mask_paths=train_mask_paths,
183        val_mask_paths=val_mask_paths,
184        sample_mask_key=sample_mask_key,
185        unsupervised_sampler=unsupervised_sampler,
186        supervised_sampler=supervised_sampler,
187        backbone=backbone,
188        model_type=model_type,
189        encoder_checkpoint=encoder_checkpoint,
190        aug_dict=aug_dict,
191        separate_backward=separate_backward,
192        check=False,
193    )

Run semisupervised segmentation training.

This proceeds in two phases:
  1. If no source_checkpoint is given, run supervised training to warmup the teacher.
  2. Run semisupervised training using mean teacher setup with invertible augmentations, using the warmup checkpoint to initialize the teacher.
Arguments:
  • name: The name for the checkpoint to be trained. The warmup checkpoint is saved under the name "{name}-warmup".
  • unsupervised_train_paths: Filepaths to the hdf5 files for the unsupervised training data. This data does not require labels.
  • unsupervised_val_paths: Filepaths to the hdf5 files for the unsupervised validation data. This data does not require labels.
  • patch_shape: The patch shape used for a training example. In order to run 2d training pass a patch shape with a singleton in the z-axis, e.g. 'patch_shape = [1, 512, 512]'.
  • save_root: Folder where the checkpoints will be saved.
  • supervised_train_paths: Filepaths to the hdf5 files for the supervised training data, requires labels. Used for both the teacher warmup and the semi-supervised loss.
  • supervised_val_paths: Filepaths to the hdf5 files for the supervised validation data, requires labels.
  • label_key: The key that holds the labels inside of the hdf5 files.
  • source_checkpoint: Warmup checkpoint used to initialize the teacher model. If not provided, run supervised training for teacher_warmup_iterations.
  • confidence_threshold: The threshold for filtering data in the unsupervised loss. The label filtering is done based on the uncertainty of network predictions, and only the data with higher certainty than this threshold is used for training.
  • raw_key: The key that holds the raw data inside of the hdf5 files.
  • batch_size: The batch size for training.
  • lr: The initial learning rate.
  • n_iterations: The number of mean-teacher training iterations. Set to 0 to stop after the teacher warmup.
  • teacher_warmup_iterations: The number of iterations for the supervised teacher warmup, only used if no warmup checkpoint exists yet and no source_checkpoint is given.
  • n_samples_train: The number of train samples per epoch. By default this will be estimated based on the patch_shape and size of the volumes used for training.
  • n_samples_val: The number of val samples per epoch. By default this will be estimated based on the patch_shape and size of the volumes used for validation.
  • train_mask_paths: Sample masks used by the unsupervised sampler to accept or reject patches for training.
  • val_mask_paths: Sample masks used by the unsupervised sampler to accept or reject patches for validation.
  • sample_mask_key: The key to the sample mask dataset inside each file.
  • unsupervised_sampler: Sampler to accept or reject patches for the unsupervised data stream.
  • supervised_sampler: Sampler to accept or reject patches for the supervised data stream.
  • backbone: The pretrained encoder. Options: "sam", "sam2", "dinov2", "dinov3" (ViT encoders of a UNETR), or "cryosiam" (3D CNN encoder of a CryoZooUNet). The ViT backbones must be set together with model_type.
  • model_type: Model type for the selected ViT backbone model family, for example "vit_b" or "vit_b_em_organelles" for "sam".
  • encoder_checkpoint: Path to the pretrained encoder weights for backbone. By default they are fetched for backbone and model_type.
  • aug_dict: The augmentations for the teacher and the student, with keys "intensity" and "geometrical". Defaults to Gaussian blur and noise, plus random flips and 90-degree rotations.
  • separate_backward: Whether to backpropagate each loss term separately to reduce peak memory.
  • check: Whether to check the training and validation loaders instead of running training.