code: def fast_rcnn_inference_single_image(
      boxes,
      scores,
      image_shape: Tuple[int, int],
      score_thresh: float,
      nms_thresh: float,
      topk_per_image: int,
  ):
      """
      Single-image inference. Return bounding-box detection results by thresholding
      on scores and applying non-maximum suppression (NMS).
  
      Args:
          Same as `fast_rcnn_inference`, but with boxes, scores, and image shapes
          per image.
  
      Returns:
          Same as `fast_rcnn_inference`, but for only one image.
      """
      valid_mask = torch.isfinite(boxes).all(dim=1) & torch.isfinite(
          scores
      ).all(dim=1)
      if not valid_mask.all():
          boxes = boxes[valid_mask]
          scores = scores[valid_mask]
  
      scores = scores[:, :-1]
      num_bbox_reg_classes = boxes.shape[1] // 4
      # Convert to Boxes to use the `clip` function ...
      boxes = Boxes(boxes.reshape(-1, 4))
      boxes.clip(image_shape)
      boxes = boxes.tensor.view(
          -1, num_bbox_reg_classes, 4
      )  # R x C x 4
  
      # 1. Filter results based on detection scores. It can make NMS more efficient
      #    by filtering out low-confidence detections.
      filter_mask = scores > score_thresh  # R x K
      # R' x 2. First column contains indices of the R predictions;
      # Second column contains indices of classes.
      filter_inds = filter_mask.nonzero()
      if num_bbox_reg_classes == 1:
          boxes = boxes[filter_inds[:, 0], 0]
      else:
          boxes = boxes[filter_mask]
      scores = scores[filter_mask]
  
      # 2. Apply NMS for each class independently.
      keep = batched_nms(boxes, scores, filter_inds[:, 1], nms_thresh)
      if topk_per_image >= 0:
          keep = keep[:topk_per_image]
      boxes, scores, filter_inds = (
          boxes[keep],
          scores[keep],
          filter_inds[keep],
      )
  
      result = Instances(image_shape)
      result.pred_boxes = Boxes(boxes)
      result.scores = scores
      result.pred_classes = filter_inds[:, 1]
      return result, filter_inds[:, 0]
  
imports: import logging
  from typing import Callable, Dict, List, Optional, Tuple, Union
  import torch
  from torch import nn
  from torch.nn import functional
  from detectron2.config import configurable
  from detectron2.data.detection_utils import get_fed_loss_cls_weights
  from detectron2.layers import ShapeSpec, batched_nms, cat, cross_entropy, nonzero_tuple
  from detectron2.modeling.box_regression import Box2BoxTransform, _dense_box_regression_loss
  from detectron2.structures import Boxes, Instances
  from detectron2.utils.events import get_event_storage
label_desc: Use image node_id 3 for calling a global function with example usage: fast_rcnn_inference_single_image(boxes, scores, image_shape, score_thresh, nms_thresh, topk_per_image) and returns: result,  filter_inds