-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtest_synthesis.py
342 lines (256 loc) · 10.1 KB
/
test_synthesis.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
# Copyright 2023 Álvaro Goldar Dieste
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
# http://www.apache.org/licenses/LICENSE-2.0
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
"""Tests the synthesis performance of a pretrained GAN network on a given dataset.
More specifically, this script:
- Loads the specified pretrained GAN model from disk.
- Loads the residual classifier pretrained on the same dataset to use it as reference.
- Evaluates the synthesis performance of the generator in the GAN in terms of FID score.
The FID score is evaluated in a per-class basis, by retrieving all available samples for a certain class, generating
the same number of fake samples, and comparing the two feature distributions that the reference network extracts from
the real and fake samples.
"""
__author__ = "alvrogd"
import argparse
import numpy as np
import scipy
import torch
import datasets
import networks
parser = argparse.ArgumentParser(
prog="test_synthesis.py",
description="Tests the synthesis performance of a pretrained GAN network on a given dataset"
)
parser.add_argument(
"--dataset_path",
type=str,
action="store",
# As saved by preprocess_dataset.py
default="preprocessed/hyperdataset"
)
# Most of the following arguments are not used in this script, but are required to instantiate the networks as in
# run_network.py
parser.add_argument(
"--data_augmentation",
type=int,
action="store",
# 0: no data augmentation, > 0: data augmentation
default=0
)
parser.add_argument(
"--network",
type=str,
action="store",
default="ResBaGAN"
)
parser.add_argument(
"--latent_size",
type=int,
action="store",
default=128
)
parser.add_argument(
"--activation",
type=str,
action="store",
default="lrelu"
)
parser.add_argument(
"--p_dropout",
type=float,
action="store",
default=0.05
)
parser.add_argument(
"--weight_init",
type=str,
action="store",
default="xavier"
)
parser.add_argument(
"--learning_rate",
type=float,
action="store",
default=0.001
)
parser.add_argument(
"--epochs",
type=int,
action="store",
default=600
)
parser.add_argument(
"--batch_size",
type=int,
action="store",
default=32
)
parser.add_argument(
"--num_workers",
type=int,
action="store",
# To speed-up dataloaders
default=4
)
parser.add_argument(
"--device",
type=str,
action="store",
# A CUDA-compatible GPU will be automatically used if available
default=f"cuda:{torch.cuda.current_device()}" if torch.cuda.is_available() else "cpu"
)
parser.add_argument(
"--model_path",
type=str,
action="store",
default="logs/ResBaGAN_model.pt"
)
parser.add_argument(
"--reference_model_path",
type=str,
action="store",
default="CNN2D_Residual_model.pt"
)
args = parser.parse_args()
print(f"[*] Arguments: {vars(args)}")
hyperparams = {
"latent_size": args.latent_size,
"activation": args.activation,
"p_dropout": args.p_dropout,
"weight_init": args.weight_init,
"learning_rate": args.learning_rate,
"epochs": args.epochs,
"batch_size": args.batch_size,
"num_workers": args.num_workers,
"device": args.device,
}
# cuDNN must be enabled manually if there is a CUDA-compatible GPU available
if hyperparams["device"] != "cpu":
# The Docker image has cuDNN installed
torch.backends.cudnn.enabled = True
# Therefore, we will tell cuDNN to search for the fastest algorithms before training to get
# the most out of the GPU
torch.backends.cudnn.benchmark = True
# Loading from disk the preprocessed dataset that will be used to test the network
dataset = datasets.read_preprocessed_dataset(args.dataset_path)
dataset.set_data_augmentation(False)
print(dataset)
# The custom HyperDataset object contains all the train, validation and test data
# --> But it will wrapped into a PyTorch data feeder for convenience
data_loader = torch.utils.data.DataLoader(
dataset,
batch_size=hyperparams["batch_size"],
shuffle=True,
num_workers=hyperparams["num_workers"],
pin_memory=hyperparams["device"] != "cpu",
)
# Building the requested and reference networks
# --> The reference network is used to compute synthethic distributions and per-class FID score
if args.network == "CNN2D" or args.network == "CNN2D_Residual":
raise ValueError("CNN2D and CNN2D_Residual networks are not GANs")
elif args.network == "ACGAN":
network = networks.ACGAN(dataset, hyperparams["device"], hyperparams)
elif args.network == "ResACGAN":
network = networks.ResACGAN(dataset, hyperparams["device"], hyperparams)
elif args.network == "BAGAN":
network = networks.BAGAN(dataset, hyperparams["device"], hyperparams)
elif args.network == "ResBaGAN":
network = networks.ResBaGAN(dataset, hyperparams["device"], hyperparams)
else:
raise ValueError(f"[!] Unknown network: {args.network}")
print(network)
reference_network = networks.CNN2D_Residual(dataset, hyperparams["device"], hyperparams)
print(reference_network)
# Loading the pretrained models
network.load_state_dict(torch.load(args.model_path, map_location=hyperparams["device"]))
network.eval()
reference_network.load_state_dict(torch.load(args.reference_model_path, map_location=hyperparams["device"]))
reference_network.eval()
discriminator = reference_network
generator = network.generator
# Now we will test the synthesis performance of the requested network by computing the FID score for each class
for class_i in range(dataset.classes_count):
print(f"[*] Computing FID score for [C{class_i}] {dataset.classes[class_i]}")
# First, we need to gather all real samples for the current class
all_samples = []
dataset.to_train()
for batch_id, (samples, targets, _) in enumerate(data_loader):
for sample, target in zip(samples, targets):
if target.item() == class_i:
all_samples.append(sample.numpy())
# Some clean-up before the next batch
del(samples, targets)
dataset.to_validation()
for batch_id, (samples, targets, _) in enumerate(data_loader):
for sample, target in zip(samples, targets):
if target.item() == class_i:
all_samples.append(sample.numpy())
# Some clean-up before the next batch
del(samples, targets)
dataset.to_test()
for batch_id, (samples, targets, _) in enumerate(data_loader):
for sample, target in zip(samples, targets):
if target.item() == class_i:
all_samples.append(sample.numpy())
# Some clean-up before the next batch
del(samples, targets)
all_samples = np.asarray(np.copy(all_samples), dtype=np.float32)
all_samples = torch.from_numpy(all_samples)
print(f"\t{all_samples.shape[0]} samples gathered")
###################################################################################################################
##### Code adapted from https://github.com/mseitzer/pytorch-fid/blob/4dfcdc2b70217883da8b8867c5cf8db7ddfffcda/src/pytorch_fid/fid_score.py
##### to just use the relevant parts
#####
##### Copyright 2018 Institute of Bioinformatics, JKU Linz
#####
##### Licensed under the Apache License, Version 2.0 (the "License");
##### you may not use this file except in compliance with the License.
##### You may obtain a copy of the License at
##### http://www.apache.org/licenses/LICENSE-2.0
##### Unless required by applicable law or agreed to in writing, software
##### distributed under the License is distributed on an "AS IS" BASIS,
##### WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
##### See the License for the specific language governing permissions and
##### limitations under the License.
act1 = np.empty((all_samples.shape[0], 128), dtype=np.float32)
act2 = np.empty((all_samples.shape[0], 128), dtype=np.float32)
with torch.no_grad():
all_samples = all_samples.to(network.device)
_, features = discriminator(all_samples)
act1 = features.cpu().numpy()
noise = torch.randn(
(all_samples.shape[0], hyperparams["latent_size"]), dtype=torch.float32, device=network.device
)
random_labels = torch.full(
(all_samples.shape[0],), class_i, dtype=torch.int64, device=network.device
)
fake_samples = generator((noise, random_labels))
_, features = discriminator(fake_samples)
act2 = features.cpu().numpy()
del(all_samples, noise, random_labels, fake_samples, features)
mu1, sigma1 = np.mean(act1, axis=0), np.cov(act1, rowvar=False)
mu2, sigma2 = np.mean(act2, axis=0), np.cov(act2, rowvar=False)
diff = mu1 - mu2
# Product might be almost singular
covmean = scipy.linalg.sqrtm(sigma1.dot(sigma2))
if not np.isfinite(covmean).all():
print(f"[!] FID calculation produces a singular product; adding {1e-6} to diagonal of cov estimates")
offset = np.eye(sigma1.shape[0]) * 1e-6
covmean = scipy.linalg.sqrtm((sigma1 + offset).dot(sigma2 + offset))
# Numerical error might give slight imaginary component
if np.iscomplexobj(covmean):
if not np.allclose(np.diagonal(covmean).imag, 0, atol=1e-3):
m = np.max(np.abs(covmean.imag))
raise ValueError(f"[!] FID calculation produces complex value: {m}")
covmean = covmean.real
tr_covmean = np.trace(covmean)
fid = diff.dot(diff) + np.trace(sigma1) + np.trace(sigma2) - 2 * tr_covmean
###################################################################################################################
print(f"\tFID score: {fid}")