-
Notifications
You must be signed in to change notification settings - Fork 0
/
engine.py
291 lines (254 loc) · 12.8 KB
/
engine.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
281
282
283
284
285
286
287
288
289
290
291
# Copyright (c) 2015-present, Facebook, Inc.
# All rights reserved.
"""
Train and eval functions used in main.py
"""
import math
import sys
from typing import Iterable, Optional
import gc
import torch
from apex import amp
from timm.data import Mixup
from timm.utils import accuracy
from utils import ModelEma
import utils
def train_one_epoch(model: torch.nn.Module, criterion,
data_loader: Iterable, optimizer: torch.optim.Optimizer, lr_schedule,
device: torch.device, epoch: int, loss_scaler, max_norm: float = 0,
model_ema: Optional[ModelEma] = None, mixup_fn: Optional[Mixup] = None,
set_training_mode = True, use_amp=False, args=None):
model.train(set_training_mode)
metric_logger = utils.MetricLogger(delimiter=" ")
metric_logger.add_meter('lr', utils.SmoothedValue(window_size=1, fmt='{value:.6f}'))
header = 'Epoch: [{}]'.format(epoch)
accum_iter = args.accum_iter
print_freq = 10
optimizer.zero_grad()
for data_iter_step, (samples, targets) in enumerate(metric_logger.log_every(data_loader, print_freq, header)):
samples = samples.to(device, non_blocking=True)
targets = targets.to(device, non_blocking=True)
if mixup_fn is not None:
samples, targets = mixup_fn(samples, targets)
if use_amp:
with torch.cuda.amp.autocast():
outputs = model(samples)
loss = criterion(samples, outputs, targets)
else:
outputs = model(samples)
loss = criterion(samples, outputs, targets)
loss_value = loss.item()
if not math.isfinite(loss_value):
print("Loss is {}, stopping training".format(loss_value))
sys.exit(1)
loss /= accum_iter
if use_amp:
with amp.scale_loss(loss, optimizer) as scaled_loss:
scaled_loss.backward()
if max_norm is not None: torch.nn.utils.clip_grad_norm_(amp.master_params(optimizer), max_norm)
else:
loss.backward()
if (data_iter_step + 1) % accum_iter == 0:
optimizer.step()
optimizer.zero_grad()
lr_schedule.step_update(epoch * len(data_loader) + data_iter_step)
torch.cuda.synchronize()
if model_ema is not None:
model_ema.update(model)
metric_logger.update(loss=loss_value)
metric_logger.update(lr=optimizer.param_groups[0]["lr"])
# gather the stats from all processes
metric_logger.synchronize_between_processes()
print("Averaged stats:", metric_logger)
return {k: meter.global_avg for k, meter in metric_logger.meters.items()}
def search_one_epoch(model: torch.nn.Module, criterion, target_flops,
data_loader: Iterable, optimizer_param: torch.optim.Optimizer,
optimizer_decoder: torch.optim.Optimizer, optimizer_arch: torch.optim.Optimizer,
lr_scheduler_param, lr_scheduler_arch, lr_scheduler_decoder,
device: torch.device, epoch: int, max_norm: float = 0,
model_ema: Optional[ModelEma] = None, mixup_fn: Optional[Mixup] = None,
set_training_mode = True, use_amp=False, finish_search=False, args=None, progressive=True, max_ratio=0.95, min_ratio=0.75):
model.train(set_training_mode)
metric_logger = utils.MetricLogger(delimiter=" ")
metric_logger.add_meter('lr_param', utils.SmoothedValue(window_size=1, fmt='{value:.6f}'))
header = 'Epoch: [{}]'.format(epoch)
print_freq = 10
accum_iter = args.accum_iter
if optimizer_decoder is not None:
metric_logger.add_meter('lr_decoder', utils.SmoothedValue(window_size=1, fmt='{value:.6f}'))
optimizer_decoder.zero_grad()
optimizer_param.zero_grad()
if not finish_search:
optimizer_arch.zero_grad()
execute_pruned = False
for data_iter_step, (samples, targets) in enumerate(metric_logger.log_every(data_loader, print_freq, header)):
samples = samples.to(device, non_blocking=True)
targets = targets.to(device, non_blocking=True)
if mixup_fn is not None:
samples, targets = mixup_fn(samples, targets)
# we use a per iteration (instead of per epoch) lr scheduler
if data_iter_step % accum_iter == 0:
if progressive:
if hasattr(model, 'module'):
model.module.adjust_masking_ratio(data_iter_step / len(data_loader) + epoch, args.warmup_epochs,
args.epochs, max_ratio=max_ratio, min_ratio=min_ratio)
else:
model.adjust_masking_ratio(data_iter_step / len(data_loader) + epoch, args.warmup_epochs,
args.epochs, max_ratio=max_ratio, min_ratio=min_ratio)
if hasattr(model, 'module'):
for m in model.module.searchable_modules:
if not m.finish_search:
m.update_w(data_iter_step / len(data_loader) + epoch, args.warmup_epochs)
else:
for m in model.searchable_modules:
if not m.finish_search:
m.update_w(data_iter_step / len(data_loader) + epoch, args.warmup_epochs)
if use_amp:
with torch.cuda.amp.autocast():
outputs, aux_loss = model(samples)
decoder_loss, score_loss = aux_loss
loss = criterion(samples, outputs, targets, model, 'arch', target_flops, finish_search)
if decoder_loss != 0.:
w_decoder = (loss / decoder_loss).data.clone()
loss_total = loss + w_decoder * decoder_loss
else:
loss_total = loss
if score_loss is not None:
loss_total += score_loss
else:
outputs, aux_loss = model(samples)
decoder_loss, score_loss = aux_loss
loss = criterion(samples, outputs, targets, model, 'arch', target_flops, finish_search)
if isinstance(loss, tuple):
base_loss, arch_loss = loss
loss_total = base_loss + arch_loss
else:
base_loss = loss.item()
loss_total = loss
if decoder_loss != 0.:
w_decoder = (base_loss / decoder_loss).data.clone()
loss_total += w_decoder * decoder_loss
if score_loss is not None:
loss_total += score_loss
loss_value = loss_total.item()
if not math.isfinite(loss_value):
print("Loss is {}, stopping training".format(loss_value))
sys.exit(1)
loss_total /= accum_iter
if use_amp:
if optimizer_decoder is not None and optimizer_arch is not None:
optimizer_group = [optimizer_param, optimizer_arch, optimizer_decoder]
elif optimizer_arch is not None:
optimizer_group = [optimizer_param, optimizer_arch]
elif optimizer_decoder is not None:
optimizer_group = [optimizer_param, optimizer_decoder]
with amp.scale_loss(loss_total, optimizer_group, loss_id=0) as scaled_loss:
scaled_loss.backward()
if max_norm is not None:
torch.nn.utils.clip_grad_norm_(amp.master_params(optimizer_param), max_norm)
if optimizer_arch is not None:
torch.nn.utils.clip_grad_norm_(amp.master_params(optimizer_arch), max_norm)
if optimizer_decoder is not None:
torch.nn.utils.clip_grad_norm_(amp.master_params(optimizer_decoder), max_norm)
else:
loss_total.backward()
if (data_iter_step + 1) % accum_iter == 0:
torch.cuda.synchronize()
optimizer_param.step()
if optimizer_arch is not None:
optimizer_arch.step()
if optimizer_decoder is not None:
optimizer_decoder.step()
optimizer_param.zero_grad()
lr_scheduler_param.step_update(epoch * len(data_loader) + data_iter_step)
if optimizer_arch is not None:
optimizer_arch.zero_grad()
lr_scheduler_arch.step_update(epoch * len(data_loader) + data_iter_step)
if optimizer_decoder is not None:
optimizer_decoder.zero_grad()
lr_scheduler_decoder.step_update(epoch * len(data_loader) + data_iter_step)
torch.cuda.synchronize()
if model_ema is not None:
model_ema.update(model)
metric_logger.update(loss_param=base_loss)
metric_logger.update(loss_total=loss_value)
metric_logger.update(lr_param=optimizer_param.param_groups[0]["lr"])
if optimizer_arch is not None:
metric_logger.update(loss_arch=arch_loss.item())
metric_logger.update(lr_arch=optimizer_arch.param_groups[0]["lr"])
if optimizer_decoder is not None and not isinstance(decoder_loss, float):
metric_logger.update(loss_decoder=decoder_loss.item())
metric_logger.update(lr_decoder=optimizer_decoder.param_groups[0]["lr"])
# UPDATING ARCHs
if not finish_search and (data_iter_step + 1) % accum_iter == 0 and ((data_iter_step + 1) // accum_iter) % (len(data_loader) // 3 // accum_iter) == 0:
print('Start Compression')
torch.cuda.synchronize()
finish_search, execute_prune, optimizer_param, optimizer_decoder, optimizer_arch = model.module.compress(
0.2, optimizer_param, optimizer_decoder, optimizer_arch)
execute_pruned |= execute_prune
if finish_search:
optimizer_arch = None
lr_scheduler_arch = None
torch.cuda.synchronize()
if model_ema is not None:
model_ema.update(model)
# gather the stats from all processes
metric_logger.synchronize_between_processes()
print("Averaged stats:", metric_logger)
stats = {k: meter.global_avg for k, meter in metric_logger.meters.items()}
return stats, finish_search, execute_pruned, optimizer_param, optimizer_decoder, optimizer_arch
@torch.no_grad()
def evaluate(data_loader, model, device, use_amp=False):
criterion = torch.nn.CrossEntropyLoss()
metric_logger = utils.MetricLogger(delimiter=" ")
header = 'Test:'
# switch to evaluation mode
model.eval()
for images, target in metric_logger.log_every(data_loader, 10, header):
images = images.to(device, non_blocking=True)
target = target.to(device, non_blocking=True)
# compute output
if use_amp:
with torch.cuda.amp.autocast():
output, _ = model(images)
loss = criterion(output, target)
else:
output, _ = model(images)
loss = criterion(output, target)
acc1, acc5 = accuracy(output, target, topk=(1, 5))
batch_size = images.shape[0]
metric_logger.update(loss=loss.item())
metric_logger.meters['acc1'].update(acc1.item(), n=batch_size)
metric_logger.meters['acc5'].update(acc5.item(), n=batch_size)
# gather the stats from all processes
# metric_logger.synchronize_between_processes()
print('* Acc@1 {top1.global_avg:.3f} Acc@5 {top5.global_avg:.3f} loss {losses.global_avg:.3f}'
.format(top1=metric_logger.acc1, top5=metric_logger.acc5, losses=metric_logger.loss))
return {k: meter.global_avg for k, meter in metric_logger.meters.items()}
@torch.no_grad()
def evaluate_finetune(data_loader, model, device, use_amp=False):
criterion = torch.nn.CrossEntropyLoss()
metric_logger = utils.MetricLogger(delimiter=" ")
header = 'Test:'
model.eval()
for images, target in metric_logger.log_every(data_loader, 10, header):
images = images.to(device, non_blocking=True)
target = target.to(device, non_blocking=True)
# compute output
if use_amp:
with torch.cuda.amp.autocast():
output = model(images)
loss = criterion(output, target)
else:
output = model(images)
loss = criterion(output, target)
acc1, acc5 = accuracy(output, target, topk=(1, 5))
batch_size = images.shape[0]
metric_logger.update(loss=loss.item())
metric_logger.meters['acc1'].update(acc1.item(), n=batch_size)
metric_logger.meters['acc5'].update(acc5.item(), n=batch_size)
# gather the stats from all processes
metric_logger.synchronize_between_processes()
print('* Acc@1 {top1.global_avg:.3f} Acc@5 {top5.global_avg:.3f} loss {losses.global_avg:.3f}'
.format(top1=metric_logger.acc1, top5=metric_logger.acc5, losses=metric_logger.loss))
return {k: meter.global_avg for k, meter in metric_logger.meters.items()}