code: def extract_train_features(
      cfg,
      temp_dir,
      train_dataset_name,
      resize_img,
      spatial_levels,
      image_helper,
      train_dataset,
      model,
  ):
      train_features = []
  
      def process_train_image(i, out_dir, verbose=False):
          if i % LOG_FREQUENCY == 0:
              logging.info(f"Train Image: {i}"),
  
          fname_out = None
          if out_dir:
              fname_out = f"{out_dir}/{i}.npy"
  
          if fname_out and g_pathmgr.exists(fname_out):
              feat = load_file(fname_out)
              train_features.append(feat)
          else:
              with PerfTimer("read_sample", PERF_STATS):
                  fname_in = train_dataset.get_filename(i)
                  if is_revisited_dataset(train_dataset_name):
                      img = (
                          image_helper.load_and_prepare_revisited_image(
                              fname_in, roi=None
                          )
                      )
                  elif is_whiten_dataset(train_dataset_name):
                      img = (
                          image_helper.load_and_prepare_whitening_image(
                              fname_in
                          )
                      )
                  else:
                      img = image_helper.load_and_prepare_image(
                          fname_in, roi=None
                      )
  
              with PerfTimer("extract_features", PERF_STATS):
                  img_scalings = cfg.IMG_RETRIEVAL.IMG_SCALINGS or [1]
                  activation_maps = extract_activation_maps(
                      img, model, img_scalings
                  )
  
              if verbose:
                  print(
                      f"Example train Image raw activation map shape: { activation_maps[0].shape }"  # NOQA
                  )
  
              with PerfTimer("post_process_features", PERF_STATS):
                  # once we have the features,
                  # we can perform: rmac | gem pooling | l2 norm
                  if cfg.IMG_RETRIEVAL.FEATS_PROCESSING_TYPE == "rmac":
                      descriptors = get_rmac_descriptors(
                          activation_maps[0],
                          spatial_levels,
                          normalize=cfg.IMG_RETRIEVAL.NORMALIZE_FEATURES,
                      )
                  elif cfg.IMG_RETRIEVAL.FEATS_PROCESSING_TYPE == "gem":
                      descriptors = get_average_gem(
                          activation_maps,
                          p=cfg.IMG_RETRIEVAL.GEM_POOL_POWER,
                          add_bias=True,
                      )
                  else:
                      descriptors = torch.mean(
                          torch.stack(activation_maps), dim=0
                      )
                      descriptors = descriptors.reshape(
                          descriptors.shape[0], -1
                      )
  
                  # Optionally l2 normalize the features.
                  if (
                      cfg.IMG_RETRIEVAL.NORMALIZE_FEATURES
                      and cfg.IMG_RETRIEVAL.FEATS_PROCESSING_TYPE
                      != "rmac"
                  ):
                      # RMAC performs normalization within the algorithm, hence we skip it here.
                      descriptors = l2n(descriptors, dim=1)
  
              if fname_out:
                  save_file(
                      descriptors.data.numpy(), fname_out, verbose=False
                  )
              train_features.append(descriptors.data.numpy())
  
      num_images = train_dataset.get_num_images()
  
      out_dir = None
      if temp_dir:
          out_dir = f"{temp_dir}/{train_dataset_name}_S{resize_img}_features_train"
          makedir(out_dir)
  
      logging.info(f"Getting features for train images: {num_images}")
      for i in range(num_images):
          process_train_image(i, out_dir, verbose=(i == 0))
  
      train_features = np.vstack(
          [x.reshape(-1, x.shape[-1]) for x in train_features]
      )
      logging.info(f"Train features size: {train_features.shape}")
  
      return train_features
  
imports: import logging
  import os
  import sys
  from argparse import Namespace
  from typing import Any, List
  import numpy
  import torch
  import torchvision
  from classy_vision.generic.util import copy_model_to_gpu, load_checkpoint
  from iopath.common.file_io import g_pathmgr
  from vissl.config import AttrDict
  from vissl.hooks import default_hook_generator
  from vissl.models import build_model
  from vissl.utils.checkpoint import get_checkpoint_folder, init_model_from_consolidated_weights
  from vissl.utils.distributed_launcher import launch_distributed
  from vissl.utils.env import set_env_vars, setup_path_manager
  from vissl.utils.extract_features_utils import ExtractedFeaturesLoader
  from vissl.utils.hydra_config import compose_hydra_configuration, convert_to_attrdict, print_cfg
  from vissl.utils.instance_retrieval_utils.data_util import CopyDaysDataset, GenericInstanceRetrievalDataset, get_average_gem, InstanceRetrievalDataset, InstanceRetrievalImageLoader, InstreDataset, is_copdays_dataset, is_instre_dataset, is_oxford_paris_dataset, is_revisited_dataset, is_whiten_dataset, l2n, MultigrainResize, RevisitedInstanceRetrievalDataset, WhiteningTrainingImageDataset
  from vissl.utils.instance_retrieval_utils.rmac import get_rmac_descriptors
  from vissl.utils.io import load_file, makedir, save_file
  from vissl.utils.logger import setup_logging, shutdown_logging
  from vissl.utils.pca import load_pca, train_and_save_pca
  from vissl.utils.perf_stats import PerfStats, PerfTimer
label_desc: Use image node_id 3 for calling a global function with example usage: extract_train_features(cfg, temp_dir, train_dataset_name, resize_img, spatial_levels, image_helper, train_dataset, model) and returns: train_features