π How-to Guide Β· task-oriented
Training Models and Generating Checkpoints¶
Overview¶
DynVision trains models through a two-rule Snakemake pipeline: a model is first initialized into a state dict, then trained into a final state dict while PyTorch Lightning checkpoints are written periodically. This guide explains how to launch a training run, where the resulting files are written, and how to recover usable weights from the Lightning checkpoints.
The training entry point is dynvision/runtime/train_model.py, orchestrated by
the train_model rule in dynvision/workflow/snake_runtime.smk.
Prerequisites¶
- An interim training dataset prepared under
project_paths.data.interim/{data_name}/train_all(with the matchingtrain_all.readymarker). See Data Processing. - If
use_ffcvis enabled, the FFCVtrain.betonandval.betonfiles underproject_paths.data.processed/{data_name}/train_all/. - A configured Snakemake environment, run from the
dynvision/workflow/directory as described in the project README. - Training defaults defined in
dynvision/configs/config_defaults.yaml(optimizer, scheduler, epochs, batch size, loss). See the Configuration Reference.
Workflow Summary¶
| Stage | Snakemake rule | Key script | Output |
|---|---|---|---|
| Initialization | init_model |
runtime/init_model.py |
β¦/{data_name}/init.pt |
| Training | train_model |
runtime/train_model.py |
β¦/{data_name}/trained.pt |
| Checkpoint export | best_checkpoint_to_statedict |
utils/checkpoint_to_statedict.py |
β¦/{data_name}/trained-best.pt |
Models are stored hierarchically:
models/
βββ {model_name}/
βββ {model_name}{model_args}_{seed}/
βββ {data_name}/
βββ init.pt # initialized state dict
βββ trained.pt # final trained state dict
βββ trained.config.yaml # resolved configuration export
βββ trained-best-{epoch}-{metric}.ckpt # top-k Lightning checkpoints
βββ trained-last-{epoch}-{metric}.ckpt # final Lightning checkpoint
Step 1: Initialize the Model¶
The init_model rule creates a model from the configuration and saves its
initial state dict. It infers input shape from the training dataset.
You normally do not need to run this rule explicitly β Snakemake builds it
automatically as a dependency of train_model. Run it on its own when you want
to inspect or reuse the initialized weights.
Step 2: Train the Model¶
The train_model rule consumes init.pt and produces trained.pt. Because the
output path embeds {model_name}, {model_args}, {seed}, and {data_name},
you select a configuration by setting those values in --config:
snakemake train_model \
--config model_name=DyRCNNx4 data_name=cifar100 seed=0 \
model_args="{rctype:full}"
During training the rule:
- Loads the initialized weights from
models/{model_name}/{model_name}{model_args}_{seed}/{data_name}/init.pt. - Trains with the optimizer, scheduler, loss, and epoch count defined in
config_defaults.yaml(overridable through--config). - Writes periodic and best Lightning checkpoints (
.ckpt) into the same{data_name}directory using themonitor_checkpointmetric. - Saves the final state dict as
trained.ptand exports the resolved configuration alongside it astrained.config.yaml.
If a Lightning checkpoint already exists for the run, training resumes from it
(restoring model, optimizer, and epoch). If no checkpoint is found, training
starts from init.pt.
Requesting a Trained Model by Path¶
Because Snakemake resolves rules by output file, you can request a trained model directly by its target path instead of by rule name:
Snakemake runs init_model first if init.pt is missing, then train_model.
Step 3: Override Training Parameters¶
Any default in config_defaults.yaml can be overridden on the command line.
Model-specific arguments go through model_args; run-level training arguments
are passed directly:
snakemake train_model \
--config model_name=DyRCNNx4 data_name=cifar100 seed=0 \
model_args="{rctype:full+tsteps:20}" \
epochs=200 \
batch_size=192 \
learning_rate=0.0008 \
use_ffcv=True
Learning-rate parameter groups apply different factors to recurrent and feedback
weights (lr_parameter_groups in config_defaults.yaml), so the recurrent and
feedback connections train with a smaller effective learning rate than the
feedforward weights.
Step 4: Generate a Clean State Dict from Checkpoints¶
The final trained.pt is written when training completes. To extract the
best-performing weights from the Lightning .ckpt files β for example after an
interrupted run, or to select the top checkpoint by validation metric β use the
best_checkpoint_to_statedict rule:
snakemake best_checkpoint_to_statedict \
--config model_name=DyRCNNx4 data_name=cifar100 seed=0 \
model_args="{rctype:full}"
This produces trained-best.pt in the run directory, a plain state dict ready
for evaluation with the test_model workflow.
Step 5: Parameter Sweeps and Multiple Seeds¶
Snakemake expands list-valued arguments into separate runs, so you can train a whole comparison set with a single command:
snakemake train_model \
--config model_name=DyRCNNx4 data_name=cifar100 \
seed="[0, 1, 2]" \
model_args="{rctype:[full, self, depthpointwise]}"
Each combination is written to its own hierarchical output directory and is
skipped if its trained.pt already exists. See
Workflow Management for more on parameter sweeps.
Common Issues and Solutions¶
- Missing input dataset: If Snakemake reports a missing
train_all.readymarker, prepare the dataset first (see Data Processing). - FFCV files not found: When
use_ffcv=True, thetrain.betonandval.betonfiles must exist; generate them or setuse_ffcv=Falseto fall back to the standard PyTorch data pipeline. - GPU out-of-memory: Lower
batch_size, increaseaccumulate_grad_batchesto keep the effective batch size, or enable a lower-precision setting viaprecision. - Resuming unintentionally: Training resumes automatically when a
.ckptexists in the run directory. Remove or rename the checkpoints to force a fresh start frominit.pt.
Related Resources¶
- Model Testing β evaluate the
trained.ptyou produce here. - Workflow Management β Snakemake rules, wildcards, and sweeps.
- Custom Models β define new architectures to train.
- Configuration Reference β full list of training parameters and their defaults.