code: def merge_branch_instances(
      instances, num_branch, nms_thresh, topk_per_image
  ):
      """
      Merge detection results from different branches of TridentNet.
      Return detection results by applying non-maximum suppression (NMS) on bounding boxes
      and keep the unsuppressed boxes and other instances (e.g mask) if any.
  
      Args:
          instances (list[Instances]): A list of N * num_branch instances that store detection
              results. Contain N images and each image has num_branch instances.
          num_branch (int): Number of branches used for merging detection results for each image.
          nms_thresh (float):  The threshold to use for box non-maximum suppression. Value in [0, 1].
          topk_per_image (int): The number of top scoring detections to return. Set < 0 to return
              all detections.
  
      Returns:
          results: (list[Instances]): A list of N instances, one for each image in the batch,
              that stores the topk most confidence detections after merging results from multiple
              branches.
      """
      if num_branch == 1:
          return instances
  
      batch_size = len(instances) // num_branch
      results = []
      for i in range(batch_size):
          instance = Instances.cat(
              [instances[i + batch_size * j] for j in range(num_branch)]
          )
  
          # Apply per-class NMS
          keep = batched_nms(
              instance.pred_boxes.tensor,
              instance.scores,
              instance.pred_classes,
              nms_thresh,
          )
          keep = keep[:topk_per_image]
          result = instance[keep]
  
          results.append(result)
  
      return results
  
file_path: detectron2/projects/TridentNet/tridentnet/trident_rcnn.py
imports: from detectron2.layers import batched_nms
  from detectron2.modeling import ROI_HEADS_REGISTRY, StandardROIHeads
  from detectron2.modeling.roi_heads.roi_heads import Res5ROIHeads
  from detectron2.structures import Instances
label_desc: Use image node_id 1 for calling a global function with example usage: merge_branch_instances(instances, num_branch, nms_thresh, topk_per_image) and returns: results, instances