Torch-fidelity is a PyTorch library offering precise, efficient, and extensible implementations of popular generative model evaluation metrics, including Inception Score (ISC), Fréchet Inception Distance (FID), Kernel Inception Distance (KID), Perceptual Path Length (PPL), and Precision and Recall (PRC). It aims for epsilon-exact numerical fidelity with reference TensorFlow implementations. The library is actively maintained, with its latest version being 0.4.0, and has a steady release cadence with significant updates, like new metrics and feature extractors.
pip install torch-fidelityVerified import paths — ran on the pinned version, not inferred.
This quickstart demonstrates how to calculate Inception Score (ISC), Fréchet Inception Distance (FID), and Kernel Inception Distance (KID) using `torch-fidelity`'s Python API. It involves defining a dummy generative model, wrapping it with `GenerativeModelModuleWrapper`, and then passing it along with a reference input (like a pre-registered dataset 'cifar10-train') to the `calculate_metrics` function. The results are returned as a dictionary.
Update your API calls and CLI arguments to use the new parameter names: `kid_kernel_poly_degree`, `kid_kernel_poly_gamma`, `kid_kernel_poly_coef0`.
Ensure `input1` and `input2` are passed as keyword arguments (e.g., `input1=generator, input2='cifar10-train'`) and update CLI arguments according to the changelog.
Use `feature_extractor_compile=False` (default) if numerical precision is critical for your evaluation. Only enable it if you have thoroughly validated its impact on your specific use case.
Understand that negative KID values are normal and reflect the mathematical properties of the metric.
Prefer lossless image formats (e.g., PNG) for evaluating generative models to maintain the highest numerical precision for metrics.
Be aware that direct comparison of InceptionV3 features with `torchvision`'s model might yield minor differences. For precise comparisons with TF-based results, rely on `torch-fidelity`'s built-in InceptionV3.
To manage disk usage, specify alternative locations using the `--cache-root` and `--datasets-root` CLI arguments or the corresponding `cache_root` and `datasets_root` keyword arguments in `calculate_metrics`. Caching can be disabled with `--no-cache` or `cache=False` (not recommended for efficiency).
Use `features='inception_v3'` (or desired feature type) instead of `feature_extractor_arch='inception_v3'`.
Use one of the supported feature extractor names listed in the error message, such as `'inception_v3_features'`.
Ensure the directory contains valid image files or provide the correct path to a directory with images.
Reduce the `batch_size` parameter when calling `calculate_metrics` or `make_features`, or use a GPU with more VRAM.