code: def give_inaturalist_datasets_for_features(opt):
      """
      This function generates a training, testing and evaluation dataloader for Metric Learning on the Inaturalist 2018 dataset.
      For Metric Learning, training and test sets are provided by given json files. Will define a train and test split
      So no random shuffling of classes.
  
      Args:
          opt: argparse.Namespace, contains all traininig-specific parameters.
      Returns:
          dict of PyTorch datasets for training, testing and evaluation.
      """
      # Load text-files containing classes and imagepaths.
      # Generate image_dicts of shape {class_idx:[list of paths to images belong to this class] ...}
      train_image_dict, test_image_dict, eval_image_dict = {}, {}, {}
  
      if opt.iter > 0:
          with open(os.path.join(opt.cluster_path), "rb") as clusterf:
              (
                  path2idx,
                  global_features,
                  global_pred_labels,
                  gt_labels,
                  masks,
              ) = pickle.load(clusterf)
              gt_labels = gt_labels + len(np.unique(global_pred_labels))
  
              for path, idx in path2idx.items():
                  if global_pred_labels[idx] == -1:
                      if (
                          "te/%d" % gt_labels[idx]
                          not in test_image_dict
                      ):
                          test_image_dict["te/%d" % gt_labels[idx]] = []
                      test_image_dict["te/%d" % gt_labels[idx]].append(
                          path
                      )
                  else:
                      if (
                          "te/%d" % global_pred_labels[idx]
                          not in train_image_dict
                      ):
                          train_image_dict[
                              "te/%d" % global_pred_labels[idx]
                          ] = []
                      train_image_dict[
                          "te/%d" % global_pred_labels[idx]
                      ].append(path)
                      if (
                          "te/%d" % global_pred_labels[idx]
                          not in test_image_dict
                      ):
                          test_image_dict[
                              "te/%d" % global_pred_labels[idx]
                          ] = []
                      test_image_dict[
                          "te/%d" % global_pred_labels[idx]
                      ].append(path)
      else:
          with open(os.path.join(opt.source_path, opt.trainset)) as f:
              FileLines = f.readlines()
              FileLines = [x.strip() for x in FileLines]
  
              for entry in FileLines:
                  info = entry.split("/")
                  if (
                      "/".join([info[-3], info[-2]])
                      not in train_image_dict
                  ):
                      train_image_dict[
                          "/".join([info[-3], info[-2]])
                      ] = []
                  train_image_dict[
                      "/".join([info[-3], info[-2]])
                  ].append(os.path.join(opt.source_path, entry))
  
          with open(
              os.path.join(opt.source_path, opt.all_trainset)
          ) as f:
              FileLines = f.readlines()
              FileLines = [x.strip() for x in FileLines]
              for entry in FileLines:
                  info = entry.split("/")
                  if (
                      "/".join([info[-3], info[-2]])
                      not in test_image_dict
                  ):
                      test_image_dict[
                          "/".join([info[-3], info[-2]])
                      ] = []
                  test_image_dict[
                      "/".join([info[-3], info[-2]])
                  ].append(os.path.join(opt.source_path, entry))
  
      with open(os.path.join(opt.source_path, opt.testset)) as f:
          FileLines = f.readlines()
          FileLines = [x.strip() for x in FileLines]
  
          for entry in FileLines:
              info = entry.split("/")
              if "/".join([info[-3], info[-2]]) not in eval_image_dict:
                  eval_image_dict["/".join([info[-3], info[-2]])] = []
              eval_image_dict["/".join([info[-3], info[-2]])].append(
                  os.path.join(opt.source_path, entry)
              )
  
      new_train_dict = {}
      class_ind_ind = 0
      for cate in train_image_dict:
          new_train_dict["te/%d" % class_ind_ind] = train_image_dict[
              cate
          ]
          class_ind_ind += 1
      train_image_dict = new_train_dict
  
      new_test_dict = {}
      class_ind_ind = 0
      for cate in test_image_dict:
          new_test_dict["te/%d" % class_ind_ind] = test_image_dict[cate]
          class_ind_ind += 1
      test_image_dict = new_test_dict
  
      new_eval_dict = {}
      class_ind_ind = 0
      for cate in eval_image_dict:
          new_eval_dict["te/%d" % class_ind_ind] = eval_image_dict[cate]
          class_ind_ind += 1
      eval_image_dict = new_eval_dict
  
      train_dataset = BaseTripletDataset(
          train_image_dict, opt, is_validation=True
      )
      test_dataset = BaseTripletDataset(
          test_image_dict, opt, is_validation=True
      )
      eval_dataset = BaseTripletDataset(
          eval_image_dict, opt, is_validation=True
      )
  
      # train_dataset.conversion       = conversion
      # val_dataset.conversion         = conversion
      # eval_dataset.conversion        = conversion
  
      return {
          "training": train_dataset,
          "testing": test_dataset,
          "eval": eval_dataset,
      }
  
imports: import pickle
  import warnings
  from numpy.core.arrayprint import IntegerFormat
  import copy
  import os
  import random
  import numpy
  import pandas
  import torch
  from PIL import Image
  from torch.utils.data import Dataset
  from torchvision import transforms
label_desc: Use image node_id 3 for calling a global function with example usage: give_inaturalist_datasets_for_features(opt) and returns: dict