-
Notifications
You must be signed in to change notification settings - Fork 13
/
main.py
executable file
·232 lines (185 loc) · 9.45 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
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
import argparse
import os
import numpy as np
import torch
from Classifiers.Fashion_Classifier import Fashion_Classifier
from Classifiers.Mnist_Classifier import Mnist_Classifier
from Classifiers.Cifar_Classifier import Cifar_Classifier
from Data.data_loader import DataLoader
from Evaluation.Reviewer import Reviewer
from Generative_Models.BEGAN import BEGAN
from Generative_Models.CGAN import CGAN
from Generative_Models.CVAE import CVAE
from Generative_Models.GAN import GAN
from Generative_Models.VAE import VAE
from Generative_Models.WGAN import WGAN
from Generative_Models.WGAN_GP import WGAN_GP
from Training.Baseline import Baseline
from Training.Ewc import Ewc
from Training.Ewc_samples import Ewc_samples
from Training.Generative_Replay import Generative_Replay
from Training.Rehearsal import Rehearsal
from log_utils import log_test_done
from utils import check_args
from Evaluation.Eval_Classifier import Reviewer_C
"""parsing and configuration"""
def parse_args():
desc = "Pytorch implementation of GAN collections"
parser = argparse.ArgumentParser(description=desc)
parser.add_argument('--gan_type', type=str, default='CVAE',
choices=['GAN', 'Classifier', 'CGAN', 'BEGAN', 'WGAN',
'WGAN_GP', 'VAE', "CVAE", "WGAN_GP"],
help='The type of GAN') # , required=True)
parser.add_argument('--dataset', type=str, default='mnist', choices=['mnist', 'fashion', 'cifar10'],
help='The name of dataset')
parser.add_argument('--conditional', type=bool, default=False)
parser.add_argument('--upperbound', type=bool, default=False,
help='This variable will be set to true automatically if task_type contains_upperbound')
parser.add_argument('--method', type=str, default='Baseline', choices=['Baseline', 'Ewc', 'Ewc_samples',
'Generative_Replay', 'Rehearsal'])
parser.add_argument('--context', type=str, default='Generation',
choices=['Classification', 'Generation', 'Not_Incremental'])
parser.add_argument('--dir', type=str, default='./Archives/', help='Working directory')
parser.add_argument('--save_dir', type=str, default='models', help='Directory name to save the model')
parser.add_argument('--result_dir', type=str, default='results', help='Directory name to save results')
parser.add_argument('--sample_dir', type=str, default='Samples', help='Directory name to save the generated images')
parser.add_argument('--log_dir', type=str, default='logs', help='Directory name to save training logs')
parser.add_argument('--data_dir', type=str, default='Data', help='Directory name for data')
parser.add_argument('--gen_dir', type=str, default='.', help='Directory name for data')
parser.add_argument('--epochs', type=int, default=1, help='The number of epochs to run')
parser.add_argument('--epoch_G', type=int, default=1, help='The number of epochs to run')
parser.add_argument('--epoch_Review', type=int, default=50, help='The number of epochs to run')
parser.add_argument('--batch_size', type=int, default=64, help='The size of batch')
parser.add_argument('--size_epoch', type=int, default=1000)
parser.add_argument('--gpu_mode', type=bool, default=True)
parser.add_argument('--device', type=int, default=0)
parser.add_argument('--verbose', type=bool, default=False)
parser.add_argument('--lrG', type=float, default=0.0002)
parser.add_argument('--lrD', type=float, default=0.0002)
parser.add_argument('--lrC', type=float, default=0.01)
parser.add_argument('--momentum', type=float, default=0.5, metavar='M', help='SGD momentum (default: 0.5)')
parser.add_argument('--beta1', type=float, default=0.5)
parser.add_argument('--beta2', type=float, default=0.999)
parser.add_argument('--seed', type=int, default=1664)
parser.add_argument('--eval', type=bool, default=True)
parser.add_argument('--train_G', type=bool, default=False)
parser.add_argument('--eval_C', type=bool, default=False)
############### UNUSED FLAGS ##########################
parser.add_argument('--trainEval', type=bool, default=False)
parser.add_argument('--knn', type=bool, default=False)
parser.add_argument('--IS', type=bool, default=False)
parser.add_argument('--FID', type=bool, default=False)
parser.add_argument('--Fitting_capacity', type=bool, default=False)
#######################################################
parser.add_argument('--num_task', type=int, default=10)
parser.add_argument('--num_classes', type=int, default=10)
parser.add_argument('--sample_transfer', type=int, default=5000)
parser.add_argument('--task_type', type=str, default="disjoint",
choices=['disjoint', 'permutations', 'upperbound_disjoint'])
parser.add_argument('--samples_per_task', type=int, default=200)
parser.add_argument('--lambda_EWC', type=int, default=5)
parser.add_argument('--nb_samples_reharsal', type=int, default=10)
parser.add_argument('--regenerate', type=bool, default=False)
return check_args(parser.parse_args())
"""main"""
def main():
# parse arguments
args = parse_args()
if args is None:
exit()
seed = args.seed
torch.manual_seed(seed)
np.random.seed(seed)
args.gpu_mode = torch.cuda.is_available()
if args.gpu_mode:
torch.backends.cudnn.deterministic = True
torch.cuda.manual_seed_all(args.seed)
if args.context == 'Generation':
print("Generation : Use of model {} with dataset {}, seed={}".format(args.gan_type, args.dataset, args.seed))
elif args.context == 'Classification':
print("Classification : Use of method {} with dataset {}, seed={}".format(args.method, args.dataset, args.seed))
if args.context == 'Generation':
# declare instance for GAN
if args.gan_type == 'GAN':
model = GAN(args)
elif args.gan_type == 'CGAN':
model = CGAN(args)
elif args.gan_type == 'VAE':
model = VAE(args)
elif args.gan_type == 'CVAE':
model = CVAE(args)
elif args.gan_type == 'WGAN':
model = WGAN(args)
elif args.gan_type == 'WGAN_GP':
model = WGAN_GP(args)
else:
raise Exception("[!] There is no option for " + args.gan_type)
elif args.context == 'Classification':
if args.dataset == 'mnist':
model = Mnist_Classifier(args)
elif args.dataset == 'fashion':
model = Fashion_Classifier(args)
elif args.dataset == 'cifar10':
model = Cifar_Classifier(args)
else:
print('Not implemented')
reviewer = Reviewer(args)
if args.method == 'Baseline':
method = Baseline(model, args, reviewer)
elif 'Ewc' in args.method:
method = Ewc(model, args)
elif 'Ewc_samples' in args.method:
method = Ewc_samples(model, args)
elif args.method == 'Generative_Replay':
method = Generative_Replay(model, args)
elif args.method == 'Rehearsal':
method = Rehearsal(model, args)
else:
print('Method not implemented')
if args.context == 'Classification':
if args.eval_C:
reviewer_C = Reviewer_C(args)
list_values = [10, 50, 100, 200, 500, 1000, 5000, 10000]
reviewer_C.review_all_tasks(args, list_values)
else:
method.run_classification_tasks()
elif args.context == 'Generation':
if args.train_G:
method.run_generation_tasks()
log_test_done(args, 'Intermediate')
if args.regenerate:
method.regenerate_datasets_for_eval()
if args.Fitting_capacity and not args.train_G:
reviewer = Reviewer(args)
# In case the training training and evaluation are done separately
if args.gpu_mode:
torch.backends.cudnn.deterministic = True
torch.cuda.manual_seed_all(args.seed)
reviewer.review_all_tasks(args)
if args.method == "Baseline" and not args.upperbound:
# Baseline produce both one lower bound and one upperbound
# it is not the same upperbound a the one trained for upperbound_disjoint
reviewer.review_all_tasks(args, Best=True)
if args.FID and not args.train_G:
reviewer = Reviewer(args)
# In case the training training and evaluation are done separately
if args.gpu_mode:
torch.backends.cudnn.deterministic = True
torch.cuda.manual_seed_all(args.seed)
reviewer.compute_all_tasks_FID(args)
if args.method == "Baseline" and not args.upperbound:
# Baseline produce both one lower bound and one upperbound
# it is not the same upperbound a the one trained for upperbound_disjoint
reviewer.compute_all_tasks_FID(args, Best=True)
if args.trainEval:
reviewer = Reviewer(args)
reviewer.review_all_trainEval(args)
if args.method == "Baseline" and not args.upperbound:
# Baseline produce both one lower bound and one upperbound
# it is not the same upperbound a the one trained for upperbound_disjoint
reviewer.review_all_trainEval(args, Best=True)
else:
print('Not Implemented')
log_test_done(args)
if __name__ == '__main__':
main()