code: def resnet50_fpn_backbone(
      pretrain_path="",
      norm_layer=FrozenBatchNorm2d,  # FrozenBatchNorm2d的功能与BatchNorm2d类似，但参数无法更新
      trainable_layers=3,
      returned_layers=None,
      extra_blocks=None,
  ):
      """
      搭建resnet50_fpn——backbone
      Args:
          pretrain_path: resnet50的预训练权重，如果不使用就默认为空
          norm_layer: 官方默认的是FrozenBatchNorm2d，即不会更新参数的bn层(因为如果batch_size设置的很小会导致效果更差，还不如不用bn层)
                      如果自己的GPU显存很大可以设置很大的batch_size，那么自己可以传入正常的BatchNorm2d层
                      (https://github.com/facebookresearch/maskrcnn-benchmark/issues/267)
          trainable_layers: 指定训练哪些层结构
          returned_layers: 指定哪些层的输出需要返回
          extra_blocks: 在输出的特征层基础上额外添加的层结构
  
      Returns:
  
      """
      resnet_backbone = ResNet(
          Bottleneck,
          [3, 4, 6, 3],
          include_top=False,
          norm_layer=norm_layer,
      )
  
      if isinstance(norm_layer, FrozenBatchNorm2d):
          overwrite_eps(resnet_backbone, 0.0)
  
      if pretrain_path != "":
          assert os.path.exists(
              pretrain_path
          ), "{} is not exist.".format(pretrain_path)
          # 载入预训练权重
          print(
              resnet_backbone.load_state_dict(
                  torch.load(pretrain_path), strict=False
              )
          )
  
      # select layers that wont be frozen
      assert 0 <= trainable_layers <= 5
      layers_to_train = [
          "layer4",
          "layer3",
          "layer2",
          "layer1",
          "conv1",
      ][:trainable_layers]
  
      # 如果要训练所有层结构的话，不要忘了conv1后还有一个bn1
      if trainable_layers == 5:
          layers_to_train.append("bn1")
  
      # freeze layers
      for name, parameter in resnet_backbone.named_parameters():
          # 只训练不在layers_to_train列表中的层结构
          if all(
              [not name.startswith(layer) for layer in layers_to_train]
          ):
              parameter.requires_grad_(False)
  
      if extra_blocks is None:
          extra_blocks = LastLevelMaxPool()
  
      if returned_layers is None:
          returned_layers = [1, 2, 3, 4]
      # 返回的特征层个数肯定大于0小于5
      assert min(returned_layers) > 0 and max(returned_layers) < 5
  
      # return_layers = {'layer1': '0', 'layer2': '1', 'layer3': '2', 'layer4': '3'}
      return_layers = {
          f"layer{k}": str(v) for v, k in enumerate(returned_layers)
      }
  
      # in_channel 为layer4的输出特征矩阵channel = 2048
      in_channels_stage2 = resnet_backbone.in_channel // 8  # 256
      # 记录resnet50提供给fpn的特征层channels
      in_channels_list = [
          in_channels_stage2 * 2 ** (i - 1) for i in returned_layers
      ]
      # 通过fpn后得到的每个特征层的channel
      out_channels = 256
      return BackboneWithFPN(
          resnet_backbone,
          return_layers,
          in_channels_list,
          out_channels,
          extra_blocks=extra_blocks,
      )
  
file_path: deep-learning-for-image-processing/pytorch_object_detection/retinaNet/backbone/resnet50_fpn_model.py
imports: import os
  import torch.nn
  import torch
  from torchvision.ops.misc import FrozenBatchNorm2d
  from .feature_pyramid_network import LastLevelMaxPool, BackboneWithFPN
label_desc: Use image node_id 2 for calling a global function with example usage: resnet50_fpn_backbone(pretrain_path, norm_layer, trainable_layers, returned_layers, extra_blocks) and returns: BackboneWithFPN