code: # Copyright (c) Facebook, Inc. and its affiliates. All Rights Reserved.
  from maskrcnn_benchmark.modeling import registry
  from torch import nn
  
  
  @registry.ROI_BOX_PREDICTOR.register("FastRCNNPredictor")
  class FastRCNNPredictor(nn.Module):
      def __init__(self, config, in_channels):
          super(FastRCNNPredictor, self).__init__()
          assert in_channels is not None
  
          num_inputs = in_channels
  
          num_classes = config.MODEL.ROI_BOX_HEAD.NUM_CLASSES
          if config.FEW_SHOT.SECOND_STAGE_METHOD == 'rn':
              num_classes = 2
          self.avgpool = nn.AdaptiveAvgPool2d(1)
          self.cls_score = nn.Linear(num_inputs, num_classes)
          num_bbox_reg_classes = 2 if config.MODEL.CLS_AGNOSTIC_BBOX_REG else num_classes
          self.bbox_pred = nn.Linear(num_inputs, num_bbox_reg_classes * 4)
  
          nn.init.normal_(self.cls_score.weight, mean=0, std=0.01)
          nn.init.constant_(self.cls_score.bias, 0)
  
          nn.init.normal_(self.bbox_pred.weight, mean=0, std=0.001)
          nn.init.constant_(self.bbox_pred.bias, 0)
  
      def forward(self, x):
          x = self.avgpool(x)
          x = x.view(x.size(0), -1)
          cls_logit = self.cls_score(x)
          bbox_pred = self.bbox_pred(x)
          return cls_logit, bbox_pred
  
  
  @registry.ROI_BOX_PREDICTOR.register("FPNPredictor")
  class FPNPredictor(nn.Module):
      def __init__(self, cfg, in_channels):
          super(FPNPredictor, self).__init__()
          num_classes = cfg.MODEL.ROI_BOX_HEAD.NUM_CLASSES
  
          num_bbox_reg_classes = 2
          if cfg.FEW_SHOT.SECOND_STAGE_METHOD == 'rn' and cfg.FEW_SHOT.SECOND_STAGE_CLS_LOSS == 'focal_loss':
              num_classes = 1 
          elif cfg.FEW_SHOT.SECOND_STAGE_METHOD == 'rn' and cfg.FEW_SHOT.SECOND_STAGE_CLS_LOSS != 'focal_loss':
              num_classes= 2
          elif cfg.FEW_SHOT.SECOND_STAGE_METHOD == 'concat' and \
              cfg.FEW_SHOT.SECOND_STAGE_CLS_LOSS == 'focal_loss' and \
              not cfg.FEW_SHOT.NEG_SUPPORT.TURN_ON:
              num_classes = 1
          elif cfg.FEW_SHOT.SECOND_STAGE_METHOD == 'concat' and \
              cfg.FEW_SHOT.SECOND_STAGE_CLS_LOSS == 'focal_loss' and \
              cfg.FEW_SHOT.NEG_SUPPORT.TURN_ON:
              num_classes = 2
          elif cfg.FEW_SHOT.SECOND_STAGE_METHOD == 'concat' and \
              cfg.FEW_SHOT.SECOND_STAGE_CLS_LOSS == 'ce_loss' and \
              not cfg.FEW_SHOT.NEG_SUPPORT.TURN_ON:
              num_classes = 2
          elif cfg.FEW_SHOT.SECOND_STAGE_METHOD == 'concat' and \
              cfg.FEW_SHOT.SECOND_STAGE_CLS_LOSS == 'ce_loss' and \
              cfg.FEW_SHOT.NEG_SUPPORT.TURN_ON:
              num_classes = 2 # originally 3, but 2 in new version neg support
          elif cfg.FEW_SHOT.SECOND_STAGE_METHOD == 'concat' and \
              cfg.FEW_SHOT.SECOND_STAGE_CLS_LOSS =='cxe_loss' and cfg.FEW_SHOT.SOFT_LABELING:
              num_classes = 2
          elif cfg.FEW_SHOT.SECOND_STAGE_METHOD == 'concat' and \
              cfg.FEW_SHOT.SECOND_STAGE_CLS_LOSS in ['mse_loss','l1_loss']:
              num_classes = 1
          else:
              raise Exception('setting not compatible {} {} {}'.format(
                      cfg.FEW_SHOT.SECOND_STAGE_METHOD,
                      cfg.FEW_SHOT.SECOND_STAGE_CLS_LOSS,
                      cfg.FEW_SHOT.NEG_SUPPORT.TURN_ON
                  ))
  
          if cfg.FEW_SHOT.SECOND_STAGE_CLS_LOSS in ['focal_loss', 'mse_loss', 'l1_loss']:
              num_bbox_reg_classes = num_classes+1
          else:
              num_bbox_reg_classes = num_classes
  
          representation_size = in_channels
  
          self.cls_score = nn.Linear(representation_size, num_classes)
          # num_bbox_reg_classes = 2 #if cfg.MODEL.CLS_AGNOSTIC_BBOX_REG else num_classes
          self.bbox_pred = nn.Linear(representation_size, num_bbox_reg_classes * 4)
  
          nn.init.normal_(self.cls_score.weight, std=0.01)
          nn.init.normal_(self.bbox_pred.weight, std=0.001)
          for l in [self.cls_score, self.bbox_pred]:
              nn.init.constant_(l.bias, 0)
  
      def forward(self, x):
          if x.ndimension() == 4:
              assert list(x.shape[2:]) == [1, 1]
              x = x.view(x.size(0), -1)
          scores = self.cls_score(x)
          bbox_deltas = self.bbox_pred(x)
  
          return scores, bbox_deltas
  
  
  def make_roi_box_predictor(cfg, in_channels):
      func = registry.ROI_BOX_PREDICTOR[cfg.MODEL.ROI_BOX_HEAD.PREDICTOR]
      return func(cfg, in_channels)
  
apis: torch.nn.AdaptiveAvgPool2d torch.nn.Linear torch.nn.init.normal_ torch.nn.init.constant_