synapse_net.training.mitochondria
Training of the mitochondria model for electron tomography (mitochondria2).
1"""Training of the mitochondria model for electron tomography (`mitochondria2`).""" 2 3from typing import List, Optional, Sequence, Tuple 4 5import torch_em 6from torch_em.data import MinInstanceSampler 7 8from .supervised_training import _get_split_paths, supervised_training 9 10 11def get_mitochondria_paths( 12 data_root: str, split_file: Optional[str] = None, val_fraction: float = 0.15, seed: int = 42, 13) -> Tuple[List[str], List[str]]: 14 """Get the training and validation data for mitochondria training. 15 16 Args: 17 data_root: The folder with the training data, hdf5 files that are found recursively. 18 split_file: A json file with the keys 'train' and 'val', holding filepaths relative to `data_root`. 19 By default the files are split randomly. 20 val_fraction: The fraction of the files used for validation in a random split. 21 seed: The seed for the random split. 22 23 Returns: 24 The filepaths for training. 25 The filepaths for validation. 26 """ 27 return _get_split_paths(data_root, split_file, "*.h5", val_fraction, seed) 28 29 30def mitochondria_training( 31 name: str, 32 train_paths: Sequence[str], 33 val_paths: Sequence[str], 34 save_root: Optional[str] = None, 35 patch_shape: Tuple[int, int, int] = (32, 256, 256), 36 batch_size: int = 8, 37 lr: float = 1e-4, 38 n_iterations: int = int(1.5e5), 39 early_stopping: int = 20, 40 mixed_precision: bool = False, 41 num_workers: int = 8, 42 checkpoint_path: Optional[str] = None, 43 resume: bool = False, 44 check: bool = False, 45) -> None: 46 """Train the mitochondria model for electron tomography, with the recipe of `mitochondria2`. 47 48 The model predicts foreground and boundaries from tomograms that are normalized with the 1st and 99th 49 percentile. Inference does not need this normalization: the instance normalization of the model makes 50 its output independent of it, so the default standardization of `segment_mitochondria` gives the same result. 51 The default batch size and patch shape need an 80 GB GPU; `mixed_precision` reduces the memory. 52 53 Args: 54 name: The name of the checkpoint. 55 train_paths: The hdf5 files for training, with the tomogram in 'raw' and the mitochondria in 56 'labels/mitochondria'. 57 val_paths: The hdf5 files for validation. 58 save_root: The folder for the checkpoint and the logs. 59 patch_shape: The patch shape for training. 60 batch_size: The batch size for training. 61 lr: The initial learning rate. 62 n_iterations: The maximal number of iterations. 63 early_stopping: The number of epochs without improvement after which training is stopped. 64 mixed_precision: Whether to train with mixed precision. The published model was trained without. 65 num_workers: The number of workers for the dataloaders. 66 checkpoint_path: A checkpoint to initialize the weights from. 67 resume: Whether to continue the previous run with this name, see `supervised_training`. 68 check: Whether to check the dataloaders instead of training. 69 """ 70 supervised_training( 71 name=name, train_paths=tuple(train_paths), val_paths=tuple(val_paths), save_root=save_root, 72 raw_key="raw", label_key="labels/mitochondria", patch_shape=patch_shape, batch_size=batch_size, lr=lr, 73 n_iterations=n_iterations, n_samples_train=500, n_samples_val=500, sampler=MinInstanceSampler(p_reject=0.95), 74 raw_transform=torch_em.transform.raw.normalize_percentile, early_stopping=early_stopping, 75 mixed_precision=mixed_precision, log_image_interval=50, checkpoint_path=checkpoint_path, resume=resume, 76 check=check, num_workers=num_workers, shuffle=False, 77 )
def
get_mitochondria_paths( data_root: str, split_file: Optional[str] = None, val_fraction: float = 0.15, seed: int = 42) -> Tuple[List[str], List[str]]:
12def get_mitochondria_paths( 13 data_root: str, split_file: Optional[str] = None, val_fraction: float = 0.15, seed: int = 42, 14) -> Tuple[List[str], List[str]]: 15 """Get the training and validation data for mitochondria training. 16 17 Args: 18 data_root: The folder with the training data, hdf5 files that are found recursively. 19 split_file: A json file with the keys 'train' and 'val', holding filepaths relative to `data_root`. 20 By default the files are split randomly. 21 val_fraction: The fraction of the files used for validation in a random split. 22 seed: The seed for the random split. 23 24 Returns: 25 The filepaths for training. 26 The filepaths for validation. 27 """ 28 return _get_split_paths(data_root, split_file, "*.h5", val_fraction, seed)
Get the training and validation data for mitochondria training.
Arguments:
- data_root: The folder with the training data, hdf5 files that are found recursively.
- split_file: A json file with the keys 'train' and 'val', holding filepaths relative to
data_root. 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
mitochondria_training( name: str, train_paths: Sequence[str], val_paths: Sequence[str], save_root: Optional[str] = None, patch_shape: Tuple[int, int, int] = (32, 256, 256), batch_size: int = 8, lr: float = 0.0001, n_iterations: int = 150000, early_stopping: int = 20, mixed_precision: bool = False, num_workers: int = 8, checkpoint_path: Optional[str] = None, resume: bool = False, check: bool = False) -> None:
31def mitochondria_training( 32 name: str, 33 train_paths: Sequence[str], 34 val_paths: Sequence[str], 35 save_root: Optional[str] = None, 36 patch_shape: Tuple[int, int, int] = (32, 256, 256), 37 batch_size: int = 8, 38 lr: float = 1e-4, 39 n_iterations: int = int(1.5e5), 40 early_stopping: int = 20, 41 mixed_precision: bool = False, 42 num_workers: int = 8, 43 checkpoint_path: Optional[str] = None, 44 resume: bool = False, 45 check: bool = False, 46) -> None: 47 """Train the mitochondria model for electron tomography, with the recipe of `mitochondria2`. 48 49 The model predicts foreground and boundaries from tomograms that are normalized with the 1st and 99th 50 percentile. Inference does not need this normalization: the instance normalization of the model makes 51 its output independent of it, so the default standardization of `segment_mitochondria` gives the same result. 52 The default batch size and patch shape need an 80 GB GPU; `mixed_precision` reduces the memory. 53 54 Args: 55 name: The name of the checkpoint. 56 train_paths: The hdf5 files for training, with the tomogram in 'raw' and the mitochondria in 57 'labels/mitochondria'. 58 val_paths: The hdf5 files for validation. 59 save_root: The folder for the checkpoint and the logs. 60 patch_shape: The patch shape for training. 61 batch_size: The batch size for training. 62 lr: The initial learning rate. 63 n_iterations: The maximal number of iterations. 64 early_stopping: The number of epochs without improvement after which training is stopped. 65 mixed_precision: Whether to train with mixed precision. The published model was trained without. 66 num_workers: The number of workers for the dataloaders. 67 checkpoint_path: A checkpoint to initialize the weights from. 68 resume: Whether to continue the previous run with this name, see `supervised_training`. 69 check: Whether to check the dataloaders instead of training. 70 """ 71 supervised_training( 72 name=name, train_paths=tuple(train_paths), val_paths=tuple(val_paths), save_root=save_root, 73 raw_key="raw", label_key="labels/mitochondria", patch_shape=patch_shape, batch_size=batch_size, lr=lr, 74 n_iterations=n_iterations, n_samples_train=500, n_samples_val=500, sampler=MinInstanceSampler(p_reject=0.95), 75 raw_transform=torch_em.transform.raw.normalize_percentile, early_stopping=early_stopping, 76 mixed_precision=mixed_precision, log_image_interval=50, checkpoint_path=checkpoint_path, resume=resume, 77 check=check, num_workers=num_workers, shuffle=False, 78 )
Train the mitochondria model for electron tomography, with the recipe of mitochondria2.
The model predicts foreground and boundaries from tomograms that are normalized with the 1st and 99th
percentile. Inference does not need this normalization: the instance normalization of the model makes
its output independent of it, so the default standardization of segment_mitochondria gives the same result.
The default batch size and patch shape need an 80 GB GPU; mixed_precision reduces the memory.
Arguments:
- name: The name of the checkpoint.
- train_paths: The hdf5 files for training, with the tomogram 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.