checkpoint_io
anomaly_match.data_io.checkpoint_io
¶
Checkpoint I/O using safetensors for secure model serialization.
Replaces pickle-based torch.save / torch.load with safetensors to
prevent arbitrary code execution when loading untrusted model files.
Checkpoint layout inside a single .safetensors file:
- Binary section — all
torch.Tensorvalues (model weights, optimizer momentum buffers, …) stored under namespaced keys (train_model.<name>,optimizer.state.<idx>.<buf>, …). - Metadata header — every non-tensor value is JSON-encoded into the
Dict[str, str]metadata that safetensors carries in its header.
save_checkpoint(save_state, path)
¶
Save a model checkpoint in safetensors format.
Tensors are stored in the safetensors binary section; everything else is JSON-encoded into the safetensors metadata header.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
save_state
|
dict[str, Any]
|
Checkpoint dictionary (same keys as previously passed to
|
required |
path
|
str | Path
|
Destination file path. The extension is forced to
|
required |
Returns:
| Type | Description |
|---|---|
Path
|
The actual path written (with |
Source code in anomaly_match/data_io/checkpoint_io.py
279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 | |
load_checkpoint(path, device='cpu')
¶
Load a model checkpoint from a .safetensors file.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
path
|
str | Path
|
Path to the |
required |
device
|
str
|
Device to map tensors to (default |
'cpu'
|
Returns:
| Type | Description |
|---|---|
dict[str, Any]
|
Checkpoint dictionary with the same structure as originally saved. |
Raises:
| Type | Description |
|---|---|
FileNotFoundError
|
If path does not exist. |
Source code in anomaly_match/data_io/checkpoint_io.py
386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 | |
read_model_normalisation(path)
¶
Read a checkpoint's embedded normalisation settings without loading tensors.
The prediction subprocess overrides cfg.fitsbolt_cfg (and net /
num_channels) from the checkpoint in :func:load_model, so the model's
embedded settings — not anything chosen in the UI — are what inference
actually runs. The prediction setup screen reads them to show a read-only
overview and to keep cfg.normalisation consistent with the model.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
path
|
str | Path
|
Path to the |
required |
Returns:
| Type | Description |
|---|---|
dict[str, Any]
|
Dict with |
dict[str, Any]
|
|
dict[str, Any]
|
|
dict[str, Any]
|
fields are |
dict[str, Any]
|
metadata (no |
dict[str, Any]
|
func: |
Source code in anomaly_match/data_io/checkpoint_io.py
read_checkpoint_normalisation(path)
¶
Read a checkpoint's fitsbolt config + channel combination, tensor-free.
Returns the two surfaces prediction needs to reproduce training's input pipeline: the fitsbolt config (resolution + per-channel normalisation) and the AnomalyMatch band-mixing matrix (applied outside fitsbolt, on the GPU). Cheap enough to call from the parent process before launching subprocesses.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
path
|
str | Path
|
Path to the |
required |
Returns:
| Type | Description |
|---|---|
DotMap | None
|
|
Any
|
|
tuple[DotMap | None, Any]
|
channel-combination matrix as an |
tuple[DotMap | None, Any]
|
returns the raw fitsbolt config that :func: |
tuple[DotMap | None, Any]
|
writes onto |
tuple[DotMap | None, Any]
|
func: |
tuple[DotMap | None, Any]
|
|
tuple[DotMap | None, Any]
|
|
tuple[DotMap | None, Any]
|
|
tuple[DotMap | None, Any]
|
|
tuple[DotMap | None, Any]
|
not exist. |
Source code in anomaly_match/data_io/checkpoint_io.py
sync_normalisation_from_checkpoint(cfg, fitsbolt_cfg, channel_combination)
¶
Make a model checkpoint the single source of truth for normalisation on cfg.
Prediction must feed the model images produced exactly as in training. Three config surfaces drive that, and all must agree with the model:
cfg.fitsbolt_cfg— the inference decode pipeline (image/Zarr paths) and the Cutana orchestrator'sexternal_fitsbolt_cfgnormalisation.cfg.normalisationimage_size/n_output_channels— the Cutana orchestratortarget_resolutionand output channel count.cfg.normalisation.channel_combination— the band-mixing matrix applied on the GPU incutana_batch_to_model_tensor_gpu(outside fitsbolt), which reduces e.g. 4-band Euclid input to the model's 3 channels.
A stale cfg (left over from training, or the pickled UI default) would
otherwise produce cutouts at the wrong resolution or with the wrong channel
mixing — a silent, severe accuracy loss, or a hard channel-count crash. This
overwrites all three from the checkpoint.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
cfg
|
Configuration to update in place. |
required | |
fitsbolt_cfg
|
DotMap | None
|
The model's embedded fitsbolt config (from
:func: |
required |
channel_combination
|
The model's band-mixing matrix (from the checkpoint's
|
required |
Returns:
| Type | Description |
|---|---|
bool
|
|
bool
|
(the caller must then decide how to handle a metadata-less checkpoint). |
Raises:
| Type | Description |
|---|---|
ValueError
|
If channel_combination is a matrix whose row count does not
equal the model's |