pytorch inception score
时间: 2023-06-21 18:07:18 浏览: 107
pytorch-FID计算
Inception Score is a measure of the quality of generated images. It was proposed in the paper "Improved Techniques for Training GANs" by Salimans et al. The score is computed by passing generated images through a pre-trained Inception v3 network and computing the KL divergence between the conditional label distribution (i.e., the predicted class probabilities) and the marginal label distribution (i.e., the class probabilities of the training set). A higher score indicates that the generated images have better quality and diversity.
Here's an implementation of Inception Score in PyTorch:
```python
import torch
import torch.nn as nn
import torchvision.models as models
from torch.utils.data import DataLoader
def inception_score(images, batch_size=32, resize=True):
"""Computes the Inception Score of the generated images."""
# Load the Inception v3 model
inception = models.inception_v3(pretrained=True, transform_input=False).cuda()
inception.eval()
# Resize and normalize the images
if resize:
images = nn.functional.interpolate(images, size=(299, 299), mode='bilinear', align_corners=False)
images = (images - 0.5) / 0.5
# Compute the conditional label distribution
preds = []
with torch.no_grad():
for i in range(0, len(images), batch_size):
batch = images[i:i+batch_size].cuda()
pred = inception(batch)
preds.append(pred)
preds = torch.cat(preds, dim=0)
py = torch.softmax(preds, dim=1)
# Compute the marginal label distribution
py = py.mean(dim=0, keepdim=True)
kl = (py * (torch.log(py) - torch.log(torch.tensor(1.0/py.shape[1])))).sum().item()
# Compute the Inception Score
iscore = torch.exp(kl).item()
return iscore
```
The input `images` should be a PyTorch tensor of shape `(N, C, H, W)` representing `N` generated images with `C` channels, height `H` and width `W`. The function returns a scalar value representing the Inception Score.
阅读全文