Skip to content

from_pretrained

Load a pretrained DG model from a local path or hub ID.

Resolves path_or_id to a local artifact directory (downloading from the hub if necessary), imports its model.py, instantiates Model with the merged config, and loads weights from model.pt.

Caller-supplied **kwargs override values in config.json. When caller kwargs are present, weights are loaded non-strictly and tensors whose shapes no longer match the instantiated model are silently dropped — this supports architectural tweaks (e.g. changing num_classes) on top of pretrained backbones.

Parameters:

Name Type Description Default
path_or_id str | Path

Path to a local artifact directory, or a hub model ID.

required
device str

Device string passed to torch.load and model.to.

'cpu'
force_download bool

Re-download the artifact even if cached.

False
finetune bool

If True, call dg.train.freeze_backbone on the model before returning it.

False
**kwargs Any

Overrides merged into the saved config.json.

{}

Returns:

Type Description
Module

The instantiated model, moved to device, with

Module

_pretrained_path set to the resolved artifact directory.