Inception Score&Mode Score

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.
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值