-
Notifications
You must be signed in to change notification settings - Fork 21
Expand file tree
/
Copy patheval.py
More file actions
111 lines (91 loc) · 3.8 KB
/
Copy patheval.py
File metadata and controls
111 lines (91 loc) · 3.8 KB
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
# ------------------------------------------------------------------
# Capsules_mnist
# By InnerPeace Wu
# ------------------------------------------------------------------
from __future__ import absolute_import
from __future__ import division
from __future__ import print_function
import argparse
import sys
from tqdm import tqdm
import tensorflow as tf
import numpy as np
from six.moves import xrange
from tensorflow.examples.tutorials.mnist import input_data
from CapsNet import CapsNet
from config import cfg
def parse_arg():
"""
parse input arguments
"""
parser = argparse.ArgumentParser(description="Train CapsNet")
parser.add_argument('--data_dir', dest='data_dir',
type=str, default=cfg.DATA_DIR,
help='Directory for storing input data')
parser.add_argument('--ckpt', dest='ckpt',
type=str, default=cfg.TRAIN_DIR,
help='path to the directory of check point')
parser.add_argument('--mode', dest='mode',
type=str, default=None,
help='evaluation mode: reconstruct, cap_tweak, adversarial')
parser.add_argument('--batch_size', dest='batch_size', type=int,
default=30, help='batch size for reconstruct evaluation')
parser.add_argument('--max_iters', dest='max_iters', type=int,
default=50, help='batch size for reconstruct evaluation')
parser.add_argument('--tweak_target', dest='tweak_target', type=int,
default=None, help='target number for capsule tweaking experiment')
parser.add_argument('--fig_dir', dest='fig_dir', type=str,
default='../figs', help='directory to save figures')
parser.add_argument('--lr', dest='lr', type=float,
default=1, help='learning rate of adversarial test')
args = parser.parse_args()
if len(sys.argv) == 1 or \
args.mode not in \
('reconstruct', 'cap_tweak', 'adversarial'):
parser.print_help()
sys.exit(1)
return args
def main():
args = parse_arg()
# Import data
mnist = input_data.read_data_sets(args.data_dir, one_hot=True)
tf.reset_default_graph()
# Create the model
caps_net = CapsNet()
# build up architecture
caps_net.eval_architecture(args.mode, args.fig_dir)
config = tf.ConfigProto()
config.gpu_options.allow_growth = True
# read check point file
if args.ckpt:
ckpt = tf.train.get_checkpoint_state(args.ckpt)
else:
raise ValueError
with tf.Session(config=config) as sess:
print("Reading parameters from %s" % ckpt.model_checkpoint_path)
caps_net.saver.restore(sess, ckpt.model_checkpoint_path)
if args.mode == 'cap_tweak':
for i in tqdm(xrange(10), desc='capsule tweaking'):
label = None
while label != i:
x, y = mnist.test.next_batch(1)
label = np.argmax(y)
caps_net.cap_tweak(sess, x, y)
elif args.mode == 'reconstruct':
for i in tqdm(xrange(args.max_iters), desc='reconstructing'):
x, y = mnist.test.next_batch(args.batch_size)
caps_net.reconstruct_eval(sess, x, y, args.batch_size)
# adversarial test
else:
for ori in xrange(10):
print('------ class {} ------'.format(ori))
label = None
while label != ori:
x, y = mnist.test.next_batch(1)
label = np.argmax(y)
for tar in xrange(10):
if ori == tar:
continue
caps_net.adversarial_eval(sess, x, ori, tar, args.lr)
if __name__ == '__main__':
main()