-
Notifications
You must be signed in to change notification settings - Fork 2
/
evaluate.py
83 lines (68 loc) · 3.33 KB
/
evaluate.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
import argparse
import sys
import tensorflow as tf
from classifiers.cifar_model import Model
from models.gan import DefenseGANBase
from utils.config import load_config
from utils.reconstruction import reconstruct_dataset
def parse_args():
parser = argparse.ArgumentParser()
parser.add_argument('--cfg', required=True, help='Config file')
if len(sys.argv) == 1:
parser.print_help()
sys.exit(1)
args, _ = parser.parse_known_args()
return args
def main(cfg, *args):
FLAGS = tf.app.flags.FLAGS
batch_size = 64
gan = DefenseGANBase(cfg=cfg, test_mode=True)
data_dict = reconstruct_dataset(gan_model=gan)
images_rec, labels, images_orig = data_dict['test']
# load pretrained classifier
model = Model('classifiers/model/', tiny=False, mode='eval', sess=gan.sess)
acc1 = gan.sess.run(model.accuracy, feed_dict={model.x_input: images_rec[:batch_size],
model.y_input: labels[:batch_size]})
acc2 = gan.sess.run(model.accuracy, feed_dict={model.x_input: images_orig[:batch_size],
model.y_input: labels[:batch_size]})
print('Acc1: {}'.format(acc1))
print('Acc2: {}'.format(acc2))
if __name__ == '__main__':
args = parse_args()
# Note: The load_config() call will convert all the parameters that are defined in
# experiments/config files into FLAGS.param_name and can be passed in from command line.
# arguments : python train.py --cfg <config_path> --<param_name> <param_value>
cfg = load_config(args.cfg)
flags = tf.app.flags
flags.DEFINE_boolean("is_train", False,
"True for training, False for testing. [False]")
flags.DEFINE_boolean("save_recs", False,
"True for saving reconstructions. [False]")
flags.DEFINE_boolean("debug", False,
"True for debug. [False]")
flags.DEFINE_boolean("test_generator", False,
"True for generator samples. [False]")
flags.DEFINE_boolean("test_decoder", False,
"True for decoder samples. [False]")
flags.DEFINE_boolean("test_again", False,
"True for not using cache. [False]")
flags.DEFINE_boolean("test_batch", False,
"True for visualizing the batches and labels. [False]")
flags.DEFINE_boolean("save_ds", False,
"True for saving the dataset in a pickle file. ["
"False]")
flags.DEFINE_boolean("tensorboard_log", True, "True for saving "
"tensorboard logs. [True]")
flags.DEFINE_boolean("train_encoder", False,
"Add an encoder to a pretrained model. ["
"False]")
flags.DEFINE_boolean("test_encoder", False, "Test encoder. [False]")
flags.DEFINE_boolean("init_with_enc", False,
"Initializes the z with an encoder, must run "
"--train_encoder first. [False]")
flags.DEFINE_integer("max_num", -1,
"True for saving the dataset in a pickle file ["
"False]")
flags.DEFINE_string("init_path", None, "Checkpoint path. [None]")
main_cfg = lambda x: main(cfg, x)
tf.app.run(main=main_cfg)