forked from TLESORT/Generative_Continual_Learning
-
Notifications
You must be signed in to change notification settings - Fork 0
/
utils.py
executable file
·178 lines (144 loc) · 6.48 KB
/
utils.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
import os, gzip, torch
import torch.nn as nn
import numpy as np
import scipy.misc
import imageio
import matplotlib as mpl
mpl.use('Agg')
import matplotlib.pyplot as plt
from torchvision import datasets, transforms
from torch.autograd import Variable
"""checking arguments"""
def check_args(args):
if "Ewc" in args.method:
args.method = args.method + '_' + str(args.lambda_EWC)
args.save_dir = os.path.join(args.dir, args.save_dir)
# --save_dir
if not os.path.exists(args.save_dir):
os.makedirs(args.save_dir)
args.result_dir = os.path.join(args.dir, args.result_dir)
# --result_dir
if not os.path.exists(args.result_dir):
os.makedirs(args.result_dir)
args.log_dir = os.path.join(args.dir, args.log_dir)
# --result_dir
if not os.path.exists(args.log_dir):
os.makedirs(args.log_dir)
args.sample_dir = os.path.join(args.dir, args.sample_dir)
# --sample_dir
if not os.path.exists(args.sample_dir):
os.makedirs(args.sample_dir)
args.data_dir = os.path.join(args.dir, args.data_dir)
# --sample_dir
if not os.path.exists(args.data_dir):
os.makedirs(args.data_dir)
args.gen_dir = os.path.join(args.data_dir, 'Generated')
# --sample_dir
if not os.path.exists(args.gen_dir):
os.makedirs(args.gen_dir)
# --epoch
try:
assert args.epoch >= 1
except:
print('number of epochs must be larger than or equal to one')
# --batch_size
try:
assert args.batch_size >= 1
except:
print('batch size must be larger than or equal to one')
if 'upperbound' in args.task_type:
args.upperbound = True
elif (not 'upperbound' in args.task_type) and args.upperbound:
args.task_type = 'upperbound_' + args.task_type
args.data_file = args.task_type + '_' + str(args.num_task) + '.pt'
#
if args.gan_type == "VAE" and args.conditional:
args.gan_type = "CVAE"
if args.gan_type == "GAN" and args.conditional:
args.gan_type = "CGAN"
if args.context == 'Generation':
args.result_dir = os.path.join(args.result_dir, args.context, args.task_type, args.dataset, args.method,
args.gan_type,
'Num_tasks_' + str(args.num_task),
'seed_' + str(args.seed))
args.save_dir = os.path.join(args.save_dir, args.context, args.task_type, args.dataset, args.method,
args.gan_type,
'Num_tasks_' + str(args.num_task), 'seed_' + str(args.seed))
args.log_dir = os.path.join(args.log_dir, args.context, args.task_type, args.dataset, args.method,
args.gan_type,
'Num_tasks_' + str(args.num_task), 'seed_' + str(args.seed))
args.sample_dir = os.path.join(args.sample_dir, args.context, args.task_type, args.dataset, args.gan_type,
args.method,
'Num_tasks_' + str(args.num_task),
'seed_' + str(args.seed))
args.gen_dir = os.path.join(args.gen_dir, args.dataset, args.gan_type, args.task_type, args.method,
'Num_tasks_' + str(args.num_task),
'seed_' + str(args.seed))
elif args.context == 'Classification':
args.result_dir = os.path.join(args.result_dir, args.context, args.dataset, args.method,
'Num_tasks_' + str(args.num_task),
'seed_' + str(args.seed))
args.save_dir = os.path.join(args.save_dir, args.context, args.dataset, args.method,
'Num_tasks_' + str(args.num_task), 'seed_' + str(args.seed))
args.log_dir = os.path.join(args.log_dir, args.context, args.dataset, args.method,
'Num_tasks_' + str(args.num_task), 'seed_' + str(args.seed))
args.sample_dir = os.path.join(args.sample_dir, args.context, args.dataset, args.task_type,
'Num_tasks_' + str(args.num_task),
'seed_' + str(args.seed))
if not os.path.exists(args.result_dir):
os.makedirs(args.result_dir)
if not os.path.exists(args.save_dir):
os.makedirs(args.save_dir)
if not os.path.exists(args.log_dir):
os.makedirs(args.log_dir)
if not os.path.exists(args.sample_dir):
os.makedirs(args.sample_dir)
if not os.path.exists(args.gen_dir):
os.makedirs(args.gen_dir)
if args.gan_type == "CVAE" or args.gan_type == "CGAN":
args.conditional = True
print("Model : ", args.gan_type)
print("Dataset : ", args.dataset)
print("Method : ", args.method)
print("Seed : ", str(args.seed))
print("Context : ", args.context)
if args.FID:
print("Doing : FID")
if args.train_G:
print("Doing : Train_G")
if args.Fitting_capacity:
print("Doing : Fitting_capacity")
return args
def print_network(net):
num_params = 0
for param in net.parameters():
num_params += param.numel()
print(net)
print('Total number of parameters: %d' % num_params)
def initialize_weights(net):
for m in net.modules():
if isinstance(m, nn.Conv2d):
m.weight.data.normal_(0, 0.02)
m.bias.data.zero_()
elif isinstance(m, nn.ConvTranspose2d):
m.weight.data.normal_(0, 0.02)
m.bias.data.zero_()
elif isinstance(m, nn.Linear):
m.weight.data.normal_(0, 0.02)
m.bias.data.zero_()
def variable(t: torch.Tensor, use_cuda=True, **kwargs):
if torch.cuda.is_available() and use_cuda:
t = t.cuda()
return Variable(t, **kwargs)
def load_datasets(args):
print(args.data_file)
train_file = args.data_file.replace('.pt', '_train.pt')
test_file = args.data_file.replace('.pt', '_test.pt')
data_train = torch.load(os.path.join(args.data_dir, 'Tasks', args.dataset, train_file))
data_test = torch.load(os.path.join(args.data_dir, 'Tasks', args.dataset, test_file))
n_inputs = data_train[0][1].size(1)
n_outputs = 0
for i in range(len(data_train)):
n_outputs = max(n_outputs, data_train[i][2].max())
n_outputs = max(n_outputs, data_test[i][2].max())
return data_train, data_test, n_inputs, n_outputs + 1, len(data_train)