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

Fix precision in calculation of FID (#192) #211

Open
wants to merge 2 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion tests/metrics/image/test_fid.py
Original file line number Diff line number Diff line change
Expand Up @@ -78,7 +78,7 @@ def test_fid_random_data_default_model(self) -> None:
299,
)
self._test_fid(
imgs=imgs, feature_dim=2048, expected_result=torch.tensor(4.48304)
imgs=imgs, feature_dim=2048, expected_result=torch.tensor(4.58449)
)

def test_fid_random_data_custom_model(self) -> None:
Expand Down
15 changes: 11 additions & 4 deletions torcheval/metrics/image/fid.py
Original file line number Diff line number Diff line change
Expand Up @@ -95,13 +95,19 @@ def __init__(
self.model.requires_grad_(False)

# Initialize state variables used to compute FID
self._add_state("real_sum", torch.zeros(feature_dim, device=device))
self._add_state(
"real_cov_sum", torch.zeros((feature_dim, feature_dim), device=device)
"real_sum", torch.zeros(feature_dim, device=device, dtype=torch.float64)
)
self._add_state("fake_sum", torch.zeros(feature_dim, device=device))
self._add_state(
"fake_cov_sum", torch.zeros((feature_dim, feature_dim), device=device)
"real_cov_sum",
torch.zeros((feature_dim, feature_dim), device=device, dtype=torch.float64),
)
self._add_state(
"fake_sum", torch.zeros(feature_dim, device=device, dtype=torch.float64)
)
self._add_state(
"fake_cov_sum",
torch.zeros((feature_dim, feature_dim), device=device, dtype=torch.float64),
)
self._add_state("num_real_images", torch.tensor(0, device=device).int())
self._add_state("num_fake_images", torch.tensor(0, device=device).int())
Expand Down Expand Up @@ -200,6 +206,7 @@ def compute(self: TFrechetInceptionDistance) -> Tensor:
fid = gaussian_frechet_distance(
real_mean.squeeze(), real_cov, fake_mean.squeeze(), fake_cov
)
fid = fid.to(torch.float32)
return fid

def _FID_parameter_check(
Expand Down