-
Notifications
You must be signed in to change notification settings - Fork 13
/
train.py
216 lines (195 loc) · 8.78 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
"""
Training script. Should be pretty adaptable to whatever.
"""
import argparse
import os
import shutil
import multiprocessing
import numpy as np
import pandas as pd
import torch
from allennlp.common.params import Params
from allennlp.training.learning_rate_schedulers import LearningRateScheduler
from allennlp.training.optimizers import Optimizer
from torch.nn import DataParallel
from torch.nn.modules import BatchNorm2d
from tqdm import tqdm
from dataloaders.vcr import VCR, VCRLoader
from utils.pytorch_misc import time_batch, save_checkpoint, clip_grad_norm, \
restore_checkpoint, print_para, restore_best_checkpoint
import logging
logging.basicConfig(format='%(asctime)s - %(levelname)s - %(name)s - %(message)s', level=logging.DEBUG)
# This is needed to make the imports work
from allennlp.models import Model
import models
#################################
#################################
######## Data loading stuff
#################################
#################################
parser = argparse.ArgumentParser(description='train')
parser.add_argument(
'-params',
dest='params',
help='Params location',
type=str,
)
parser.add_argument(
'-rationale',
action="store_true",
help='use rationale',
)
parser.add_argument(
'-folder',
dest='folder',
help='folder location',
type=str,
)
parser.add_argument(
'-restore',
dest='restore',
action="store_true",
help="reload checkpoint"
)
parser.add_argument(
'-train',
dest='train',
action="store_true",
help="train checkpoint"
)
parser.add_argument(
'-test',
dest='test',
action="store_true",
help="test checkpoint"
)
parser.add_argument(
'-no_tqdm',
dest='no_tqdm',
action='store_true',
)
args = parser.parse_args()
params = Params.from_file(args.params)
train, val, test = VCR.splits(mode='rationale' if args.rationale else 'answer',
embs_to_load=params['dataset_reader'].get('embs', 'bert_da'),
only_use_relevant_dets=params['dataset_reader'].get('only_use_relevant_dets', True))
NUM_GPUS = torch.cuda.device_count()
NUM_CPUS = multiprocessing.cpu_count()
if NUM_GPUS == 0:
raise ValueError("you need gpus!")
def _to_gpu(td):
if NUM_GPUS > 1:
return td
for k in td:
if k != 'metadata':
td[k] = {k2: v.cuda(non_blocking=True) for k2, v in td[k].items()} if isinstance(td[k], dict) else td[k].cuda(
non_blocking=True)
return td
num_workers = (4 * NUM_GPUS if NUM_CPUS == 32 else 2*NUM_GPUS)-1
print(f"Using {num_workers} workers out of {NUM_CPUS} possible", flush=True)
loader_params = {'batch_size': 96 // NUM_GPUS, 'num_gpus':NUM_GPUS, 'num_workers':num_workers}
train_loader = VCRLoader.from_dataset(train, **loader_params)
val_loader = VCRLoader.from_dataset(val, **loader_params)
test_loader = VCRLoader.from_dataset(test, **loader_params)
ARGS_RESET_EVERY = 100
print("Loading {} for {}".format(params['model'].get('type', 'WTF?'), 'rationales' if args.rationale else 'answer'), flush=True)
model = Model.from_params(vocab=train.vocab, params=params['model'])
# for submodule in model.detector.backbone.modules():
# if isinstance(submodule, BatchNorm2d):
# submodule.track_running_stats = False
model = DataParallel(model).cuda() if NUM_GPUS > 1 else model.cuda()
optimizer = Optimizer.from_params([x for x in model.named_parameters() if x[1].requires_grad],
params['trainer']['optimizer'])
lr_scheduler_params = params['trainer'].pop("learning_rate_scheduler", None)
scheduler = LearningRateScheduler.from_params(optimizer, lr_scheduler_params) if lr_scheduler_params else None
if os.path.exists(args.folder) and args.restore:
print('restore is True')
print("Found folder! restoring", flush=True)
start_epoch, val_metric_per_epoch = restore_checkpoint(model, optimizer, serialization_dir=args.folder,
learning_rate_scheduler=scheduler)
else:
print("Making directories")
os.makedirs(args.folder, exist_ok=True)
start_epoch, val_metric_per_epoch = 0, []
shutil.copy2(args.params, args.folder)
param_shapes = print_para(model)
num_batches = 0
if args.train:
print('It is training!!!!!')
for epoch_num in range(start_epoch, params['trainer']['num_epochs'] + start_epoch):
train_results = []
norms = []
model.train()
for b, (time_per_batch, batch) in enumerate(time_batch(train_loader if args.no_tqdm else tqdm(train_loader), reset_every=ARGS_RESET_EVERY)):
batch = _to_gpu(batch)
optimizer.zero_grad()
output_dict = model(**batch)
loss = output_dict['loss'].mean() + output_dict['cnn_regularization_loss'].mean()
loss.backward()
num_batches += 1
if scheduler:
scheduler.step_batch(num_batches)
norms.append(
clip_grad_norm(model.named_parameters(), max_norm=params['trainer']['grad_norm'], clip=True, verbose=False)
)
optimizer.step()
train_results.append(pd.Series({'epoch': epoch_num,
'loss': output_dict['loss'].mean().item(),
'crl': output_dict['cnn_regularization_loss'].mean().item(),
'accuracy': (model.module if NUM_GPUS > 1 else model).get_metrics(
reset=(b % ARGS_RESET_EVERY) == 0)[
'accuracy'],
'sec_per_batch': time_per_batch,
'hr_per_epoch': len(train_loader) * time_per_batch / 3600,
}))
if b % ARGS_RESET_EVERY == 0 and b > 0:
norms_df = pd.DataFrame(pd.DataFrame(norms[-ARGS_RESET_EVERY:]).mean(), columns=['norm']).join(
param_shapes[['shape', 'size']]).sort_values('norm', ascending=False)
print("e{:2d}b{:5d}/{:5d}. norms: \nsumm:\n{}\n~~~~~~~~~~~~~~~~~~\n".format(
epoch_num, b, len(train_loader),
pd.DataFrame(train_results[-ARGS_RESET_EVERY:]).mean(),
), flush=True)
print("---\nTRAIN EPOCH {:2d}:\n{}\n----".format(epoch_num, pd.DataFrame(train_results).mean()))
val_probs = []
val_labels = []
val_loss_sum = 0.0
model.eval()
for b, (time_per_batch, batch) in enumerate(time_batch(val_loader)):
with torch.no_grad():
batch = _to_gpu(batch)
output_dict = model(**batch)
val_probs.append(output_dict['label_probs'].detach().cpu().numpy())
val_labels.append(batch['label'].detach().cpu().numpy())
val_loss_sum += output_dict['loss'].mean().item() * batch['label'].shape[0]
val_labels = np.concatenate(val_labels, 0)
val_probs = np.concatenate(val_probs, 0)
val_loss_avg = val_loss_sum / val_labels.shape[0]
val_metric_per_epoch.append(float(np.mean(val_labels == val_probs.argmax(1))))
if scheduler:
scheduler.step(val_metric_per_epoch[-1], epoch_num)
print("Val epoch {} has acc {:.3f} and loss {:.3f}".format(epoch_num, val_metric_per_epoch[-1], val_loss_avg),
flush=True)
if int(np.argmax(val_metric_per_epoch)) < (len(val_metric_per_epoch) - 1 - params['trainer']['patience']):
print("Stopping at epoch {:2d}".format(epoch_num))
break
save_checkpoint(model, optimizer, args.folder, epoch_num, val_metric_per_epoch,
is_best=int(np.argmax(val_metric_per_epoch)) == (len(val_metric_per_epoch) - 1))
if args.test:
print('It is validation!!!!')
print("STOPPING. now running the best model on the validation set", flush=True)
# Load best
restore_best_checkpoint(model, args.folder)
model.eval()
val_probs = []
val_labels = []
for b, (time_per_batch, batch) in enumerate(time_batch(val_loader)):
with torch.no_grad():
batch = _to_gpu(batch)
output_dict = model(**batch)
val_probs.append(output_dict['label_probs'].detach().cpu().numpy())
val_labels.append(batch['label'].detach().cpu().numpy())
val_labels = np.concatenate(val_labels, 0)
val_probs = np.concatenate(val_probs, 0)
acc = float(np.mean(val_labels == val_probs.argmax(1)))
print("Final val accuracy is {:.3f}".format(acc))
np.save(os.path.join(args.folder, f'valpreds.npy'), val_probs)