class_docstr: This module implements :paper:`FPN`.
  It creates pyramid features built on top of some input feature maps.
class_docstr_tok: ["This","module","implements",":","paper",":","`","FPN","`",".","It","creates","pyramid","features","built","on","top","of","some","input","feature","maps","."]
docstr: Args:
      bottom_up (Backbone): module representing the bottom up subnetwork.
          Must be a subclass of :class:`Backbone`. The multi-scale feature
          maps generated by the bottom up network, and listed in `in_features`,
          are used to generate FPN levels.
      in_features (list[str]): names of the input feature maps coming
          from the backbone to which FPN is attached. For example, if the
          backbone produces ["res2", "res3", "res4"], any *contiguous* sublist
          of these may be used; order must be from high to low resolution.
      out_channels (int): number of channels in the output feature maps.
      norm (str): the normalization to use.
      top_block (nn.Module or None): if provided, an extra operation will
          be performed on the output of the last (smallest resolution)
          FPN output, and the result will extend the result list. The top_block
          further downsamples the feature map. It must have an attribute
          "num_levels", meaning the number of extra FPN levels added by
          this block, and "in_feature", which is a string representing
          its input feature (e.g., p5).
      fuse_type (str): types for fusing the top down features and the lateral
          ones. It can be "sum" (default), which sums up element-wise; or "avg",
          which takes the element-wise mean of the two.
      square_pad (int): If > 0, require input images to be padded to specific square size.
docstr_tok: ["Args",":","bottom_up","(","Backbone",")",":","module","representing","the","bottom","up","subnetwork",".","Must","be","a","subclass","of",":","class",":","`","Backbone","`",".","The","multi-scale","feature","maps","generated","by","the","bottom","up","network",",","and","listed","in","`","in_features","`",",","are","used","to","generate","FPN","levels",".","in_features","(","list","[","str","]",")",":","names","of","the","input","feature","maps","coming","from","the","backbone","to","which","FPN","is","attached",".","For","example",",","if","the","backbone","produces","[","``","res2","''",",","``","res3","''",",","``","res4","''","]",",","any","*","contiguous","*","sublist","of","these","may","be","used",";","order","must","be","from","high","to","low","resolution",".","out_channels","(","int",")",":","number","of","channels","in","the","output","feature","maps",".","norm","(","str",")",":","the","normalization","to","use",".","top_block","(","nn.Module","or","None",")",":","if","provided",",","an","extra","operation","will","be","performed","on","the","output","of","the","last","(","smallest","resolution",")","FPN","output",",","and","the","result","will","extend","the","result","list",".","The","top_block","further","downsamples","the","feature","map",".","It","must","have","an","attribute","``","num_levels","''",",","meaning","the","number","of","extra","FPN","levels","added","by","this","block",",","and","``","in_feature","''",",","which","is","a","string","representing","its","input","feature","(","e.g.",",","p5",")",".","fuse_type","(","str",")",":","types","for","fusing","the","top","down","features","and","the","lateral","ones",".","It","can","be","``","sum","''","(","default",")",",","which","sums","up","element-wise",";","or","``","avg","''",",","which","takes","the","element-wise","mean","of","the","two",".","square_pad","(","int",")",":","If",">","0",",","require","input","images","to","be","padded","to","specific","square","size","."]
code: def __init__(
      self,
      bottom_up,
      in_features,
      out_channels,
      norm="",
      top_block=None,
      fuse_type="sum",
      square_pad=0,
  ):
      """
      Args:
          bottom_up (Backbone): module representing the bottom up subnetwork.
              Must be a subclass of :class:`Backbone`. The multi-scale feature
              maps generated by the bottom up network, and listed in `in_features`,
              are used to generate FPN levels.
          in_features (list[str]): names of the input feature maps coming
              from the backbone to which FPN is attached. For example, if the
              backbone produces ["res2", "res3", "res4"], any *contiguous* sublist
              of these may be used; order must be from high to low resolution.
          out_channels (int): number of channels in the output feature maps.
          norm (str): the normalization to use.
          top_block (nn.Module or None): if provided, an extra operation will
              be performed on the output of the last (smallest resolution)
              FPN output, and the result will extend the result list. The top_block
              further downsamples the feature map. It must have an attribute
              "num_levels", meaning the number of extra FPN levels added by
              this block, and "in_feature", which is a string representing
              its input feature (e.g., p5).
          fuse_type (str): types for fusing the top down features and the lateral
              ones. It can be "sum" (default), which sums up element-wise; or "avg",
              which takes the element-wise mean of the two.
          square_pad (int): If > 0, require input images to be padded to specific square size.
      """
      super(FPN, self).__init__()
      assert isinstance(bottom_up, Backbone)
      assert in_features, in_features
  
      # Feature map strides and channels from the bottom up network (e.g. ResNet)
      input_shapes = bottom_up.output_shape()
      strides = [input_shapes[f].stride for f in in_features]
      in_channels_per_feature = [
          input_shapes[f].channels for f in in_features
      ]
  
      _assert_strides_are_log2_contiguous(strides)
      lateral_convs = []
      output_convs = []
  
      use_bias = norm == ""
      for idx, in_channels in enumerate(in_channels_per_feature):
          lateral_norm = get_norm(norm, out_channels)
          output_norm = get_norm(norm, out_channels)
  
          lateral_conv = Conv2d(
              in_channels,
              out_channels,
              kernel_size=1,
              bias=use_bias,
              norm=lateral_norm,
          )
          output_conv = Conv2d(
              out_channels,
              out_channels,
              kernel_size=3,
              stride=1,
              padding=1,
              bias=use_bias,
              norm=output_norm,
          )
          weight_init.c2_xavier_fill(lateral_conv)
          weight_init.c2_xavier_fill(output_conv)
          stage = int(math.log2(strides[idx]))
          self.add_module("fpn_lateral{}".format(stage), lateral_conv)
          self.add_module("fpn_output{}".format(stage), output_conv)
  
          lateral_convs.append(lateral_conv)
          output_convs.append(output_conv)
      # Place convs into top-down order (from low to high resolution)
      # to make the top-down computation in forward clearer.
      self.lateral_convs = lateral_convs[::-1]
      self.output_convs = output_convs[::-1]
      self.top_block = top_block
      self.in_features = tuple(in_features)
      self.bottom_up = bottom_up
      # Return feature names are "p<stage>", like ["p2", "p3", ..., "p6"]
      self._out_feature_strides = {
          "p{}".format(int(math.log2(s))): s for s in strides
      }
      # top block output feature maps.
      if self.top_block is not None:
          for s in range(stage, stage + self.top_block.num_levels):
              self._out_feature_strides["p{}".format(s + 1)] = 2 ** (
                  s + 1
              )
  
      self._out_features = list(self._out_feature_strides.keys())
      self._out_feature_channels = {
          k: out_channels for k in self._out_features
      }
      self._size_divisibility = strides[-1]
      self._square_pad = square_pad
      assert fuse_type in {"avg", "sum"}
      self._fuse_type = fuse_type
  
imports: import math
  import fvcore.nn.weight_init
  import torch
  import torch.nn.functional
  from torch import nn
  from detectron2.layers import Conv2d, ShapeSpec, get_norm
  from .backbone import Backbone
  from .build import BACKBONE_REGISTRY
  from .resnet import build_resnet_backbone
label_desc: Use image node_id 1 to create a new FPN object from inherited base classes: Backbone with example: obj = FPN(bottom_up, in_features, out_channels, norm, top_block, fuse_type, square_pad)