MCPcopy Create free account
hub / github.com/CompVis/zigma / compute

Method compute

utils/torchmetric_kid.py:247–273  ·  view source on GitHub ↗

Calculate KID score based on accumulated extracted features from the two distributions. Returns a tuple of mean and standard deviation of KID scores calculated on subsets of extracted features. Implementation inspired by `Fid Score`_

(self)

Source from the content-addressed store, hash-verified

245 self.fake_features.append(features)
246
247 def compute(self) -> Tuple[Tensor, Tensor]:
248 """Calculate KID score based on accumulated extracted features from the two distributions. Returns a tuple
249 of mean and standard deviation of KID scores calculated on subsets of extracted features.
250
251 Implementation inspired by `Fid Score`_
252 """
253 real_features = dim_zero_cat(self.real_features)
254 fake_features = dim_zero_cat(self.fake_features)
255
256 n_samples_real = real_features.shape[0]
257 if n_samples_real < self.subset_size:
258 raise ValueError("Argument `subset_size` should be smaller than the number of samples")
259 n_samples_fake = fake_features.shape[0]
260 if n_samples_fake < self.subset_size:
261 raise ValueError("Argument `subset_size` should be smaller than the number of samples")
262
263 kid_scores_ = []
264 for _ in range(self.subsets):
265 perm = torch.randperm(n_samples_real)
266 f_real = real_features[perm[: self.subset_size]]
267 perm = torch.randperm(n_samples_fake)
268 f_fake = fake_features[perm[: self.subset_size]]
269
270 o = poly_mmd(f_real, f_fake, self.degree, self.gamma, self.coef)
271 kid_scores_.append(o)
272 kid_scores = torch.stack(kid_scores_)
273 return kid_scores.mean(), kid_scores.std(unbiased=False)
274
275 def reset(self) -> None:
276 if not self.reset_real_features:

Callers

nothing calls this directly

Calls 1

poly_mmdFunction · 0.85

Tested by

no test coverage detected