File size: 12,185 Bytes
a00ee36 |
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 |
# Copyright (c) Facebook, Inc. and its affiliates.
# All rights reserved.
#
# This source code is licensed under the license found in the
# LICENSE file in the root directory of this source tree.
#
# All contributions by Andy Brock:
# Copyright (c) 2019 Andy Brock
#
# MIT License
#
""" Tensorflow inception score code
Derived from https://github.com/openai/improved-gan
Code derived from tensorflow/tensorflow/models/image/imagenet/classify_image.py
THIS CODE REQUIRES TENSORFLOW 1.3 or EARLIER to run in PARALLEL BATCH MODE
To use this code, run sample.py on your model with --sample_npz, and then
pass the experiment name in the --experiment_name.
This code also saves pool3 stats to an npz file for FID calculation
"""
from __future__ import absolute_import
from __future__ import division
from __future__ import print_function
import os.path
import sys
import tarfile
import math
from tqdm import tqdm, trange
from argparse import ArgumentParser
import numpy as np
from six.moves import urllib
import tensorflow as tf
import pickle
import h5py as h5
import json
MODEL_DIR = "../inception_net"
DATA_URL = (
"http://download.tensorflow.org/models/image/imagenet/inception-2015-12-05.tgz"
)
softmax = None
def prepare_parser():
usage = "Parser for TF1.3- Inception Score scripts."
parser = ArgumentParser(description=usage)
parser.add_argument(
"--experiment_name",
type=str,
default="",
help="Which experiment" "s samples.npz file to pull and evaluate",
)
parser.add_argument(
"--experiment_root",
type=str,
default="samples",
help="Default location where samples are stored (default: %(default)s)",
)
parser.add_argument(
"--batch_size",
type=int,
default=500,
help="Default overall batchsize (default: %(default)s)",
)
parser.add_argument(
"--kmeans_subsampled",
type=int,
default=-1,
help="Reduced number of instances to test with (using this number of centroids).",
)
parser.add_argument(
"--seed", type=int, default=0, help="Seed (default: %(default)s)"
)
## Ground-truth data arguments ##
parser.add_argument(
"--use_ground_truth_data",
action="store_true",
default=False,
help="Use ground truth data to store its reference inception moments",
)
parser.add_argument(
"--data_root",
type=str,
default="data",
help="Default location where data is stored (default: %(default)s)",
)
parser.add_argument(
"--which_dataset",
type=str,
default="imagenet",
choices=["imagenet", "imagenet_lt", "coco"],
help="Dataset choice.",
)
parser.add_argument(
"--resolution",
type=int,
default=64,
help="Resolution to train with " "(default: %(default)s)",
)
parser.add_argument(
"--split", type=str, default="train", help="Data split (default: %(default)s)"
)
parser.add_argument(
"--strat_name",
type=str,
default="",
choices=["", "few", "low", "many"],
help="Stratified split for FID in ImageNet-LT validation (default: %(default)s)",
)
return parser
def run(config):
assert (
config["strat_name"] != ""
and config["which_dataset"] == "imagenet_lt"
and config["split"] == "val"
) or config["strat_name"] == ""
# Inception with TF1.3 or earlier.
# Call this function with list of images. Each of elements should be a
# numpy array with values ranging from 0 to 255.
def get_inception_score(images, splits=10, normalize=True):
assert type(images) == list
assert type(images[0]) == np.ndarray
assert len(images[0].shape) == 3
# assert(np.max(images[0]) > 10)
# assert(np.min(images[0]) >= 0.0)
inps = []
for img in images:
if normalize:
img = np.uint8(255 * (img + 1) / 2.0)
img = img.astype(np.float32)
inps.append(np.expand_dims(img, 0))
bs = config["batch_size"]
with tf.Session() as sess:
preds, pools = [], []
n_batches = int(math.ceil(float(len(inps)) / float(bs)))
for i in trange(n_batches):
inp = inps[(i * bs) : min((i + 1) * bs, len(inps))]
inp = np.concatenate(inp, 0)
pred, pool = sess.run([softmax, pool3], {"ExpandDims:0": inp})
preds.append(pred)
pools.append(pool)
preds = np.concatenate(preds, 0)
scores = []
for i in range(splits):
part = preds[
(i * preds.shape[0] // splits) : (
(i + 1) * preds.shape[0] // splits
),
:,
]
kl = part * (np.log(part) - np.log(np.expand_dims(np.mean(part, 0), 0)))
kl = np.mean(np.sum(kl, 1))
scores.append(np.exp(kl))
return np.mean(scores), np.std(scores), np.squeeze(np.concatenate(pools, 0))
# Init inception
def _init_inception():
global softmax, pool3
if not os.path.exists(MODEL_DIR):
os.makedirs(MODEL_DIR)
filename = DATA_URL.split("/")[-1]
filepath = os.path.join(MODEL_DIR, filename)
if not os.path.exists(filepath):
def _progress(count, block_size, total_size):
sys.stdout.write(
"\r>> Downloading %s %.1f%%"
% (filename, float(count * block_size) / float(total_size) * 100.0)
)
sys.stdout.flush()
filepath, _ = urllib.request.urlretrieve(DATA_URL, filepath, _progress)
print()
statinfo = os.stat(filepath)
print("Succesfully downloaded", filename, statinfo.st_size, "bytes.")
tarfile.open(filepath, "r:gz").extractall(MODEL_DIR)
with tf.gfile.FastGFile(
os.path.join(MODEL_DIR, "classify_image_graph_def.pb"), "rb"
) as f:
graph_def = tf.GraphDef()
graph_def.ParseFromString(f.read())
_ = tf.import_graph_def(graph_def, name="")
# Works with an arbitrary minibatch size.
with tf.Session() as sess:
pool3 = sess.graph.get_tensor_by_name("pool_3:0")
ops = pool3.graph.get_operations()
for op_idx, op in enumerate(ops):
for o in op.outputs:
shape = o.get_shape()
shape = [s.value for s in shape]
new_shape = []
for j, s in enumerate(shape):
if s == 1 and j == 0:
new_shape.append(None)
else:
new_shape.append(s)
o.__dict__["_shape_val"] = tf.TensorShape(new_shape)
w = sess.graph.get_operation_by_name("softmax/logits/MatMul").inputs[1]
logits = tf.matmul(tf.squeeze(pool3, [1, 2]), w)
softmax = tf.nn.softmax(logits)
# if softmax is None: # No need to functionalize like this.
_init_inception()
if config["use_ground_truth_data"]:
# HDF5 file name
if config["which_dataset"] in ["imagenet", "imagenet_lt"]:
dataset_name_prefix = "ILSVRC"
elif config["which_dataset"] == "coco":
dataset_name_prefix = "COCO"
hdf5_filename = "%s%i%s%s%s_xy.hdf5" % (
dataset_name_prefix,
config["resolution"],
"longtail"
if config["which_dataset"] == "imagenet_lt" and config["split"] == "train"
else "",
"_val" if config["split"] == "val" else "",
"_test"
if config["split"] == "val" and config["which_dataset"] == "coco"
else "",
)
with h5.File(os.path.join(config["data_root"], hdf5_filename), "r") as f:
data_imgs = f["imgs"][:]
data_labels = f["labels"][:]
ims = data_imgs.transpose(0, 2, 3, 1)
else:
if config["strat_name"] != "":
fname = "%s/%s/samples%s_seed%i_strat_%s.pickle" % (
config["experiment_root"],
config["experiment_name"],
"_kmeans" + str(config["kmeans_subsampled"])
if config["kmeans_subsampled"] > -1
else "",
config["seed"],
config["strat_name"],
)
else:
fname = "%s/%s/samples%s_seed%i.pickle" % (
config["experiment_root"],
config["experiment_name"],
"_kmeans" + str(config["kmeans_subsampled"])
if config["kmeans_subsampled"] > -1
else "",
config["seed"],
)
print("loading %s ..." % fname)
file_to_read = open(fname, "rb")
ims = pickle.load(file_to_read)["x"]
print("loading %s ..." % fname)
print("number of images saved are ", len(ims))
file_to_read.close()
ims = ims.swapaxes(1, 2).swapaxes(2, 3)
import time
t0 = time.time()
inc_mean, inc_std, pool_activations = get_inception_score(
list(ims), splits=10, normalize=not config["use_ground_truth_data"]
)
t1 = time.time()
print("Saving pool to numpy file for FID calculations...")
mu = np.mean(pool_activations, axis=0)
sigma = np.cov(pool_activations, rowvar=False)
if config["use_ground_truth_data"]:
np.savez(
"%s/%s%s_res%i_tf_inception_moments_ground_truth.npz"
% (
config["data_root"],
config["which_dataset"],
"_val" if config["split"] == "val" else "",
config["resolution"],
),
**{"mu": mu, "sigma": sigma}
)
else:
np.savez(
"%s/%s/TF_pool%s_%s.npz"
% (
config["experiment_root"],
config["experiment_name"],
"_val" if config["split"] == "val" else "",
"_strat_" + config["strat_name"] if config["strat_name"] != "" else "",
),
**{"mu": mu, "sigma": sigma}
)
print(
"Inception took %3f seconds, score of %3f +/- %3f."
% (t1 - t0, inc_mean, inc_std)
)
# If ground-truth data moments, also compute the moments for stratified FID.
if (
config["split"] == "val"
and config["which_dataset"] == "imagenet_lt"
and config["use_ground_truth_data"]
):
samples_per_class = np.load(
"BigGAN_PyTorch/imagenet_lt/imagenet_lt_samples_per_class.npy",
allow_pickle=True,
)
for strat_name in ["_many", "_low", "_few"]:
if strat_name == "_many":
pool_ = pool_activations[samples_per_class[data_labels] >= 100]
elif strat_name == "_low":
pool_ = pool_activations[samples_per_class[data_labels] < 100]
labels_ = data_labels[samples_per_class[data_labels] < 100]
pool_ = pool_[samples_per_class[labels_] > 20]
elif strat_name == "_few":
pool_ = pool_activations[samples_per_class[data_labels] <= 20]
print("Size for strat ", strat_name, " is ", len(pool_))
mu = np.mean(pool_, axis=0)
sigma = np.cov(pool_, rowvar=False)
np.savez(
"%s/%s%s_res%i_tf_inception_moments%s_ground_truth.npz"
% (
config["data_root"],
config["which_dataset"],
"_val" if config["split"] == "val" else "",
config["resolution"],
strat_name,
),
**{"mu": mu, "sigma": sigma}
)
def main():
# parse command line and run
parser = prepare_parser()
config = vars(parser.parse_args())
print(config)
run(config)
if __name__ == "__main__":
main()
|