code: def _compare_mifid(preds, target, cosine_distance_eps: float = 0.1):
      """Reference implementation.
  
      Implementation taken from:
      https://github.com/jybai/generative-memorization-benchmark/blob/main/src/competition_scoring.py
  
      Adjusted slightly to work with our code. We replace the feature extraction with our own, since we already check in
      FID that we use the correct feature extractor. This saves us from needing to download tensorflow for comparison.
  
      """
  
      def normalize_rows(x: np.ndarray):
          return np.nan_to_num(
              x / np.linalg.norm(x, ord=2, axis=1, keepdims=True)
          )
  
      def cosine_distance(features1, features2):
          features1_nozero = features1[np.sum(features1, axis=1) != 0]
          features2_nozero = features2[np.sum(features2, axis=1) != 0]
          norm_f1 = normalize_rows(features1_nozero)
          norm_f2 = normalize_rows(features2_nozero)
  
          d = 1.0 - np.abs(np.matmul(norm_f1, norm_f2.T))
          return np.mean(np.min(d, axis=1))
  
      def distance_thresholding(d, eps):
          return d if d < eps else 1
  
      def calculate_frechet_distance(
          mu1, sigma1, mu2, sigma2, eps=1e-6
      ):
          mu1 = np.atleast_1d(mu1)
          mu2 = np.atleast_1d(mu2)
  
          sigma1 = np.atleast_2d(sigma1)
          sigma2 = np.atleast_2d(sigma2)
  
          diff = mu1 - mu2
  
          # product might be almost singular
          covmean, _ = sqrtm(sigma1.dot(sigma2), disp=False)
          if not np.isfinite(covmean).all():
              offset = np.eye(sigma1.shape[0]) * eps
              covmean = sqrtm((sigma1 + offset).dot(sigma2 + offset))
  
          # numerical error might give slight imaginary component
          if np.iscomplexobj(covmean):
              if not np.allclose(
                  np.diagonal(covmean).imag, 0, atol=1e-3
              ):
                  m = np.max(np.abs(covmean.imag))
                  raise Exception(f"Imaginary component {m}")
              covmean = covmean.real
  
          tr_covmean = np.trace(covmean)
          return (
              diff.dot(diff)
              + np.trace(sigma1)
              + np.trace(sigma2)
              - 2 * tr_covmean
          )
  
      def calculate_activation_statistics(act):
          mu = np.mean(act, axis=0)
          sigma = np.cov(act, rowvar=False)
          return mu, sigma, act
  
      def calculate_mifid(m1, s1, features1, m2, s2, features2):
          fid = calculate_frechet_distance(m1, s1, m2, s2)
          distance = cosine_distance(features1, features2)
          return fid, distance
  
      net = NoTrainInceptionV3(
          name="inception-v3-compat", features_list=[str(768)]
      )
      preds_act = net(preds).numpy()
      target_act = net(target).numpy()
  
      m1, s1, features1 = calculate_activation_statistics(preds_act)
      m2, s2, features2 = calculate_activation_statistics(target_act)
  
      fid_private, distance_private = calculate_mifid(
          m1, s1, features1, m2, s2, features2
      )
      distance_private_thresholded = distance_thresholding(
          distance_private, cosine_distance_eps
      )
      return fid_private / (distance_private_thresholded + 1e-15)
  
file_path: torchmetrics/tests/unittests/image/test_mifid.py
imports: from contextlib import nullcontext
  from functools import partial
  import numpy
  import pytest
  import torch
  from scipy.linalg import sqrtm
  from torchmetrics.image.mifid import MemorizationInformedFrechetInceptionDistance, NoTrainInceptionV3
  from torchmetrics.utilities.imports import _TORCH_FIDELITY_AVAILABLE
label_desc: Use image node_id 1 for calling a global function with example usage: _compare_mifid(preds, target, cosine_distance_eps) and returns: unknown, np, np, unknown, unknown, mu,  sigma,  act, fid,  distance