code: def find_images_and_targets(
      folder: str,
      types: Optional[Union[List, Tuple, Set]] = None,
      class_to_idx: Optional[Dict] = None,
      leaf_name_only: bool = True,
      sort: bool = True,
  ):
      """Walk folder recursively to discover images and map them to classes by folder names.
  
      Args:
          folder: root of folder to recrusively search
          types: types (file extensions) to search for in path
          class_to_idx: specify mapping for class (folder name) to class index if set
          leaf_name_only: use only leaf-name of folder walk for class names
          sort: re-sort found images by name (for consistent ordering)
  
      Returns:
          A list of image and target tuples, class_to_idx mapping
      """
      types = (
          get_img_extensions(as_set=True) if not types else set(types)
      )
      labels = []
      filenames = []
      for root, subdirs, files in os.walk(
          folder, topdown=False, followlinks=True
      ):
          rel_path = (
              os.path.relpath(root, folder) if (root != folder) else ""
          )
          label = (
              os.path.basename(rel_path)
              if leaf_name_only
              else rel_path.replace(os.path.sep, "_")
          )
          for f in files:
              base, ext = os.path.splitext(f)
              if ext.lower() in types:
                  filenames.append(os.path.join(root, f))
                  labels.append(label)
      if class_to_idx is None:
          # building class index
          unique_labels = set(labels)
          sorted_labels = list(sorted(unique_labels, key=natural_key))
          class_to_idx = {c: idx for idx, c in enumerate(sorted_labels)}
      images_and_targets = [
          (f, class_to_idx[l])
          for f, l in zip(filenames, labels)
          if l in class_to_idx
      ]
      if sort:
          images_and_targets = sorted(
              images_and_targets, key=lambda k: natural_key(k[0])
          )
      return images_and_targets, class_to_idx
  
file_path: pytorch-image-models/timm/data/readers/reader_image_folder.py
imports: import os
  from typing import Dict, List, Optional, Set, Tuple, Union
  from timm.utils.misc import natural_key
  from .class_map import load_class_map
  from .img_extensions import get_img_extensions
  from .reader import Reader
label_desc: Use image node_id 1 for calling a global function with example usage: find_images_and_targets(folder, types, class_to_idx, leaf_name_only, sort) and returns: images_and_targets,  class_to_idx