FID代码pytorch环境下的
时间: 2023-09-21 18:13:35 浏览: 89
pytorch-FID计算
FID(Fréchet Inception Distance)是一种用于评估生成对抗网络(GAN)生成图像质量的指标。以下是在PyTorch环境下计算FID的示例代码:
```python
import torch
from torch.nn.functional import adaptive_avg_pool2d
from torchvision.models import inception_v3
import numpy as np
from scipy import linalg
def get_activations(images, model, batch_size=50, dims=2048):
"""计算图像集的Inception-v3特征向量"""
model.eval()
# 激活值列表
act = np.zeros((images.shape[0], dims))
# 加载批次
for i in range(0, images.shape[0], batch_size):
batch = torch.from_numpy(images[i:i+batch_size]).type(torch.FloatTensor)
batch = batch.cuda()
with torch.no_grad():
pred = model(batch)[0]
# pool到一个维度
pred = adaptive_avg_pool2d(pred, output_size=(1, 1)).squeeze(dim=2).squeeze(dim=2).cpu().numpy()
act[i:i+batch_size] = pred
return act
def calculate_frechet_distance(mu1, sigma1, mu2, sigma2, eps=1e-6):
"""计算两个高斯分布之间的FID"""
mu1 = np.atleast_1d(mu1)
mu2 = np.atleast_1d(mu2)
sigma1 = np.atleast_2d(sigma1)
sigma2 = np.atleast_2d(sigma2)
assert mu1.shape == mu2.shape, 'mu1和mu2的形状不同'
assert sigma1.shape == sigma2.shape, 'sigma1和sigma2的形状不同'
diff = mu1 - mu2
# sqrtm并不总是稳定,所以需要尝试/捕捉异常
try:
sqrtm = linalg.sqrtm(np.dot(sigma1, sigma2))
except:
print('FID计算过程中出现奇异值;添加eps以提高数值稳定性')
offset = np.eye(sigma1.shape[0]) * eps
sqrtm = linalg.sqrtm(np.dot(sigma1 + offset, sigma2 + offset))
# 检查sqrtm是否为虚数
if np.iscomplexobj(sqrtm):
if not np.allclose(np.diagonal(sqrtm).imag, 0, atol=1e-3):
m = np.max(np.abs(sqrtm.imag))
raise ValueError('Imaginary component {}'.format(m))
sqrtm = sqrtm.real
return diff.dot(diff) + np.trace(sigma1) + np.trace(sigma2) - 2 * np.trace(sqrtm)
def calculate_fretchet(images_real, images_fake, batch_size):
"""计算真实和生成图像之间的FID"""
block_idx = InceptionV3.BLOCK_INDEX_BY_DIM[2048]
model = InceptionV3([block_idx])
# 计算真实图像的激活值
act_real = get_activations(images_real, model, batch_size)
# 计算生成图像的激活值
act_fake = get_activations(images_fake, model, batch_size)
# 计算真实图像的mu和sigma
mu_real = np.mean(act_real, axis=0)
sigma_real = np.cov(act_real, rowvar=False)
# 计算生成图像的mu和sigma
mu_fake = np.mean(act_fake, axis=0)
sigma_fake = np.cov(act_fake, rowvar=False)
# 计算FID
fid = calculate_frechet_distance(mu_real, sigma_real, mu_fake, sigma_fake)
return fid
```
要使用此代码,您需要安装PyTorch和torchvision。然后,您可以将真实和生成的图像作为Numpy数组传递给`calculate_fretchet`函数,并指定批处理大小。函数将返回FID分数。
阅读全文