-
Notifications
You must be signed in to change notification settings - Fork 28
/
main.py
169 lines (147 loc) · 7.49 KB
/
main.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
import numpy as np
import argparse
from agents import SingleTaskAgent, StandardAgent, MultiTaskSeparateAgent, MultiTaskJointAgent
from utils import CIFAR10Loader, CIFAR100Loader, OmniglotLoader
def parse_args():
parser = argparse.ArgumentParser(formatter_class=argparse.RawTextHelpFormatter)
mode = parser.add_mutually_exclusive_group()
mode.add_argument('--train', action='store_true')
mode.add_argument('--eval', action='store_true')
parser.add_argument('--setting', type=int, default=0, help='0: Standard experiment \n'
'1: Standard experiment (recording each class\' accuracy separately) \n'
'2: Single task experiment \n'
'3: Multi-task experiment (trained separately) \n'
'4: Multi-task experiment (trained separately with biased sample probability) \n'
'5: Multi-task experiment (trained jointly) \n'
'6: Multi-task experiment (trained jointly with biased weighted loss)')
parser.add_argument('--data', type=int, default=1, help='0: CIFAR-10 \n'
'1: CIFAR-100 \n'
'2: Omniglot \n')
parser.add_argument('--task', type=int, default=None, help='Which class to distinguish (for setting 2)')
parser.add_argument('--save_path', type=str, default='.')
parser.add_argument('--save_model', action='store_true')
parser.add_argument('--save_history', action='store_true')
parser.add_argument('--verbose', action='store_true')
return parser.parse_args()
def train(args):
if args.data == 0:
train_data = CIFAR10Loader(batch_size=128, train=True, drop_last=True)
test_data = CIFAR10Loader(batch_size=128, train=False, drop_last=False)
multi_task_type = 'binary'
num_epochs = 20
elif args.data == 1:
train_data = CIFAR100Loader(batch_size=128, train=True, drop_last=True)
test_data = CIFAR100Loader(batch_size=128, train=False, drop_last=False)
multi_task_type = 'multiclass'
num_epochs = 20
elif args.data == 2:
train_data = OmniglotLoader(batch_size=128, train=True, drop_last=True)
test_data = OmniglotLoader(batch_size=128, train=False, drop_last=False)
multi_task_type = 'multiclass'
num_epochs = 100 # Need more tests to determine
else:
raise ValueError('Unknown data ID: {}'.format(args.data))
num_classes_single = train_data.num_classes_single
num_classes_multi = train_data.num_classes_multi
num_tasks = len(num_classes_multi)
num_channels = train_data.num_channels
if args.setting == 0:
agent = SingleTaskAgent(num_classes=num_classes_single,
num_channels=num_channels)
train_data = train_data.get_loader()
test_data = test_data.get_loader()
elif args.setting == 1:
agent = StandardAgent(num_classes_single=num_classes_single,
num_classes_multi=num_classes_multi,
multi_task_type=multi_task_type,
num_channels=num_channels)
train_data = train_data.get_loader()
elif args.setting == 2:
assert args.task in list(range(num_tasks)), 'Unknown task: {}'.format(args.task)
agent = SingleTaskAgent(num_classes=num_classes_multi[args.task],
num_channels=num_channels)
train_data = train_data.get_loader(args.task)
test_data = test_data.get_loader(args.task)
elif args.setting == 3:
agent = MultiTaskSeparateAgent(num_classes=num_classes_multi,
num_channels=num_channels)
elif args.setting == 4:
prob = np.arange(num_tasks) + 1
prob = prob / sum(prob)
agent = MultiTaskSeparateAgent(num_classes=num_classes_multi,
num_channels=num_channels,
task_prob=prob.tolist())
elif args.setting == 5:
agent = MultiTaskJointAgent(num_classes=num_classes_multi,
multi_task_type=multi_task_type,
num_channels=num_channels)
elif args.setting == 6:
weight = np.arange(num_tasks) + 1
weight = weight / sum(weight)
agent = MultiTaskJointAgent(num_classes=num_classes_multi,
multi_task_type=multi_task_type,
num_channels=num_channels,
loss_weight=weight.tolist())
else:
raise ValueError('Unknown setting: {}'.format(args.setting))
agent.train(train_data=train_data,
test_data=test_data,
num_epochs=num_epochs,
save_history=args.save_history,
save_path=args.save_path,
verbose=args.verbose
)
if args.save_model:
agent.save_model(args.save_path)
def eval(args):
if args.data == 0:
data = CIFAR10Loader(batch_size=128, train=False, drop_last=False)
multi_task_type = 'binary'
elif args.data == 1:
data = CIFAR100Loader(batch_size=128, train=False, drop_last=False)
multi_task_type = 'multiclass'
elif args.data == 2:
data = OmniglotLoader(batch_size=128, train=False, drop_last=False)
multi_task_type = 'multiclass'
else:
raise ValueError('Unknown data ID: {}'.format(args.data))
num_classes_single = data.num_classes_single
num_classes_multi = data.num_classes_multi
num_tasks = len(num_classes_multi)
num_channels = data.num_channels
if args.setting == 0:
agent = SingleTaskAgent(num_classes=num_classes_single,
num_channels=num_channels)
data = data.get_loader()
elif args.setting == 1:
agent = StandardAgent(num_classes_single=num_classes_single,
num_classes_multi=num_classes_multi,
multi_task_type=multi_task_type,
num_channels=num_channels)
elif args.setting == 2:
assert args.task in list(range(num_tasks)), 'Unknown task: {}'.format(args.task)
agent = SingleTaskAgent(num_classes=num_classes_multi[args.task],
num_channels=num_channels)
data = data.get_loader(args.task)
elif args.setting == 3 or args.setting == 4:
agent = MultiTaskSeparateAgent(num_classes=num_classes_multi,
num_channels=num_channels)
elif args.setting == 5 or args.setting == 6:
agent = MultiTaskJointAgent(num_classes=num_classes_multi,
multi_task_type=multi_task_type,
num_channels=num_channels)
else:
raise ValueError('Unknown setting: {}'.format(args.setting))
agent.load_model(args.save_path)
accuracy = agent.eval(data)
print('Accuracy: {}'.format(accuracy))
def main():
args = parse_args()
if args.train:
train(args)
elif args.eval:
eval(args)
else:
print('No flag is assigned. Please assign either \'--train\' or \'--eval\'.')
if __name__ == '__main__':
main()