simet.metrics.fid¶
simet.metrics.fid ¶
FID ¶
FID(base_eps=1e-06)
Bases: Metric[float]
Frechet Inception Distance computed from pre-extracted features.
Expects 2D feature arrays for real and synthetic datasets with the
same dimensionality (D). Computes per-set mean and unbiased covariance,
then evaluates:
FID = ||μ_r − μ_s||² + Tr(Σ_r + Σ_s − 2·(Σ_r^{1/2} Σ_s Σ_r^{1/2})^{1/2})
Numerical behavior
- Uses double precision (
float64) for statistics by default. - Runs linear algebra on CUDA if available; otherwise on CPU.
- Adds a small diagonal (
base_eps) to covariance matrices. - Tries a pure-PyTorch eigen/sqrt path with escalating eps; falls back
to SciPy’s
sqrtmif needed.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
base_eps
|
float, default=1e-6
|
Diagonal regularization added to covariance matrices. Also used as a minimum eigenvalue floor. |
1e-06
|
Attributes:
| Name | Type | Description |
|---|---|---|
_LINALG_DEVICE |
device
|
Device for linear algebra ops. |
_STAT_DTYPE |
dtype
|
Dtype for statistics (default: float64). |
_BASE_EPS |
float
|
Stabilizing epsilon for covariances/eigenvalues. |
Raises:
| Type | Description |
|---|---|
ValueError
|
If feature arrays are not 2D, have mismatched dims, or have fewer than two samples (unbiased covariance requires n≥2). |
Source code in simet/metrics/fid.py
43 44 45 46 47 48 49 50 | |
compute ¶
compute(loader)
Compute FID from loader.real_features and loader.synth_features.
Steps
1) Move features to _LINALG_DEVICE in _STAT_DTYPE.
2) Validate shapes: both 2D and same feature dimension.
3) Compute (μ, Σ) for real and synthetic sets.
4) Compute the sandwich covariance square root and final FID.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
loader
|
DatasetLoader
|
Must expose |
required |
Returns:
| Name | Type | Description |
|---|---|---|
float |
float
|
Non-negative FID score (lower is better). |
Raises:
| Type | Description |
|---|---|
ValueError
|
If shapes are invalid or sample counts are < 2. |
Source code in simet/metrics/fid.py
58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 | |