290def build_resnet_backbone(cfg):
291 """
292 Create a ResNet instance from config.
293 Returns:
294 ResNet: a :class:`ResNet` instance.
295 """
296
297
298 pretrain = cfg.MODEL.BACKBONE.PRETRAIN
299 pretrain_path = cfg.MODEL.BACKBONE.PRETRAIN_PATH
300 last_stride = cfg.MODEL.BACKBONE.LAST_STRIDE
301 bn_norm = cfg.MODEL.BACKBONE.NORM
302 with_ibn = cfg.MODEL.BACKBONE.WITH_IBN
303 with_se = cfg.MODEL.BACKBONE.WITH_SE
304 with_nl = cfg.MODEL.BACKBONE.WITH_NL
305 depth = cfg.MODEL.BACKBONE.DEPTH
306
307
308 num_blocks_per_stage = {
309 '18x': [2, 2, 2, 2],
310 '34x': [3, 4, 6, 3],
311 '50x': [3, 4, 6, 3],
312 '101x': [3, 4, 23, 3],
313 '152x': [3, 8, 36, 3],
314 }[depth]
315
316 nl_layers_per_stage = {
317 '18x': [0, 0, 0, 0],
318 '34x': [0, 0, 0, 0],
319 '50x': [0, 2, 3, 0],
320 '101x': [0, 2, 9, 0],
321 '152x': [0, 4, 12, 0]
322 }[depth]
323
324 block = {
325 '18x': BasicBlock,
326 '34x': BasicBlock,
327 '50x': Bottleneck,
328 '101x': Bottleneck,
329 '152x': Bottleneck,
330 }[depth]
331
332 model = ResNet(last_stride, bn_norm, with_ibn, with_se, with_nl, block,
333 num_blocks_per_stage, nl_layers_per_stage)
334 if pretrain:
335
336 if pretrain_path:
337 try:
338 state_dict = torch.load(pretrain_path, map_location=torch.device('cpu'))
339 logger.info(f"Loading pretrained model from {pretrain_path}")
340 except FileNotFoundError as e:
341 logger.info(f'{pretrain_path} is not found! Please check this path.')
342 raise e
343 except KeyError as e:
344 logger.info("State dict keys error! Please check the state dict.")
345 raise e
346 else:
347 key = depth
348 if with_ibn: key = 'ibn_' + key
349 if with_se: key = 'se_' + key
350
351 state_dict = init_pretrained_weights(key)
352
353 incompatible = model.load_state_dict(state_dict, strict=False)
354 if incompatible.missing_keys:
355 logger.info(
356 get_missing_parameters_message(incompatible.missing_keys)
357 )
358 if incompatible.unexpected_keys:
359 logger.info(
360 get_unexpected_parameters_message(incompatible.unexpected_keys)
361 )
362
363 return model