-
Notifications
You must be signed in to change notification settings - Fork 16
/
Copy pathmain.py
277 lines (232 loc) · 9.18 KB
/
main.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
#
# For licensing see accompanying LICENSE file.
# Copyright (C) 2020 Apple Inc. All Rights Reserved.
#
import os
import pathlib
import random
import numpy as np
import torch
import torch.nn as nn
from torch.utils.tensorboard import SummaryWriter
import data
import schedulers
import trainers
import utils
from args import args
def main():
# Seed.
if args.seed is not None:
random.seed(args.seed)
torch.manual_seed(args.seed)
torch.cuda.manual_seed(args.seed)
torch.cuda.manual_seed_all(args.seed)
np.random.seed(args.seed)
# If saving models or saving data, create a folder for storing these files.
# args.save => saving models, tensorboard logs, etc.
# args.save_data => just saving results.
if args.save or args.save_data:
i = 0
while True:
run_base_dir = pathlib.Path(
f"{args.log_dir}/{args.name}+try={str(i)}"
)
if not run_base_dir.exists():
os.makedirs(run_base_dir)
args.name = args.name + f"+try={i}"
break
i += 1
(run_base_dir / "settings.txt").write_text(str(args))
args.run_base_dir = run_base_dir
print(f"=> Saving data in {run_base_dir}")
# Get dataloader.
data_loader = getattr(data, args.set)()
curr_acc1 = 0.0
# Make a list of models, instead of a single model.
# This is not for training subspaces, but rather for the ensemble & SWA baselines.
models = [utils.get_model() for _ in range(args.num_models)]
# when training the SWA baseline, turn off the gradient to all but the first model.
if args.trainswa:
for i in range(1, args.num_models):
for p in models[i].parameters():
p.requires_grad = False
# Resume a model from a saved checkpoint.
num_models_filled = 0
num_models = -1
if args.resume:
for i, resume in enumerate(args.resume):
if type(resume) == tuple:
# can use a tuple to provide how many models to load.
resume, num_models = resume
if os.path.isfile(resume):
print(f"=> Loading checkpoint '{resume}'")
checkpoint = torch.load(resume, map_location="cpu")
pretrained_dicts = [
{k[7:]: v for k, v in c.items()}
for c in checkpoint["state_dicts"]
]
n = 0
for pretrained_dict in pretrained_dicts:
print(num_models_filled)
model_dict = models[num_models_filled].state_dict()
pretrained_dict = {
k: v
for k, v in pretrained_dict.items()
if k in model_dict
}
model_dict.update(pretrained_dict)
models[num_models_filled].load_state_dict(model_dict)
num_models_filled += 1
n += 1
if num_models > 0 and n >= num_models:
break
print(
f"=> Loaded checkpoint '{resume}' (epoch {checkpoint['epoch']})"
)
else:
print(f"=> No checkpoint found at '{resume}'")
# Put models on the GPU.
models = [utils.set_gpu(m) for m in models]
# Get training loss.
if args.label_smoothing is None:
criterion = nn.CrossEntropyLoss()
else:
print("adding label smoothing!")
criterion = LabelSmoothing(smoothing=args.label_smoothing)
criterion = criterion.to(args.device)
if args.save:
writer = SummaryWriter(log_dir=run_base_dir)
else:
writer = None
# Get the "trainer", which specified how the model is trained.
trainer = getattr(trainers, args.trainer or "default")
print(f"=> Using trainer {trainer}")
train, test = trainer.train, trainer.test
# Call "init" on the trainer.
trainer.init(models, writer, data_loader)
# Since we have have a list of models, we also use a list of optimizers & schedulers.
# When training subspaces, this list is of length 1.
metrics = {}
optimizers = [utils.get_optimizer(args, m) for m in models]
lr_schedulers = [
schedulers.get_policy(args.lr_policy or "cosine_lr")(o, args)
for o in optimizers
if o is not None
]
# more logic for resuming a checkpoint, specifically concerned with the "pretrained" argument.
# if args.pretrained, then we are not resuming. This means that we start from epoch 0.
# if not args.pretrained, we are resuming and have to set the epoch, etc. appropriately.
init_epoch = 0
num_models_filled = 0
if args.resume and not args.pretrained:
for i, resume in enumerate(args.resume):
if os.path.isfile(resume):
print(f"=> Loading checkpoint '{resume}'")
checkpoint = torch.load(resume, map_location="cpu")
init_epoch = checkpoint["epoch"]
curr_acc1 = checkpoint["curr_acc1"]
for opt in checkpoint["optimizers"]:
if args.late_start >= 0:
continue
optimizers[num_models_filled].load_state_dict(opt)
num_models_filled += 1
best_acc1 = 0.0
train_loss = 0.0
# Save the initialization.
if init_epoch == 0 and args.save:
print("saving checkpoint")
utils.save_cpt(init_epoch, 0, models, optimizers, best_acc1, curr_acc1)
# If the start epoch == the end epoch, just do evaluation "test".
if init_epoch == args.epochs:
curr_acc1, metrics = test(
models, writer, criterion, data_loader, init_epoch,
)
if args.save or args.save_data:
metrics["epoch"] = init_epoch
utils.write_result_to_csv(
name=args.name + f"+curr_epoch={init_epoch}",
curr_acc1=curr_acc1,
best_acc1=best_acc1,
train_loss=train_loss,
**metrics,
)
# Train from init_epoch -> args.epochs.
for epoch in range(init_epoch, args.epochs):
for lr_scheduler in lr_schedulers:
lr_scheduler(epoch, None)
train_loss = train(
models, writer, data_loader, optimizers, criterion, epoch,
)
if type(train_loss) is tuple:
train_loss, optimizers = train_loss
if (
args.test_freq is None
or (epoch % args.test_freq == 0)
or epoch == args.epochs - 1
):
curr_acc1, metrics = test(
models, writer, criterion, data_loader, epoch,
)
if curr_acc1 > best_acc1:
best_acc1 = curr_acc1
metrics["epoch"] = epoch + 1
# This is for the SWA baseline -- we need to lookup if this an epoch for which we are saving a checkpoint.
# If so we save a checkpoint and move it to the corresponding place in the models list.
if args.trainswa and (epoch + 1) in args.swa_save_epochs:
j = args.swa_save_epochs.index(epoch + 1)
for m1, m2 in zip(models[0].modules(), models[j].modules()):
if isinstance(m1, nn.Conv2d):
m2.weight = nn.Parameter(m1.weight.clone().detach())
m2.weight.requires_grad = False
elif isinstance(m1, nn.BatchNorm2d):
m2.weight = nn.Parameter(m1.weight.clone().detach())
m2.bias = nn.Parameter(m1.bias.clone().detach())
m2.weight.requires_grad = False
m2.bias.requires_grad = False
# Save checkpoint.
if (
args.save
and args.save_epochs is not None
and (epoch + 1) in args.save_epochs
):
it = (epoch + 1) * len(data_loader.train_loader)
utils.save_cpt(
epoch + 1, it, models, optimizers, best_acc1, curr_acc1
)
# Save results.
if args.save or args.save_data:
utils.write_result_to_csv(
name=args.name,
curr_acc1=curr_acc1,
best_acc1=best_acc1,
train_loss=train_loss,
**metrics,
)
# Save final checkpiont.
if args.save:
it = args.epochs * len(data_loader.train_loader)
utils.save_cpt(
args.epochs, it, models, optimizers, best_acc1, curr_acc1
)
return curr_acc1, metrics
class LabelSmoothing(nn.Module):
"""
NLL loss with label smoothing.
"""
def __init__(self, smoothing=0.0):
"""
Constructor for the LabelSmoothing module.
:param smoothing: label smoothing factor
"""
super(LabelSmoothing, self).__init__()
self.confidence = 1.0 - smoothing
self.smoothing = smoothing
def forward(self, x, target):
logprobs = torch.nn.functional.log_softmax(x, dim=-1)
nll_loss = -logprobs.gather(dim=-1, index=target.unsqueeze(1))
nll_loss = nll_loss.squeeze(1)
smooth_loss = -logprobs.mean(dim=-1)
loss = self.confidence * nll_loss + self.smoothing * smooth_loss
return loss.mean()
if __name__ == "__main__":
main()