359def build_resnest_backbone(cfg):
360 """
361 Create a ResNest instance from config.
362 Returns:
363 ResNet: a :class:`ResNet` instance.
364 """
365
366
367 pretrain = cfg.MODEL.BACKBONE.PRETRAIN
368 pretrain_path = cfg.MODEL.BACKBONE.PRETRAIN_PATH
369 last_stride = cfg.MODEL.BACKBONE.LAST_STRIDE
370 bn_norm = cfg.MODEL.BACKBONE.NORM
371 with_ibn = cfg.MODEL.BACKBONE.WITH_IBN
372 with_se = cfg.MODEL.BACKBONE.WITH_SE
373 with_nl = cfg.MODEL.BACKBONE.WITH_NL
374 depth = cfg.MODEL.BACKBONE.DEPTH
375
376
377 num_blocks_per_stage = {
378 "50x": [3, 4, 6, 3],
379 "101x": [3, 4, 23, 3],
380 "200x": [3, 24, 36, 3],
381 "269x": [3, 30, 48, 8],
382 }[depth]
383
384 nl_layers_per_stage = {
385 "50x": [0, 2, 3, 0],
386 "101x": [0, 2, 3, 0],
387 "200x": [0, 2, 3, 0],
388 "269x": [0, 2, 3, 0],
389 }[depth]
390
391 stem_width = {
392 "50x": 32,
393 "101x": 64,
394 "200x": 64,
395 "269x": 64,
396 }[depth]
397
398 model = ResNest(last_stride, bn_norm, with_ibn, with_nl, Bottleneck, num_blocks_per_stage,
399 nl_layers_per_stage, radix=2, groups=1, bottleneck_width=64,
400 deep_stem=True, stem_width=stem_width, avg_down=True,
401 avd=True, avd_first=False)
402 if pretrain:
403
404 if pretrain_path:
405 try:
406 state_dict = torch.load(pretrain_path, map_location=torch.device('cpu'))
407 logger.info(f"Loading pretrained model from {pretrain_path}")
408 except FileNotFoundError as e:
409 logger.info(f'{pretrain_path} is not found! Please check this path.')
410 raise e
411 except KeyError as e:
412 logger.info("State dict keys error! Please check the state dict.")
413 raise e
414 else:
415 state_dict = torch.hub.load_state_dict_from_url(
416 model_urls['resnest' + depth[:-1]], progress=True, check_hash=True, map_location=torch.device('cpu'))
417
418 incompatible = model.load_state_dict(state_dict, strict=False)
419 if incompatible.missing_keys:
420 logger.info(
421 get_missing_parameters_message(incompatible.missing_keys)
422 )
423 if incompatible.unexpected_keys:
424 logger.info(
425 get_unexpected_parameters_message(incompatible.unexpected_keys)
426 )
427 return model