MonAI Handlers
Handlers for experiment tracking on Weights & Biases with MonAI Engines.
WandbStatsHandler
WandbStatsHandler
defines a set of Ignite Event-handlers for all the Weights & Biases logging
logic. It can be used for any Ignite Engine(trainer, validator and evaluator) and support both
epoch level and iteration level. The expected data source is Ignite engine.state.output
and
engine.state.metrics
.
Default behaviors
- When EPOCH_COMPLETED, write each dictionary item in
engine.state.metrics
to Weights & Biases. - When ITERATION_COMPLETED, write each dictionary item in
self.output_transform(engine.state.output)
to Weights & Biases.
Usage:
# WandbStatsHandler for logging training metrics and losses at
# every iteration to Weights & Biases
train_wandb_stats_handler = WandbStatsHandler(output_transform=lambda x: x)
train_wandb_stats_handler.attach(trainer)
# WandbStatsHandler for logging validation metrics and losses at
# every iteration to Weights & Biases
val_wandb_stats_handler = WandbStatsHandler(
output_transform=lambda x: None,
global_epoch_transform=lambda x: trainer.state.epoch,
)
val_wandb_stats_handler.attach(evaluator)
Example notebooks:
Pull Request to add WandbStatsHandler
to MonAI repository
There is an open pull request
to add WandbStatsHandler
to MonAI.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
iteration_log |
bool
|
Whether to write data to Weights & Biases when iteration completed,
default to |
True
|
epoch_log |
bool
|
Whether to write data to Weights & Biases when epoch completed, default to
|
True
|
epoch_event_writer |
Optional[Callable[[Engine, Any], Any]]
|
Customized callable Weights & Biases writer for epoch level. Must accept parameter "engine" and "summary_writer", use default event writer if None. |
None
|
epoch_interval |
int
|
The epoch interval at which the epoch_event_writer is called. Defaults to 1. |
1
|
iteration_event_writer |
Optional[Callable[[Engine, Any], Any]]
|
Customized callable Weights & Biases writer for iteration level. Must accept parameter "engine" and "summary_writer", use default event writer if None. |
None
|
iteration_interval |
int
|
The iteration interval at which the iteration_event_writer is called. Defaults to 1. |
1
|
output_transform |
Callable
|
A callable that is used to transform the
|
lambda : x[0]
|
global_epoch_transform |
Callable
|
A callable that is used to customize global epoch number. For example, in evaluation, the evaluator engine might want to use trainer engines epoch number when plotting epoch vs metric curves. |
lambda : x
|
state_attributes |
Optional[Sequence[str]]
|
Expected attributes from |
None
|
tag_name |
str
|
When iteration output is a scalar, tag_name is used to plot, defaults to |
DEFAULT_TAG
|
Source code in wandb_addons/monai/stats_handler.py
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 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 |
|
attach(engine)
Register a set of Ignite Event-Handlers to a specified Ignite engine.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
engine |
Engine
|
Ignite Engine, it can be a trainer, validator or evaluator. |
required |
Source code in wandb_addons/monai/stats_handler.py
close()
epoch_completed(engine)
Handler for train or validation/evaluation epoch completed Event. Write epoch level events
to Weights & Biases, default values are from Ignite engine.state.metrics
dict.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
engine |
Engine
|
Ignite Engine, it can be a trainer, validator or evaluator. |
required |
Source code in wandb_addons/monai/stats_handler.py
iteration_completed(engine)
Handler for train or validation/evaluation iteration completed Event. Write iteration level
events to Weighs & Biases, default values are from Ignite engine.state.output
.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
engine |
Engine
|
Ignite Engine, it can be a trainer, validator or evaluator. |
required |
Source code in wandb_addons/monai/stats_handler.py
WandbModelCheckpointSaver
Bases: BaseSaveHandler
WandbModelCheckpointSaver
is a save handler for PyTorch Ignite that saves model checkpoints as
Weights & Biases Artifacts.
Usage:
from wandb_addons.monai import WandbModelCheckpointSaver
checkpoint_handler = Checkpoint(
{"model": model, "optimizer": optimizer},
WandbModelCheckpointSaver(),
n_saved=1,
filename_prefix="best_checkpoint",
score_name=metric_name,
global_step_transform=global_step_from_engine(trainer)
)
evaluator.add_event_handler(Events.COMPLETED, checkpoint_handler)