-
Notifications
You must be signed in to change notification settings - Fork 0
/
metrics.py
101 lines (82 loc) · 3.27 KB
/
metrics.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
# example of calculating the frechet inception distance in Keras
import numpy
import os
import cv2
import argparse
import torch
import numpy as np
from scipy.linalg import sqrtm
from keras.applications.inception_v3 import InceptionV3
from keras.applications.inception_v3 import preprocess_input
os.environ["TF_CPP_MIN_LOG_LEVEL"] = '3'
# calculate frechet inception distance
def calculate_fid(model, images1, images2):
# calculate activations
act1 = model.predict(images1)
act2 = model.predict(images2)
# calculate mean and covariance statistics
mu1, sigma1 = act1.mean(axis=0), np.cov(act1, rowvar=False)
mu2, sigma2 = act2.mean(axis=0), np.cov(act2, rowvar=False)
# calculate sum squared difference between means
ssdiff = numpy.sum((mu1 - mu2)**2.0)
# calculate sqrt of product between cov
covmean = sqrtm(np.dot(sigma1, sigma2))
# check and correct imaginary numbers from sqrt
if np.iscomplexobj(covmean):
covmean = covmean.real
# calculate score
fid = ssdiff + np.trace(sigma1 + sigma2 - 2.0 * covmean)
return fid
#act1 =generatedImg ,act2 = realImg
def calculate_fid_modify(act1,act2):
# calculate activations
# act1 = model.predict(images1)
# act2 = model.predict(images2)
# calculate mean and covariance statistics
mu1, sigma1 = act1.mean(axis=0), np.cov(act1, rowvar=False)
mu2, sigma2 = act2.mean(axis=0), np.cov(act2, rowvar=False)
# calculate sum squared difference between means
ssdiff = numpy.sum((mu1 - mu2)**2.0)
# calculate sqrt of product between cov
covmean = sqrtm(np.dot(sigma1, sigma2))
# check and correct imaginary numbers from sqrt
if np.iscomplexobj(covmean):
covmean = covmean.real
# calculate score
fid = ssdiff + np.trace(sigma1 + sigma2 - 2.0 * covmean)
return fid
def data_list(dirPath):
generated_Dataset = []
real_Dataset = []
for root, dirs, files in os.walk(dirPath):
for filename in sorted(files):
if "generated" in filename:
generatedPath = root + '/' + filename
generatedImg = cv2.imread(generatedPath).astype('float32')
generated_Dataset.append(generatedImg)
realPath = root + '/' + filename.replace('generated', 'real')
realImg = cv2.imread(realPath).astype('float32')
real_Dataset.append(realImg)
return generated_Dataset, real_Dataset
if __name__ == '__main__':
parser = argparse.ArgumentParser()
# parser.add_argument('--dataset_dir', type=str, default='./results/hrnet/', help='results')
parser.add_argument('--dataset_dir', type=str, default='./results/ssngan/', help='results')
parser.add_argument('--name', type=str, default='sketch', help='name of dataset')
opt = parser.parse_args()
dirPath = os.path.join(opt.dataset_dir, opt.name)
generatedImg, realImg = data_list(dirPath)
dataset_size = len(generatedImg)
print("dataset:", dataset_size)
images1 = torch.Tensor(generatedImg)
images2 = torch.Tensor(realImg)
print('shape: ', images1.shape, images2.shape)
# prepare the inception v3 model
model = InceptionV3(include_top=False, pooling='avg')
# pre-process images
images1 = preprocess_input(images1)
images2 = preprocess_input(images2)
# fid between images1 and images2
fid = calculate_fid(model, images1, images2)
print('FID : %.3f' % fid)
print('FID_average : %.3f' % (fid / dataset_size))