code: def _evaluate(self, Gs, Gs_kwargs, num_gpus):
      minibatch_size = num_gpus * self.minibatch_per_gpu
  
      # Construct TensorFlow graph for each GPU.
      result_expr = []
      for gpu_idx in range(num_gpus):
          with tf.device("/gpu:%d" % gpu_idx):
              Gs_clone = Gs.clone()
  
              # Generate images.
              latents = tf.random_normal(
                  [self.minibatch_per_gpu] + Gs_clone.input_shape[1:]
              )
              labels = self._get_random_labels_tf(
                  self.minibatch_per_gpu
              )
              dlatents = Gs_clone.components.mapping.get_output_for(
                  latents, labels, **Gs_kwargs
              )
              images = Gs_clone.get_output_for(
                  latents, None, **Gs_kwargs
              )
  
              # Downsample to 256x256. The attribute classifiers were built for 256x256.
              if images.shape[2] > 256:
                  factor = images.shape[2] // 256
                  images = tf.reshape(
                      images,
                      [
                          -1,
                          images.shape[1],
                          images.shape[2] // factor,
                          factor,
                          images.shape[3] // factor,
                          factor,
                      ],
                  )
                  images = tf.reduce_mean(images, axis=[3, 5])
  
              # Run classifier for each attribute.
              result_dict = dict(
                  latents=latents, dlatents=dlatents[:, -1]
              )
              for attrib_idx in self.attrib_indices:
                  classifier = misc.load_pkl(
                      classifier_urls[attrib_idx]
                  )
                  logits = classifier.get_output_for(images, None)
                  predictions = tf.nn.softmax(
                      tf.concat([logits, -logits], axis=1)
                  )
                  result_dict[attrib_idx] = predictions
              result_expr.append(result_dict)
  
      # Sampling loop.
      results = []
      for begin in range(0, self.num_samples, minibatch_size):
          self._report_progress(begin, self.num_samples)
          results += tflib.run(result_expr)
      results = {
          key: np.concatenate([value[key] for value in results], axis=0)
          for key in results[0].keys()
      }
  
      # Calculate conditional entropy for each attribute.
      conditional_entropies = defaultdict(list)
      for attrib_idx in self.attrib_indices:
          # Prune the least confident samples.
          pruned_indices = list(range(self.num_samples))
          pruned_indices = sorted(
              pruned_indices,
              key=lambda i: -np.max(results[attrib_idx][i]),
          )
          pruned_indices = pruned_indices[: self.num_keep]
  
          # Fit SVM to the remaining samples.
          svm_targets = np.argmax(
              results[attrib_idx][pruned_indices], axis=1
          )
          for space in ["latents", "dlatents"]:
              svm_inputs = results[space][pruned_indices]
              try:
                  svm = sklearn.svm.LinearSVC()
                  svm.fit(svm_inputs, svm_targets)
                  svm.score(svm_inputs, svm_targets)
                  svm_outputs = svm.predict(svm_inputs)
              except:
                  svm_outputs = svm_targets  # assume perfect prediction
  
              # Calculate conditional entropy.
              p = [
                  [
                      np.mean(
                          [
                              case == (row, col)
                              for case in zip(svm_outputs, svm_targets)
                          ]
                      )
                      for col in (0, 1)
                  ]
                  for row in (0, 1)
              ]
              conditional_entropies[space].append(
                  conditional_entropy(p)
              )
  
      # Calculate separability scores.
      scores = {
          key: 2 ** np.sum(values)
          for key, values in conditional_entropies.items()
      }
      self._report_result(scores["latents"], suffix="_z")
      self._report_result(scores["dlatents"], suffix="_w")
  
file_path: insightface/reconstruction/ostec/external/stylegan2/metrics/linear_separability.py
imports: from collections import defaultdict
  import numpy
  import sklearn.svm
  import tensorflow
  import dnnlib.tflib
  from metrics import metric_base
  from training import misc
label_desc: Use image node_id 2 for calling the LS obj's underlying member method code with example usage: obj._evaluate(Gs, Gs_kwargs, num_gpus) without return types