trainer¶
trainer
¶
Image-scale training utilities: streaming datasets, coupling strategies, and a mixed-precision training loop with checkpoint/resume.
BaseCoupling
¶
Bases: ABC
Given a batch x1 of data samples, return a paired (x0, x1).
IndependentCoupling
¶
OTCoupling
¶
Bases: BaseCoupling
Mini-batch optimal-transport coupling on squared-L2 cost.
Draws x0 ~ N(0, I) and then permutes it within the batch so that
each (x0, x1) pair minimises the batch's total transport cost. Uses
scipy.optimize.linear_sum_assignment if available, otherwise falls
back to a deterministic greedy nearest-neighbour matching.
ImageFolderStream
¶
Bases: Dataset
Streaming dataset over a directory of images.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
root
|
Union[str, Path]
|
directory containing image files (searched recursively). |
required |
image_size
|
Optional[int]
|
if given, images are resized to |
None
|
mode
|
str
|
PIL mode to convert to ( |
'RGB'
|
transform
|
Optional[Callable]
|
optional PIL-image transform applied before tensor
conversion. If it returns a |
None
|
extensions
|
Sequence[str]
|
file extensions to include. |
_IMAGE_EXTENSIONS
|
Source code in deltaflow/trainer/data.py
TrainConfig
dataclass
¶
TrainConfig(max_steps=1000, grad_accum_steps=1, mixed_precision=True, amp_dtype=None, grad_clip=1.0, log_every=50, checkpoint_every=500, checkpoint_dir='checkpoints', ema_beta=0.999, device=None, resume_from=None, log_fn=print)
Configuration for train.
Attributes:
| Name | Type | Description |
|---|---|---|
max_steps |
int
|
total optimizer steps to run (after |
grad_accum_steps |
int
|
accumulate gradients over this many minibatches before each optimizer step. |
mixed_precision |
bool
|
if |
amp_dtype |
Optional[dtype]
|
override the autocast dtype. If |
grad_clip |
Optional[float]
|
max L2 norm for gradient clipping, |
log_every |
int
|
print a training-metric line every N optimizer steps. |
checkpoint_every |
int
|
write a checkpoint every N optimizer steps. |
checkpoint_dir |
Union[str, Path]
|
directory to write checkpoints into. Created if missing. |
ema_beta |
Optional[float]
|
if not |
device |
Optional[str]
|
torch device string, defaults to CUDA if available. |
resume_from |
Optional[Union[str, Path]]
|
optional path to a checkpoint saved by this loop. |
build_loader
¶
Convenience wrapper around torch.utils.data.DataLoader with
sensible defaults for image-scale flow-matching training.
Source code in deltaflow/trainer/data.py
load_checkpoint
¶
Load a checkpoint written by save_checkpoint. Returns the step.
Source code in deltaflow/trainer/loop.py
save_checkpoint
¶
Write a checkpoint dictionary to path (creates parent dir).
Source code in deltaflow/trainer/loop.py
train
¶
Train model in-place under loss_fn for config.max_steps steps.
Returns the (possibly EMA-shadowed) model that was trained. The original
model is always updated in place. If EMA is enabled, an EMA copy is
also kept and written into checkpoints, and can be retrieved from the
latest checkpoint's "ema_model" key.
Source code in deltaflow/trainer/loop.py
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 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 | |