-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathrun_cifar.py
589 lines (446 loc) · 20.8 KB
/
run_cifar.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
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
# coding=utf-8
# Copyright 2024 Ifigeneia Apostolopoulou
#
# 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.
"""
Train and Evaluate Wide ResNet 28-10 with Distance-Aware Bottleneck on CIFAR-10.
"""
import os
os.environ["TF_CPP_MIN_LOG_LEVEL"] = "2" # print errors only
from absl import app
from absl import flags
from absl import logging
from tensorboard.plugins.hparams import api as hp
import tensorflow as tf
import tensorflow_datasets as tfds
import datasets
from models import wide_resnet_dab
import utils.schedules as schedules
import utils.metrics_utils as metrics_utils
import hparams.cifar_hparams as cifar_hps
FLAGS = flags.FLAGS
def main(argv):
# =========================== experimental setup =========================== #
#################### set seeds ####################
tf.random.set_seed(FLAGS.seed)
seeds = tf.random.experimental.stateless_split([FLAGS.seed, FLAGS.seed + 1], 2)[
:, 0
]
#################### set-up directories ####################
dir_path = os.path.dirname(os.path.realpath(__file__))
output_dir = os.path.join(
dir_path,
f"cifar_summaries_dab/seed_{FLAGS.seed}",
)
summary_writer = tf.summary.create_file_writer(output_dir)
################## set-up distributed training ##################
strategy = tf.distribute.MirroredStrategy()
batch_size = FLAGS.per_core_batch_size * FLAGS.num_cores
################## set-up datasets ##################
normalize_by_cifar=datasets.normalize_by_cifar
train_builder = datasets.Cifar10Dataset(
split=tfds.Split.TRAIN,
seed=seeds[0],
validation_percent=0.0,
)
train_dataset = train_builder.load(batch_size=batch_size,strategy=strategy)
#
test_builder = datasets.Cifar10Dataset(
split=tfds.Split.TEST,
drop_remainder=True,
)
test_dataset = test_builder.load(batch_size=batch_size,strategy=strategy)
steps_per_epoch = train_builder.num_examples // batch_size
steps_per_eval = test_builder.num_examples // batch_size
ood_dataset_names = FLAGS.ood_dataset
ood_datasets, steps_per_ood = datasets.load_ood_datasets(
ood_dataset_names=ood_dataset_names,
in_dataset_builder=test_builder,
batch_size=batch_size,
drop_remainder=True,
)
ood_datasets = {
name: strategy.experimental_distribute_dataset(ds)
for name, ds in ood_datasets.items()
}
if FLAGS.corruptions_interval > 0:
corrupted_datasets = {}
corruption_types, max_intensity = datasets.load_corrupted_cifar_test_info()
for corruption_type in corruption_types:
for severity in range(1, max_intensity + 1):
dataset = datasets.Cifar10CorruptedDataset(
corruption_type=corruption_type,
severity=severity,
split=tfds.Split.TEST,
drop_remainder=True,
).load(batch_size=batch_size,strategy=strategy)
corrupted_datasets[
f"{corruption_type}_{severity}"
] = dataset
with strategy.scope():
################## build model ##################
NUM_CLASSES=cifar_hps.NUM_CLASSES
logging.info("Building WideResNet model")
model = wide_resnet_dab(
input_shape=cifar_hps.IMAGE_SHAPE,
depth=28,
dab_dim=FLAGS.dab_dim,
codebook_size=FLAGS.codebook_size,
dab_tau=FLAGS.dab_tau,
width_multiplier=10,
num_classes=NUM_CLASSES,
l2=FLAGS.l2,
seed=seeds[1],
)
logging.info("Model input shape: %s", model.input_shape)
logging.info("Model output shape: %s", model.output_shape)
logging.info("Model number of weights: %s", model.count_params())
model.summary()
################## set-up optimizers ##################
# Linearly scale learning rate and the decay epochs by vanilla settings.
base_lr = FLAGS.base_learning_rate * batch_size / 128
train_epochs = FLAGS.train_epochs
lr_decay_epochs = [
(int(start_epoch_str) * train_epochs) // 200
for start_epoch_str in FLAGS.lr_decay_epochs
]
# encoder & decoder optimizer
learning_rate = schedules.WarmUpPiecewiseConstantSchedule(
steps_per_epoch=steps_per_epoch,
base_learning_rate=base_lr,
decay_ratio=FLAGS.lr_decay_ratio,
decay_epochs=lr_decay_epochs,
warmup_epochs=FLAGS.lr_warmup_epochs,
)
optimizer = tf.keras.optimizers.SGD(
learning_rate=learning_rate,
momentum=1.0 - FLAGS.one_minus_momentum,
nesterov=True,
)
# codebook optimizer
codebook_optimizer = tf.keras.optimizers.Adam(
FLAGS.rdfc_learning_rate
)
################## set-up checkpoints ##################
checkpoint = tf.train.Checkpoint(
model=model, optimizer=optimizer, codebook_optimizer=codebook_optimizer
)
latest_checkpoint = tf.train.latest_checkpoint(output_dir)
initial_epoch = 0
if latest_checkpoint:
# checkpoint.restore must be within a strategy.scope() so that optimizer
# slot variables are mirrored.
checkpoint.restore(latest_checkpoint)
logging.info("Loaded checkpoint %s", latest_checkpoint)
initial_epoch = optimizer.iterations.numpy() // steps_per_epoch
if FLAGS.saved_model_dir:
logging.info("Saved model dir : %s", FLAGS.saved_model_dir)
latest_checkpoint = tf.train.latest_checkpoint(FLAGS.saved_model_dir)
checkpoint.restore(latest_checkpoint)
logging.info("Loaded checkpoint %s", latest_checkpoint)
if FLAGS.eval_only:
initial_epoch = train_epochs - 1 # Run just one epoch of eval
################## create metrics ##################
metrics = {
"train/negative_log_likelihood": tf.keras.metrics.Mean(),
"train/accuracy": tf.keras.metrics.SparseCategoricalAccuracy(),
"train/loss": tf.keras.metrics.Mean(),
"test/negative_log_likelihood": tf.keras.metrics.Mean(),
"test/accuracy": tf.keras.metrics.SparseCategoricalAccuracy(),
}
#OOD metrics.
for dataset_name in ood_dataset_names:
ood_metrics = metrics_utils.create_uncertainty_metrics(
uncertainty_scores={"codebook_distance"},
metric_prefix=dataset_name+"_ood",
report_auprc=True,
report_auroc=True,
)
metrics.update(ood_metrics)
#Calibration metrics.
calibration_metrics = metrics_utils.create_uncertainty_metrics(
uncertainty_scores={"codebook_distance"},
metric_prefix="calibration",
report_auprc=False,
report_auroc=True,
)
metrics.update(calibration_metrics)
#Noise corruption metrics.
if FLAGS.corruptions_interval > 0:
for dataset_name in corrupted_datasets:
corrupted_ood_metrics = metrics_utils.create_uncertainty_metrics(
uncertainty_scores= {"codebook_distance"},
metric_prefix=dataset_name+"_corrupted",
report_auprc=True,
report_auroc=True,
)
metrics.update(corrupted_ood_metrics)
# =========================== learning algorithm =========================== #
@tf.function
def rdfc_step(iterator):
"""
This function computes Rate Distortion Finite Cardinality (RDFC) [1].
In this phase, the codebook (the centroids) is trained.
References
[1] Banerjee A, Dhillon I, Ghosh J, Merugu S. An information theoretic analysis of
maximum likelihood mixture estimation for exponential families. ICML 2004.
"""
def centroid_step_fn(inputs):
"""
Train the codebook, i.e., find the centroids.
"""
images = inputs["features"]
with tf.GradientTape() as tape:
images=normalize_by_cifar(images)
outputs = model(images, training=True)
_, uncertainty = tf.split(outputs, [NUM_CLASSES, 1], axis=-1)
# Scale the loss given the distributed strategy will reduce sum all gradients.
scaled_loss = (
tf.reduce_mean(uncertainty) / strategy.num_replicas_in_sync
)
# train only codebook
grads = tape.gradient(scaled_loss, model.trainable_variables)
codebook_optimizer.apply_gradients([(grad, var) for grad, var in zip(grads, model.trainable_variables) if "centroid" in var.name])
def centroid_probs_step_fn(inputs):
"""
Gather conditional centroid probabilities (E-step) needed for the prior centroid probabilities.
"""
images = inputs["features"]
images=normalize_by_cifar(images)
model(images, training=True)
# =============================================================================
# flag network as initialized
model.get_layer("dense_dab").initialized.assign(
tf.constant(True, dtype=tf.bool)
)
# update centroids
model.get_layer("dense_dab").reset_codebook_covariance()
for _ in tf.range(tf.cast(steps_per_epoch, tf.int32)):
strategy.run(centroid_step_fn, args=(next(iterator),))
model.get_layer("dense_dab").set_codebook_covariance()
# update centroid probabilities
model.get_layer("dense_dab").reset_centroid_probs()
for i in tf.range(tf.cast(steps_per_epoch, tf.int32)):
strategy.run(centroid_probs_step_fn, args=(next(iterator),))
model.get_layer("dense_dab").set_centroid_probs()
@tf.function
def train_step(iterator):
"""
This step trains the encoder & decoder.
"""
def step_fn(inputs):
"""Per-Replica StepFn."""
images = inputs["features"]
labels = inputs["labels"]
with tf.GradientTape() as tape:
images=normalize_by_cifar(images)
outputs = model(images, training=True)
logits, uncertainty = tf.split(outputs, [NUM_CLASSES, 1], axis=-1)
negative_log_likelihood = tf.reduce_mean(
tf.keras.losses.sparse_categorical_crossentropy(
labels, logits, from_logits=True
)
)
l2_loss = sum([l for l in model.losses])
loss = (
negative_log_likelihood
+ l2_loss
+ FLAGS.beta * tf.reduce_mean(uncertainty)
)
# Scale the loss given the distributed strategy will reduce sum all gradients.
scaled_loss = loss / strategy.num_replicas_in_sync
grads = tape.gradient(scaled_loss, model.trainable_variables)
# train only encoder/ decoder
optimizer.apply_gradients([(grad, var) for grad, var in zip(grads, model.trainable_variables) if "centroid" not in var.name])
metrics["train/loss"].update_state(loss)
metrics["train/negative_log_likelihood"].update_state(
negative_log_likelihood
)
metrics["train/accuracy"].update_state(labels, logits)
# =============================================================================
for _ in tf.range(tf.cast(steps_per_epoch, tf.int32)):
strategy.run(step_fn, args=(next(iterator),))
#=========================== evaluation methods =========================== #
@tf.function(experimental_autograph_options=tf.autograph.experimental.Feature.LISTS)
def test_step(iterator, dataset_split, steps_per_eval):
"""
Evaluation.
"""
def step_fn(inputs):
"""Per-Replica StepFn."""
images = inputs["features"]
labels = inputs["labels"]
images=normalize_by_cifar(images)
outputs = model(images, training=False)
logits, codebook_distance = tf.split(outputs, [NUM_CLASSES, 1], axis=-1)
codebook_distance = tf.squeeze(codebook_distance)
## prepare return values
return_values_dict = {}
return_values_dict["codebook_distance"] = codebook_distance
if dataset_split == "clean":
metrics_utils.update_accuracy_metrics(metrics,labels,logits)
matches=tf.keras.metrics.sparse_categorical_accuracy(labels, logits)
return_values_dict["matches"] = tf.cast(matches,tf.int32)
elif dataset_split == "ood":
return_values_dict["ood_labels"] = 1 - inputs["is_in_distribution"]
return return_values_dict
# =============================================================================
all_return_values_dict = {}
# create arrays with 1 entry per batch evaluation
if dataset_split == "clean":
all_return_values_dict["matches"] = tf.TensorArray(
tf.int32, size=steps_per_eval, dynamic_size=False
)
elif dataset_split == "ood":
all_return_values_dict["ood_labels"] = tf.TensorArray(
tf.int32, size=steps_per_eval, dynamic_size=False
)
all_return_values_dict["codebook_distance"] = tf.TensorArray(
tf.float32, size=steps_per_eval, dynamic_size=False
)
for i in tf.range(tf.cast(steps_per_eval, tf.int32)):
batch_return_values_dict = strategy.run(step_fn, args=(next(iterator),))
for name, val in batch_return_values_dict.items():
if not tf.is_tensor(batch_return_values_dict[name]):
batch_return_values_dict[name]=batch_return_values_dict[name].values
# gather dicitionaries across replicas and update array
all_return_values_dict[name] = all_return_values_dict[name].write(
i, tf.concat((batch_return_values_dict[name]), axis=0)
)
# return as tensor
for name, val in all_return_values_dict.items():
all_return_values_dict[name] = all_return_values_dict[name].concat()
return all_return_values_dict
# =========================== training loop =========================== #
train_iterator = iter(train_dataset)
for epoch in range(initial_epoch, train_epochs):
if not FLAGS.eval_only:
logging.info("Starting to train epoch: %s", epoch)
# train encoder & decoder
train_step(train_iterator)
# train codebook to quantize datapoints' encoders
logging.info("Starting RDFC epoch: %s", epoch)
rdfc_step(train_iterator)
logging.info(
"Train Loss: %.4f, Test NLL: %.4f, Accuracy: %.2f%%",
metrics["train/loss"].result(),
metrics["train/negative_log_likelihood"].result(),
metrics["train/accuracy"].result() * 100,
)
if (
FLAGS.checkpoint_interval > 0
and (epoch + 1) % FLAGS.checkpoint_interval == 0
):
checkpoint_name = checkpoint.save(
os.path.join(output_dir, "checkpoint")
)
logging.info("Saved checkpoint to %s", checkpoint_name)
logging.info("Starting to eval epoch: %s", epoch)
test_iterator = iter(test_dataset)
# evaluate on in-distribution, test data
return_values = test_step(
iterator=test_iterator,
dataset_split="clean",
steps_per_eval=steps_per_eval
)
logging.info(
"Test NLL: %.4f, Accuracy: %.2f%%",
metrics["test/negative_log_likelihood"].result(),
metrics["test/accuracy"].result() * 100,
)
metrics_utils.update_uncertainty_metrics(
strategy=strategy,
metrics=metrics,
metric_prefix="calibration",
binary_labels=1-return_values["matches"],
uncertainty_scores=return_values["codebook_distance"],
uncertainty_name= "codebook_distance"
)
# evaluate OOD detection
for ood_dataset_name, ood_dataset in ood_datasets.items():
ood_iterator = iter(ood_dataset)
ood_return_values = test_step(
ood_iterator,
"ood",
steps_per_ood[ood_dataset_name],
)
metrics_utils.update_uncertainty_metrics(
strategy=strategy,
metrics=metrics,
metric_prefix=ood_dataset_name+"_ood",
binary_labels=ood_return_values["ood_labels"],
uncertainty_scores=ood_return_values["codebook_distance"],
uncertainty_name= "codebook_distance",
update_auprc=True,
)
# evaluate noise-corrupted images detection
if (
FLAGS.corruptions_interval > 0
and (epoch + 1) % FLAGS.corruptions_interval == 0
):
for ood_dataset_name, ood_dataset in corrupted_datasets.items():
ood_iterator = iter(ood_dataset)
corrupted_return_values = test_step(
ood_iterator,
"corrupted",
steps_per_eval,
)
ood_scores= tf.concat(
[
return_values["codebook_distance"],
tf.squeeze(corrupted_return_values["codebook_distance"]),
],
axis=-1,
)
num_examples = return_values["codebook_distance"].shape[0]
ood_labels = tf.concat(
[tf.zeros(shape=num_examples), tf.ones(shape=num_examples)], axis=0
)
metrics_utils.update_uncertainty_metrics(
strategy=strategy,
metrics=metrics,
metric_prefix=ood_dataset_name+"_corrupted",
binary_labels= ood_labels,
uncertainty_scores=ood_scores,
uncertainty_name="codebook_distance",
update_auprc=True,
verbose=False,
)
# update and reset metrics
with summary_writer.as_default():
for name, metric in metrics.items():
tf.summary.scalar(name, metric.result(), step=epoch + 1)
for metric in metrics.values():
metric.reset_states()
# =========================== save model and hyperparams =========================== #
checkpoint_name = checkpoint.save(os.path.join(output_dir, "checkpoint"))
logging.info("Saved last checkpoint to %s", checkpoint_name)
with summary_writer.as_default():
hp.hparams(
{
"num_cores": FLAGS.num_cores,
"base_learning_rate": FLAGS.base_learning_rate,
"lr_decay_ratio":FLAGS.lr_decay_ratio,
"rdfc_learning_rate": FLAGS.rdfc_learning_rate,
"one_minus_momentum": FLAGS.one_minus_momentum,
"l2": FLAGS.l2,
"codebook_size": FLAGS.codebook_size,
"bottleneck_dimension": FLAGS.dab_dim,
"beta": FLAGS.beta,
"temperature": FLAGS.dab_tau,
"per_core_batch_size": FLAGS.per_core_batch_size,
"seed": FLAGS.seed,
}
)
if __name__ == "__main__":
app.run(main)