code: def get_detection_api_from_dataset(
      dataset,
      supported_types: Sequence[
          Tuple["str", Union[Type, Tuple[Type]]]
      ] = DEFAULT_SUPPROTED_DETECTION_DATASETS,
      default_to_coco: bool = True,
      none_if_not_found=False,
  ):
      """
      Adapted from:
      https://github.com/pytorch/vision/blob/main/references/detection/engine.py
  
      :param dataset: The test dataset.
      :param supported_types: The supported API types
      :param default_to_coco: If True, if no API object can be found, the dataset
          will be converted to COCO.
      :param none_if_not_found: If True, it will return None if no valid
          detection API object is found. Else, it will consider `default_to_coco`
          or will raise an error.
  
      :return: The detection object.
      """
  
      recursion_result = None
      if isinstance(dataset, Subset):
          recursion_result = get_detection_api_from_dataset(
              dataset.dataset, supported_types, none_if_not_found=True
          )
      elif (
          isinstance(dataset, AvalancheDataset)
          and len(dataset._datasets) == 1
      ):
          recursion_result = get_detection_api_from_dataset(
              dataset._datasets[0],
              supported_types,
              none_if_not_found=True,
          )
      elif isinstance(dataset, (AvalancheDataset, ConcatDataset)):
          if isinstance(dataset, AvalancheDataset):
              datasets_list = dataset._datasets
          else:
              datasets_list = dataset.datasets
  
          for dataset in datasets_list:
              res = get_detection_api_from_dataset(
                  dataset, supported_types, none_if_not_found=True
              )
              if res is not None:
                  recursion_result = res
                  break
  
      if recursion_result is not None:
          return recursion_result
  
      for supported_n, supported_t in supported_types:
          candidate_api = getattr(dataset, supported_n, None)
          if candidate_api is not None:
              if isinstance(candidate_api, supported_t):
                  return candidate_api
      if none_if_not_found:
          return None
      elif default_to_coco:
          return convert_to_coco_api(dataset)
      else:
          raise ValueError("Could not find a valid dataset API object")
  
imports: import os
  from pathlib import Path
  import torch
  import json
  from typing import Any, Generic, List, Union, TypeVar, Tuple, Dict, TYPE_CHECKING, Type, Callable, Sequence, Optional
  from avalanche.benchmarks.utils.data import AvalancheDataset
  from torch import Tensor
  from json import JSONEncoder
  from torch.utils.data import Subset, ConcatDataset
  from typing_extensions import Protocol
  from avalanche.evaluation import PluginMetric
  from avalanche.evaluation.metric_results import MetricValue
  from avalanche.evaluation.metric_utils import get_metric_name
label_desc: Use image node_id 5 for calling a global function with example usage: get_detection_api_from_dataset(dataset, supported_types, default_to_coco, none_if_not_found) and returns: recursion_result, None, convert_to_coco_api, candidate_api