code: def _to_instances(result, confidence_thresh=None):
      if result.masks is None:
          return None
  
      classes = np.rint(result.boxes.cls.detach().cpu().numpy()).astype(
          int
      )
      boxes = result.boxes.xywhn.detach().cpu().numpy().astype(float)
      masks = result.masks.data.detach().cpu().numpy() > 0.5
      confs = result.boxes.conf.detach().cpu().numpy().astype(float)
  
      # convert from center coords to corner coords
      boxes[:, 0] -= boxes[:, 2] / 2.0
      boxes[:, 1] -= boxes[:, 3] / 2.0
  
      detections = []
      for cls, box, mask, conf in zip(classes, boxes, masks, confs):
          if confidence_thresh is not None and conf < confidence_thresh:
              continue
  
          label = result.names[cls]
          w, h = mask.shape
          tmp = np.copy(box)
          tmp[2] += tmp[0]
          tmp[3] += tmp[1]
          tmp[0] *= h
          tmp[2] *= h
          tmp[1] *= w
          tmp[3] *= w
          tmp = [int(b) for b in tmp]
          y0, x0, y1, x1 = tmp
          sub_mask = mask[x0:x1, y0:y1]
  
          detection = fol.Detection(
              label=label,
              bounding_box=list(box),
              mask=sub_mask.astype(bool),
              confidence=conf,
          )
          detections.append(detection)
  
      return fol.Detections(detections=detections)
  
imports: import itertools
  import numpy
  from PIL import Image
  from fiftyone.core.config import Config
  import fiftyone.core.labels
  from fiftyone.core.models import Model
  import fiftyone.utils.torch
  import fiftyone.core.utils
label_desc: Use image node_id 5 for calling a global function with example usage: _to_instances(result, confidence_thresh) and returns: fol, None