-
Notifications
You must be signed in to change notification settings - Fork 62
/
metric.py
429 lines (358 loc) · 15.1 KB
/
metric.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
import math
import os
import timeit
import math
import numpy as np
import ot
import torch
from torch import nn
import torch.nn.functional as F
import torchvision.datasets as dset
import torchvision.transforms as transforms
import torchvision.utils as vutils
import torchvision.models as models
import pdb
from tqdm import tqdm
from scipy.stats import entropy
from numpy.linalg import norm
from scipy import linalg
def giveName(iter): # 7 digit name.
ans = str(iter)
return ans.zfill(7)
def make_dataset(dataset, dataroot, imageSize):
"""
:param dataset: must be in 'cifar10 | lsun | imagenet | folder | lfw | fake'
:return: pytorch dataset for DataLoader to utilize
"""
if dataset in ['imagenet', 'folder', 'lfw']:
# folder dataset
dataset = dset.ImageFolder(root=dataroot,
transform=transforms.Compose([
transforms.Resize(imageSize),
transforms.CenterCrop(imageSize),
transforms.ToTensor(),
transforms.Normalize(
(0.5, 0.5, 0.5), (0.5, 0.5, 0.5)),
]))
elif dataset == 'lsun':
dataset = dset.LSUN(db_path=dataroot, classes=['bedroom_train'],
transform=transforms.Compose([
transforms.Resize(imageSize),
transforms.CenterCrop(imageSize),
transforms.ToTensor(),
transforms.Normalize(
(0.5, 0.5, 0.5), (0.5, 0.5, 0.5)),
]))
elif dataset == 'cifar10':
dataset = dset.CIFAR10(root=dataroot, download=True,
transform=transforms.Compose([
transforms.Resize(imageSize),
transforms.ToTensor(),
transforms.Normalize(
(0.5, 0.5, 0.5), (0.5, 0.5, 0.5)),
]))
elif dataset == 'celeba':
dataset = dset.ImageFolder(root=dataroot,
transform=transforms.Compose([
transforms.CenterCrop(138),
transforms.Resize(imageSize),
transforms.ToTensor(),
transforms.Normalize(
(0.5, 0.5, 0.5), (0.5, 0.5, 0.5)),
]))
else:
raise Exception('--dataset must be in cifar10 | lsun | imagenet | folder | lfw | fake')
assert dataset
return dataset
def sampleFake(netG, nz, sampleSize, batchSize, saveFolder):
print('sampling fake images ...')
saveFolder = saveFolder + '0/'
try:
os.makedirs(saveFolder)
except OSError:
pass
noise = torch.FloatTensor(batchSize, nz, 1, 1).cuda()
iter = 0
for i in range(0, 1 + sampleSize // batchSize):
noise.data.normal_(0, 1)
fake = netG(noise)
for j in range(0, len(fake.data)):
if iter < sampleSize:
vutils.save_image(fake.data[j].mul(0.5).add(
0.5), saveFolder + giveName(iter) + ".png")
iter += 1
if iter >= sampleSize:
break
def sampleTrue(dataset, imageSize, dataroot, sampleSize, batchSize, saveFolder, workers=4):
print('sampling real images ...')
saveFolder = saveFolder + '0/'
dataset = make_dataset(dataset, dataroot, imageSize)
dataloader = torch.utils.data.DataLoader(
dataset, shuffle=True, batch_size=batchSize, num_workers=int(workers))
if not os.path.exists(saveFolder):
try:
os.makedirs(saveFolder)
except OSError:
pass
iter = 0
for i, data in enumerate(dataloader, 0):
img, _ = data
for j in range(0, len(img)):
vutils.save_image(img[j].mul(0.5).add(
0.5), saveFolder + giveName(iter) + ".png")
iter += 1
if iter >= sampleSize:
break
if iter >= sampleSize:
break
class ConvNetFeatureSaver(object):
def __init__(self, model='resnet34', workers=4, batchSize=64):
'''
model: inception_v3, vgg13, vgg16, vgg19, resnet18, resnet34,
resnet50, resnet101, or resnet152
'''
self.model = model
self.batch_size = batchSize
self.workers = workers
if self.model.find('vgg') >= 0:
self.vgg = getattr(models, model)(pretrained=True).cuda().eval()
self.trans = transforms.Compose([
transforms.Resize(224),
transforms.ToTensor(),
transforms.Normalize((0.485, 0.456, 0.406),
(0.229, 0.224, 0.225)),
])
elif self.model.find('resnet') >= 0:
resnet = getattr(models, model)(pretrained=True)
resnet.cuda().eval()
resnet_feature = nn.Sequential(resnet.conv1, resnet.bn1,
resnet.relu,
resnet.maxpool, resnet.layer1,
resnet.layer2, resnet.layer3,
resnet.layer4).cuda().eval()
self.resnet = resnet
self.resnet_feature = resnet_feature
self.trans = transforms.Compose([
transforms.Resize(224),
transforms.ToTensor(),
transforms.Normalize((0.485, 0.456, 0.406),
(0.229, 0.224, 0.225)),
])
elif self.model == 'inception' or self.model == 'inception_v3':
inception = models.inception_v3(
pretrained=True, transform_input=False).cuda().eval()
inception_feature = nn.Sequential(inception.Conv2d_1a_3x3,
inception.Conv2d_2a_3x3,
inception.Conv2d_2b_3x3,
nn.MaxPool2d(3, 2),
inception.Conv2d_3b_1x1,
inception.Conv2d_4a_3x3,
nn.MaxPool2d(3, 2),
inception.Mixed_5b,
inception.Mixed_5c,
inception.Mixed_5d,
inception.Mixed_6a,
inception.Mixed_6b,
inception.Mixed_6c,
inception.Mixed_6d,
inception.Mixed_7a,
inception.Mixed_7b,
inception.Mixed_7c,
).cuda().eval()
self.inception = inception
self.inception_feature = inception_feature
self.trans = transforms.Compose([
transforms.Resize(299),
transforms.ToTensor(),
transforms.Normalize((0.5, 0.5, 0.5), (0.5, 0.5, 0.5)),
])
else:
raise NotImplementedError
def save(self, imgFolder, save2disk=False):
dataset = dset.ImageFolder(root=imgFolder, transform=self.trans)
dataloader = torch.utils.data.DataLoader(
dataset, batch_size=self.batch_size, num_workers=self.workers)
print('extracting features...')
feature_pixl, feature_conv, feature_smax, feature_logit = [], [], [], []
for img, _ in tqdm(dataloader):
with torch.no_grad():
input = img.cuda()
if self.model == 'vgg' or self.model == 'vgg16':
fconv = self.vgg.features(input).view(input.size(0), -1)
flogit = self.vgg.classifier(fconv)
# flogit = self.vgg.logitifier(fconv)
elif self.model.find('resnet') >= 0:
fconv = self.resnet_feature(
input).mean(3).mean(2).squeeze()
flogit = self.resnet.fc(fconv)
elif self.model == 'inception' or self.model == 'inception_v3':
fconv = self.inception_feature(
input).mean(3).mean(2).squeeze()
flogit = self.inception.fc(fconv)
else:
raise NotImplementedError
fsmax = F.softmax(flogit)
feature_pixl.append(img)
feature_conv.append(fconv.data.cpu())
feature_logit.append(flogit.data.cpu())
feature_smax.append(fsmax.data.cpu())
feature_pixl = torch.cat(feature_pixl, 0).to('cpu')
feature_conv = torch.cat(feature_conv, 0).to('cpu')
feature_logit = torch.cat(feature_logit, 0).to('cpu')
feature_smax = torch.cat(feature_smax, 0).to('cpu')
if save2disk:
torch.save(feature_conv, os.path.join(
imgFolder, 'feature_pixl.pth'))
torch.save(feature_conv, os.path.join(
imgFolder, 'feature_conv.pth'))
torch.save(feature_logit, os.path.join(
imgFolder, 'feature_logit.pth'))
torch.save(feature_smax, os.path.join(
imgFolder, 'feature_smax.pth'))
return feature_pixl, feature_conv, feature_logit, feature_smax
def distance(X, Y, sqrt):
nX = X.size(0)
nY = Y.size(0)
X = X.view(nX,-1)
X2 = (X*X).sum(1).resize_(nX,1)
Y = Y.view(nY,-1)
Y2 = (Y*Y).sum(1).resize_(nY,1)
M = torch.zeros(nX, nY)
M.copy_(X2.expand(nX, nY) + Y2.expand(nY, nX).transpose(0, 1) -
2 * torch.mm(X, Y.transpose(0, 1)))
del X, X2, Y, Y2
if sqrt:
M = ((M + M.abs()) / 2).sqrt()
return M
def wasserstein(M, sqrt):
if sqrt:
M = M.abs().sqrt()
emd = ot.emd2([], [], M.numpy())
return emd
class Score_knn:
acc = 0
acc_real = 0
acc_fake = 0
precision = 0
recall = 0
tp = 0
fp = 0
fn = 0
tn = 0
def knn(Mxx, Mxy, Myy, k, sqrt):
n0 = Mxx.size(0)
n1 = Myy.size(0)
label = torch.cat((torch.ones(n0), torch.zeros(n1)))
M = torch.cat((torch.cat((Mxx, Mxy), 1), torch.cat(
(Mxy.transpose(0, 1), Myy), 1)), 0)
if sqrt:
M = M.abs().sqrt()
INFINITY = float('inf')
val, idx = (M + torch.diag(INFINITY * torch.ones(n0 + n1))
).topk(k, 0, False)
count = torch.zeros(n0 + n1)
for i in range(0, k):
count = count + label.index_select(0, idx[i])
pred = torch.ge(count, (float(k) / 2) * torch.ones(n0 + n1)).float()
s = Score_knn()
s.tp = (pred * label).sum()
s.fp = (pred * (1 - label)).sum()
s.fn = ((1 - pred) * label).sum()
s.tn = ((1 - pred) * (1 - label)).sum()
s.precision = s.tp / (s.tp + s.fp + 1e-10)
s.recall = s.tp / (s.tp + s.fn + 1e-10)
s.acc_real = s.tp / (s.tp + s.fn)
s.acc_fake = s.tn / (s.tn + s.fp)
s.acc = torch.eq(label, pred).float().mean()
s.k = k
return s
def mmd(Mxx, Mxy, Myy, sigma):
scale = Mxx.mean()
Mxx = torch.exp(-Mxx / (scale * 2 * sigma * sigma))
Mxy = torch.exp(-Mxy / (scale * 2 * sigma * sigma))
Myy = torch.exp(-Myy / (scale * 2 * sigma * sigma))
mmd = math.sqrt(Mxx.mean() + Myy.mean() - 2 * Mxy.mean())
return mmd
def entropy_score(X, Y, epsilons):
Mxy = distance(X, Y, False)
scores = []
for epsilon in epsilons:
scores.append(ent(Mxy.t(), epsilon))
return scores
def ent(M, epsilon):
n0 = M.size(0)
n1 = M.size(1)
neighbors = M.lt(epsilon).float()
sums = neighbors.sum(0).repeat(n0, 1)
sums[sums.eq(0)] = 1
neighbors = neighbors.div(sums)
probs = neighbors.sum(1) / n1
rem = 1 - probs.sum()
if rem < 0:
rem = 0
probs = torch.cat((probs, rem*torch.ones(1)), 0)
e = {}
e['probs'] = probs
probs = probs[probs.gt(0)]
e['ent'] = -probs.mul(probs.log()).sum()
return e
eps = 1e-20
def inception_score(X):
kl = X * ((X+eps).log()-(X.mean(0)+eps).log().expand_as(X))
score = np.exp(kl.sum(1).mean())
return score
def mode_score(X, Y):
kl1 = X * ((X+eps).log()-(X.mean(0)+eps).log().expand_as(X))
kl2 = X.mean(0) * ((X.mean(0)+eps).log()-(Y.mean(0)+eps).log())
score = np.exp(kl1.sum(1).mean() - kl2.sum())
return score
def fid(X, Y):
m = X.mean(0)
m_w = Y.mean(0)
X_np = X.numpy()
Y_np = Y.numpy()
C = np.cov(X_np.transpose())
C_w = np.cov(Y_np.transpose())
C_C_w_sqrt = linalg.sqrtm(C.dot(C_w), True).real
score = m.dot(m) + m_w.dot(m_w) - 2 * m_w.dot(m) + \
np.trace(C + C_w - 2 * C_C_w_sqrt)
return np.sqrt(score)
class Score:
emd = 0
mmd = 0
knn = None
def compute_score(real, fake, k=1, sigma=1, sqrt=True):
Mxx = distance(real, real, False)
Mxy = distance(real, fake, False)
Myy = distance(fake, fake, False)
s = Score()
s.emd = wasserstein(Mxy, sqrt)
s.mmd = mmd(Mxx, Mxy, Myy, sigma)
s.knn = knn(Mxx, Mxy, Myy, k, sqrt)
return s
def compute_score_raw(dataset, imageSize, dataroot, sampleSize, batchSize,
saveFolder_r, saveFolder_f, netG, nz,
conv_model='resnet34', workers=4):
sampleTrue(dataset, imageSize, dataroot, sampleSize, batchSize,
saveFolder_r, workers=workers)
sampleFake(netG, nz, sampleSize, batchSize, saveFolder_f, )
convnet_feature_saver = ConvNetFeatureSaver(model=conv_model,
batchSize=batchSize, workers=workers)
feature_r = convnet_feature_saver.save(saveFolder_r)
feature_f = convnet_feature_saver.save(saveFolder_f)
# 4 feature spaces and 7 scores + incep + modescore + fid
score = np.zeros(4 * 7 + 3)
for i in range(0, 4):
print('compute score in space: ' + str(i))
Mxx = distance(feature_r[i], feature_r[i], False)
Mxy = distance(feature_r[i], feature_f[i], False)
Myy = distance(feature_f[i], feature_f[i], False)
score[i * 7] = wasserstein(Mxy, True)
score[i * 7 + 1] = mmd(Mxx, Mxy, Myy, 1)
tmp = knn(Mxx, Mxy, Myy, 1, False)
score[(i * 7 + 2):(i * 7 + 7)] = \
tmp.acc, tmp.acc_real, tmp.acc_fake, tmp.precision, tmp.recall
score[28] = inception_score(feature_f[3])
score[29] = mode_score(feature_r[3], feature_f[3])
score[30] = fid(feature_r[3], feature_f[3])
return score