|
| 1 | +#!/usr/bin/env python3 |
| 2 | +"""Calculates the Frechet Inception Distance (FID) to evalulate GANs |
| 3 | +
|
| 4 | +The FID metric calculates the distance between two distributions of images. |
| 5 | +Typically, we have summary statistics (mean & covariance matrix) of one |
| 6 | +of these distributions, while the 2nd distribution is given by a GAN. |
| 7 | +
|
| 8 | +When run as a stand-alone program, it compares the distribution of |
| 9 | +images that are stored as PNG/JPEG at a specified location with a |
| 10 | +distribution given by summary statistics (in pickle format). |
| 11 | +
|
| 12 | +The FID is calculated by assuming that X_1 and X_2 are the activations of |
| 13 | +the pool_3 layer of the inception net for generated samples and real world |
| 14 | +samples respectivly. |
| 15 | +
|
| 16 | +See --help to see further details. |
| 17 | +
|
| 18 | +Code apapted from https://github.com/bioinf-jku/TTUR to use PyTorch instead |
| 19 | +of Tensorflow |
| 20 | +
|
| 21 | +Copyright 2018 Institute of Bioinformatics, JKU Linz |
| 22 | +
|
| 23 | +Licensed under the Apache License, Version 2.0 (the "License"); |
| 24 | +you may not use this file except in compliance with the License. |
| 25 | +You may obtain a copy of the License at |
| 26 | +
|
| 27 | + http://www.apache.org/licenses/LICENSE-2.0 |
| 28 | +
|
| 29 | +Unless required by applicable law or agreed to in writing, software |
| 30 | +distributed under the License is distributed on an "AS IS" BASIS, |
| 31 | +WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 32 | +See the License for the specific language governing permissions and |
| 33 | +limitations under the License. |
| 34 | +""" |
| 35 | +import os |
| 36 | +import pathlib |
| 37 | +from argparse import ArgumentParser, ArgumentDefaultsHelpFormatter |
| 38 | + |
| 39 | +import torch |
| 40 | +import numpy as np |
| 41 | +from scipy.misc import imread |
| 42 | +from scipy import linalg |
| 43 | +from torch.autograd import Variable |
| 44 | +from torch.nn.functional import adaptive_avg_pool2d |
| 45 | + |
| 46 | +from inception import InceptionV3 |
| 47 | + |
| 48 | + |
| 49 | +parser = ArgumentParser(formatter_class=ArgumentDefaultsHelpFormatter) |
| 50 | +parser.add_argument('path', type=str, nargs=2, |
| 51 | + help=('Path to the generated images or ' |
| 52 | + 'to .npz statistic files')) |
| 53 | +parser.add_argument('--batch-size', type=int, default=64, |
| 54 | + help='Batch size to use') |
| 55 | +parser.add_argument('--dims', type=int, default=2048, |
| 56 | + choices=list(InceptionV3.BLOCK_INDEX_BY_DIM), |
| 57 | + help=('Dimensionality of Inception features to use. ' |
| 58 | + 'By default, uses pool3 features')) |
| 59 | +parser.add_argument('-c', '--gpu', default='', type=str, |
| 60 | + help='GPU to use (leave blank for CPU only)') |
| 61 | + |
| 62 | + |
| 63 | +def get_activations(images, model, batch_size=64, dims=2048, |
| 64 | + cuda=False, verbose=False): |
| 65 | + """Calculates the activations of the pool_3 layer for all images. |
| 66 | +
|
| 67 | + Params: |
| 68 | + -- images : Numpy array of dimension (n_images, 3, hi, wi). The values |
| 69 | + must lie between 0 and 1. |
| 70 | + -- model : Instance of inception model |
| 71 | + -- batch_size : the images numpy array is split into batches with |
| 72 | + batch size batch_size. A reasonable batch size depends |
| 73 | + on the hardware. |
| 74 | + -- dims : Dimensionality of features returned by Inception |
| 75 | + -- cuda : If set to True, use GPU |
| 76 | + -- verbose : If set to True and parameter out_step is given, the number |
| 77 | + of calculated batches is reported. |
| 78 | + Returns: |
| 79 | + -- A numpy array of dimension (num images, dims) that contains the |
| 80 | + activations of the given tensor when feeding inception with the |
| 81 | + query tensor. |
| 82 | + """ |
| 83 | + model.eval() |
| 84 | + |
| 85 | + d0 = images.shape[0] |
| 86 | + if batch_size > d0: |
| 87 | + print(('Warning: batch size is bigger than the data size. ' |
| 88 | + 'Setting batch size to data size')) |
| 89 | + batch_size = d0 |
| 90 | + |
| 91 | + n_batches = d0 // batch_size |
| 92 | + n_used_imgs = n_batches * batch_size |
| 93 | + |
| 94 | + pred_arr = np.empty((n_used_imgs, dims)) |
| 95 | + for i in range(n_batches): |
| 96 | + if verbose: |
| 97 | + print('\rPropagating batch %d/%d' % (i + 1, n_batches), |
| 98 | + end='', flush=True) |
| 99 | + start = i * batch_size |
| 100 | + end = start + batch_size |
| 101 | + |
| 102 | + batch = torch.from_numpy(images[start:end]).type(torch.FloatTensor) |
| 103 | + batch = Variable(batch, volatile=True) |
| 104 | + if cuda: |
| 105 | + batch = batch.cuda() |
| 106 | + |
| 107 | + pred = model(batch)[0] |
| 108 | + |
| 109 | + # If model output is not scalar, apply global spatial average pooling. |
| 110 | + # This happens if you choose a dimensionality not equal 2048. |
| 111 | + if pred.shape[2] != 1 or pred.shape[3] != 1: |
| 112 | + pred = adaptive_avg_pool2d(pred, output_size=(1, 1)) |
| 113 | + |
| 114 | + pred_arr[start:end] = pred.cpu().data.numpy().reshape(batch_size, -1) |
| 115 | + |
| 116 | + if verbose: |
| 117 | + print(' done') |
| 118 | + |
| 119 | + return pred_arr |
| 120 | + |
| 121 | + |
| 122 | +def calculate_frechet_distance(mu1, sigma1, mu2, sigma2, eps=1e-6): |
| 123 | + """Numpy implementation of the Frechet Distance. |
| 124 | + The Frechet distance between two multivariate Gaussians X_1 ~ N(mu_1, C_1) |
| 125 | + and X_2 ~ N(mu_2, C_2) is |
| 126 | + d^2 = ||mu_1 - mu_2||^2 + Tr(C_1 + C_2 - 2*sqrt(C_1*C_2)). |
| 127 | +
|
| 128 | + Stable version by Dougal J. Sutherland. |
| 129 | +
|
| 130 | + Params: |
| 131 | + -- mu1 : Numpy array containing the activations of a layer of the |
| 132 | + inception net (like returned by the function 'get_predictions') |
| 133 | + for generated samples. |
| 134 | + -- mu2 : The sample mean over activations, precalculated on an |
| 135 | + representive data set. |
| 136 | + -- sigma1: The covariance matrix over activations for generated samples. |
| 137 | + -- sigma2: The covariance matrix over activations, precalculated on an |
| 138 | + representive data set. |
| 139 | +
|
| 140 | + Returns: |
| 141 | + -- : The Frechet Distance. |
| 142 | + """ |
| 143 | + |
| 144 | + mu1 = np.atleast_1d(mu1) |
| 145 | + mu2 = np.atleast_1d(mu2) |
| 146 | + |
| 147 | + sigma1 = np.atleast_2d(sigma1) |
| 148 | + sigma2 = np.atleast_2d(sigma2) |
| 149 | + |
| 150 | + assert mu1.shape == mu2.shape, \ |
| 151 | + 'Training and test mean vectors have different lengths' |
| 152 | + assert sigma1.shape == sigma2.shape, \ |
| 153 | + 'Training and test covariances have different dimensions' |
| 154 | + |
| 155 | + diff = mu1 - mu2 |
| 156 | + |
| 157 | + # Product might be almost singular |
| 158 | + covmean, _ = linalg.sqrtm(sigma1.dot(sigma2), disp=False) |
| 159 | + if not np.isfinite(covmean).all(): |
| 160 | + msg = ('fid calculation produces singular product; ' |
| 161 | + 'adding %s to diagonal of cov estimates') % eps |
| 162 | + print(msg) |
| 163 | + offset = np.eye(sigma1.shape[0]) * eps |
| 164 | + covmean = linalg.sqrtm((sigma1 + offset).dot(sigma2 + offset)) |
| 165 | + |
| 166 | + # Numerical error might give slight imaginary component |
| 167 | + if np.iscomplexobj(covmean): |
| 168 | + if not np.allclose(np.diagonal(covmean).imag, 0, atol=1e-3): |
| 169 | + m = np.max(np.abs(covmean.imag)) |
| 170 | + raise ValueError('Imaginary component {}'.format(m)) |
| 171 | + covmean = covmean.real |
| 172 | + |
| 173 | + tr_covmean = np.trace(covmean) |
| 174 | + |
| 175 | + return (diff.dot(diff) + np.trace(sigma1) + |
| 176 | + np.trace(sigma2) - 2 * tr_covmean) |
| 177 | + |
| 178 | + |
| 179 | +def calculate_activation_statistics(images, model, batch_size=64, |
| 180 | + dims=2048, cuda=False, verbose=False): |
| 181 | + """Calculation of the statistics used by the FID. |
| 182 | + Params: |
| 183 | + -- images : Numpy array of dimension (n_images, 3, hi, wi). The values |
| 184 | + must lie between 0 and 1. |
| 185 | + -- model : Instance of inception model |
| 186 | + -- batch_size : The images numpy array is split into batches with |
| 187 | + batch size batch_size. A reasonable batch size |
| 188 | + depends on the hardware. |
| 189 | + -- dims : Dimensionality of features returned by Inception |
| 190 | + -- cuda : If set to True, use GPU |
| 191 | + -- verbose : If set to True and parameter out_step is given, the |
| 192 | + number of calculated batches is reported. |
| 193 | + Returns: |
| 194 | + -- mu : The mean over samples of the activations of the pool_3 layer of |
| 195 | + the inception model. |
| 196 | + -- sigma : The covariance matrix of the activations of the pool_3 layer of |
| 197 | + the inception model. |
| 198 | + """ |
| 199 | + act = get_activations(images, model, batch_size, dims, cuda, verbose) |
| 200 | + mu = np.mean(act, axis=0) |
| 201 | + sigma = np.cov(act, rowvar=False) |
| 202 | + return mu, sigma |
| 203 | + |
| 204 | + |
| 205 | +def _compute_statistics_of_path(path, model, batch_size, dims, cuda): |
| 206 | + if path.endswith('.npz'): |
| 207 | + f = np.load(path) |
| 208 | + m, s = f['mu'][:], f['sigma'][:] |
| 209 | + f.close() |
| 210 | + else: |
| 211 | + path = pathlib.Path(path) |
| 212 | + files = list(path.glob('*.jpg')) + list(path.glob('*.png')) |
| 213 | + |
| 214 | + imgs = np.array([imread(str(fn)).astype(np.float32) for fn in files]) |
| 215 | + |
| 216 | + # Bring images to shape (B, 3, H, W) |
| 217 | + imgs = imgs.transpose((0, 3, 1, 2)) |
| 218 | + |
| 219 | + # Rescale images to be between 0 and 1 |
| 220 | + imgs /= 255 |
| 221 | + |
| 222 | + m, s = calculate_activation_statistics(imgs, model, batch_size, |
| 223 | + dims, cuda) |
| 224 | + |
| 225 | + return m, s |
| 226 | + |
| 227 | + |
| 228 | +def calculate_fid_given_paths(paths, batch_size, cuda, dims): |
| 229 | + """Calculates the FID of two paths""" |
| 230 | + for p in paths: |
| 231 | + if not os.path.exists(p): |
| 232 | + raise RuntimeError('Invalid path: %s' % p) |
| 233 | + |
| 234 | + block_idx = InceptionV3.BLOCK_INDEX_BY_DIM[dims] |
| 235 | + |
| 236 | + model = InceptionV3([block_idx]) |
| 237 | + if cuda: |
| 238 | + model.cuda() |
| 239 | + |
| 240 | + m1, s1 = _compute_statistics_of_path(paths[0], model, batch_size, |
| 241 | + dims, cuda) |
| 242 | + m2, s2 = _compute_statistics_of_path(paths[1], model, batch_size, |
| 243 | + dims, cuda) |
| 244 | + fid_value = calculate_frechet_distance(m1, s1, m2, s2) |
| 245 | + |
| 246 | + return fid_value |
| 247 | + |
| 248 | + |
| 249 | +if __name__ == '__main__': |
| 250 | + args = parser.parse_args() |
| 251 | + os.environ['CUDA_VISIBLE_DEVICES'] = args.gpu |
| 252 | + |
| 253 | + fid_value = calculate_fid_given_paths(args.path, |
| 254 | + args.batch_size, |
| 255 | + args.gpu != '', |
| 256 | + args.dims) |
| 257 | + print('FID: ', fid_value) |
0 commit comments