code: def check_and_transform_label_format(
      labels: np.ndarray,
      nb_classes: Optional[int],
      return_one_hot: bool = True,
  ) -> np.ndarray:
      """
      Check label format and transform to one-hot-encoded labels if necessary
  
      :param labels: An array of integer labels of shape `(nb_samples,)`, `(nb_samples, 1)` or `(nb_samples, nb_classes)`.
      :param nb_classes: The number of classes. If None the number of classes is determined automatically.
      :param return_one_hot: True if returning one-hot encoded labels, False if returning index labels.
      :return: Labels with shape `(nb_samples, nb_classes)` (one-hot) or `(nb_samples,)` (index).
      """
      labels_return = labels
  
      if (
          len(labels.shape) == 2 and labels.shape[1] > 1
      ):  # multi-class, one-hot encoded
          if not return_one_hot:
              labels_return = np.argmax(labels, axis=1)
              labels_return = np.expand_dims(labels_return, axis=1)
      elif len(labels.shape) == 2 and labels.shape[1] == 1:
          if nb_classes is None:
              nb_classes = int(np.max(labels) + 1)
          if nb_classes > 2:  # multi-class, index labels
              if return_one_hot:
                  labels_return = to_categorical(labels, nb_classes)
              else:
                  labels_return = np.expand_dims(labels, axis=1)
          elif nb_classes == 2:  # binary, index labels
              if return_one_hot:
                  labels_return = to_categorical(labels, nb_classes)
          else:
              raise ValueError(
                  "Shape of labels not recognised."
                  "Please provide labels in shape (nb_samples,) or (nb_samples, "
                  "nb_classes)"
              )
      elif len(labels.shape) == 1:  # index labels
          if return_one_hot:
              labels_return = to_categorical(labels, nb_classes)
          else:
              labels_return = np.expand_dims(labels, axis=1)
      else:
          raise ValueError(
              "Shape of labels not recognised."
              "Please provide labels in shape (nb_samples,) or (nb_samples, "
              "nb_classes)"
          )
  
      return labels_return
  
imports: from __future__ import absolute_import, division, print_function, unicode_literals
  import logging
  import math
  import os
  import shutil
  import sys
  import tarfile
  import warnings
  import zipfile
  from functools import wraps
  from inspect import signature
  from typing import TYPE_CHECKING, Callable, List, Dict, Optional, Tuple, Union
  import numpy
  import six
  from scipy.special import gammainc
  from tqdm.auto import tqdm
  from art import config
label_desc: Use image node_id 13 for calling a global function with example usage: check_and_transform_label_format(labels, nb_classes, return_one_hot) and returns: labels_return