Compute FID score between generated samples and real data. §4 — "We report FID score... Our best results are FID: 3.17" Requires the pytorch-fid package: pip install pytorch-fid For CIFAR-10, you need pre-computed stats for the real training set, or provide a directory of real ima
(
generated_dir: str,
real_stats_path: Optional[str] = None,
batch_size: int = 50,
device: str = "cuda",
dims: int = 2048,
)
| 164 | |
| 165 | |
| 166 | def compute_fid( |
| 167 | generated_dir: str, |
| 168 | real_stats_path: Optional[str] = None, |
| 169 | batch_size: int = 50, |
| 170 | device: str = "cuda", |
| 171 | dims: int = 2048, |
| 172 | ) -> float: |
| 173 | """Compute FID score between generated samples and real data. |
| 174 | |
| 175 | §4 — "We report FID score... Our best results are FID: 3.17" |
| 176 | |
| 177 | Requires the pytorch-fid package: pip install pytorch-fid |
| 178 | |
| 179 | For CIFAR-10, you need pre-computed stats for the real training set, |
| 180 | or provide a directory of real images. |
| 181 | |
| 182 | Args: |
| 183 | generated_dir: Directory containing generated .png images |
| 184 | real_stats_path: Path to pre-computed .npz stats for real data, |
| 185 | OR directory containing real images |
| 186 | batch_size: Batch size for Inception feature extraction |
| 187 | device: Device for computation |
| 188 | dims: Inception feature dimensionality (2048 = pool3) |
| 189 | |
| 190 | Returns: |
| 191 | FID score (float). Lower is better. |
| 192 | """ |
| 193 | try: |
| 194 | from pytorch_fid import fid_score |
| 195 | except ImportError: |
| 196 | logger.error( |
| 197 | "pytorch-fid not installed. Install with: pip install pytorch-fid\n" |
| 198 | "Then re-run evaluation." |
| 199 | ) |
| 200 | raise |
| 201 | |
| 202 | if real_stats_path is None: |
| 203 | raise ValueError( |
| 204 | "Must provide real_stats_path: either a .npz file with pre-computed " |
| 205 | "Inception statistics, or a directory of real CIFAR-10 images." |
| 206 | ) |
| 207 | |
| 208 | fid = fid_score.calculate_fid_given_paths( |
| 209 | [generated_dir, real_stats_path], |
| 210 | batch_size=batch_size, |
| 211 | device=torch.device(device), |
| 212 | dims=dims, |
| 213 | ) |
| 214 | |
| 215 | logger.info(f"FID score: {fid:.2f}") |
| 216 | return fid |
| 217 | |
| 218 | |
| 219 | if __name__ == "__main__": |