-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtrain.py
More file actions
268 lines (220 loc) · 8.65 KB
/
train.py
File metadata and controls
268 lines (220 loc) · 8.65 KB
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
import sys
import yaml
from os.path import join, abspath
import os
import argparse
import atexit
import dill
import shutil
from tqdm import trange, tqdm
import random
from models.util import get_model
import util
from util import Average, Logger
from evaluate import evaluate
from datasets import get_dataset
import torch
from torch.utils.data import DataLoader
from torch.profiler import profile, record_function, ProfilerActivity
CHECKPOINT_FREQ = 1
CHECKPOINT_DIRNAME = "checkpoints"
# global parameters, automatically saved on exit
train_history = None
model = None
config = None
RECORD_PROFILE = False
PROFILE_NBATCH = 5
def train_loop(model, train_dataloader, config, val_dataloader=None):
optimizer = torch.optim.AdamW(model.parameters(), **config['optimizer']['params'])
epochs = trange(config['num_epochs'], desc="Epoch")
sched = config.get("scheduler")
if sched is not None:
Sched = {
"cyclic_lr": torch.optim.lr_scheduler.CyclicLR
}[sched["type"]]
args = sched.get("args", [])
kwargs = sched.get("kwargs", {})
scheduler = Sched(optimizer, *args, **kwargs)
else:
scheduler = None
for epoch in epochs:
epoch_stats = {}
train_loss = Average()
train_acc = Average()
model.train()
## profile model computation on a few batches
if RECORD_PROFILE and epoch == 0:
print("Profiling model...")
with profile(activities=[
ProfilerActivity.CPU, ProfilerActivity.CUDA],
profile_memory=True, record_shapes=True) as prof:
for i, (x, l) in enumerate(train_dataloader):
if i == PROFILE_NBATCH:
break
optimizer.zero_grad()
with record_function(f"batch_{i}"):
yh = model(x)
loss = model.loss_function(yh, l)
with record_function(f"batch_{i}_backward"):
loss.backward()
prof.export_chrome_trace(join(log_dir, "training_chrome_trace.json"))
print("Operations sorted by CUDA time:")
print(prof.key_averages().table(sort_by="cuda_time_total", row_limit=100))
## train
batches = tqdm(train_dataloader, leave=False, desc="Batch", mininterval=10)
for (X, label) in batches:
optimizer.zero_grad()
# ffwd, compute training loss and acc
loss, acc = train_step(model, X, label)
optimizer.step()
if scheduler is not None:
scheduler.step()
train_loss.update(loss)
train_acc.update(acc)
batch_stats = {
"train_loss": loss,
"train_acc": acc,
"avg_tr_acc": train_acc.get(),
"avg_tr_loss": train_loss.get()
}
batches.set_postfix(batch_stats, refresh=False)
epoch_stats = {
"train_loss": train_loss.get(),
"train_acc": train_acc.get()
}
if val_dataloader:
val_stats = evaluate(model, val_dataloader)
epoch_stats = {
**epoch_stats,
"val_loss": val_stats['loss'],
"val_acc": val_stats['acc']
}
train_history.append(epoch_stats)
config['execution']['epochs_completed'] += 1
epochs.set_postfix(epoch_stats)
if (epoch+1) % CHECKPOINT_FREQ == 0:
checkpoint_dir = join(out_dir, CHECKPOINT_DIRNAME)
os.makedirs(checkpoint_dir, exist_ok=True)
model_class = config['model']['classname']
torch.save(model.state_dict(), join(checkpoint_dir, f"{model_class}_epoch{epoch}.pt"))
save_progress()
def train_step(model, X, label):
prediction = model(X)
loss = model.loss_function(prediction, label)
acc = model.accuracy(prediction, label)
loss.backward()
return loss.item(), acc.item()
def parse_args():
parser = argparse.ArgumentParser()
default_out_dir = f"out/test_out/{util.curr_time_str()}"
parser.add_argument("--out_dir", type=str, default=default_out_dir,
help="Output directory for this experiment only.")
parser.add_argument("--param", type=str, help="Base parameter file")
parsed = parser.parse_args()
return vars(parsed)
def create_dir(dir):
try:
os.makedirs(dir, exist_ok=False)
print(f"Created output directory '{dir}'")
except:
try:
overwrite = input(f"Warning: directory '{dir}' exists. " \
+ "Are you sure you want to overwrite its contents? [y/n] ")
except EOFError:
overwrite = "y"
if overwrite.lower() in ("yes", "y"):
shutil.rmtree(dir)
os.makedirs(dir, exist_ok=False)
else:
sys.exit(1)
WARNING = '\033[93m'
ENDC = '\033[0m'
# store model and history on checkpoint / program exit
def save_progress():
if model is not None:
model_class = config['model']['classname']
torch.save(model.state_dict(), join(out_dir, model_class + ".pt"))
if train_history is not None and len(train_history) > 0:
f = open(join(out_dir, "train_history.dill"), "wb")
dill.dump(train_history, f)
config['execution']['stats'] = train_history[-1]
# output parameter file within the folder
config['execution']['local_stop'] = util.curr_time_str()
f = open(join(out_dir, 'train_config.yaml'), "w")
yaml.safe_dump(config, f)
def generate_synthetic_data(dirname, n=100, num_classes=35, shape=None):
shutil.rmtree(dirname, ignore_errors=True)
os.makedirs(dirname, exist_ok=False)
for i in range(n):
if shape is None:
i_shape = (1, 50, random.randint(4, 10))
X = torch.randn(i_shape)
label = random.randint(0, num_classes-1)
obj = (X, label)
torch.save(obj, join(dirname, f"example_file_{i}.pt"))
if __name__ == "__main__":
config = parse_args()
param_file = config['param']
out_dir = config['out_dir'] = abspath(config['out_dir'])
f = open(param_file)
# import param file as dict
train_params = yaml.safe_load(f)
print(f"Loaded {param_file}")
config = {
**train_params,
**config,
'execution': {
'epochs_completed': 0,
'local_start': util.curr_time_str(),
'program_exit': 'FAILURE'
}
}
train_data_dir = config['train_data_dir']
if isinstance(train_data_dir, str):
train_data_dir = config['train_data_dir'] = abspath(config['train_data_dir'])
if 'val_data_dir' in config:
val_data_dir = config['val_data_dir']
if isinstance(val_data_dir, str):
val_data_dir = config['val_data_dir'] = abspath(config['val_data_dir'])
else:
val_data_dir = None
#generate_synthetic_data(train_data_dir, 10)
# (recursively) make output directory, solely for the experiment
log_dir = join(out_dir, "log")
create_dir(log_dir)
# hacky way to redirect to special output files,
# as shell redirection with slurm was getting odd with all the
# directories that needed to exists
sys.stderr = Logger(join(log_dir, "stderr.txt"), sys.stderr)
sys.stdout = Logger(join(log_dir, "stdout.txt"), sys.stdout)
atexit.register(save_progress)
config['device'] = 'cuda' if torch.cuda.is_available() else 'cpu'
print(f"Training using {config['device']}")
collate = None # util.collate_examples_list if config.get('collate') == 'single' \
#else util.collate_examples_pad
# load data
print("Loading training data")
train_data = get_dataset(train_data_dir, device=config['device'])
train_dataloader = DataLoader(
train_data, config['batch_size'], shuffle=True,
collate_fn=collate
)
val_dataloader = None
if val_data_dir is not None:
print("Loading validation data")
val_data = get_dataset(val_data_dir, device=config['device'])
val_dataloader = DataLoader(
val_data, config['batch_size'], shuffle=True,
collate_fn=collate
)
else:
print("No validation dataset given.")
model = get_model(config)
print("Model Architecture:")
print(model)
config['model']['classname'] = model.__class__.__name__
config['model']['num_params'] = sum(p.numel() for p in model.parameters() if p.requires_grad)
print("Total parameters:", config['model']['num_params'])
train_history = []
train_loop(model, train_dataloader, config, val_dataloader)
config['execution']['program_exit'] = 'SUCCESS'