synapse_net.training.mitochondria_vol_em

Training of the mitochondria model for FIB-SEM volume EM (mitochondria_fibsem).

 1"""Training of the mitochondria model for FIB-SEM volume EM (`mitochondria_fibsem`)."""
 2
 3from typing import List, Mapping, Optional, Sequence, Tuple
 4
 5from torch_em.data import MinInstanceSampler
 6
 7from .supervised_training import _get_split_paths, supervised_training
 8from .transform import RemoveWhitePatchesAndNormalize
 9from ..inference.inference import _MODELS
10
11VOL_EM_SCALE_FACTORS = _MODELS["mitochondria_fibsem"].unet["scale_factors"]
12"""The U-Net downscaling factors: two anisotropic levels for the 5:1 anisotropy of the data."""
13
14
15def get_vol_em_mitochondria_paths(
16    data_roots: Optional[Mapping[str, str]] = None,
17    split_file: Optional[str] = None,
18    val_fraction: float = 0.15,
19    seed: int = 42,
20) -> Tuple[List[str], List[str]]:
21    """Get the training and validation data for volume EM mitochondria training.
22
23    Args:
24        data_roots: The folders with the training data, by root name. The hdf5 files are found recursively in
25            them. If a `split_file` is given, they override the roots stored in it.
26        split_file: A json file with the keys 'train' and 'val', and optionally 'roots', whose entries have the
27            form '<root name>/<relative path>'. By default the files are split randomly.
28        val_fraction: The fraction of the files used for validation in a random split.
29        seed: The seed for the random split.
30
31    Returns:
32        The filepaths for training.
33        The filepaths for validation.
34    """
35    return _get_split_paths(data_roots, split_file, "*.h5", val_fraction, seed)
36
37
38def vol_em_mitochondria_training(
39    name: str,
40    train_paths: Sequence[str],
41    val_paths: Sequence[str],
42    save_root: Optional[str] = None,
43    patch_shape: Tuple[int, int, int] = (32, 512, 512),
44    batch_size: int = 4,
45    lr: float = 1e-4,
46    n_iterations: int = int(5e4),
47    early_stopping: int = 20,
48    mixed_precision: bool = False,
49    num_workers: int = 8,
50    checkpoint_path: Optional[str] = None,
51    resume: bool = False,
52    check: bool = False,
53) -> None:
54    """Train the mitochondria model for FIB-SEM volume EM, with the recipe of `mitochondria_fibsem`.
55
56    The model is an anisotropic U-Net without normalization layers that predicts foreground and boundaries,
57    trained at 25 nm in z and 5 nm in xy. The white filler borders of the blocks are removed before the
58    percentile normalization; `synapse_net.inference.mitochondria.segment_mitochondria_fibsem` does the same
59    at inference. The default batch size and patch shape need an 80 GB GPU.
60
61    Args:
62        name: The name of the checkpoint.
63        train_paths: The hdf5 files for training, with the data in 'raw' and the mitochondria in
64            'labels/mitochondria'.
65        val_paths: The hdf5 files for validation.
66        save_root: The folder for the checkpoint and the logs.
67        patch_shape: The patch shape for training.
68        batch_size: The batch size for training.
69        lr: The initial learning rate.
70        n_iterations: The maximal number of iterations.
71        early_stopping: The number of epochs without improvement after which training is stopped.
72        mixed_precision: Whether to train with mixed precision. The published model was trained without.
73        num_workers: The number of workers for the dataloaders.
74        checkpoint_path: A checkpoint to initialize the weights from.
75        resume: Whether to continue the previous run with this name, see `supervised_training`.
76        check: Whether to check the dataloaders instead of training.
77    """
78    supervised_training(
79        name=name, train_paths=tuple(train_paths), val_paths=tuple(val_paths), save_root=save_root,
80        raw_key="raw", label_key="labels/mitochondria", patch_shape=patch_shape, batch_size=batch_size, lr=lr,
81        n_iterations=n_iterations, n_samples_train=500, n_samples_val=500, sampler=MinInstanceSampler(p_reject=0.95),
82        raw_transform=RemoveWhitePatchesAndNormalize(), norm=None, scale_factors=VOL_EM_SCALE_FACTORS,
83        early_stopping=early_stopping, mixed_precision=mixed_precision, log_image_interval=50,
84        checkpoint_path=checkpoint_path, resume=resume, check=check, num_workers=num_workers, shuffle=False,
85    )
VOL_EM_SCALE_FACTORS = [[1, 2, 2], [1, 2, 2], [2, 2, 2], [2, 2, 2]]

The U-Net downscaling factors: two anisotropic levels for the 5:1 anisotropy of the data.

def get_vol_em_mitochondria_paths( data_roots: Optional[Mapping[str, str]] = None, split_file: Optional[str] = None, val_fraction: float = 0.15, seed: int = 42) -> Tuple[List[str], List[str]]:
16def get_vol_em_mitochondria_paths(
17    data_roots: Optional[Mapping[str, str]] = None,
18    split_file: Optional[str] = None,
19    val_fraction: float = 0.15,
20    seed: int = 42,
21) -> Tuple[List[str], List[str]]:
22    """Get the training and validation data for volume EM mitochondria training.
23
24    Args:
25        data_roots: The folders with the training data, by root name. The hdf5 files are found recursively in
26            them. If a `split_file` is given, they override the roots stored in it.
27        split_file: A json file with the keys 'train' and 'val', and optionally 'roots', whose entries have the
28            form '<root name>/<relative path>'. By default the files are split randomly.
29        val_fraction: The fraction of the files used for validation in a random split.
30        seed: The seed for the random split.
31
32    Returns:
33        The filepaths for training.
34        The filepaths for validation.
35    """
36    return _get_split_paths(data_roots, split_file, "*.h5", val_fraction, seed)

