Base models API
DeepVisionModel(*, backend, pre, post, loss_fn, device=None, pred_params=None, use_amp=None, caps=None, autocast_dtype=torch.float16, builder=None)
¶
Bases: torch.nn.Module
High-level model façade used by deepvisiontools. All available wrappers are DeepVisionModels. This façade exposes public methods : train_step, eval_step, predict to run model in/without targets presence. This façade allows to save checkpoints with all needed parametrization.
Source code in src/deepvisiontools/models/base/basemodel.py
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 | |
train_step(images, targets=None, *, extra_args=None)
¶
Generic train step: - targets are OPTIONAL - extra_args are forwarded to backend; if targets exist, we inject the preprocessed targets into extra_args["targets"] so facades that require targets during forward (Cascade Mask/RCNN) can use them.
Source code in src/deepvisiontools/models/base/basemodel.py
246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 | |
save(path, *, builder=None, model_kwargs=None, mode='deepvisiontools')
¶
Save a portable model artifact for inference.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
path
|
str
|
path to saved model. Extension must be .pt or .pth unless |
required |
builder
|
Optional[str]
|
if specific builder reconstruction is necessary. If None derives it automatically. Defaults to None. |
None
|
model_kwargs
|
Optional[Dict[str, typing.Any]]
|
if specific builder kwargs are requested. If None derives it automatically. Defaults to None. |
None
|
mode
|
Literal["deepvisiontools", "safetensors"]
|
if need to save backend state_dict only as safetensors (hugging face API compatible). Defaults to "deepvisiontools". |
'deepvisiontools'
|
Note
- we recommend to use everything by default, unless you have something very specific.
- saftensors mode is convenient for hugging face deposit. A full json builder config is also provided to rebuild model.
Source code in src/deepvisiontools/models/base/basemodel.py
458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 | |
ModelFactory
¶
Factory object used to build or restore a DeepVisionModel.
ModelFactory(...) is intentionally implemented through __new__: calling the
class returns a ready-to-use model wrapper directly instead of returning a
persistent factory instance.
Call parameters
ModelFactory(...) accepts the following call-time parameters:
name: Registered model wrapper name, such as"smp","yolo"or"yoloseg".cfg: Configuration dictionary with keys"name"and"kwargs".checkpoint: Path to a portable checkpoint saved with the deepvisiontools checkpoint helpers.weights: Tuple(builder_name, weights_path)used to build a model and load a state dictionary.**build_kwargs: Extra keyword arguments forwarded either to the selected builder or to the loading helpers, depending on the selected construction path.
For specific builder kwargs check documentation for the various model wrappers (build_yolo, build_smp, etc.).
Precedence
Calling ModelFactory(...) follows this precedence:
- if
checkpointis provided, restore the model from the saved builder/kwargs and checkpoint state; - elif
weightsis provided, build throughMODEL_REGISTRYand load the provided state dictionary; - elif
cfgis provided, build withcfg["name"]andcfg["kwargs"]; - else,
namemust be provided and remaining keyword arguments are forwarded to the selected builder.
All builders return a DeepVisionModel façade exposing train_step,
eval_step, predict, preprocessing, postprocessing and loss handling.
Usage
from deepvisiontools.models import ModelFactory
model = ModelFactory(
"smp",
arch="Unet",
encoder_name="resnet18",
encoder_weights="imagenet",
in_channels=3,
num_classes=1,
)
cfg = {
"name": "smp",
"kwargs": {
"arch": "Unet",
"encoder_name": "resnet18",
"encoder_weights": "imagenet",
"in_channels": 3,
"num_classes": 3,
},
}
model = ModelFactory.from_config(cfg)
model = ModelFactory.from_checkpoint(
"/path/to/checkpoint.pt",
map_location="cuda",
strict=False,
)
model = ModelFactory.from_weights(
("smp", "/path/to/weights.pth"),
device="cuda",
use_amp=True,
strict=False,
map_location="cpu",
arch="Unet",
encoder_name="resnet18",
encoder_weights=None,
in_channels=3,
num_classes=1,
)
from_config(cfg)
classmethod
¶
Generate model from given config.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
cfg
|
Dict[str, typing.Any]
|
config to be used. |
required |
Returns:
| Type | Description |
|---|---|
deepvisiontools.models.base.basemodel.DeepVisionModel
|
DeepVisionModel |
Source code in src/deepvisiontools/models/base/basemodel.py
836 837 838 839 840 841 842 843 844 845 846 | |
from_checkpoint(path, *, map_location='cpu', strict=True)
classmethod
¶
Rebuild model from given checkpoint.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
path
|
str
|
Path to checkpoint. |
required |
map_location
|
str, **optional**
|
device of model. Defaults to "cpu". |
'cpu'
|
strict
|
bool, **optional**
|
requires strict state_dict matching. Defaults to True. |
True
|
Returns:
| Type | Description |
|---|---|
deepvisiontools.models.base.basemodel.DeepVisionModel
|
DeepVisionModel |
Source code in src/deepvisiontools/models/base/basemodel.py
848 849 850 851 852 853 854 855 856 857 858 859 860 861 862 863 864 865 | |
from_weights(builder_and_path, *, device=None, use_amp=None, strict=True, map_location=None, **build_kwargs)
classmethod
¶
Instantiate a model using the registry, then load a raw state_dict.
Supported raw weight formats: - torch files (.pt / .pth / etc.): interpreted as full-wrapper state_dict - safetensors (.safetensors): interpreted as backend-only state_dict
Notes:
- This method is for loading raw weights when the caller already knows how the
target model must be built (via builder + build_kwargs).
- For portable DeepVision artifacts with sidecar metadata, prefer
from_checkpoint(...), which can reconstruct the model from saved metadata.
Source code in src/deepvisiontools/models/base/basemodel.py
867 868 869 870 871 872 873 874 875 876 877 878 879 880 881 882 883 884 885 886 887 888 889 890 891 892 893 894 895 896 897 898 899 900 901 902 903 904 905 906 907 908 909 910 911 912 913 914 915 916 917 918 919 920 921 922 923 924 925 926 927 928 929 930 931 932 933 934 935 936 937 938 939 940 941 942 | |
available_configs()
staticmethod
¶
Return available config presets for each registered model wrapper.
The returned object is pretty-printable and can also be converted to a
nested dict via .to_dict().
Output shape (dict form): { "bbox": {"yolo": [{"yolo11n": "..."}, ...], ...}, "instance_mask": {...}, "semantic_mask": {...}, }
Notes
- Builders may optionally expose
__dvt_available_configs__: Dict[str(task), Dict[str(config_name), str(description)]] - This method does NOT change how models are built; it only introspects registry metadata.
Source code in src/deepvisiontools/models/base/basemodel.py
944 945 946 947 948 949 950 951 952 953 954 955 956 957 958 959 960 961 962 963 964 965 966 967 968 969 970 971 972 973 974 975 976 977 978 979 980 981 982 983 984 985 986 987 988 989 990 991 992 993 994 995 996 997 | |
load(path, *, map_location='cpu', strict=True)
classmethod
¶
Load a portable model artifact saved by DeepVisionModel.save(). User decides "raw vs ema" by choosing the file (e.g. model_best.pt vs model_best_ema.pt).
Source code in src/deepvisiontools/models/base/basemodel.py
999 1000 1001 1002 1003 1004 1005 1006 1007 | |
load_checkpoint(path, builder, map_location='cpu', strict=True)
¶
Load a model from a checkpoint saved by save_checkpoint, rebuild the architecture
with the recorded builder name + kwargs, load weights (strict or lenient), and restore
the model's extra state via set_extra_state when available.
The loader tolerates both the current flat format and an older nested meta format.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
path
|
str
|
Path to the checkpoint produced by |
required |
builder
|
str | collections.abc.Callable
|
Either:
- a callable of the form |
required |
map_location
|
str | torch.device
|
|
'cpu'
|
strict
|
bool
|
If |
True
|
Returns:
| Type | Description |
|---|---|
tuple[torch.nn.Module, dict]
|
Tuple[torch.nn.Module, dict]: |
Usage
# Using a registry builder callable
model, meta = load_checkpoint(
"/tmp/ckpt.pt",
MODEL_REGISTRY.build,
map_location="cuda",
strict=False, # allow minor mismatches
)
print(meta["builder"], meta["model_kwargs"]["arch"]) # doctest: +ELLIPSIS
# After load, model extras (e.g., prediction params) are restored automatically
_ = model.eval()
Source code in src/deepvisiontools/models/base/checkpoints.py
458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 | |
load_training_state(path, registry_build, map_location='cpu', strict=True)
¶
Load a training checkpoint saved by save_training_state / save_checkpoint and
rebuild the model.
This function is a thin wrapper over load_checkpoint that returns a dict with
the rebuilt model and a compact checkpoint meta.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
path
|
str
|
Path to the saved training checkpoint. |
required |
registry_build
|
collections.abc.Callable
|
Callable of the form |
required |
map_location
|
str | torch.device
|
Map location for |
'cpu'
|
strict
|
bool
|
Whether to enforce strict key matching for
|
True
|
Returns:
| Type | Description |
|---|---|
dict[str, typing.Any]
|
Dict[str, Any]: |
dict[str, typing.Any]
|
at least |
Usage
out = load_training_state("/tmp/train_ckpt.pt", MODEL_REGISTRY.build, map_location="cuda")
model = out["model"]
meta = out["ckpt"]
print(meta["builder"], meta["model_kwargs"]["arch"]) # doctest: +ELLIPSIS
Source code in src/deepvisiontools/models/base/checkpoints.py
619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 | |
load_whole_object(path, map_location='cpu')
¶
Load a whole-object pickle saved by save_whole_object.
⚠️ Prefer state-dict based checkpoints for robustness. This is intended for quick experiments where exact Python object restoration is acceptable.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
path
|
str
|
Source file path pointing to a package produced by |
required |
map_location
|
str | torch.device
|
Device mapping for |
'cpu'
|
Returns:
| Type | Description |
|---|---|
torch.nn.Module
|
Tuple[torch.nn.Module, Dict[str, Any]]: |
dict[str, typing.Any]
|
dictionary retrieved from disk (contains keys like |
Usage
obj, pkg = load_whole_object("/tmp/model_obj.pt", map_location="cpu")
isinstance(obj, torch.nn.Module)
>>> True
pkg.get("version") # doctest: +ELLIPSIS
Source code in src/deepvisiontools/models/base/checkpoints.py
691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 | |
save_checkpoint(path, model, *, builder, model_kwargs, epoch=None, optimizer=None, scaler=None, ema_state_dict=None, extra=None, version=_DEFAULT_VERSION)
¶
Save a training checkpoint suitable for later restoration with load_checkpoint.
Note
The checkpoint contains:
- version string,
- the builder name and its model_kwargs (to rebuild the model),
- the model state_dict,
- optional model_extra from model.get_extra_state() (e.g., prediction params),
- training-time state: epoch, optimizer, AMP scaler, EMA weights,
- arbitrary extra metadata,
- RNG states (CPU and CUDA).
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
path
|
str
|
Destination file path (e.g., |
required |
model
|
torch.nn.Module
|
Model whose weights and extra state will be saved. |
required |
builder
|
str
|
Registry key or logical name used to rebuild the model later
(e.g., |
required |
model_kwargs
|
Dict[str, typing.Any]
|
Exact kwargs that, together with |
required |
epoch
|
Optional[int]
|
Current epoch index to store. Defaults to |
None
|
optimizer
|
Optional[torch.optim.Optimizer]
|
Optimizer to serialize via
|
None
|
scaler
|
Optional[torch.cuda.amp.GradScaler]
|
AMP scaler; saved via
|
None
|
ema_state_dict
|
Optional[Dict[str, typing.Any]]
|
Exponential Moving Average
weights (already a |
None
|
extra
|
Optional[Dict[str, typing.Any]]
|
Arbitrary metadata to persist
(e.g., run config, metrics). Defaults to |
None
|
version
|
str
|
Semantic or internal version tag for compatibility checks.
Defaults to |
deepvisiontools.models.base.checkpoints._DEFAULT_VERSION
|
Returns:
| Type | Description |
|---|---|
None
|
None |
Usage
save_checkpoint(
"/tmp/ckpt.pt",
model,
builder="smp",
model_kwargs=dict(
arch="Unet",
encoder_name="resnet18",
in_channels=3,
num_classes=1,
),
epoch=12,
optimizer=optim,
scaler=scaler, # optional
ema_state_dict=ema.state_dict() if ema else None,
extra={"val_iou": 0.71},
)
Source code in src/deepvisiontools/models/base/checkpoints.py
245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 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 | |
save_training_state(path, model, optimizer, *, builder, model_kwargs, scheduler=None, scaler=None, ema=None, epoch=None, **meta)
¶
Save a runnable training state that extends save_checkpoint with scheduler
and EMA information. Intended for training restarts.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
path
|
str
|
Destination file path. |
required |
model
|
torch.nn.Module
|
Model to save. |
required |
optimizer
|
torch.optim.Optimizer
|
Optimizer to serialize ( |
required |
builder
|
str
|
Registry name used to rebuild the model (e.g., |
required |
model_kwargs
|
Dict[str, typing.Any]
|
Keyword arguments to rebuild the model. |
required |
scheduler
|
Optional[typing.Any]
|
LR scheduler; saved via |
None
|
scaler
|
Optional[torch.cuda.amp.GradScaler]
|
AMP scaler. Defaults to |
None
|
ema
|
Optional[torch.nn.Module]
|
EMA-wrapped model; its |
None
|
epoch
|
Optional[int]
|
Epoch index to persist. Defaults to |
None
|
**meta
|
typing.Any
|
Arbitrary metadata merged into the |
{}
|
Returns:
| Type | Description |
|---|---|
None
|
None |
Usage
save_training_state(
"/tmp/train_ckpt.pt",
model,
optimizer,
builder="smp",
model_kwargs={"arch": "Unet", "encoder_name": "resnet18", "num_classes": 1},
scheduler=scheduler,
scaler=scaler,
ema=ema_model,
epoch=5,
best_val_iou=0.73,
)
Source code in src/deepvisiontools/models/base/checkpoints.py
554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 | |
save_whole_object(path, model, *, version=_DEFAULT_VERSION, note='')
¶
Save the entire Python object (torch.nn.Module) via pickling.
⚠️ Not recommended for long-term portability—prefer save_checkpoint (weights +
explicit rebuild info). Whole-object saves may break across library versions.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
path
|
str
|
Destination file path. |
required |
model
|
torch.nn.Module
|
The model object to pickle. |
required |
version
|
str
|
Version tag written into the package. Defaults to
|
deepvisiontools.models.base.checkpoints._DEFAULT_VERSION
|
note
|
str
|
Free-form note stored alongside the object. Defaults to |
''
|
Returns:
| Type | Description |
|---|---|
None
|
None |
Usage
save_whole_object("/tmp/model_obj.pt", model, note="dev snapshot")
Source code in src/deepvisiontools/models/base/checkpoints.py
662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 | |