cellmap_flow.finetune.trainable
Building blocks for handing the finetune trainer a model’s network.
The trainer trains a torch module that takes the normalized patch
(B, C, Z, Y, X) and gives the model’s output (B, C’, Z’, Y’, X’). Many
networks are not quite that: a 2D network wants (N, C, Y, X), a model
normalizes its own input, crops a halo off its output or ends in a
sigmoid its serving applies. A model type’s trainable_model() wraps
its network in these, composed with torch.nn.Sequential, rather than
writing its own wrapper; they hold no parameters of their own, so what is
trained is the network’s.
finetune_modes says what a module can be finetuned with: LoRA attaches
adapters beside a network’s Conv and Linear layers, which needs a Python
module tree, while a full finetune trains every parameter, which a
compiled TorchScript module has too.
Classes
Run a 2D network on each z slice of (B, C, Z, Y, X). |
|
Cut |
|
(x - mean) / (std + eps), mean and std over |
|
(x - mean) / (std + eps) with a given mean and std (one number, or one |
|
(x - lo) / (hi - lo + eps), lo and hi the |
|
x * gain + offset (one number, or one per channel): bioimage.io's |
|
x clamped to [min, max] (either may be None): bioimage.io's |
Functions
|
What |
Module Contents
- cellmap_flow.finetune.trainable.finetune_modes(module: torch.nn.Module) Tuple[str, ...]
What
modulecan be finetuned with: (“lora”, “full”), (“full”,) or ().A TorchScript module trains in full but takes no adapters (its layers are compiled, so PEFT cannot wrap them); a module without parameters cannot be trained at all; any other with Conv or Linear layers takes both.
- class cellmap_flow.finetune.trainable.SliceWise(net: torch.nn.Module)
Run a 2D network on each z slice of (B, C, Z, Y, X).
The slices go through
netas one batch of B * Z images and come back as (B, C’, Z, Y’, X’): a 2D model served slice by slice is trained the way it is served.- net
- forward(x)
- class cellmap_flow.finetune.trainable.Crop(crop: Sequence[int])
Cut
cropvoxels off each side of the spatial axes (the lastlen(crop)): a model’s halo, which its serving cuts off too.- crop
- forward(x)
- class cellmap_flow.finetune.trainable.ZeroMeanUnitVariance(dims: Sequence[int] | None = None, eps: float = 1e-06)
(x - mean) / (std + eps), mean and std over
dimsof each sample (all but the batch by default): bioimage.io’szero_mean_unit_variance.- forward(x)
- class cellmap_flow.finetune.trainable.FixedZeroMeanUnitVariance(mean, std, eps: float = 1e-06, channel_dim: int = 1)
(x - mean) / (std + eps) with a given mean and std (one number, or one per channel): bioimage.io’s
fixed_zero_mean_unit_variance.- forward(x)
- class cellmap_flow.finetune.trainable.ScaleRange(min_percentile: float = 0.0, max_percentile: float = 100.0, dims: Sequence[int] | None = None, eps: float = 1e-06)
(x - lo) / (hi - lo + eps), lo and hi the
min_percentileandmax_percentile(0-100) overdimsof each sample: bioimage.io’sscale_range, and Cellpose’s 1-99% normalization.- forward(x)