MCPcopy Create free account
hub / github.com/VCIP-RGBD/DFormer / init_weights

Method init_weights

mmseg/models/backbones/scnet.py:293–327  ·  view source on GitHub ↗
(self)

Source from the content-addressed store, hash-verified

291 # self.apply(self.init_weights)
292
293 def init_weights(self):
294 logger = get_root_logger()
295 if self.init_cfg is None:
296 logger.warn(f"No pre-trained weights for {self.__class__.__name__}, training start from scratch")
297
298 for m in self.modules():
299 if isinstance(m, nn.Linear):
300 trunc_normal_init(m, std=0.02, bias=0.0)
301 elif isinstance(m, nn.LayerNorm):
302 constant_init(m, val=1.0, bias=0.0)
303 else:
304 assert "checkpoint" in self.init_cfg, (
305 f"Only support specify `Pretrained` in `init_cfg` in {self.__class__.__name__} "
306 )
307 ckpt = CheckpointLoader.load_checkpoint(self.init_cfg["checkpoint"], logger=logger, map_location="cpu")
308 if "state_dict" in ckpt:
309 _state_dict = ckpt["state_dict"]
310 elif "model" in ckpt:
311 _state_dict = ckpt["model"]
312 else:
313 _state_dict = ckpt
314
315 state_dict = OrderedDict()
316 for k, v in _state_dict.items():
317 if k.startswith("backbone."):
318 state_dict[k[9:]] = v
319 else:
320 state_dict[k] = v
321
322 # strip prefix of state_dict
323 if list(state_dict.keys())[0].startswith("module."):
324 state_dict = {k[7:]: v for k, v in state_dict.items()}
325
326 # load state_dict
327 load_state_dict(self, state_dict, strict=False, logger=logger)
328
329 def forward(self, x):
330 outs = []

Callers

nothing calls this directly

Calls 2

get_root_loggerFunction · 0.85
load_state_dictFunction · 0.85

Tested by

no test coverage detected