code: def get_predictions(
      pred: torch.Tensor,
      num_classes: int,
      objectness_confidence: float = 0.5,
      nms_confidence_level: float = 0.4,
      device: str = "cpu",
  ) -> Union[torch.Tensor, int]:
      """
      This function filters the bounding boxes predicted by the network by first
      discarding bounding boxes that has low objectness score, and then proceeds
      to filter overlapping bounding boxes using the non-maximum suppression
      algorithm.
  
      Parameters:
      -----------
          pred:           a tensor (predicted output) of shape
                          'batch_size x num_bboxes x bbox_attrs'
          num_classes:    the number of unique classes as provided by COCO.
          objectness_confidence_level:    probability threshold for bounding boxes
                                          containing a valid object.
          nms_convidence_level:           threshold for overlapping bounding boxes
  
      Returns:
      --------
          The prediction with reasonable bounding boxes.
      """
      nB = pred.shape[0]  # number of batches
      bbox_attr = pred.shape[
          2
      ]  # center_x, center_y, height, width, class_probabilites
      nBBOX = pred.shape[1]  # number of bounding boxes
      conf_mask = (
          (pred[:, :, 4] > objectness_confidence).float().unsqueeze(2)
      )
      pred = pred * conf_mask
  
      # transform the predicted centers, height and width to top-left corner and
      # right bottom corner coordinates to aid the ease computation of the IoU
      bbox_corner = pred.new(pred.shape)
      bbox_corner[:, :, 0] = pred[:, :, 0] - (
          pred[:, :, 2] / 2
      )  # top-left_x
      bbox_corner[:, :, 1] = pred[:, :, 1] - (
          pred[:, :, 3] / 2
      )  # top-left_y
      bbox_corner[:, :, 2] = pred[:, :, 0] + (
          pred[:, :, 2] / 2
      )  # bottom_right_x
      bbox_corner[:, :, 3] = pred[:, :, 1] + (
          pred[:, :, 3] / 2
      )  # bottom_right_y
      pred[:, :, :4] = bbox_corner[:, :, :4]
  
      n_pred = pred.view(-1, bbox_attr)
      idxs = (
          torch.arange(nB)
          .reshape(-1, 1)
          .repeat(1, nBBOX)
          .view(-1)
          .to(device)
      )  # image indices
  
      max_conf, max_idx = torch.max(
          n_pred[:, 5 : 5 + num_classes], 1
      )  # maximum class score and the index
      max_conf = max_conf.float().unsqueeze(1).to(device)
      max_idx = max_idx.float().unsqueeze(1).to(device)
      n_pred = torch.cat(
          [idxs.unsqueeze(1), n_pred[:, :5], max_conf, max_idx], 1
      )  # batch_idx, x1, y1, x2, y2, objectness_score, class_score, class_idx
  
      valid_bbox_indices = batched_nms(
          n_pred[:, 1:5].clone(),
          n_pred[:, 5].clone(),
          n_pred[:, 7].clone(),
          nms_confidence_level,
      )
  
      if len(valid_bbox_indices):
          return n_pred[valid_bbox_indices, :]
      return None
  
file_path: ImageAI/imageai/yolov3/utils.py
imports: import math
  from typing import Union, List, Tuple
  import torch
  import numpy
  import cv2
  from torchvision.ops import batched_nms
label_desc: Use image node_id 6 for calling a global function with example usage: get_predictions(pred, num_classes, objectness_confidence, nms_confidence_level, device) and returns: None, n_pred