-
Notifications
You must be signed in to change notification settings - Fork 0
/
train.py
267 lines (230 loc) · 8.75 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
from __future__ import absolute_import
from __future__ import print_function
import importlib
import logging
import os
import time
from pathlib import Path
import numpy as np
import torch
import torch.nn as nn
import datasets
import models
from args import parse_args
from utils.general_utils import (
save_checkpoint,
create_subdirs,
parse_configs_file,
clone_results_to_latest_subdir,
setup_seed
)
from utils.model import (
get_layers,
prepare_model,
initialize_scaled_score,
scale_rand_init,
current_model_pruned_fraction,
)
from utils.schedules import get_lr_policy, get_optimizer
class EarlyStopper:
def __init__(self, patience=1, min_delta=0):
self.patience = patience
self.min_delta = min_delta
self.counter = 0
self.max_validation_prec = -1
def early_stop(self, validation_prec):
if validation_prec > self.max_validation_prec:
self.max_validation_prec = validation_prec
self.counter = 0
elif validation_prec < (self.max_validation_prec + self.min_delta):
self.counter += 1
if self.counter >= self.patience:
return True
return False
def main():
args = parse_args()
if args.configs is not None:
parse_configs_file(args)
# sanity checks
if args.exp_mode in ["prune", "finetune"] and not args.resume:
assert args.source_net, "Provide checkpoint to prune/finetune"
# create resutls dir (for logs, checkpoints, etc.)
result_main_dir = os.path.join(Path(args.result_dir), args.exp_name, args.exp_mode)
if os.path.exists(result_main_dir):
n = len(next(os.walk(result_main_dir))[-2]) # prev experiments with same name
else:
n = 0
os.makedirs(result_main_dir, exist_ok=True)
result_sub_dir = os.path.join(
result_main_dir,
"{}--k-{:.4f}_trainer-{}_epochs-{}_arch-{}".format(
n,
args.k,
args.trainer,
args.epochs,
args.arch,
),
)
create_subdirs(result_sub_dir)
# add logger
logging.basicConfig(level=logging.INFO, format="%(message)s")
logger = logging.getLogger()
logger.addHandler(
logging.FileHandler(os.path.join(result_sub_dir, "setup.log"), "a")
)
logger.info(args)
setup_seed(args.seed)
device = "cuda:0" if torch.cuda.is_available() else "cpu"
# Create model
# ConvLayer and LinearLayer are classes, not instances.
ConvLayer, LinearLayer = get_layers(args.layer_type)
unstructured = True if args.layer_type == "unstructured" else False
model = models.__dict__[args.arch](
ConvLayer, LinearLayer, num_classes=args.num_classes,
k=args.k, unstructured=unstructured
).to(device)
# Customize models for training/pruning/fine-tuning
prepare_model(model, args)
# Dataloader
D = datasets.__dict__[args.dataset](args, normalize=args.normalize)
train_loader, val_loader, test_loader = D.data_loaders()
# autograd
criterion = nn.CrossEntropyLoss()
optimizer = get_optimizer(model, args)
lr_policy = get_lr_policy(args.lr_schedule)(optimizer, args)
# For bi-level only
mask_optimizer = torch.optim.SGD(
model.parameters(),
lr=args.mask_lr,
momentum=args.momentum,
weight_decay=args.wd,
)
mask_lr_policy = get_lr_policy(args.mask_lr_schedule)(mask_optimizer, args)
# train & val method
trainer = importlib.import_module(f"trainer.{args.trainer}").train
val = getattr(importlib.import_module("utils.eval"), args.val_method)
# Load source_net (if checkpoint provided).
# Only load the state_dict (required for pruning and fine-tuning)
if args.source_net:
if os.path.isfile(args.source_net):
logger.info("=> loading source model from '{}'".format(args.source_net))
checkpoint = torch.load(args.source_net, map_location=device)
if args.source_net.split(".")[-1] == "pt":
checkpoint = {"state_dict": checkpoint}
model.load_state_dict(checkpoint["state_dict"], strict=False)
logger.info("=> loaded checkpoint '{}'".format(args.source_net))
else:
raise ValueError("=> no checkpoint found at '{}'".format(args.source_net))
# Init scores once source net is loaded.
if args.exp_mode == "prune":
if args.scaled_score_init:
# NOTE: scaled_init_scores will overwrite the scores in the pre-trained net.
initialize_scaled_score(model)
else:
# Scaled random initialization. Useful when training a high sparse net from scratch.
# If not used, a sparse net (without batch-norm) from scratch will not converge.
# With batch-norm its not really necessary.
scale_rand_init(model, args.k)
early_stopper = EarlyStopper(patience=10, min_delta=0.01)
best_prec1 = 0
start_epoch = 0
assert not (args.source_net and args.resume), (
"Incorrect setup: "
"resume => required to resume a previous experiment (loads all parameters)|| "
"source_net => required to start pruning/fine-tuning from a source model (only load state_dict)"
)
# resume (if checkpoint provided). Continue training with previous settings.
if args.resume:
if os.path.isfile(args.resume):
logger.info("=> loading checkpoint '{}'".format(args.resume))
checkpoint = torch.load(args.resume, map_location=device)
start_epoch = checkpoint["epoch"]
best_prec1 = checkpoint["best_prec1"]
model.load_state_dict(checkpoint["state_dict"], strict=False)
optimizer.load_state_dict(checkpoint["optimizer"])
logger.info(
"=> loaded checkpoint '{}' (epoch {})".format(
args.resume, checkpoint["epoch"]
)
)
else:
logger.info("=> no checkpoint found at '{}'".format(args.resume))
raise ValueError("=> no checkpoint found at '{}'".format(args.resume))
# Evaluate
if args.evaluate or args.exp_mode in ["finetune"]:
p1, _ = val(model, device, test_loader, criterion, args, None)
logger.info(f"Validation accuracy {args.val_method} for source-net: {p1}")
if args.evaluate:
return
# Start training
for epoch in range(start_epoch, args.epochs + args.warmup_epochs):
start = time.time()
lr_policy(epoch)
mask_lr_policy(epoch)
if args.trainer == "bilevel":
optimizer = (optimizer, mask_optimizer)
# train
trainer(
model,
device,
(train_loader, val_loader),
criterion,
optimizer,
epoch,
args,
)
# evaluate on test set
if args.val_method == "smooth":
prec1, radii = val(
model, device, test_loader, criterion, args, epoch
)
logger.info(f"Epoch {epoch}, mean provable Radii {radii}")
prec1, _ = val(model, device, test_loader, criterion, args, epoch)
# remember best prec@1 and save checkpoint
if args.trainer == "bilevel":
optimizer = optimizer[0]
is_best = prec1 > best_prec1
best_prec1 = max(prec1, best_prec1)
save_checkpoint(
{
"epoch": epoch + 1,
"arch": args.arch,
"state_dict": model.state_dict(),
"best_prec1": best_prec1,
"optimizer": optimizer.state_dict(),
},
is_best,
args,
result_dir=os.path.join(result_sub_dir, "checkpoint"),
save_dense=args.save_dense,
)
# clone_results_to_latest_subdir(
# result_sub_dir, os.path.join(result_main_dir, "latest_exp")
# )
logger.info("This epoch duration :{}".format(time.time() - start))
logger.info(
f"Epoch {epoch}, val-method {args.val_method}, validation accuracy {prec1}, best_prec {best_prec1}"
)
if early_stopper.early_stop(best_prec1):
break
save_checkpoint(
{
"epoch": args.epochs,
"arch": args.arch,
"state_dict": model.state_dict(),
"best_prec1": best_prec1,
"optimizer": optimizer.state_dict(),
},
True if args.epochs == 0 else False,
args,
result_dir=os.path.join(result_sub_dir, "checkpoint"),
save_dense=args.save_dense,
)
# clone_results_to_latest_subdir(
# result_sub_dir, os.path.join(result_main_dir, "latest_exp")
# )
current_model_pruned_fraction(
model, os.path.join(result_sub_dir, "checkpoint"), verbose=True
)
if __name__ == "__main__":
main()