code: def to_icevision_record(sample: Dict[str, Any]):
      record = BaseRecord([])
  
      metadata = sample.get(DataKeys.METADATA, None) or {}
  
      if "image_id" in metadata:
          record_id_component = RecordIDRecordComponent()
          record_id_component.set_record_id(metadata["image_id"])
  
      component = ClassMapRecordComponent(tasks.detection)
      component.set_class_map(metadata.get("class_map", None))
      record.add_component(component)
  
      if isinstance(sample[DataKeys.INPUT], str):
          input_component = FilepathRecordComponent()
          input_component.set_filepath(sample[DataKeys.INPUT])
      else:
          if "filepath" in metadata:
              input_component = FilepathRecordComponent()
              input_component.filepath = metadata["filepath"]
          else:
              input_component = ImageRecordComponent()
          input_component.composite = record
          image = sample[DataKeys.INPUT]
          if isinstance(image, Tensor):
              image = image.permute(1, 2, 0).numpy()
          elif isinstance(image, Image.Image):
              image = np.array(image)
          input_component.set_img(image)
  
          record.add_component(
              OriginalSizeRecordComponent(
                  metadata.get("size", image.shape[:2])
              )
          )
      record.add_component(input_component)
  
      if DataKeys.TARGET in sample:
          if "labels" in sample[DataKeys.TARGET]:
              labels_component = InstancesLabelsRecordComponent()
              labels_component.add_labels_by_id(
                  sample[DataKeys.TARGET]["labels"]
              )
              record.add_component(labels_component)
  
          if "bboxes" in sample[DataKeys.TARGET]:
              bboxes = [
                  BBox.from_xywh(
                      bbox["xmin"],
                      bbox["ymin"],
                      bbox["width"],
                      bbox["height"],
                  )
                  for bbox in sample[DataKeys.TARGET]["bboxes"]
              ]
              bboxes_component = BBoxesRecordComponent()
              bboxes_component.set_bboxes(bboxes)
              record.add_component(bboxes_component)
  
          if _ICEVISION_GREATER_EQUAL_0_11_0:
              masks = sample[DataKeys.TARGET].get("masks", None)
  
              if masks is not None:
                  component = InstanceMasksRecordComponent()
  
                  if len(masks) > 0 and isinstance(masks[0], Mask):
                      component.set_masks(masks)
                  else:
                      # TODO: This treats invalid examples as negative examples
                      if len(masks) == 0 or not (
                          len(masks)
                          == len(record.detection.bboxes)
                          == len(record.detection.label_ids)
                      ):
                          data = np.zeros(
                              (0, record.height, record.width), np.uint8
                          )
                          labels_component.label_ids = []
                          bboxes_component.bboxes = []
                      else:
                          data = np.stack(masks, axis=0)
                      mask_array = MaskArray(data)
                      component.set_mask_array(mask_array)
                      component.set_masks(_split_mask_array(mask_array))
  
                  record.add_component(component)
          else:
              mask_array = sample[DataKeys.TARGET].get(
                  "mask_array", None
              )
              if mask_array is not None:
                  component = MasksRecordComponent()
                  component.set_masks(mask_array)
                  record.add_component(component)
  
          if "keypoints" in sample[DataKeys.TARGET]:
              keypoints = []
  
              keypoints_metadata = sample[DataKeys.TARGET].get(
                  "keypoints_metadata",
                  [None] * len(sample[DataKeys.TARGET]["keypoints"]),
              )
  
              for keypoints_list, keypoints_metadata in zip(
                  sample[DataKeys.TARGET]["keypoints"],
                  keypoints_metadata,
              ):
                  xyv = []
                  for keypoint in keypoints_list:
                      xyv.extend(
                          (
                              keypoint["x"],
                              keypoint["y"],
                              keypoint["visible"],
                          )
                      )
  
                  keypoints.append(
                      KeyPoints.from_xyv(xyv, keypoints_metadata)
                  )
              component = KeyPointsRecordComponent()
              component.set_keypoints(keypoints)
              record.add_component(component)
  
      return record
  
imports: from dataclasses import dataclass
  from typing import Any, Callable, Dict, List, Optional, Tuple
  import numpy
  from torch import Tensor, nn
  from flash.core.data.io.input import DataKeys
  from flash.core.data.io.input_transform import InputTransform
  from flash.core.utilities.imports import _ICEVISION_AVAILABLE, _ICEVISION_GREATER_EQUAL_0_11_0, _TOPIC_IMAGE_AVAILABLE, requires
label_desc: Use image node_id 2 for calling a global function with example usage: to_icevision_record(sample) and returns: record