-
Notifications
You must be signed in to change notification settings - Fork 23
/
train_imagenet.py
280 lines (243 loc) · 9.32 KB
/
train_imagenet.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
# Some part borrowed from official tutorial https://github.com/pytorch/examples/blob/master/imagenet/main.py
from __future__ import print_function
from __future__ import absolute_import
import os
import sys
import numpy as np
import argparse
import importlib
import time
import logging
from pathlib import Path
import copy
import math
import torch
import torch.nn as nn
from torch.utils.data.dataset import Dataset
from torchvision import datasets, transforms
from torch.utils.tensorboard import SummaryWriter
import models
import data
from args import parse_args
from utils.schedules import get_lr_policy, get_optimizer
from utils.logging import (
save_checkpoint,
create_subdirs,
parse_configs_file,
clone_results_to_latest_subdir,
)
from utils.semisup import get_semisup_dataloader
from utils.model import (
get_layers,
prepare_model,
initialize_scaled_score,
show_gradients,
current_model_pruned_fraction,
sanity_check_paramter_updates,
)
# TODO: update wrn, resnet models. Save both subnet and dense version.
# TODO: take care of BN, bias in pruning, support structured pruning
def main():
args = parse_args()
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
result_sub_dir = os.path.join(
result_main_dir,
"{}--k-{:.2f}_trainer-{}_lr-{}_epochs-{}_warmuplr-{}_warmupepochs-{}".format(
n + 1,
args.k,
args.trainer,
args.lr,
args.epochs,
args.warmup_lr,
args.warmup_epochs,
),
)
else:
os.makedirs(result_main_dir, exist_ok=True)
result_sub_dir = os.path.join(
result_main_dir,
"1--k-{:.2f}_trainer-{}_lr-{}_epochs-{}_warmuplr-{}_warmupepochs-{}".format(
args.k,
args.trainer,
args.lr,
args.epochs,
args.warmup_lr,
args.warmup_epochs,
),
)
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)
# seed cuda
torch.manual_seed(args.seed)
torch.cuda.manual_seed(args.seed)
torch.cuda.manual_seed_all(args.seed)
np.random.seed(args.seed)
# Select GPUs
use_cuda = not args.no_cuda and torch.cuda.is_available()
gpu_list = [int(i) for i in args.gpu.strip().split(",")]
device = torch.device(f"cuda:{gpu_list[0]}" if use_cuda else "cpu")
# Create model
cl, ll = get_layers(args.layer_type)
if len(gpu_list) > 1:
print("Using multiple GPUs")
model = nn.DataParallel(
models.__dict__[args.arch](
cl, ll, args.init_type, num_classes=args.num_classes
),
gpu_list,
).to(device)
else:
model = models.__dict__[args.arch](
cl, ll, args.init_type, num_classes=args.num_classes
).to(device)
logger.info(model)
# Customize models for training/pruning/fine-tuning
prepare_model(model, args)
# Setup tensorboard writer
writer = SummaryWriter(os.path.join(result_sub_dir, "tensorboard"))
# Dataloader
D = data.__dict__[args.dataset](args, normalize=args.normalize)
train_loader, test_loader = D.data_loaders()
logger.info(args.dataset, D, len(train_loader.dataset), len(test_loader.dataset))
# Semi-sup dataloader
if args.is_semisup:
logger.info("Using semi-supervised training")
sm_loader = get_semisup_dataloader(args, D.tr_train)
else:
sm_loader = None
# autograd
criterion = nn.CrossEntropyLoss()
optimizer = get_optimizer(model, args)
lr_policy = get_lr_policy(args.lr_schedule)(optimizer, args)
logger.info([criterion, optimizer, lr_policy])
# 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)
model.load_state_dict(
checkpoint["state_dict"], strict=False
) # allows loading dense models
logger.info("=> loaded checkpoint '{}'".format(args.source_net))
else:
logger.info("=> no checkpoint found at '{}'".format(args.resume))
# Init scores once source net is loaded.
# NOTE: scaled_init_scores will overwrite the scores in the pre-trained net.
if args.scaled_score_init:
initialize_scaled_score(model)
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 preiovus settings.
if args.resume:
if os.path.isfile(args.resume):
logger.info("=> loading checkpoint '{}'".format(args.resume))
checkpoint = torch.load(args.resume)
args.start_epoch = checkpoint["epoch"]
best_prec1 = checkpoint["best_prec1"]
model.load_state_dict(checkpoint["state_dict"])
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))
if args.evaluate:
p1, _ = val(model, device, test_loader, criterion, args, writer)
logger.info(f"Validation accuracy {args.val_method} for source-net: {p1}")
if args.evaluate:
return
best_prec1 = 0
args.epochs = int(math.ceil(args.epochs / args.n_repeats))
logger.info("New logs")
logger.info(args)
show_gradients(model)
# Do not select source-net as last checkpoint as it might even be a dense model.
# Most other function won't works well with a dense layer checkpoint.
last_ckpt = copy.deepcopy(model.state_dict())
# Start training
for epoch in range(args.start_epoch, args.epochs + args.warmup_epochs):
lr_policy(epoch) # adjust learning rate
# train
trainer(
model,
device,
train_loader,
sm_loader,
criterion,
optimizer,
epoch,
args,
writer,
)
# evaluate on test set
print("Evaluating on a weaker attack to save time!")
if args.val_method == "smooth":
prec1, radii = val(
model, device, test_loader, criterion, args, writer, epoch
)
logger.info(f"Epoch {epoch}, mean provable Radii {radii}")
else:
prec1, _ = val(model, device, test_loader, criterion, args, writer, epoch)
# remember best prec@1 and save checkpoint
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,
)
logger.info(
f"Epoch {epoch}, val-method {args.val_method}, validation accuracy {prec1}, best_prec {best_prec1}"
)
if args.exp_mode in ["prune", "finetune"]:
logger.info(
"Pruned model: {:.2f}%".format(
current_model_pruned_fraction(
model, os.path.join(result_sub_dir, "checkpoint"), verbose=False
)
)
)
# clone results to latest subdir (sync after every epoch)
# Latest_subdir: stores results from latest run of an experiment.
clone_results_to_latest_subdir(
result_sub_dir, os.path.join(result_main_dir, "latest_exp")
)
# Check what parameters got updated in the current epoch.
sw, ss = sanity_check_paramter_updates(model, last_ckpt)
logger.info(
f"Sanity check (exp-mode: {args.exp_mode}): Weight update - {sw}, Scores update - {ss}"
)
current_model_pruned_fraction(
model, os.path.join(result_sub_dir, "checkpoint"), verbose=True
)
if __name__ == "__main__":
main()