code: def tensor_to_image(tensor: Tensor, keepdim: bool = False) -> Any:
      """Converts a PyTorch tensor image to a numpy image.
  
      In case the tensor is in the GPU, it will be copied back to CPU.
  
      Args:
          tensor: image of the form :math:`(H, W)`, :math:`(C, H, W)` or
              :math:`(B, C, H, W)`.
          keepdim: If ``False`` squeeze the input image to match the shape
              :math:`(H, W, C)` or :math:`(H, W)`.
  
      Returns:
          image of the form :math:`(H, W)`, :math:`(H, W, C)` or :math:`(B, H, W, C)`.
  
      Example:
          >>> img = torch.ones(1, 3, 3)
          >>> tensor_to_image(img).shape
          (3, 3)
  
          >>> img = torch.ones(3, 4, 4)
          >>> tensor_to_image(img).shape
          (4, 4, 3)
      """
      if not isinstance(tensor, Tensor):
          raise TypeError(
              f"Input type is not a Tensor. Got {type(tensor)}"
          )
  
      if len(tensor.shape) > 4 or len(tensor.shape) < 2:
          raise ValueError(
              "Input size must be a two, three or four dimensional tensor"
          )
  
      input_shape = tensor.shape
      image = tensor.cpu().detach().numpy()
  
      if len(input_shape) == 2:
          # (H, W) -> (H, W)
          pass
      elif len(input_shape) == 3:
          # (C, H, W) -> (H, W, C)
          if input_shape[0] == 1:
              # Grayscale for proper plt.imshow needs to be (H,W)
              image = image.squeeze()
          else:
              image = image.transpose(1, 2, 0)
      elif len(input_shape) == 4:
          # (B, C, H, W) -> (B, H, W, C)
          image = image.transpose(0, 2, 3, 1)
          if input_shape[0] == 1 and not keepdim:
              image = image.squeeze(0)
          if input_shape[1] == 1:
              image = image.squeeze(-1)
      else:
          raise ValueError(
              f"Cannot process tensor with shape {input_shape}"
          )
  
      return image
  
file_path: kornia/kornia/utils/image.py
imports: from functools import wraps
  from typing import Any, Callable, List
  import torch
  from torch import nn
  from kornia.core import Tensor
label_desc: Use image node_id 5 for calling a global function with example usage: tensor_to_image(tensor, keepdim) and returns: image