Giter Club home page Giter Club logo

pytorch-sfid's Introduction

pytorch-sfid

Computing the Sliding Fréchet Inception Distance (SFID) between fake and real images with continous conditions, as suggested by Ding et al., 2021. The package is heavily inspired by pytorch-fid-wrapper, which again is based on pytorch-fid.

NB: For ncenters given intervals and ncond conditions, the images are sorted in ncenters^ncond number of (overlapping) bins. Since this number of bins increases rapidly with ncond, a large number of conditions will cause out-of-memory error. In practice, up to 5 conditions is possible for a standard computer.

Install

pip install git+https://github.com/evenmn/pytorch-sfid

Prerequisites

  1. torch
  2. pytorch-fid-wrapper

Usage

The package is centered around a function get_sfid, which takes a set of real and fake images and the corresponding conditions as torch tensors. The function set_config might be used to change the default ncenters and radius, but they can also be specified on-the-fly:

import pytorch_sfid as ps

# optional
ps.set_config(ncenters=NCENTERS, radius=RADIUS, batch_size=BATCH_SIZE, dims=DIMS, device=DEVICE)

# optional
real_stats = ps.get_stats(real_images=REAL_IMAGES, real_attr=REAL_ATTR)

# get SFID
sfid = ps.get_sfid(FAKE_IMAGES, FAKE_ATTR, real_images=REAL_IMAGES, real_attr=REAL_ATTR)

or

sfid = ps.get_sfid(FAKE_IMAGES, FAKE_ATTR, real_stats=real_stats)

The number of centers and the radius should be chosen such that most bins will contain sufficiently many images.

What does the code do?

The attributes are sorted into bins based on overlapping intervals. Then the indices of the images residing each bin is stored in an ncond x ncentersx nimg tensor, which is iterated. For each bin, we obtain the FID-score of the images, and the SFID score is found from summing over the FID score of all the bins:

.

The code itself utilizes torch only, and should be sufficiently fast. However, to find the FID values, we use pytorch-fid, which can be perceived as slow (even though it is as fast as it can be).

To do

  1. Make more conditions possible by ignoring certain conditions or merge bins of similar conditions.
  2. Allow different ncenters and radii for different conditions

Acknowledgements

I will kindly thank Halvard Sutterud (@halvarsu) for implementation help and mental support during the development of this package.

License

APACHE LICENSE, VERSION 2.0

pytorch-sfid's People

Contributors

evenmn avatar

Stargazers

 avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar

Forkers

molu1019

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.