code: def torch_tensor_from_numpy(
      x: torch.Tensor | NDArray[t.Any], device: str = "cpu"
  ) -> torch.Tensor:
      if isinstance(x, np.ndarray):
          # Needs to create a copy if the array is not writeable
          x = np.copy(x) if not x.flags["WRITEABLE"] else x
          return torch.from_numpy(x).to(device)
      elif isinstance(x, torch.Tensor):
          return x
      else:
          raise TypeError(
              f"Expected numpy.ndarray or torch.Tensor, got {type(x).__name__}"
          )
  
imports: from __future__ import annotations
  import json
  import os
  import time
  import typing
  from pathlib import Path
  import attr
  import cv2
  import numpy
  import torch
  import torchvision
  from PIL import Image
label_desc: Use image node_id 2 for calling a global function with example usage: torch_tensor_from_numpy(x, device) and returns: torch, x