code: def knn_precision_recall_features(
      ref_features,
      eval_features,
      feature_net,
      nhood_sizes,
      row_batch_size,
      col_batch_size,
      num_gpus,
  ):
      """Calculates k-NN precision and recall for two sets of feature vectors."""
      state = dnnlib.EasyDict()
      # num_images = ref_features.shape[0]
      num_features = feature_net.output_shape[1]
      state.ref_features = ref_features
      state.eval_features = eval_features
  
      # Initialize DistanceBlock and ManifoldEstimators.
      distance_block = DistanceBlock(num_features, num_gpus)
      state.ref_manifold = ManifoldEstimator(
          distance_block,
          state.ref_features,
          row_batch_size,
          col_batch_size,
          nhood_sizes,
      )
      state.eval_manifold = ManifoldEstimator(
          distance_block,
          state.eval_features,
          row_batch_size,
          col_batch_size,
          nhood_sizes,
      )
  
      # Evaluate precision and recall using k-nearest neighbors.
      # print('Evaluating k-NN precision and recall with %i samples...' % num_images)
      # start = time.time()
  
      # Precision: How many points from eval_features are in ref_features manifold.
      (
          state.precision,
          state.realism_scores,
          state.nearest_neighbors,
      ) = state.ref_manifold.evaluate(
          state.eval_features,
          return_realism=True,
          return_neighbors=True,
      )
      state.knn_precision = state.precision.mean(axis=0)
  
      # Recall: How many points from ref_features are in eval_features manifold.
      state.recall = state.eval_manifold.evaluate(state.ref_features)
      state.knn_recall = state.recall.mean(axis=0)
  
      # elapsed_time = time.time() - start
      # print('Done evaluation in: %gs' % elapsed_time)
  
      return state
  
file_path: insightface/reconstruction/ostec/external/stylegan2/metrics/precision_recall.py
imports: import os
  import numpy
  import tensorflow
  import dnnlib
  import dnnlib.tflib
  from metrics import metric_base
  from training import misc
label_desc: Use image node_id 2 for calling a global function with example usage: knn_precision_recall_features(ref_features, eval_features, feature_net, nhood_sizes, row_batch_size, col_batch_size, num_gpus) and returns: state