TF 2.0 is out! Get hands-on practice at TF World, Oct 28-31. Use code TF20 for 20% off select passes. Register now

tf.contrib.gan.eval.kernel_classifier_distance

View source on GitHub

Kernel "classifier" distance for evaluating a generative model.

Aliases:

  • tf.contrib.gan.eval.classifier_metrics.kernel_classifier_distance
tf.contrib.gan.eval.kernel_classifier_distance(
    real_images,
    generated_images,
    classifier_fn,
    num_classifier_batches=1,
    max_block_size=1024,
    dtype=None
)

This is based on the Kernel Inception distance, but for an arbitrary embedding.

This technique is described in detail in https://arxiv.org/abs/1801.01401. Given two distributions P and Q of activations, this function calculates

E_{X, X' ~ P}[k(X, X')] + E_{Y, Y' ~ Q}[k(Y, Y')]
  - 2 E_{X ~ P, Y ~ Q}[k(X, Y)]

where k is the polynomial kernel

k(x, y) = ( x^T y / dimension + 1 )^3.

This captures how different the distributions of real and generated images' visual features are. Like the Frechet distance (and unlike the Inception score), this is a true distance and incorporates information about the target images. Unlike the Frechet score, this function computes an unbiased and asymptotically normal estimator, which makes comparing estimates across models much more intuitive.

The estimator used takes time quadratic in max_block_size. Larger values of max_block_size will decrease the variance of the estimator but increase the computational cost. This differs slightly from the estimator used by the original paper; it is the block estimator of https://arxiv.org/abs/1307.1954.

NOTE: the blocking code assumes that real_activations and generated_activations are both in random order. If either is sorted in a meaningful order, the estimator will behave poorly.

NOTE: This function consumes images, computes their activations, and then computes the classifier score. If you would like to precompute many activations for real and generated images for large batches, or to compute multiple scores based on the same images, please use kernel_clasifier_distance_from_activations(), which this method also uses.

Args:

  • real_images: Real images to use to compute Kernel Inception distance.
  • generated_images: Generated images to use to compute Kernel Inception distance.
  • classifier_fn: A function that takes images and produces activations based on a classifier.
  • num_classifier_batches: Number of batches to split images in to in order to efficiently run them through the classifier network.
  • max_block_size: integer, default 1024. The distance estimator splits samples into blocks for computational efficiency. Larger values are more computationally expensive but decrease the variance of the distance estimate.
  • dtype: if not None, coerce activations to this dtype before computations.

Returns:

The Kernel Inception Distance. A floating-point scalar of the same type as the output of the activations.