deeplabcut.pose_estimation_pytorch.runners.snapshots
Code to handle storing models.
Classes:
| Name | Description |
|---|---|
TorchSnapshotManager |
Class handling model checkpoint I/O. |
TorchSnapshotManager
dataclass
Class handling model checkpoint I/O.
Attributes:
| Name | Type | Description |
|---|---|---|
snapshot_prefix |
str
|
The prefix to use when saving snapshots. |
model_folder |
Path
|
The path to the directory where model snapshots should be stored. |
key_metric |
str | None
|
If defined, the metric is used to save the best model. Otherwise no best model is used. |
key_metric_asc |
bool
|
Whether the key metric is ascending (larger values are better). |
max_snapshots |
int
|
The maximum number of snapshots to store for the training run. This does not include the best model (e.g., setting max_snapshots=5 will mean that the 5 latest models will be kept, plus the best model) |
save_epochs |
int
|
The number of epochs between each model save |
save_optimizer_state |
bool
|
Whether to store the optimizer state. This makes snapshots much heavier, but allows to resume training as if it was never stopped. |
Examples:
Storing snapshots while training
model: nn.Module loader = DLCLoader(...) snapshot_manager = TorchSnapshotManager( "snapshot", loader.model_folder, key_metric="test.mAP", ) ... for epoch in range(num_epochs): train_epoch(model, data) snapshot_manager.update({ "metadata": { "metrics": {"mAP": ...} }, "model": model.state_dict(), "optimizer": optimizer.state_dict() })
Methods:
| Name | Description |
|---|---|
best |
Returns: the path to the best snapshot, if it exists |
last |
Returns: path to the last snapshot that was saved, if any snapshot exists |
snapshot_path |
Args: |
snapshots |
Args: |
update |
Saves the model state dict if the epoch is one that requires a save. |
Source code in deeplabcut/pose_estimation_pytorch/runners/snapshots.py
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 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 | |
best
Returns: the path to the best snapshot, if it exists
Source code in deeplabcut/pose_estimation_pytorch/runners/snapshots.py
last
Returns: path to the last snapshot that was saved, if any snapshot exists
Source code in deeplabcut/pose_estimation_pytorch/runners/snapshots.py
snapshot_path
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
|
int
|
the number of epochs for which a snapshot was trained |
required |
|
bool
|
whether this is the best performing model for the training run |
False
|
Returns:
| Type | Description |
|---|---|
Path
|
the path where the model should be stored |
Source code in deeplabcut/pose_estimation_pytorch/runners/snapshots.py
snapshots
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
|
bool
|
Whether to place the snapshot with the best performance in the last position in the list, even if it wasn't the last epoch. |
True
|
Returns:
| Type | Description |
|---|---|
list[Snapshot]
|
The snapshots for a training run, sorted by the number of epochs they were
trained for. If |
Source code in deeplabcut/pose_estimation_pytorch/runners/snapshots.py
update
Saves the model state dict if the epoch is one that requires a save.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
|
int
|
the number of epochs the model was trained for |
required |
|
dict
|
the state dict to store |
required |
|
bool
|
whether this is the last epoch in the training run, which forces a model save no matter the epoch number |
False
|
Returns:
| Type | Description |
|---|---|
None
|
the path to the saved snapshot if one |