PyTorch implementation of Fréchet inception distance (FID).
Caution
Use the original implementations when directly comparing with results that are reported in research papers.
-
Fréchet inception distance (FID)
-
Kernel inception distance (KID)
-
CleanFID
-
SwAV-FID
-
Fréchet DINO distance (FDD)
-
Precision & Recall (P&R)
-
Density & Coverage (D&C)
-
Probabilistic Precision & Recall (PP&PR)
-
Topological Precision & Recall (TopP&R)
-
pip:
pip install git+https://github.com/STomoya/ptfid.git
-
From source:
git clone https://github.com/STomoya/ptfid.git cd ptfid pip install --user -e .
-
(Optional) install xformers for DINOv2 models:
--extra-index-urlshould be changed depending on the environment.pip install xformers --extra-index-url https://download.pytorch.org/whl/cu118
-
Compute FID between two image folders. This command will create a
result.jsoncontaining the computed scores.python -m ptfid ./dir/to/dataset1 ./dir/to/dataset2
-
Additionally compute other metrics. Supported metrics are listed here. You can also explicity disable metrics by passing a flag like
--no-<metric>.python -m ptfid ./dir/to/dataset1 ./dir/to/dataset2 --no-fid --kid --pr --dc --pppr --toppr
Some metrics is required to determine the real dataset to correctly compute the scores (i.e., P&R). By default,
ptfidassumes that the first path argument points to the real dataset. You can change this behavior using--dataset1-is-realor--no-dataset1-is-realflag.python -m ptfid ./dir/to/fake ./dir/to/real --no-fid --pr --no-dataset1-is-real
-
You can determine the feature extractor using the
--feature-extractoroption.In addition to Inception v3,
ptfidsupports ResNet50-SwAV, DINOv2, CLIP models, and also models fromtimmandopen_clipusing thetimm:andclip:prefix. See the examples for details.Some examples
-
SwAV-FID
python -m ptfid ./dir/to/dataset1 ./dir/to/dataset2 \ --feature-extractor resnet50 \ --normalizer imagenet \ --resizer pillow \ --interpolation bilinear -
FDD
python -m ptfid ./dir/to/dataset1 ./dir/to/dataset2 \ --feature-extractor dinov2 \ --normalizer imagenet \ --resizer pillow -
CLIP ViT-L
python -m ptfid ./dir/to/dataset1 ./dir/to/dataset2 \ --feature-extractor clip \ --normalizer openai \ --resizer pillow -
Timm models
python -m ptfid ./dir/to/dataset1 ./dir/to/dataset2 \ --feature-extractor timm:convnext_tiny.in12k_ft_in1k \ --normalizer imagenet \ --resizer clean -
OpenCLIP models
python -m ptfid ./dir/to/dataset1 ./dir/to/dataset2 \ --feature-extractor clip:ViT-L-14.openai \ --normalizer openai \ --resizer clean
-
Important
It is recommended to specify the --normalizer option too, which defaults to Inception normalization. --normalizer imagenet uses the ImageNet mean and std, --normalizer openai uses the mean and std used to train the OpenAI CLIP models, and --normalizer custom --mean 0.5 0.5 0.5 --std 0.5 0.5 0.5 sets the mean and std to the values provided by the user.
Important
It is also recommended to specify the --resizer option, which defaults to the pytorch implementation of the tensorflow v1 bilinear interpolation. --resizer torch uses torch interpolation function, --resizer pillow uses pillow's resize function, and --resizer clean uses the clean-resize method proposed in the CleanFID paper. Only use --resizer inception when --feature-extractor inceptionv3, otherwise clean is recommended.
You can also change the interpolation mode using the --interpolation option. The default is set to bicubic. Only bicubic and bilinear is supported, and this option does not affect --resizer inception which only has a bilinear implementation.
-
Log process to a file.
python -m ptfid ./dir/to/dataset1 ./dir/to/dataset2 --log-file log.log
-
Command:
If
ptfidwas installed via pip, you can replacepython -m ptfidwith theptfidcommand.ptfid ./dir/to/dataset1 ./dir/to/dataset2
Help of ptfid
$ python -m ptfid --help
Usage: python -m ptfid [OPTIONS] DATASET_DIR1 DATASET_DIR2
Calculate generative metrics given two image folders.
╭─ Arguments ──────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────╮
│ * dataset_dir1 TEXT Dir to dataset. [default: None] [required] │
│ * dataset_dir2 TEXT Dir to dataset. [default: None] [required] │
╰──────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────╯
╭─ Options ────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────╮
│ --feature-extractor TEXT Feature extractor name. [default: inceptionv3] │
│ --fid --no-fid Flag for FID. [default: fid] │
│ --kid --no-kid Flag for KID. [default: no-kid] │
│ --pr --no-pr Flag for P&R. [default: no-pr] │
│ --dc --no-dc Flag for D&C. [default: no-dc] │
│ --pppr --no-pppr Flag for PP&PR. [default: no-pppr] │
│ --toppr --no-toppr Flag for TopP&R. [default: no-toppr] │
│ --eps FLOAT epsilon to avoid zero devision. [default: 1e-12] │
│ --fid-compute-method [original|efficient|gpu] Method to compute FID. [default: efficient] │
│ --kid-times FLOAT Multiply KID by. [default: 100.0] │
│ --kid-subsets INTEGER Number of subsets to compute KID. [default: 100] │
│ --kid-subset-size INTEGER Number of samples per subset. [default: 1000] │
│ --kid-degree FLOAT degree of polynomial kernel. [default: 3.0] │
│ --kid-gamma FLOAT gamma of polynomial kernel. [default: None] │
│ --kid-coef0 FLOAT coef0 of polynomial kernel. [default: 1.0] │
│ --pr-nearest-k INTEGER k for nearest neighbors. [default: 5] │
│ --pppr-alpha FLOAT Alpha for PP&PR. [default: 1.2] │
│ --toppr-alpha FLOAT Alpha for TopP&R. [default: 0.1] │
│ --toppr-kernel TEXT Kernel for TopP&R. [default: cosine] │
│ --toppr-randproj --no-toppr-randproj Random projection for TopP&R. [default: toppr-randproj] │
│ --toppr-f1 --no-toppr-f1 Compute F1-score for TopP&R. [default: toppr-f1] │
│ --seed INTEGER Random state seed. [default: 0] │
│ --resizer [clean|torch|tensorflow|pillow] Resize method. [default: tensorflow] │
│ --interpolation [bilinear|bicubic] Interpolation mode. [default: bicubic] │
│ --normalizer [imagenet|openai|inception|custom] Normalize method. [default: inception] │
│ --batch-size INTEGER Batch size. [default: 32] │
│ --mean <FLOAT FLOAT FLOAT>... Mean for custom normalizer. [default: None, None, None] │
│ --std <FLOAT FLOAT FLOAT>... Std for custom normalizer. [default: None, None, None] │
│ --num-workers INTEGER Number of workers. [default: 8] │
│ --device [cpu|cuda] Device. [default: cuda] │
│ --dataset1-is-real --no-dataset1-is-real Switch real dataset. [default: dataset1-is-real] │
│ --log-file TEXT File to output logs. [default: None] │
│ --result-file TEXT JSON file to save results to. [default: results.json] │
│ --help Show this message and exit. │
╰──────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────╯
-
Use
ptfidfrom source:from ptfid import calculate_metrics_from_folders results = calculate_metrics_from_folders( dataset_dir1='./dir/to/images1', dataset_dir2='./dir/to/images2', feature_extractor='inceptionv3', fid=True, resizer='tensorflow', normalizer='inception', result_file='results.json', )
-
User defined feature extractors can also be specified:
from ptfid import calculate_metrics_from_folders feature_extractor = create_model(...) # output feature must have `.ndim == 2` input_size = (224, 224) results = calculate_metrics_from_folders( dataset_dir1='./dir/to/images1', dataset_dir2='./dir/to/images2', feature_extractor=feature_extractor, fid=True, resizer='clean', normalizer='imagenet', image_size=input_size, # `image_size` argument is required. result_file='results.json', )
-
StyleGAN2-ADA: https://github.com/NVlabs/stylegan2-ada-pytorch
-
seeds:
0-49999
-
CleanFID: https://github.com/GaParmar/clean-fid
-
SwAV-FID: https://github.com/stanis-morozov/self-supervised-gan-eval
| original | ptfid |
abs. diff | |
|---|---|---|---|
| FID | |||
| FID (efficient) | |||
| FID (GPU) | |||
| CleanFID | |||
| FDD | |||
| SwAV-FID |
| original | ptfid |
abs. diff | |
|---|---|---|---|
| FID | |||
| FID (efficient) | |||
| FID (GPU) | |||
| CleanFID | |||
| FDD | |||
| SwAV-FID |
The source codes of this repository are based on several publicly available implementations of FID calculations. Many thanks to those who have published their excellent works. Specifically, official implementations of FID, CleanFID, FDD, SwAV-FID, D&C, PP&PR, TopP&R. Also, efficient computation of FID, and fast computation of FID on GPU.
This repository is created as part of the following research paper.
@inproceedings{10.1007/978-3-031-92808-6_9,
author = {Sawada, Tomoya and Katsurai, Marie},
title = {Evaluation of Illustration Generators with Domain-Specific Representations},
booktitle = {ECCV 2024 Workshops AI for Visual Arts Workshop and Challenges (AI4VA)},
year = {2025},
pages = {139--154},
doi = {10.1007/978-3-031-92808-6_9}
}- FID
@inproceedings{NIPS2017_8a1d6947,
author = {Heusel, Martin and Ramsauer, Hubert and Unterthiner, Thomas and Nessler, Bernhard and Hochreiter, Sepp},
booktitle = {Advances in Neural Information Processing Systems (NIPS)},
pages = {6626--6637},
publisher = {Curran Associates, Inc.},
title = {GANs Trained by a Two Time-Scale Update Rule Converge to a Local Nash Equilibrium},
volume = {30},
year = {2017}
}- KID
@inproceedings{bińkowski2018demystifying,
author = {Mikołaj Bińkowski and Dougal J. Sutherland and Michael Arbel and Arthur Gretton},
booktitle = {International Conference on Learning Representations (ICLR)},
title = {Demystifying {MMD} {GAN}s},
year = {2018},
url = {https://openreview.net/forum?id=r1lUOzWCW},
}- CleanFID
@inproceedings{Parmar_2022_CVPR,
author = {Parmar, Gaurav and Zhang, Richard and Zhu, Jun-Yan},
booktitle = {Proceedings of the IEEE/CVF Conference on Computer Vision and Pattern Recognition (CVPR)},
title = {On Aliased Resizing and Surprising Subtleties in GAN Evaluation},
month = {June},
year = {2022},
pages = {11410-11420}
}- SwAV-FID
@inproceedings{morozov2021on,
author = {Stanislav Morozov and Andrey Voynov and Artem Babenko},
booktitle = {International Conference on Learning Representations (ICLR)},
title = {On Self-Supervised Image Representations for {GAN} Evaluation},
year = {2021},
url = {https://openreview.net/forum?id=NeRdBeTionN}
}- FDD
@inproceedings{stein2023exposing,
author = {George Stein and Jesse C. Cresswell and Rasa Hosseinzadeh and Yi Sui and Brendan Leigh Ross and Valentin Villecroze and Zhaoyan Liu and Anthony L. Caterini and Eric Taylor and Gabriel Loaiza-Ganem},
booktitle = {Thirty-seventh Conference on Neural Information Processing Systems (NeurIPS)},
title = {Exposing flaws of generative model evaluation metrics and their unfair treatment of diffusion models},
year = {2023},
url = {https://openreview.net/forum?id=08zf7kTOoh}
}- P&R
@inproceedings{NEURIPS2018_f7696a9b,
author = {Sajjadi, Mehdi S. M. and Bachem, Olivier and Lucic, Mario and Bousquet, Olivier and Gelly, Sylvain},
booktitle = {Advances in Neural Information Processing Systems (NeurIPS)},
publisher = {Curran Associates, Inc.},
title = {Assessing Generative Models via Precision and Recall},
volume = {31},
year = {2018},
pages = {5228--5237},
}@inproceedings{NEURIPS2019_0234c510,
author = {Kynk\"{a}\"{a}nniemi, Tuomas and Karras, Tero and Laine, Samuli and Lehtinen, Jaakko and Aila, Timo},
booktitle = {Advances in Neural Information Processing Systems (NeurIPS)},
editor = {H. Wallach and H. Larochelle and A. Beygelzimer and F. d\textquotesingle Alch\'{e}-Buc and E. Fox and R. Garnett},
publisher = {Curran Associates, Inc.},
title = {Improved Precision and Recall Metric for Assessing Generative Models},
volume = {32},
year = {2019},
pages = {3927--3936},
}
- D&C
@inproceedings{pmlr-v119-naeem20a,
author = {Naeem, Muhammad Ferjad and Oh, Seong Joon and Uh, Youngjung and Choi, Yunjey and Yoo, Jaejun},
booktitle = {Proceedings of the 37th International Conference on Machine Learning},
title = {Reliable Fidelity and Diversity Metrics for Generative Models},
series = {Proceedings of Machine Learning Research (PMLR)},
volume = {119},
year = {2020},
pages = {7176--7185},
}- PP&PR
@inproceedings{Park_2023_ICCV,
author = {Park, Dogyun and Kim, Suhyun},
booktitle = {Proceedings of the IEEE/CVF International Conference on Computer Vision (ICCV)},
title = {Probabilistic Precision and Recall Towards Reliable Evaluation of Generative Models},
month = {October},
year = {2023},
pages = {20099-20109}
}- TopP&R
@inproceedings{kim2023toppr,
author = {Pum Jun Kim and Yoojin Jang and Jisu Kim and Jaejun Yoo},
booktitle = {Thirty-seventh Conference on Neural Information Processing Systems (NeurIPS)},
title = {TopP\&R: Robust Support Estimation Approach for Evaluating Fidelity and Diversity in Generative Models},
year = {2023},
url = {https://openreview.net/forum?id=2gUCMr6fDY}
}