-
Notifications
You must be signed in to change notification settings - Fork 9
/
not_use_dummy_inference_inceptionscore.py
100 lines (94 loc) · 3.28 KB
/
not_use_dummy_inference_inceptionscore.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
# calculate inception score for cifar-10 in Keras
from math import floor
from numpy import ones
from numpy import expand_dims
from numpy import log
from numpy import mean
from numpy import std
from numpy import exp
from numpy.random import shuffle
from keras.applications.inception_v3 import InceptionV3
from keras.applications.inception_v3 import preprocess_input
from keras.datasets import cifar10
from skimage.transform import resize
from numpy import asarray
from glob import glob
from natsort import natsorted
import cv2
import numpy as np
from tqdm import tqdm
import tensorflow as tf
import os
os.environ['TF_XLA_FLAGS'] = '--tf_xla_enable_xla_devices'
os.environ["CUDA_DEVICE_ORDER"]= "PCI_BUS_ID"
os.environ["CUDA_VISIBLE_DEVICES"]= '0'
# scale an array of images to a new size
def scale_images(images, new_shape):
images_list = list()
for image in tqdm(images):
# resize with nearest neighbor interpolation
new_image = resize(image, new_shape, 0)
# store
images_list.append(new_image)
return asarray(images_list)
# assumes images have any shape and pixels in [0,255]
def calculate_inception_score(images, n_split=1, eps=1E-16):
# load inception v3 model
model = InceptionV3()
# enumerate splits of images/predictions
scores = list()
n_part = floor(images.shape[0] / n_split)
for idx, i in enumerate(range(n_split), start=0):
# retrieve images
ix_start, ix_end = i * n_part, (i+1) * n_part
subset = images[ix_start:ix_end]
# convert from uint8 to float32
subset = subset.astype('float32')
# scale images to the required size
subset = scale_images(subset, (299,299,3))
# pre-process images, scale to [-1,1]
subset = preprocess_input(subset)
# predict p(y|x)
batch = tf.data.Dataset.from_tensor_slices((subset,)).batch(256).map(preprocess_input)
p_yx = []
for subset in tqdm(batch):
subset = subset[0]
p_y = model.predict(subset)
p_yx.extend(p_y)
p_yx = np.array(p_yx)
print(p_yx.shape)
# calculate p(y)
p_y = expand_dims(p_yx.mean(axis=0), 0)
# calculate KL divergence using log probabilities
kl_d = p_yx * (log(p_yx + eps) - log(p_y + eps))
# sum over classes
sum_kl_d = kl_d.sum(axis=1)
# average over images
avg_kl_d = mean(sum_kl_d)
# undo the log
is_score = exp(avg_kl_d)
# store
scores.append(is_score)
print('Inception score of class {}: {}'.format(idx, is_score))
with open('experiments/inceptionscore.txt', 'a') as file:
file.write('Inception score of class {}: {}\n'.format(idx, is_score))
# average across images
is_avg, is_std = mean(scores), std(scores)
return is_avg, is_std
# # load cifar10 images
# (images, _), (_, _) = cifar10.load_data()
# # shuffle images
# shuffle(images)
# print('loaded', images.shape)
# calculate inception score
for path in natsorted(glob('experiments/inference_inception/*')):
images = []
for im_path in tqdm(natsorted(glob(path+'/*'))[:50000]):
images.append(cv2.cvtColor(cv2.imread(im_path), cv2.COLOR_BGR2RGB))
images = np.array(images)
is_mean, is_std = calculate_inception_score(images)
print('Inception score for epoch {}: ({}, {})'.format(os.path.split(path)[-1], is_mean, is_std))
with open('experiments/inceptionscore.txt', 'a') as file:
file.write('-'*30+'\n')
file.write('Inception score for epoch {}: ({}, {})\n'.format(os.path.split(path)[-1], is_mean, is_std))
file.write('-'*30+'\n\n')