-
Notifications
You must be signed in to change notification settings - Fork 39
/
Copy pathmain.py
131 lines (96 loc) · 6.11 KB
/
main.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
from frechet_kernel_Inception_distance import *
from inception_score import *
from glob import glob
import os
def inception_score() :
filenames = glob(os.path.join('./fake', '*.*'))
images = [get_images(filename) for filename in filenames]
images = np.transpose(images, axes=[0, 3, 1, 2])
# A smaller BATCH_SIZE reduces GPU memory usage, but at the cost of a slight slowdown
BATCH_SIZE = 1
# Run images through Inception.
inception_images = tf.placeholder(tf.float32, [BATCH_SIZE, 3, None, None])
logits = inception_logits(inception_images)
IS = get_inception_score(BATCH_SIZE, images, inception_images, logits, splits=10)
print()
print("IS : ", IS)
def frechet_inception_distance() :
filenames = glob(os.path.join('./real_target', '*.*'))
real_images = [get_images(filename) for filename in filenames]
real_images = np.transpose(real_images, axes=[0, 3, 1, 2])
filenames = glob(os.path.join('./fake', '*.*'))
fake_images = [get_images(filename) for filename in filenames]
fake_images = np.transpose(fake_images, axes=[0, 3, 1, 2])
# A smaller BATCH_SIZE reduces GPU memory usage, but at the cost of a slight slowdown
BATCH_SIZE = 1
# Run images through Inception.
inception_images = tf.placeholder(tf.float32, [BATCH_SIZE, 3, None, None])
real_activation = tf.placeholder(tf.float32, [None, None], name='activations1')
fake_activation = tf.placeholder(tf.float32, [None, None], name='activations2')
fcd = frechet_classifier_distance_from_activations(real_activation, fake_activation)
activations = inception_activations(inception_images)
FID = get_fid(fcd, BATCH_SIZE, real_images, fake_images, inception_images, real_activation, fake_activation, activations)
print()
print("FID : ", FID / 100)
def kernel_inception_distance() :
filenames = glob(os.path.join('./real_target', '*.*'))
real_images = [get_images(filename) for filename in filenames]
real_images = np.transpose(real_images, axes=[0, 3, 1, 2])
filenames = glob(os.path.join('./fake', '*.*'))
fake_images = [get_images(filename) for filename in filenames]
fake_images = np.transpose(fake_images, axes=[0, 3, 1, 2])
# A smaller BATCH_SIZE reduces GPU memory usage, but at the cost of a slight slowdown
BATCH_SIZE = 1
# Run images through Inception.
inception_images = tf.placeholder(tf.float32, [BATCH_SIZE, 3, None, None])
real_activation = tf.placeholder(tf.float32, [None, None], name='activations1')
fake_activation = tf.placeholder(tf.float32, [None, None], name='activations2')
kcd_mean, kcd_stddev = kernel_classifier_distance_and_std_from_activations(real_activation, fake_activation, max_block_size=10)
activations = inception_activations(inception_images)
KID_mean = get_kid(kcd_mean, BATCH_SIZE, real_images, fake_images, inception_images, real_activation, fake_activation, activations)
KID_stddev = get_kid(kcd_stddev, BATCH_SIZE, real_images, fake_images, inception_images, real_activation, fake_activation, activations)
print()
print("KID_mean : ", KID_mean * 100)
print("KID_stddev : ", KID_stddev * 100)
def mean_kernel_inception_distance() :
source_alpha = 0.98
target_alpha = 1 - source_alpha
filenames = glob(os.path.join('./real_source', '*.*'))
real_source_images = [get_images(filename) for filename in filenames]
real_source_images = np.transpose(real_source_images, axes=[0, 3, 1, 2])
filenames = glob(os.path.join('./real_target', '*.*'))
real_target_images = [get_images(filename) for filename in filenames]
real_target_images = np.transpose(real_target_images, axes=[0, 3, 1, 2])
filenames = glob(os.path.join('./fake', '*.*'))
fake_images = [get_images(filename) for filename in filenames]
fake_images = np.transpose(fake_images, axes=[0, 3, 1, 2])
# A smaller BATCH_SIZE reduces GPU memory usage, but at the cost of a slight slowdown
BATCH_SIZE = 1
# Run images through Inception.
inception_images = tf.placeholder(tf.float32, [BATCH_SIZE, 3, None, None])
real_activation = tf.placeholder(tf.float32, [None, None], name='activations1')
fake_activation = tf.placeholder(tf.float32, [None, None], name='activations2')
fcd = frechet_classifier_distance_from_activations(real_activation, fake_activation)
kcd_mean, kcd_stddev = kernel_classifier_distance_and_std_from_activations(real_activation, fake_activation,
max_block_size=10)
activations = inception_activations(inception_images)
FID = get_fid(fcd, BATCH_SIZE, real_target_images, fake_images, inception_images, real_activation, fake_activation, activations)
KID_mean = get_kid(kcd_mean, BATCH_SIZE, real_target_images, fake_images, inception_images, real_activation, fake_activation, activations)
KID_stddev = get_kid(kcd_stddev, BATCH_SIZE, real_target_images, fake_images, inception_images, real_activation, fake_activation, activations)
mean_FID = get_fid(fcd, BATCH_SIZE, real_source_images, fake_images, inception_images, real_activation, fake_activation, activations)
mean_KID_mean = get_kid(kcd_mean, BATCH_SIZE, real_source_images, fake_images, inception_images, real_activation, fake_activation, activations)
mean_KID_stddev = get_kid(kcd_stddev, BATCH_SIZE, real_source_images, fake_images, inception_images, real_activation, fake_activation, activations)
mean_FID = (target_alpha * FID + source_alpha * mean_FID) / 2.0
mean_KID_mean = (target_alpha * KID_mean + source_alpha * mean_KID_mean) / 2.0
mean_KID_stddev = (target_alpha * KID_stddev + source_alpha * mean_KID_stddev) / 2.0
# mean_FID = (2 * FID * mean_FID) / (FID + mean_FID)
# mean_KID_mean = (2 * KID_mean * mean_KID_mean) / (KID_mean + mean_KID_mean)
# mean_KID_stddev = (2 * KID_stddev * mean_KID_stddev) / (KID_stddev + mean_KID_stddev)
print()
print("mean_FID : ", mean_FID / 100)
print("mean_KID_mean : ", mean_KID_mean * 100)
print("mean_KID_stddev : ", mean_KID_stddev * 100)
inception_score()
frechet_inception_distance()
kernel_inception_distance()
# mean_kernel_inception_distance()