code: def convert_to_tensor(
      data: Any,
      dtype: DtypeLike | torch.dtype = None,
      device: None | str | torch.device = None,
      wrap_sequence: bool = False,
      track_meta: bool = False,
      safe: bool = False,
  ) -> Any:
      """
      Utility to convert the input data to a PyTorch Tensor, if `track_meta` is True, the output will be a `MetaTensor`,
      otherwise, the output will be a regular torch Tensor.
      If passing a dictionary, list or tuple, recursively check every item and convert it to PyTorch Tensor.
  
      Args:
          data: input data can be PyTorch Tensor, numpy array, list, dictionary, int, float, bool, str, etc.
              will convert Tensor, Numpy array, float, int, bool to Tensor, strings and objects keep the original.
              for dictionary, list or tuple, convert every item to a Tensor if applicable.
          dtype: target data type to when converting to Tensor.
          device: target device to put the converted Tensor data.
          wrap_sequence: if `False`, then lists will recursively call this function.
              E.g., `[1, 2]` -> `[tensor(1), tensor(2)]`. If `True`, then `[1, 2]` -> `tensor([1, 2])`.
          track_meta: whether to track the meta information, if `True`, will convert to `MetaTensor`.
              default to `False`.
          safe: if `True`, then do safe dtype convert when intensity overflow. default to `False`.
              E.g., `[256, -12]` -> `[tensor(0), tensor(244)]`.
              If `True`, then `[256, -12]` -> `[tensor(255), tensor(0)]`.
  
      """
  
      def _convert_tensor(tensor: Any, **kwargs: Any) -> Any:
          if not isinstance(tensor, torch.Tensor):
              # certain numpy types are not supported as being directly convertible to Pytorch tensors
              if (
                  isinstance(tensor, np.ndarray)
                  and tensor.dtype in UNSUPPORTED_TYPES
              ):
                  tensor = tensor.astype(
                      UNSUPPORTED_TYPES[tensor.dtype]
                  )
  
              # if input data is not Tensor, convert it to Tensor first
              tensor = torch.as_tensor(tensor, **kwargs)
          if track_meta and not isinstance(
              tensor, monai.data.MetaTensor
          ):
              return monai.data.MetaTensor(tensor)
          if not track_meta and isinstance(
              tensor, monai.data.MetaTensor
          ):
              return tensor.as_tensor()
          return tensor
  
      if safe:
          data = safe_dtype_range(data, dtype)
      dtype = get_equivalent_dtype(dtype, torch.Tensor)
      if isinstance(data, torch.Tensor):
          return _convert_tensor(data).to(
              dtype=dtype,
              device=device,
              memory_format=torch.contiguous_format,
          )
      if isinstance(data, np.ndarray):
          # skip array of string classes and object, refer to:
          # https://github.com/pytorch/pytorch/blob/v1.9.0/torch/utils/data/_utils/collate.py#L13
          if re.search(r"[SaUO]", data.dtype.str) is None:
              # numpy array with 0 dims is also sequence iterable,
              # `ascontiguousarray` will add 1 dim if img has no dim, so we only apply on data with dims
              if data.ndim > 0:
                  data = np.ascontiguousarray(data)
              return _convert_tensor(data, dtype=dtype, device=device)
      elif (has_cp and isinstance(data, cp_ndarray)) or isinstance(
          data, (float, int, bool)
      ):
          return _convert_tensor(data, dtype=dtype, device=device)
      elif isinstance(data, list):
          list_ret = [
              convert_to_tensor(
                  i, dtype=dtype, device=device, track_meta=track_meta
              )
              for i in data
          ]
          return (
              _convert_tensor(list_ret, dtype=dtype, device=device)
              if wrap_sequence
              else list_ret
          )
      elif isinstance(data, tuple):
          tuple_ret = tuple(
              convert_to_tensor(
                  i, dtype=dtype, device=device, track_meta=track_meta
              )
              for i in data
          )
          return (
              _convert_tensor(tuple_ret, dtype=dtype, device=device)
              if wrap_sequence
              else tuple_ret
          )
      elif isinstance(data, dict):
          return {
              k: convert_to_tensor(
                  v, dtype=dtype, device=device, track_meta=track_meta
              )
              for k, v in data.items()
          }
  
      return data
  
imports: from __future__ import annotations
  import re
  from collections.abc import Sequence
  from typing import Any
  import numpy
  import torch
  import monai
  from monai.config.type_definitions import DtypeLike, NdarrayTensor
  from monai.utils import optional_import
label_desc: Use image node_id 7 for calling a global function with example usage: convert_to_tensor(data, dtype, device, wrap_sequence, track_meta, safe) and returns: data, tensor, _convert_tensor, monai, tensor, _convert_tensor, _convert_tensor, unknown, unknown, unknown