-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathtrain.py
311 lines (218 loc) · 10.1 KB
/
train.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
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
###################################
# Train script
###################################
import argparse
import os,sys
import time
import numpy as np
import torch
import torch.optim as optim
import torch.utils.data as torchdata
from torch import nn
from resnet import ResNetRatioEstimator
from data_utils import *
parser = argparse.ArgumentParser('Gamma Resnet')
parser.add_argument("--n_data", type=int, default=100000, help='Number of samples to load.')
parser.add_argument("--n_val", type=int, default=10000, help='Number of samples to load.')
parser.add_argument("--num_features", type=int, default=1, help='Number of features being studied.')
parser.add_argument('--resume', action='store_true')
parser.add_argument('--gpu', type=int, default=0)
parser.add_argument('--nepochs', type=int, default=75)
parser.add_argument('--batch_size', type=int, default=500)
parser.add_argument('--test_batch_size', type=int, default=1000)
parser.add_argument('--lr', type=float, default=1e-3)
parser.add_argument('--weight_decay', type=float, default=1e-5)
parser.add_argument('--dropout', type=float, default=0.0)
parser.add_argument('--label', type=str)
parser.add_argument('--load_dir', type=str, default=None)
parser.add_argument('--path_data', type=str, default=None, help='Path to train data.')
parser.add_argument('--path_val', type=str, default=None)
parser.add_argument('--subidx_file', type=str, default=None)
parser.add_argument('--epoch', type=int, default=0)
parser.add_argument('--optimizer', type=str, default='AdamW')
args = parser.parse_args()
device = torch.device('cuda:' + str(args.gpu) if torch.cuda.is_available() else 'cpu')
args.device = device
def count_parameters(model):
'''
Args:
model: NN in pytorch
Returns:
number of params in model
'''
return sum(p.numel() for p in model.parameters() if p.requires_grad)
def compute_loss(image, theta, loss_fn, model, device='cuda:0'):
'''
Args:
image (np.array, torch.Tensor): image input into model
theta (np.array, torch.Tensor): parameter of interesting corresponding to image
loss_fn: loss function predefined in torch
model (ResNetRatioEstimator)
device (optional, torch.device): default torch.device('cuda:0'); gpu device for sending x
Returns:
loss from inputing image and theta into model
'''
if type(image) != torch.Tensor:
image = torch.from_numpy(image).type(torch.float32).to(device)
else:
image = image.type(torch.float32).to(device)
if type(theta) != torch.Tensor:
theta = torch.from_numpy(theta).type(torch.float32).to(device)
else:
theta = theta.type(torch.float32).to(device)
batch_size = theta.shape[0]
output,_ = model(image, x_aux=theta)
# make the target classification labels
labels = torch.ones(2*batch_size).type_as(output) # two atoms
labels[1::2] = 0.0
loss = loss_fn(torch.reshape(output, labels.size()), labels)
return loss
def compute_test_loss(image, theta, loss_fn, model, device='cuda:0'):
'''
Args:
image (np.array, torch.Tensor): image input into model
theta (np.array, torch.Tensor): parameter of interesting corresponding to image
loss_fn: loss function predefined in torch
model (ResNetRatioEstimator)
device (optional, torch.device): default torch.device('cuda:0'); gpu device for sending x
Returns:
loss from inputing image and theta into model
'''
with torch.no_grad():
if type(image) != torch.Tensor:
image = torch.from_numpy(image).type(torch.float32).to(device)
else:
image = image.type(torch.float32).to(device)
if type(theta) != torch.Tensor:
theta = torch.from_numpy(theta).type(torch.float32).to(device)
else:
theta = theta.type(torch.float32).to(device)
batch_size = theta.shape[0]
output,_ = model(image, x_aux=theta)
# make the target classification labels
labels = torch.ones(2*batch_size).type_as(output) # two atoms
labels[1::2] = 0.0
loss = loss_fn(torch.reshape(output, labels.size()), labels)
return loss
##################################################################################################
PATH_data = args.path_data
PATH_val = PATH_data + args.path_val
print('Loading train images', flush=True)
print(PATH_data, flush=True)
# load in parameters
gammas = np.load(PATH_data + 'gammas_all.npy')[:args.n_data]
gammas_val = np.load(PATH_val + 'gammas_all.npy')[:args.n_val]
# load in pre computed mean and std of images
mean = np.load(PATH_data + 'im_mean_{}.npy'.format(args.n_data))
std = np.load(PATH_data + 'im_std_{}.npy'.format(args.n_data))
print(PATH_data + 'im_mean_{}'.format(args.n_data), flush=True)
# load validation set
print('Loading validation images', flush=True)
conditional = []
for i in range(args.n_val):
im = np.load(PATH_val + 'images/SLimage_{}.npy'.format(i+1))
conditional.append(im)
print('Shape of conditional: {}'.format(np.shape(conditional)), flush=True)
# whiten validation images (train images will be whitened in training loop)
conditional = (conditional - mean)/std
# whiten parameters
thetas = np.append(gammas, gammas_val) - np.mean(gammas, axis=0)
print('Shape of data: {}'.format(np.shape(thetas)), flush=True)
# make train+val sets
n_data = args.n_data
n_val = args.n_val
n_train = n_data
train_set = DatasetMixed(PATH_data + 'images/', thetas[:n_train], n_train)
val_set = LensingDataset(thetas[n_train:], conditional)
trainloader = torchdata.DataLoader(train_set, batch_size=args.batch_size, shuffle=True, pin_memory=True, num_workers=16)
valloader = torchdata.DataLoader(val_set, batch_size=args.test_batch_size, shuffle=False)
print('Number of batches: {}'.format(len(trainloader)))
################################################################################
# make or load directories
rootdir = PATH_data + 'models/'
if not args.resume:
save_dir = rootdir + '%s_%s_dout%s_lr%s_bs%s_ndata%s/' % (args.label, args.optimizer, args.dropout, args.lr, args.batch_size, args.n_data)
os.makedirs(save_dir, exist_ok=True)
print(save_dir, flush=True)
save_figures = save_dir + 'plots/'
os.makedirs(save_figures, exist_ok=True)
save_arrays = save_dir + 'arrays/'
os.makedirs(save_arrays,exist_ok=True)
ini, fin = 1, args.nepochs + 1
train_losses, val_losses = [],[]
else:
print('Loading saved info!', flush=True)
print('PATH: ' + args.load_dir, flush=True)
load_arrays = args.load_dir + 'arrays/'
save_figures = args.load_dir + 'plots/'
save_arrays = args.load_dir + 'arrays/'
ini, fin = args.epoch + 1, args.epoch + args.nepochs + 1
print('Retrieving loss curves')
train_losses = np.load(load_arrays + 'train_losses.npy').tolist()
val_losses = np.load(load_arrays + 'val_losses.npy').tolist()
train_losses = train_losses[:int(args.epoch*n_train/args.batch_size)]
val_losses = val_losses[:int(args.epoch*n_train/args.batch_size)]
#################################################################################
# initialize model
model = ResNetRatioEstimator(cfg=18, n_aux=1, n_out=args.num_features).to(device)
if (args.optimizer == 'AdamW'):
optimizer = optim.AdamW(model.parameters(), lr=args.lr, weight_decay=args.weight_decay)
elif (args.optimizer == 'SGD'):
optimizer = optim.SGD(model.parameters(), lr=args.lr, weight_decay=args.weight_decay, momentum=0.9, nesterov=True)
elif (args.optimizer == 'Adam'):
optimizer = optim.Adam(model.parameters(), lr=args.lr, weight_decay=args.weight_decay)
scheduler = optim.lr_scheduler.ReduceLROnPlateau(optimizer, mode='min', factor=0.1, patience=3, threshold=0.01, threshold_mode='abs', cooldown=2, verbose=True)
loss_fn = nn.BCELoss()
# load checkpoint if resuming training
if args.resume:
print('Loading checkpoint!', flush=True)
checkpoints = torch.load(load_arrays + 'epoch%s_checkpt.pth' % args.epoch)
args = checkpoints['args']
model.load_state_dict(checkpoints['state_dict'])
optimizer.load_state_dict(checkpoints['optimizer_state_dict'])
scheduler.load_state_dict(checkpoints['scheduler_state_dict'])
print(model)
print(count_parameters(model))
##################################################################################
# for early stopping
loss_prevepoch = np.inf
count_earlystop = 0
# train loop
for epoch in range(ini, fin):
print('\n Epoch %s' % epoch, flush=True)
# the sum of validation loss of all batches in a epoch
loss_total = 0.
for count, x in enumerate(trainloader):
model.train()
theta, x = x
x = (x - mean)/std # whiten data
optimizer.zero_grad()
loss = compute_loss(x, theta, loss_fn, model, device)
train_losses.append(loss.item())
loss.backward()
optimizer.step()
model.eval()
# average over the # of batches
val_loss = 0.
for count_val, y in enumerate(valloader):
theta, y = y
val_loss += compute_test_loss(y, theta, loss_fn, model, device)
val_losses.append(val_loss.item()/len(valloader))
loss_total += val_loss.item()/len(valloader)
loss_epoch = loss_total/len(trainloader)
print('Val loss: {}'.format(loss_epoch), flush=True)
scheduler.step(loss_epoch)
print('lr: {}'.format(optimizer.param_groups[0]['lr']), flush=True)
np.save(save_arrays + 'train_losses', train_losses)
np.save(save_arrays + 'val_losses', val_losses)
torch.save({'args': args,
'state_dict': model.state_dict(),
'optimizer_state_dict': optimizer.state_dict(),
'scheduler_state_dict': scheduler.state_dict()}, os.path.join(save_arrays, 'epoch%s_checkpt.pth' % epoch))
# early stopping
if loss_prevepoch - loss_epoch < 0.001:
count_earlystop += 1
else:
count_earlystop = 0
loss_prevepoch = loss_epoch
if count_earlystop == 6: break