Args: cfg: The configuration of the model to build. weight_init: How model weights should be initialized. If None, ImageNet pre-trained backbone weights are loaded from Timm. pretrained_backbone: Whether to load an ImageNet-pretrained weig
(
cfg: dict,
weight_init: None | WeightInitialization = None,
pretrained_backbone: bool = False,
)
| 149 | |
| 150 | @staticmethod |
| 151 | def build( |
| 152 | cfg: dict, |
| 153 | weight_init: None | WeightInitialization = None, |
| 154 | pretrained_backbone: bool = False, |
| 155 | ) -> PoseModel: |
| 156 | """ |
| 157 | Args: |
| 158 | cfg: The configuration of the model to build. |
| 159 | weight_init: How model weights should be initialized. If None, ImageNet |
| 160 | pre-trained backbone weights are loaded from Timm. |
| 161 | pretrained_backbone: Whether to load an ImageNet-pretrained weights for |
| 162 | the backbone. This should only be set to True when building a model |
| 163 | which will be trained on a transfer learning task. |
| 164 | |
| 165 | Returns: |
| 166 | the built pose model |
| 167 | """ |
| 168 | cfg["backbone"]["pretrained"] = pretrained_backbone |
| 169 | backbone = BACKBONES.build(dict(cfg["backbone"])) |
| 170 | |
| 171 | neck = None |
| 172 | if cfg.get("neck"): |
| 173 | neck = NECKS.build(dict(cfg["neck"])) |
| 174 | |
| 175 | heads = {} |
| 176 | for name, head_cfg in cfg["heads"].items(): |
| 177 | head_cfg = copy.deepcopy(head_cfg) |
| 178 | if "type" in head_cfg["criterion"]: |
| 179 | head_cfg["criterion"] = CRITERIONS.build(head_cfg["criterion"]) |
| 180 | else: |
| 181 | weights = {} |
| 182 | criterions = {} |
| 183 | for loss_name, criterion_cfg in head_cfg["criterion"].items(): |
| 184 | weights[loss_name] = criterion_cfg.get("weight", 1.0) |
| 185 | criterion_cfg = {k: v for k, v in criterion_cfg.items() if k != "weight"} |
| 186 | criterions[loss_name] = CRITERIONS.build(criterion_cfg) |
| 187 | |
| 188 | aggregator_cfg = {"type": "WeightedLossAggregator", "weights": weights} |
| 189 | head_cfg["aggregator"] = LOSS_AGGREGATORS.build(aggregator_cfg) |
| 190 | head_cfg["criterion"] = criterions |
| 191 | |
| 192 | head_cfg["target_generator"] = TARGET_GENERATORS.build(head_cfg["target_generator"]) |
| 193 | head_cfg["predictor"] = PREDICTORS.build(head_cfg["predictor"]) |
| 194 | heads[name] = HEADS.build(head_cfg) |
| 195 | |
| 196 | model = PoseModel(cfg=cfg, backbone=backbone, neck=neck, heads=heads) |
| 197 | |
| 198 | if weight_init is not None: |
| 199 | logging.info(f"Loading pretrained model weights: {weight_init}") |
| 200 | logging.info(f"The pose model is loading from {weight_init.snapshot_path}") |
| 201 | snapshot = torch.load(weight_init.snapshot_path, map_location="cpu") |
| 202 | state_dict = snapshot["model"] |
| 203 | |
| 204 | # load backbone state dict |
| 205 | model.backbone.load_state_dict(filter_state_dict(state_dict, "backbone")) |
| 206 | |
| 207 | # if there's a neck, load state dict |
| 208 | if model.neck is not None: |
no test coverage detected