Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Python API #59

Open
wants to merge 8 commits into
base: master
Choose a base branch
from
Open

Python API #59

wants to merge 8 commits into from

Conversation

kklemon
Copy link

@kklemon kklemon commented Dec 29, 2020

Many thanks for your awesome project! While it works perfectly using the provided command line script, it's not straightforward to use it directly from code, e.g. when the FID computation should be integrated into the training loop.

To improve on this issue, I refactored the FID code to be now contained in a single class and with the methods less coupled to each other. The former command line script still works as before, with only minor changes to the interface. Loading activation statistics from a file also works as before but computed stats can now also be cached automatically using the --cache flag.

Example usage from code may look as follows:

from pytorch_fid import FrechetInceptionDistance

model = FrechetInceptionDistance.get_inception_model()

# Optional: move to GPU
model = model.cuda()

fid = FrechetInceptionDistance(model)
stats1 = fid.get_activation_statistics(batches1)
stats2 = fid.get_activation_statistics(batches2)
score = fid.calculate_frechet_distance(*stats1, *stats2)

print(score)

Tests have been updated as well and linter passes.

@kklemon kklemon changed the title Refactoring to make usage from code easier Allow easier usage from code Dec 29, 2020
@mseitzer
Copy link
Owner

Hi,

thank you for the PR; I was aware of this problem as well and wanted to introduce a Python API at some point. So it's great that you took the time to implement this!

At the moment, I do not have time to review your PR, but I will get to it eventually. Just wanted to let you know that I am aware of your PR.

@kklemon
Copy link
Author

kklemon commented Jan 11, 2021

Thank you for the reply. I'm currently not 100% satisfied with my proposed approach and hence might a few more changes in the future.

@kklemon kklemon changed the title Allow easier usage from code Python API Feb 5, 2021
@murnanedaniel
Copy link

Any chance of this being merged? Or possibly just a recommendation on how to use the library as a Python API?

@kklemon
Copy link
Author

kklemon commented Feb 7, 2022

@murnanedaniel you can either use my fork of this repository, but which also hasn't been updated in a while and may no longer work or look for other implementations with a better API. https://github.com/w86763777/pytorch-gan-metrics seems to do a good job, for example.

@charliebudd
Copy link

charliebudd commented May 24, 2022

I just went away an implemented this and was going to open a PR when I saw this, guess I should have checked the branches!

I also moved to an object orientated aproach so I generaly approve of the approach, allthough I would have the model instatiated within the class just to simplify the end users code. An optional override could be supplied for the model but I imagine the vast majority of users would use the default.

Edit:
Just checked out the link above. I think I'll just move to this implementation.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants