Build model with specified default_cfg and optional model_cfg This helper fn aids in the construction of a model including: * handling default_cfg and associated pretrained weight loading * passing through optional model_cfg for models with config based arch spec * features_o
(
model_cls: Union[Type[ModelT], Callable[..., ModelT]],
variant: str,
pretrained: bool,
pretrained_cfg: Optional[Dict] = None,
pretrained_cfg_overlay: Optional[Dict] = None,
model_cfg: Optional[Any] = None,
feature_cfg: Optional[Dict] = None,
pretrained_strict: bool = True,
pretrained_filter_fn: Optional[Callable] = None,
cache_dir: Optional[Union[str, Path]] = None,
kwargs_filter: Optional[Tuple[str]] = None,
**kwargs,
)
| 382 | |
| 383 | |
| 384 | def build_model_with_cfg( |
| 385 | model_cls: Union[Type[ModelT], Callable[..., ModelT]], |
| 386 | variant: str, |
| 387 | pretrained: bool, |
| 388 | pretrained_cfg: Optional[Dict] = None, |
| 389 | pretrained_cfg_overlay: Optional[Dict] = None, |
| 390 | model_cfg: Optional[Any] = None, |
| 391 | feature_cfg: Optional[Dict] = None, |
| 392 | pretrained_strict: bool = True, |
| 393 | pretrained_filter_fn: Optional[Callable] = None, |
| 394 | cache_dir: Optional[Union[str, Path]] = None, |
| 395 | kwargs_filter: Optional[Tuple[str]] = None, |
| 396 | **kwargs, |
| 397 | ) -> ModelT: |
| 398 | """ Build model with specified default_cfg and optional model_cfg |
| 399 | |
| 400 | This helper fn aids in the construction of a model including: |
| 401 | * handling default_cfg and associated pretrained weight loading |
| 402 | * passing through optional model_cfg for models with config based arch spec |
| 403 | * features_only model adaptation |
| 404 | * pruning config / model adaptation |
| 405 | |
| 406 | Args: |
| 407 | model_cls: Model class |
| 408 | variant: Model variant name |
| 409 | pretrained: Load the pretrained weights |
| 410 | pretrained_cfg: Model's pretrained weight/task config |
| 411 | pretrained_cfg_overlay: Entries that will override those in pretrained_cfg |
| 412 | model_cfg: Model's architecture config |
| 413 | feature_cfg: Feature extraction adapter config |
| 414 | pretrained_strict: Load pretrained weights strictly |
| 415 | pretrained_filter_fn: Filter callable for pretrained weights |
| 416 | cache_dir: Override model cache dir for Hugging Face Hub and Torch checkpoints |
| 417 | kwargs_filter: Kwargs keys to filter (remove) before passing to model |
| 418 | **kwargs: Model args passed through to model __init__ |
| 419 | """ |
| 420 | pruned = kwargs.pop('pruned', False) |
| 421 | features = False |
| 422 | feature_cfg = feature_cfg or {} |
| 423 | |
| 424 | # resolve and update model pretrained config and model kwargs |
| 425 | pretrained_cfg = resolve_pretrained_cfg( |
| 426 | variant, |
| 427 | pretrained_cfg=pretrained_cfg, |
| 428 | pretrained_cfg_overlay=pretrained_cfg_overlay |
| 429 | ) |
| 430 | pretrained_cfg = pretrained_cfg.to_dict() |
| 431 | |
| 432 | _update_default_model_kwargs(pretrained_cfg, kwargs, kwargs_filter) |
| 433 | |
| 434 | # Setup for feature extraction wrapper done at end of this fn |
| 435 | if kwargs.pop('features_only', False): |
| 436 | features = True |
| 437 | feature_cfg.setdefault('out_indices', (0, 1, 2, 3, 4)) |
| 438 | if 'out_indices' in kwargs: |
| 439 | feature_cfg['out_indices'] = kwargs.pop('out_indices') |
| 440 | if 'feature_cls' in kwargs: |
| 441 | feature_cfg['feature_cls'] = kwargs.pop('feature_cls') |
no test coverage detected