-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtrain.py
347 lines (289 loc) · 10.7 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
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
'''
Train a encoder-only neural network
Author: Longshen Ou
'''
import os
import sys
import mlconfig
import random
import time
import numpy as np
import pandas as pd
import time
from tqdm import tqdm
from models.models import *
import torch
import torchvision as tv
import torch.nn as nn
import torch.nn.functional as F
import torchvision.transforms as T
import torchvision.transforms.functional as TF
from torch.utils.data import Dataset, DataLoader
from PIL import Image as Img
from utils import *
from dataset import MyDataset
from recon import recon
from torch.utils.tensorboard import SummaryWriter
random.seed(10)
np.random.seed(0)
torch.manual_seed(10)
def seed_worker(worker_id):
worker_seed = torch.initial_seed() % 2 ** 32
np.random.seed(worker_seed)
random.seed(worker_seed)
g = torch.Generator()
g.manual_seed(0)
def train(hparam, ctn=False):
model_name = hparam['model_name']
if hparam['use_tensorboard']:
if not os.path.exists(hparam['output_dir']):
os.mkdir(hparam['output_dir'])
else:
raise Exception('Log already exists!')
hparam.save(jpath(hparam['output_dir'], 'hparam.yaml'))
writer = SummaryWriter(jpath(hparam['output_dir'], 'tensorboard'))
# if not os.path.exists(hparam['log_fn']):
# with open(hparam['log_fn'], 'w'):
# pass
dataset_train = MyDataset(hparam['train_path'], split='train')
train_loader = DataLoader(
dataset=dataset_train,
batch_size=hparam['batch_size'],
shuffle=True,
num_workers=hparam['num_workers'],
worker_init_fn=seed_worker,
generator=g,
)
dataset_valid = MyDataset(hparam['valid_path'], split='valid')
valid_loader = DataLoader(
dataset=dataset_valid,
batch_size=hparam['batch_size'],
shuffle=False,
num_workers=hparam['num_workers'],
worker_init_fn=seed_worker,
generator=g,
)
dataset_test = MyDataset(hparam['test_path'], split='test')
test_loader = DataLoader(
dataset=dataset_test,
batch_size=hparam['batch_size'],
shuffle=False,
num_workers=hparam['num_workers'],
worker_init_fn=seed_worker,
generator=g,
)
# Model, optimizer, loss function, earlystopping
net = get_model(model_name, hparam).to(hparam['device'])
# if ctn == True:
# net.load_state_dict(torch.load('models/' + model_name + '.pth'))
print(net)
optimizer = torch.optim.AdamW(net.parameters(), lr=hparam['LR'], weight_decay=hparam['WEIGHT_DECAY'])
loss_func = nn.CrossEntropyLoss()
early_stopping = EarlyStopping(hparam['PATIENCE'], verbose=False, savefolder=hparam['output_dir'])
param_num = check_model(net)
with open(jpath(hparam['output_dir'], 'net_params.txt'), 'w') as f:
f.write('{}\n'.format(net))
f.write('Num of param: {:,}'.format(param_num))
if hparam['check_only'] == True:
return
# Training and validating
time_begin = time.time()
for epoch in range(hparam['EPOCH']):
net.train()
running_loss = 0.0
if epoch > 0:
pbar = tqdm(train_loader)
for step, batch in enumerate(pbar):
b_x, b_y = pack_batch(batch, hparam)
out = net(b_x)
out = torch.reshape(out, (-1, out.shape[-1]))
b_y = torch.reshape(b_y, (-1,))
# out = out.permute(0, 2, 1)
loss = loss_func(out, b_y)
optimizer.zero_grad()
loss.backward()
optimizer.step()
running_loss += loss.item()
pbar.set_description(
'Epoch: {} | Step {} / {} | loss {:.4f}'.format(epoch, step + 1, len(train_loader), loss.item()))
if epoch % hparam['REDUCE_EPOCH'] == 0 and epoch > 0:
for param_group in optimizer.param_groups:
param_group['lr'] *= hparam['REDUCE_FACTOR']
else:
running_loss = 1e9
avg_train_loss = running_loss / len(train_loader)
print('Epoch: {} | Training Loss: {:.4f} '.format(epoch, avg_train_loss), end='')
# Validating
running_loss = 0.0
net.eval()
with torch.no_grad():
for step, batch in enumerate(valid_loader):
batch_x, batch_y = pack_batch(batch, hparam)
out = net(batch_x.to(hparam['device']))
out = out.permute(0, 2, 1)
loss = loss_func(out, batch_y.to(hparam['device']))
running_loss += loss.item()
pred = compute_pred(out).flatten()
batch_y = batch_y.data.cpu().numpy().flatten()
avg_valid_loss = running_loss / len(valid_loader)
print(
' | Validation Loss: {:.4f} '.format(avg_valid_loss), end='')
# Validating by test data
running_loss = 0.0
with torch.no_grad():
for step, batch in enumerate(test_loader):
batch_x, batch_y = pack_batch(batch, hparam)
out = net(batch_x.to(hparam['device']))
out = out.permute(0, 2, 1)
loss = loss_func(out, batch_y.to(hparam['device']))
running_loss += loss.item()
avg_test_loss = running_loss / len(test_loader)
print(' | Test Loss: {:.4f} '.format(avg_test_loss))
# Visualization
if hparam['use_tensorboard']:
writer.add_scalars('Training Loss Graph', {'train_loss': avg_train_loss,
'validation_loss': avg_valid_loss,
'test_loss': avg_test_loss}, epoch)
log_str = 'Epoch: {} | Training Loss: {:.4f} | Validation Loss: {:.4f} | Test Loss: {:.4f}\n'.format(
epoch, avg_train_loss, avg_valid_loss, avg_test_loss)
with open(hparam['log_fn'], 'a') as f:
f.write(log_str)
time.sleep(0.5)
if hparam['EARLY_STOP']:
early_stopping(avg_valid_loss, net, epoch)
if early_stopping.early_stop == True:
print("Early Stopping!")
with open(hparam['log_fn'], 'a') as f:
f.write('Early stop at epoch {}. Best model from epoch {}'.format(epoch, early_stopping.best_epoch))
break
def infer(hparam):
'''
Generate noisy samples using the trained model.
'''
model_path = jpath(hparam['output_dir'], 'checkpoint.pth')
net = get_model(hparam['model_name'], hparam).to(hparam['device'])
net.load_state_dict(torch.load(model_path))
dataset_test = MyDataset(hparam['test_path'], split='test')
# test_loader = DataLoader(
# dataset=dataset_test,
# batch_size=hparam['batch_size'],
# shuffle=False,
# num_workers=hparam['num_workers'],
# worker_init_fn=seed_worker,
# generator=g,
# )
ret = {}
net.eval()
with torch.no_grad():
data = read_json(hparam['test_path'])
for id in tqdm(data):
clean = data[id]['ref']
t1, t2 = dataset_test.tokenize(clean)
input = torch.tensor([t1, t2], device=hparam['device'])
syn = []
for i in range(hparam['generation_coverage']):
out = net(input)
seq = logits_to_seq(out)
syn.append(post_process(seq, hparam['batch_size'])[0])
ret[id] = {'ref': clean, 'syn': syn}
# print_json(ret)
save_json(ret, jpath(hparam['output_dir'], 'synthesized.json'))
def evaluate(hparam):
'''
Perform reconstruction on the generated samples, and compare with reference.
'''
recon(
input_path=jpath(hparam['output_dir'], 'synthesized.json'),
coverage=hparam['generation_coverage'],
output_path=jpath(hparam['output_dir'], 'recon_out.json'),
result_root=hparam['output_dir'],
ref_data_path=jpath(hparam['dataset_root'], 'test.json'),
res_save_path=jpath(hparam['output_dir'], 'recon_result.json'),
fig_save_path=jpath(hparam['output_dir'], 'recon.png'),
recon_ref_path=hparam['recon_ref_path']
)
pass
def pack_batch(batch, hparam):
b_clean1 = batch[:, 0, :]
b_clean2 = batch[:, 1, :]
b_noisy1 = batch[:, 2, :]
b_noisy2 = batch[:, 3, :]
b_clean = torch.cat((b_clean1, b_clean2), dim=0)
b_noisy = torch.cat((b_noisy1, b_noisy2), dim=0)
b_x = b_clean.to(hparam['device'])
b_y = b_noisy.to(hparam['device'])
return b_x, b_y
def logits_to_seq(logits):
'''
Apply sampling to output logits, generate the bp sequence
logits: [bs*2, len/2, vocab_size]
'''
ret = torch.zeros(size=(logits.shape[0], logits.shape[1]), dtype=torch.long) # [bs*2, len/2]
t = torch.softmax(logits, dim=2) # [bs*2, len/2, vocab_size]
for i in range(ret.shape[0]):
for j in range(ret.shape[1]):
dist = t[i][j]
ch = np.random.choice(np.arange(5), p=dist.cpu().numpy())
ret[i][j] = ch
return ret
def post_process(out, bs):
'''
Resume a batch of data back to full length
Ensure the characters are legal
out: a batch of output
'''
# assert out.shape[0] // 2 == bs
assert out.shape[0] % 2 == 0
half = out.shape[0] // 2
vocab_dict = {
1: 'A',
2: 'T',
3: 'C',
4: 'G',
}
out_1, out_2 = out[:half], out[half:]
# print(out_1.shape, out_2.shape)
ret = []
for i in range(half):
t1 = depad(out_1[i].tolist())
t2 = depad(out_2[i].tolist())
t = t1 + t2[::-1]
# Convert ids to characters
# While ensure there is no '0' inside the sequence
res = []
for j in t:
if j != 0:
res.append(vocab_dict[j])
else:
res.append(vocab_dict[random.choice([1, 2, 3, 4])])
res = ''.join(res)
ret.append(res)
return ret
def depad(seq):
'''
Remove 0 at the end of sequence
seq: a list of numbers
'''
while seq[-1] == 0:
seq.pop(-1)
return seq
def compute_pred(out):
pred_y = out.detach()
pred_y[pred_y >= 0.5] = 1
pred_y[pred_y < 0.5] = 0
pred_y = pred_y.data.cpu().numpy()
return pred_y
def check_model(model):
pytorch_total_params = sum(p.numel() for p in model.parameters())
pytorch_train_params = sum(p.numel() for p in model.parameters() if p.requires_grad)
print('Totalparams:', format(pytorch_total_params, ','))
print('Trainableparams:', format(pytorch_train_params, ','))
return pytorch_train_params
if __name__ == '__main__':
arg_path = sys.argv[1] # e.g., './hparams/iterative_bt/s2t.yaml'
args = mlconfig.load(arg_path)
hparam = args
os.environ["CUDA_VISIBLE_DEVICES"] = hparam['gpu']
train(hparam)
infer(hparam)
evaluate(hparam)