callbacks
FileWriter
Bases: BaseWriter
Writer for saving predictions to files in various formats including tensors, images, videos, and ITK images. Custom writer functions can be provided to extend supported formats. Args: path: Directory path where output files will be saved. writer: Either a string specifying a built-in writer or a custom writer function. Built-in writers: - "tensor": Saves raw tensor data (.pt) - "image": Saves as image file (.png) - "video": Saves as video file - "itk_nrrd": Saves as ITK NRRD file (.nrrd) - "itk_seg_nrrd": Saves as ITK segmentation NRRD file (.seg.nrrd) - "itk_nifti": Saves as ITK NIfTI file (.nii.gz) Custom writers must: - Accept (path, tensor) arguments - Handle single tensor input (no batch dimension) - Save output to the specified path
Source code in src/lighter/callbacks/writer/file.py
write(tensor, identifier)
Writes the tensor to a file using the specified writer.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
tensor
|
Tensor
|
The tensor to write. |
required |
identifier
|
int | str
|
Identifier for naming the file. |
required |
Source code in src/lighter/callbacks/writer/file.py
Freezer
Bases: Callback
Callback to freeze model parameters during training. Parameters can be frozen by exact name or prefix. Freezing can be applied indefinitely or until a specified step/epoch.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
names
|
str | list[str] | None
|
Full names of parameters to freeze. |
None
|
name_starts_with
|
str | list[str] | None
|
Prefixes of parameter names to freeze. |
None
|
except_names
|
str | list[str] | None
|
Names of parameters to exclude from freezing. |
None
|
except_name_starts_with
|
str | list[str] | None
|
Prefixes of parameter names to exclude from freezing. |
None
|
until_step
|
int | None
|
Maximum step to freeze parameters until. |
None
|
until_epoch
|
int | None
|
Maximum epoch to freeze parameters until. |
None
|
Raises:
Type | Description |
---|---|
ValueError
|
If neither |
ValueError
|
If both |
Source code in src/lighter/callbacks/freezer.py
15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 |
|
_on_batch_start(trainer, pl_module)
Freezes or unfreezes model parameters based on the current step or epoch.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
trainer
|
Trainer
|
The trainer instance. |
required |
pl_module
|
System
|
The System instance. |
required |
Source code in src/lighter/callbacks/freezer.py
_set_model_requires_grad(model, requires_grad)
Sets the requires_grad attribute for model parameters, effectively freezing or unfreezing them.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
model
|
Module | System
|
The model whose parameters to modify. |
required |
requires_grad
|
bool
|
Whether to allow gradients (unfreeze) or not (freeze). |
required |
Source code in src/lighter/callbacks/freezer.py
on_train_batch_start(trainer, pl_module, batch, batch_idx)
Called at the start of each training batch to potentially freeze parameters.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
trainer
|
Trainer
|
The trainer instance. |
required |
pl_module
|
System
|
The System instance. |
required |
batch
|
Any
|
The current batch. |
required |
batch_idx
|
int
|
The index of the batch. |
required |
Source code in src/lighter/callbacks/freezer.py
TableWriter
Bases: BaseWriter
Writer for saving predictions in a table format, such as CSV.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
path
|
str | Path
|
CSV filepath. |
required |
writer
|
str | Callable
|
Writer function or name of a registered writer. |
required |
Source code in src/lighter/callbacks/writer/table.py
on_predict_epoch_end(trainer, pl_module)
Called at the end of the prediction epoch to save predictions to a CSV file.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
trainer
|
Trainer
|
The trainer instance. |
required |
pl_module
|
System
|
The System instance. |
required |
Source code in src/lighter/callbacks/writer/table.py
write(tensor, identifier)
Writes the tensor as a table record using the specified writer.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
tensor
|
Any
|
The tensor to record. Should not have a batch dimension. |
required |
identifier
|
int | str
|
Identifier for the record. |
required |