code: def empty_instances(
      batch_img_metas: List[dict],
      device: torch.device,
      task_type: str,
      instance_results: OptInstanceList = None,
      mask_thr_binary: Union[int, float] = 0,
      box_type: Union[str, type] = "hbox",
      use_box_type: bool = False,
      num_classes: int = 80,
      score_per_cls: bool = False,
  ) -> List[InstanceData]:
      """Handle predicted instances when RoI is empty.
  
      Note: If ``instance_results`` is not None, it will be modified
      in place internally, and then return ``instance_results``
  
      Args:
          batch_img_metas (list[dict]): List of image information.
          device (torch.device): Device of tensor.
          task_type (str): Expected returned task type. it currently
              supports bbox and mask.
          instance_results (list[:obj:`InstanceData`]): List of instance
              results.
          mask_thr_binary (int, float): mask binarization threshold.
              Defaults to 0.
          box_type (str or type): The empty box type. Defaults to `hbox`.
          use_box_type (bool): Whether to warp boxes with the box type.
              Defaults to False.
          num_classes (int): num_classes of bbox_head. Defaults to 80.
          score_per_cls (bool):  Whether to generate classwise score for
              the empty instance. ``score_per_cls`` will be True when the model
              needs to produce raw results without nms. Defaults to False.
  
      Returns:
          list[:obj:`InstanceData`]: Detection results of each image
      """
      assert task_type in ("bbox", "mask"), (
          "Only support bbox and mask," f" but got {task_type}"
      )
  
      if instance_results is not None:
          assert len(instance_results) == len(batch_img_metas)
  
      results_list = []
      for img_id in range(len(batch_img_metas)):
          if instance_results is not None:
              results = instance_results[img_id]
              assert isinstance(results, InstanceData)
          else:
              results = InstanceData()
  
          if task_type == "bbox":
              _, box_type = get_box_type(box_type)
              bboxes = torch.zeros(0, box_type.box_dim, device=device)
              if use_box_type:
                  bboxes = box_type(bboxes, clone=False)
              results.bboxes = bboxes
              score_shape = (
                  (0, num_classes + 1) if score_per_cls else (0,)
              )
              results.scores = torch.zeros(score_shape, device=device)
              results.labels = torch.zeros(
                  (0,), device=device, dtype=torch.long
              )
          else:
              # TODO: Handle the case where rescale is false
              img_h, img_w = batch_img_metas[img_id]["ori_shape"][:2]
              # the type of `im_mask` will be torch.bool or torch.uint8,
              # where uint8 if for visualization and debugging.
              im_mask = torch.zeros(
                  0,
                  img_h,
                  img_w,
                  device=device,
                  dtype=torch.bool
                  if mask_thr_binary >= 0
                  else torch.uint8,
              )
              results.masks = im_mask
          results_list.append(results)
      return results_list
  
imports: from functools import partial
  from typing import List, Optional, Sequence, Tuple, Union
  import numpy
  import torch
  from mmengine.structures import InstanceData
  from mmengine.utils import digit_version
  from six.moves import map, zip
  from torch import Tensor
  from torch.autograd import Function
  from torch.nn import functional
  from mmdet.structures import SampleList
  from mmdet.structures.bbox import BaseBoxes, get_box_type, stack_boxes
  from mmdet.structures.mask import BitmapMasks, PolygonMasks
  from mmdet.utils import OptInstanceList
label_desc: Use image node_id 3 for calling a global function with example usage: empty_instances(batch_img_metas, device, task_type, instance_results, mask_thr_binary, box_type, use_box_type, num_classes, score_per_cls) and returns: results_list