Get the training and validation data for volume EM mitochondria training.

Arguments:
  • data_roots: The folders with the training data, by root name. The hdf5 files are found recursively in them. If a split_file is given, they override the roots stored in it.
  • split_file: A json file with the keys 'train' and 'val', and optionally 'roots', whose entries have the form '/'. By default the files are split randomly.
  • val_fraction: The fraction of the files used for validation in a random split.
  • seed: The seed for the random split.
Returns:

The filepaths for training. The filepaths for validation.

def vol_em_mitochondria_training( name: str, train_paths: Sequence[str], val_paths: Sequence[str], save_root: Optional[str] = None, patch_shape: Tuple[int, int, int] = (32, 512, 512), batch_size: int = 4, lr: float = 0.0001, n_iterations: int = 50000, early_stopping: int = 20, mixed_precision: bool = False, num_workers: int = 8, checkpoint_path: Optional[str] = None, resume: bool = False, check: bool = False) -> None:
39def vol_em_mitochondria_training(
40    name: str,
41    train_paths: Sequence[str],
42    val_paths: Sequence[str],
43    save_root: Optional[str] = None,
44    patch_shape: Tuple[int, int, int] = (32, 512, 512),
45    batch_size: int = 4,
46    lr: float = 1e-4,
47    n_iterations: int = int(5e4),
48    early_stopping: int = 20,
49    mixed_precision: bool = False,
50    num_workers: int = 8,
51    checkpoint_path: Optional[str] = None,
52    resume: bool = False,
53    check: bool = False,
54) -> None:
55    """Train the mitochondria model for FIB-SEM volume EM, with the recipe of `mitochondria_fibsem`.
56
57    The model is an anisotropic U-Net without normalization layers that predicts foreground and boundaries,
58    trained at 25 nm in z and 5 nm in xy. The white filler borders of the blocks are removed before the
59    percentile normalization; `synapse_net.inference.mitochondria.segment_mitochondria_fibsem` does the same
60    at inference. The default batch size and patch shape need an 80 GB GPU.
61
62    Args:
63        name: The name of the checkpoint.
64        train_paths: The hdf5 files for training, with the data in 'raw' and the mitochondria in
65            'labels/mitochondria'.
66        val_paths: The hdf5 files for validation.
67        save_root: The folder for the checkpoint and the logs.
68        patch_shape: The patch shape for training.
69        batch_size: The batch size for training.
70        lr: The initial learning rate.
71        n_iterations: The maximal number of iterations.
72        early_stopping: The number of epochs without improvement after which training is stopped.
73        mixed_precision: Whether to train with mixed precision. The published model was trained without.
74        num_workers: The number of workers for the dataloaders.
75        checkpoint_path: A checkpoint to initialize the weights from.
76        resume: Whether to continue the previous run with this name, see `supervised_training`.
77        check: Whether to check the dataloaders instead of training.
78    """
79    supervised_training(
80        name=name, train_paths=tuple(train_paths), val_paths=tuple(val_paths), save_root=save_root,
81        raw_key="raw", label_key="labels/mitochondria", patch_shape=patch_shape, batch_size=batch_size, lr=lr,
82        n_iterations=n_iterations, n_samples_train=500, n_samples_val=500, sampler=MinInstanceSampler(p_reject=0.95),
83        raw_transform=RemoveWhitePatchesAndNormalize(), norm=None, scale_factors=VOL_EM_SCALE_FACTORS,
84        early_stopping=early_stopping, mixed_precision=mixed_precision, log_image_interval=50,
85        checkpoint_path=checkpoint_path, resume=resume, check=check, num_workers=num_workers, shuffle=False,
86    )

Train the mitochondria model for FIB-SEM volume EM, with the recipe of mitochondria_fibsem.

The model is an anisotropic U-Net without normalization layers that predicts foreground and boundaries, trained at 25 nm in z and 5 nm in xy. The white filler borders of the blocks are removed before the percentile normalization; synapse_net.inference.mitochondria.segment_mitochondria_fibsem does the same at inference. The default batch size and patch shape need an 80 GB GPU.

Arguments:
  • name: The name of the checkpoint.
  • train_paths: The hdf5 files for training, with the data in 'raw' and the mitochondria in 'labels/mitochondria'.
  • val_paths: The hdf5 files for validation.
  • save_root: The folder for the checkpoint and the logs.
  • patch_shape: The patch shape for training.
  • batch_size: The batch size for training.
  • lr: The initial learning rate.
  • n_iterations: The maximal number of iterations.
  • early_stopping: The number of epochs without improvement after which training is stopped.
  • mixed_precision: Whether to train with mixed precision. The published model was trained without.
  • num_workers: The number of workers for the dataloaders.
  • checkpoint_path: A checkpoint to initialize the weights from.
  • resume: Whether to continue the previous run with this name, see supervised_training.
  • check: Whether to check the dataloaders instead of training.