pytorch-image-generation-metrics


Namepytorch-image-generation-metrics JSON
Version 0.6.1 PyPI version JSON
download
home_pagehttps://github.com/w86763777/pytorch_image_generation_metrics
SummaryPackage for calculating image generation metrics using Pytorch
upload_time2024-06-14 03:20:10
maintainerNone
docs_urlNone
authorYi-Lun Wu
requires_python>=3.6
licenseNone
keywords pytorch inception score is frechet inception distance fid
VCS
bugtrack_url
requirements No requirements were recorded.
Travis-CI No Travis.
coveralls test coverage No coveralls.
            # Pytorch Implementation of Common Image Generation Metrics

![PyPI](https://img.shields.io/pypi/v/pytorch_image_generation_metrics)

## Installation
```
pip install pytorch-image-generation-metrics
```

## Quick Start
```python
from pytorch_image_generation_metrics import get_inception_score, get_fid

images = ... # [N, 3, H, W] normalized to [0, 1]
IS, IS_std = get_inception_score(images)        # Inception Score
FID = get_fid(images, 'path/to/fid_ref.npz') # Frechet Inception Distance
```
The file `path/to/fid_ref.npz` is compatiable with the [official FID implementation](https://github.com/bioinf-jku/TTUR).

## Notes
The FID implementation is inspired by [pytorch-fid](https://github.com/mseitzer/pytorch-fid).

This repository is developed for personal research. If you find this package useful, please feel free to open issues.

## Features
- Currently, this package supports the following metrics:
  - [Inception Score](https://github.com/openai/improved-gan) (IS)
  - [Fréchet Inception Distance](https://github.com/bioinf-jku/TTUR) (FID)
- The computation procedures for IS and FID are integrated to avoid multiple forward passes.
- Supports reading images on the fly to prevent out-of-memory issues, especially for large-scale images.
- Supports computation on GPU to speed up some CPU operations, such as `np.cov` and `scipy.linalg.sqrtm`.

## Reproducing Results of Official Implementations on CIFAR-10

|                     |Train IS  |Test IS   |Train(50k) vs Test(10k)<br>FID|
|---------------------|:--------:|:--------:|:----------------------------:|
|Official             |11.24±0.20|10.98±0.22|3.1508                        |
|ours                 |11.26±0.13|10.97±0.19|3.1525                        |
|ours `use_torch=True`|11.26±0.15|10.97±0.20|3.1457                        |

The results differ slightly from the official implementations due to the framework differences between PyTorch and TensorFlow.

## Documentation

### Prepare Statistical Reference for FID
- [Download](https://drive.google.com/drive/folders/1UBdzl6GtNMwNQ5U-4ESlIer43tNjiGJC?usp=sharing) the pre-calculated reference, or
- Calculate the statistical reference for your custom dataset using the command-line tool:
    ```bash
    python -m pytorch_image_generation_metrics.fid_ref \
        --path path/to/images \
        --output path/to/fid_ref.npz
    ```
    See [fid_ref.py](./pytorch_image_generation_metrics/fid_ref.py) for details.

### Inception Features
- When getting IS or FID, the `InceptionV3` model will be loaded into `torch.device('cuda:0')` by default.
- Change the `device` argument in the `get_*` functions to set the torch device.

### Using `torch.Tensor` as images

- Prepare images as `torch.float32` tensors with shape `[N, 3, H, W]`, normalized to `[0,1]`.
    ```python
    from pytorch_image_generation_metrics import (
        get_inception_score,
        get_fid,
        get_inception_score_and_fid
    )

    images = ... # [N, 3, H, W]
    assert 0 <= images.min() and images.max() <= 1

    # Inception Score
    IS, IS_std = get_inception_score(
        images)

    # Frechet Inception Distance
    FID = get_fid(
        images, 'path/to/fid_ref.npz')

    # Inception Score & Frechet Inception Distance
    (IS, IS_std), FID = get_inception_score_and_fid(
        images, 'path/to/fid_ref.npz')

    ```

### Using PyTorch DataLoader to Provide Images

1. Use `pytorch_image_generation_metrics.ImageDataset` to collect images from your storage or use your custom `torch.utils.data.Dataset`.
    ```python
    from pytorch_image_generation_metrics import ImageDataset
    from torch.utils.data import DataLoader

    dataset = ImageDataset(path_to_dir, exts=['png', 'jpg'])
    loader = DataLoader(dataset, batch_size=50, num_workers=4)
    ```

    You can wrap a generative model in a dataset to support generating images on the fly.
    ```python
    class GeneratorDataset(Dataset):
        def __init__(self, G, noise_dim):
            self.G = G
            self.noise_dim = noise_dim

        def __len__(self):
            return 50000

        def __getitem__(self, index):
            return self.G(torch.randn(1, self.noise_dim))

    dataset = GeneratorDataset(G, noise_dim=128)
    loader = DataLoader(dataset, batch_size=50, num_workers=0)
    ```

2. Calculate metrics
    ```python
    from pytorch_image_generation_metrics import (
        get_inception_score,
        get_fid,
        get_inception_score_and_fid
    )

    # Inception Score
    IS, IS_std = get_inception_score(
        loader)

    # Frechet Inception Distance
    FID = get_fid(
        loader, 'path/to/fid_ref.npz')

    # Inception Score & Frechet Inception Distance
    (IS, IS_std), FID = get_inception_score_and_fid(
        loader, 'path/to/fid_ref.npz')
    ```

### Load Images from a Directory

- Calculate metrics for images in a directory and its subfolders.
    ```python
    from pytorch_image_generation_metrics import (
        get_inception_score_from_directory,
        get_fid_from_directory,
        get_inception_score_and_fid_from_directory)

    IS, IS_std = get_inception_score_from_directory(
        'path/to/images')
    FID = get_fid_from_directory(
        'path/to/images', 'path/to/fid_ref.npz')
    (IS, IS_std), FID = get_inception_score_and_fid_from_directory(
        'path/to/images', 'path/to/fid_ref.npz')
    ```

### Accelerating Matrix Computation with PyTorch

- Set `use_torch=True` when calling functions like `get_inception_score`, `get_fid`, etc.

- **WARNING**: when `use_torch=True` is used, the FID might be `nan` due to the unstable implementation of matrix sqrt root.

## Tested Versions
- `python 3.9 + torch 1.13.1 + CUDA 11.7`
- `python 3.9 + torch 2.3.0 + CUDA 12.1`

## License

This implementation is licensed under the Apache License 2.0.

This implementation is derived from [pytorch-fid](https://github.com/mseitzer/pytorch-fid), licensed under the Apache License 2.0.

FID was introduced by Martin Heusel, Hubert Ramsauer, Thomas Unterthiner, Bernhard Nessler and Sepp Hochreiter in "GANs Trained by a Two Time-Scale Update Rule Converge to a Local Nash Equilibrium", see [https://arxiv.org/abs/1706.08500](https://arxiv.org/abs/1706.08500)

The original implementation of FID is by the Institute of Bioinformatics, JKU Linz, licensed under the Apache License 2.0.
See [https://github.com/bioinf-jku/TTUR](https://github.com/bioinf-jku/TTUR).



            

Raw data

            {
    "_id": null,
    "home_page": "https://github.com/w86763777/pytorch_image_generation_metrics",
    "name": "pytorch-image-generation-metrics",
    "maintainer": null,
    "docs_url": null,
    "requires_python": ">=3.6",
    "maintainer_email": null,
    "keywords": "PyTorch, Inception Score, IS, Frechet Inception Distance, FID",
    "author": "Yi-Lun Wu",
    "author_email": "w86763777@gmail.com",
    "download_url": "https://files.pythonhosted.org/packages/d4/ee/ed446ad931b6fb0178739ef79524493247426fa3a40dccc4524ec411aa10/pytorch_image_generation_metrics-0.6.1.tar.gz",
    "platform": null,
    "description": "# Pytorch Implementation of Common Image Generation Metrics\n\n![PyPI](https://img.shields.io/pypi/v/pytorch_image_generation_metrics)\n\n## Installation\n```\npip install pytorch-image-generation-metrics\n```\n\n## Quick Start\n```python\nfrom pytorch_image_generation_metrics import get_inception_score, get_fid\n\nimages = ... # [N, 3, H, W] normalized to [0, 1]\nIS, IS_std = get_inception_score(images)        # Inception Score\nFID = get_fid(images, 'path/to/fid_ref.npz') # Frechet Inception Distance\n```\nThe file `path/to/fid_ref.npz` is compatiable with the [official FID implementation](https://github.com/bioinf-jku/TTUR).\n\n## Notes\nThe FID implementation is inspired by [pytorch-fid](https://github.com/mseitzer/pytorch-fid).\n\nThis repository is developed for personal research. If you find this package useful, please feel free to open issues.\n\n## Features\n- Currently, this package supports the following metrics:\n  - [Inception Score](https://github.com/openai/improved-gan) (IS)\n  - [Fr\u00e9chet Inception Distance](https://github.com/bioinf-jku/TTUR) (FID)\n- The computation procedures for IS and FID are integrated to avoid multiple forward passes.\n- Supports reading images on the fly to prevent out-of-memory issues, especially for large-scale images.\n- Supports computation on GPU to speed up some CPU operations, such as `np.cov` and `scipy.linalg.sqrtm`.\n\n## Reproducing Results of Official Implementations on CIFAR-10\n\n|                     |Train IS  |Test IS   |Train(50k) vs Test(10k)<br>FID|\n|---------------------|:--------:|:--------:|:----------------------------:|\n|Official             |11.24\u00b10.20|10.98\u00b10.22|3.1508                        |\n|ours                 |11.26\u00b10.13|10.97\u00b10.19|3.1525                        |\n|ours `use_torch=True`|11.26\u00b10.15|10.97\u00b10.20|3.1457                        |\n\nThe results differ slightly from the official implementations due to the framework differences between PyTorch and TensorFlow.\n\n## Documentation\n\n### Prepare Statistical Reference for FID\n- [Download](https://drive.google.com/drive/folders/1UBdzl6GtNMwNQ5U-4ESlIer43tNjiGJC?usp=sharing) the pre-calculated reference, or\n- Calculate the statistical reference for your custom dataset using the command-line tool:\n    ```bash\n    python -m pytorch_image_generation_metrics.fid_ref \\\n        --path path/to/images \\\n        --output path/to/fid_ref.npz\n    ```\n    See [fid_ref.py](./pytorch_image_generation_metrics/fid_ref.py) for details.\n\n### Inception Features\n- When getting IS or FID, the `InceptionV3` model will be loaded into `torch.device('cuda:0')` by default.\n- Change the `device` argument in the `get_*` functions to set the torch device.\n\n### Using `torch.Tensor` as images\n\n- Prepare images as `torch.float32` tensors with shape `[N, 3, H, W]`, normalized to `[0,1]`.\n    ```python\n    from pytorch_image_generation_metrics import (\n        get_inception_score,\n        get_fid,\n        get_inception_score_and_fid\n    )\n\n    images = ... # [N, 3, H, W]\n    assert 0 <= images.min() and images.max() <= 1\n\n    # Inception Score\n    IS, IS_std = get_inception_score(\n        images)\n\n    # Frechet Inception Distance\n    FID = get_fid(\n        images, 'path/to/fid_ref.npz')\n\n    # Inception Score & Frechet Inception Distance\n    (IS, IS_std), FID = get_inception_score_and_fid(\n        images, 'path/to/fid_ref.npz')\n\n    ```\n\n### Using PyTorch DataLoader to Provide Images\n\n1. Use `pytorch_image_generation_metrics.ImageDataset` to collect images from your storage or use your custom `torch.utils.data.Dataset`.\n    ```python\n    from pytorch_image_generation_metrics import ImageDataset\n    from torch.utils.data import DataLoader\n\n    dataset = ImageDataset(path_to_dir, exts=['png', 'jpg'])\n    loader = DataLoader(dataset, batch_size=50, num_workers=4)\n    ```\n\n    You can wrap a generative model in a dataset to support generating images on the fly.\n    ```python\n    class GeneratorDataset(Dataset):\n        def __init__(self, G, noise_dim):\n            self.G = G\n            self.noise_dim = noise_dim\n\n        def __len__(self):\n            return 50000\n\n        def __getitem__(self, index):\n            return self.G(torch.randn(1, self.noise_dim))\n\n    dataset = GeneratorDataset(G, noise_dim=128)\n    loader = DataLoader(dataset, batch_size=50, num_workers=0)\n    ```\n\n2. Calculate metrics\n    ```python\n    from pytorch_image_generation_metrics import (\n        get_inception_score,\n        get_fid,\n        get_inception_score_and_fid\n    )\n\n    # Inception Score\n    IS, IS_std = get_inception_score(\n        loader)\n\n    # Frechet Inception Distance\n    FID = get_fid(\n        loader, 'path/to/fid_ref.npz')\n\n    # Inception Score & Frechet Inception Distance\n    (IS, IS_std), FID = get_inception_score_and_fid(\n        loader, 'path/to/fid_ref.npz')\n    ```\n\n### Load Images from a Directory\n\n- Calculate metrics for images in a directory and its subfolders.\n    ```python\n    from pytorch_image_generation_metrics import (\n        get_inception_score_from_directory,\n        get_fid_from_directory,\n        get_inception_score_and_fid_from_directory)\n\n    IS, IS_std = get_inception_score_from_directory(\n        'path/to/images')\n    FID = get_fid_from_directory(\n        'path/to/images', 'path/to/fid_ref.npz')\n    (IS, IS_std), FID = get_inception_score_and_fid_from_directory(\n        'path/to/images', 'path/to/fid_ref.npz')\n    ```\n\n### Accelerating Matrix Computation with PyTorch\n\n- Set `use_torch=True` when calling functions like `get_inception_score`, `get_fid`, etc.\n\n- **WARNING**: when `use_torch=True` is used, the FID might be `nan` due to the unstable implementation of matrix sqrt root.\n\n## Tested Versions\n- `python 3.9 + torch 1.13.1 + CUDA 11.7`\n- `python 3.9 + torch 2.3.0 + CUDA 12.1`\n\n## License\n\nThis implementation is licensed under the Apache License 2.0.\n\nThis implementation is derived from [pytorch-fid](https://github.com/mseitzer/pytorch-fid), licensed under the Apache License 2.0.\n\nFID was introduced by Martin Heusel, Hubert Ramsauer, Thomas Unterthiner, Bernhard Nessler and Sepp Hochreiter in \"GANs Trained by a Two Time-Scale Update Rule Converge to a Local Nash Equilibrium\", see [https://arxiv.org/abs/1706.08500](https://arxiv.org/abs/1706.08500)\n\nThe original implementation of FID is by the Institute of Bioinformatics, JKU Linz, licensed under the Apache License 2.0.\nSee [https://github.com/bioinf-jku/TTUR](https://github.com/bioinf-jku/TTUR).\n\n\n",
    "bugtrack_url": null,
    "license": null,
    "summary": "Package for calculating image generation metrics using Pytorch",
    "version": "0.6.1",
    "project_urls": {
        "Homepage": "https://github.com/w86763777/pytorch_image_generation_metrics"
    },
    "split_keywords": [
        "pytorch",
        " inception score",
        " is",
        " frechet inception distance",
        " fid"
    ],
    "urls": [
        {
            "comment_text": "",
            "digests": {
                "blake2b_256": "df379b1376b1b2bebcdb3b5a45ff4a70313fa2cc8b22caeb217ed4b3c4d55116",
                "md5": "2a5413d2aaf93281f21fbe4bff5ee40c",
                "sha256": "6635307f4878866b25c835baf919253f3baea3e7bc55153f5e7e1e2a697d0073"
            },
            "downloads": -1,
            "filename": "pytorch_image_generation_metrics-0.6.1-py3-none-any.whl",
            "has_sig": false,
            "md5_digest": "2a5413d2aaf93281f21fbe4bff5ee40c",
            "packagetype": "bdist_wheel",
            "python_version": "py3",
            "requires_python": ">=3.6",
            "size": 25320,
            "upload_time": "2024-06-14T03:20:08",
            "upload_time_iso_8601": "2024-06-14T03:20:08.175401Z",
            "url": "https://files.pythonhosted.org/packages/df/37/9b1376b1b2bebcdb3b5a45ff4a70313fa2cc8b22caeb217ed4b3c4d55116/pytorch_image_generation_metrics-0.6.1-py3-none-any.whl",
            "yanked": false,
            "yanked_reason": null
        },
        {
            "comment_text": "",
            "digests": {
                "blake2b_256": "d4eeed446ad931b6fb0178739ef79524493247426fa3a40dccc4524ec411aa10",
                "md5": "0e05d74ec1d08d6fd30ca9f5e6616507",
                "sha256": "2e81f81de0fe331e7c769b203695c2a8c60ace89b6229141c6774a31a49c13c6"
            },
            "downloads": -1,
            "filename": "pytorch_image_generation_metrics-0.6.1.tar.gz",
            "has_sig": false,
            "md5_digest": "0e05d74ec1d08d6fd30ca9f5e6616507",
            "packagetype": "sdist",
            "python_version": "source",
            "requires_python": ">=3.6",
            "size": 21849,
            "upload_time": "2024-06-14T03:20:10",
            "upload_time_iso_8601": "2024-06-14T03:20:10.339506Z",
            "url": "https://files.pythonhosted.org/packages/d4/ee/ed446ad931b6fb0178739ef79524493247426fa3a40dccc4524ec411aa10/pytorch_image_generation_metrics-0.6.1.tar.gz",
            "yanked": false,
            "yanked_reason": null
        }
    ],
    "upload_time": "2024-06-14 03:20:10",
    "github": true,
    "gitlab": false,
    "bitbucket": false,
    "codeberg": false,
    "github_user": "w86763777",
    "github_project": "pytorch_image_generation_metrics",
    "github_not_found": true,
    "lcname": "pytorch-image-generation-metrics"
}
        
Elapsed time: 1.07905s