-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathtrainer.py
380 lines (276 loc) · 11.1 KB
/
trainer.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
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
import os
import copy
import torch
import torch.nn as nn
import numpy as np
import pandas as pd
import matplotlib.pyplot as plt
from tqdm import tqdm
from scheduler import CosineAnnealingLR, CyclicLR
from torch.optim.lr_scheduler import StepLR
from utils import freeze_until, get_optimizer, set_lr, set_base_lr, get_lr
class Trainer():
def __init__(self, model, criterion, train_dl, val_dl, device, sizes, no_acc = False):
self.model = model
self.criterion = criterion
self.train_dl = train_dl
self.val_dl = val_dl
self.train_iteration_per_epoch = len(self.train_dl)
self.train_dset_size = sizes['train_dset_size']
self.val_dset_size = sizes['val_dset_size']
self.lr_list = []
self.device = device
self.no_acc = no_acc
def train(self, batch, optimizer, scheduler):
self.model.train()
if(scheduler is not None):
scheduler.step()
(images, labels) = batch
images = images.to(self.device)
labels = labels.to(self.device)
optimizer.zero_grad()
with torch.set_grad_enabled(True):
outputs = self.model(images)
if(self.no_acc == False):
_, preds = torch.max(outputs, 1)
loss = self.criterion(outputs, labels)
loss.backward()
optimizer.step()
cur_loss = loss.item() * labels.size(0)
cur_corrects = -1
if(self.no_acc == False):
cur_corrects = torch.sum(preds == labels).item()
return cur_loss, cur_corrects
def evaluate(self, dataloader, dset_size):
self.model.eval()
running_loss = 0.0
running_corrects = 0
for batch in dataloader:
(images, labels) = batch
images = images.to(self.device)
labels = labels.to(self.device)
with torch.set_grad_enabled(False):
outputs = self.model(images)
if(self.no_acc == False):
_, preds = torch.max(outputs, 1)
loss = self.criterion(outputs, labels)
running_loss += loss.item() * labels.size(0)
if(self.no_acc == False):
running_corrects += torch.sum(preds == labels).item()
eval_loss = running_loss / dset_size
eval_acc = -1
if(self.no_acc == False):
eval_acc = running_corrects / dset_size * 100.0
return eval_loss, eval_acc
def train_last_layer(self, lr, cycle_num, cycle_len = None, cycle_mult = None):
use_sgdr = True
if(cycle_len == None):
use_sgdr = False
cycle_len, cycle_mult = 1, 1
children_num = len(list(self.model.children()))
freeze_until(self.model, children_num - 2)
optimizer = get_optimizer(self.model, [lr], None)
cur_epoch = 0
for cycle in range(cycle_num):
scheduler = None
if(use_sgdr == True):
scheduler = CosineAnnealingLR(optimizer, 0, total = self.train_iteration_per_epoch * cycle_len)
for epoch in range(cycle_len):
cur_epoch += 1
running_loss = 0.0
running_corrects = 0
for batch in tqdm(self.train_dl):
cur_lr = get_lr(optimizer)
self.lr_list.append(cur_lr)
cur_loss, cur_corrects = self.train(batch, optimizer, scheduler)
running_loss += cur_loss
running_corrects += cur_corrects
epoch_loss_train = running_loss / self.train_dset_size
epoch_acc_train = -1
if(self.no_acc == False):
epoch_acc_train = running_corrects / self.train_dset_size * 100.0
epoch_loss_val, epoch_acc_val = self.evaluate(self.val_dl, self.val_dset_size)
print('Epoch : {}, Train Loss : {:.6f}, Train Acc : {:.6f}, Val Loss : {:.6f}, Val Acc : {:.6f}'.format(
cur_epoch, epoch_loss_train, epoch_acc_train, epoch_loss_val, epoch_acc_val))
cycle_len *= cycle_mult
return self.model
def train_last_layer_clr(self, lr_max, lr_min, epoch_num, cycle_num, div):
children_num = len(list(self.model.children()))
freeze_until(self.model, children_num - 2)
optimizer = get_optimizer(self.model, [lr_min], None)
cur_epoch = 0
for cycle in range(cycle_num):
scheduler = CyclicLR(optimizer, [lr_max], div, total = self.train_iteration_per_epoch * epoch_num)
for epoch in range(epoch_num):
cur_epoch += 1
running_loss = 0.0
running_corrects = 0
for batch in tqdm(self.train_dl):
cur_lr = get_lr(optimizer)
self.lr_list.append(cur_lr)
cur_loss, cur_corrects = self.train(batch, optimizer, scheduler)
running_loss += cur_loss
running_corrects += cur_corrects
epoch_loss_train = running_loss / self.train_dset_size
epoch_acc_train = -1
if(self.no_acc == False):
epoch_acc_train = running_corrects / self.train_dset_size * 100.0
epoch_loss_val, epoch_acc_val = self.evaluate(self.val_dl, self.val_dset_size)
print('Epoch : {}, Train Loss : {:.6f}, Train Acc : {:.6f}, Val Loss : {:.6f}, Val Acc : {:.6f}'.format(
cur_epoch, epoch_loss_train, epoch_acc_train, epoch_loss_val, epoch_acc_val))
return self.model
def train_all_layers(self, lrs, param_places=[1,2,3], cycle_num = 3, cycle_len = None, cycle_mult = None):
use_sgdr = True
if(cycle_len == None):
use_sgdr = False
cycle_len, cycle_mult = 1, 1
freeze_until(self.model, -1)
optimizer = get_optimizer(self.model, lrs, param_places)
cur_epoch = 0
for cycle in range(cycle_num):
scheduler = None
if(use_sgdr == True):
scheduler = CosineAnnealingLR(optimizer, 0, total = self.train_iteration_per_epoch * cycle_len)
for epoch in range(cycle_len):
cur_epoch += 1
running_loss = 0.0
running_corrects = 0
for batch in tqdm(self.train_dl):
cur_lr = get_lr(optimizer)
self.lr_list.append(cur_lr)
cur_loss, cur_corrects = self.train(batch, optimizer, scheduler)
running_loss += cur_loss
running_corrects += cur_corrects
epoch_loss_train = running_loss / self.train_dset_size
epoch_acc_train = -1
if(self.no_acc == False):
epoch_acc_train = running_corrects / self.train_dset_size * 100.0
epoch_loss_val, epoch_acc_val = self.evaluate(self.val_dl, self.val_dset_size)
print('Epoch : {}, Train Loss : {:.6f}, Train Acc : {:.6f}, Val Loss : {:.6f}, Val Acc : {:.6f}'.format(
cur_epoch, epoch_loss_train, epoch_acc_train, epoch_loss_val, epoch_acc_val))
cycle_len *= cycle_mult
return self.model
# self, lr_max, lr_min, epoch_num, div
def train_all_layers_clr(self, lrs_max, lrs_min, epoch_num, cycle_num, div, param_places=[1,2,3]):
freeze_until(self.model, -1)
optimizer = get_optimizer(self.model, lrs_min, param_places)
cur_epoch = 0
for cycle in range(cycle_num):
scheduler = CyclicLR(optimizer, lrs_max, div, total = self.train_iteration_per_epoch * epoch_num)
for epoch in range(epoch_num):
cur_epoch += 1
running_loss = 0.0
running_corrects = 0
for batch in tqdm(self.train_dl):
cur_lr = get_lr(optimizer)
self.lr_list.append(cur_lr)
cur_loss, cur_corrects = self.train(batch, optimizer, scheduler)
running_loss += cur_loss
running_corrects += cur_corrects
epoch_loss_train = running_loss / self.train_dset_size
epoch_acc_train = -1
if(self.no_acc == False):
epoch_acc_train = running_corrects / self.train_dset_size * 100.0
epoch_loss_val, epoch_acc_val = self.evaluate(self.val_dl, self.val_dset_size)
print('Epoch : {}, Train Loss : {:.6f}, Train Acc : {:.6f}, Val Loss : {:.6f}, Val Acc : {:.6f}'.format(
cur_epoch, epoch_loss_train, epoch_acc_train, epoch_loss_val, epoch_acc_val))
return self.model
def train_all_layers_scratch(self, lr, cycle_num = 3, cycle_len = None, cycle_mult = None):
use_sgdr = True
if(cycle_len == None):
use_sgdr = False
cycle_len, cycle_mult = 1, 1
freeze_until(self.model, -1)
optimizer = get_optimizer(self.model, [lr], None)
cur_epoch = 0
for cycle in range(cycle_num):
scheduler = None
if(use_sgdr == True):
scheduler = CosineAnnealingLR(optimizer, 0, total = self.train_iteration_per_epoch * cycle_len)
for epoch in range(cycle_len):
cur_epoch += 1
running_loss = 0.0
running_corrects = 0
for batch in tqdm(self.train_dl):
cur_lr = get_lr(optimizer)
self.lr_list.append(cur_lr)
cur_loss, cur_corrects = self.train(batch, optimizer, scheduler)
running_loss += cur_loss
running_corrects += cur_corrects
epoch_loss_train = running_loss / self.train_dset_size
epoch_acc_train = -1
if(self.no_acc == False):
epoch_acc_train = running_corrects / self.train_dset_size * 100.0
epoch_loss_val, epoch_acc_val = self.evaluate(self.val_dl, self.val_dset_size)
print('Epoch : {}, Train Loss : {:.6f}, Train Acc : {:.6f}, Val Loss : {:.6f}, Val Acc : {:.6f}'.format(
cur_epoch, epoch_loss_train, epoch_acc_train, epoch_loss_val, epoch_acc_val))
cycle_len *= cycle_mult
return self.model
def train_all_layers_scratch_clr(self, lr_max, lr_min, epoch_num, cycle_num, div):
freeze_until(self.model, -1)
optimizer = get_optimizer(self.model, [lr_min], None)
cur_epoch = 0
for cycle in range(cycle_num):
scheduler = CyclicLR(optimizer, [lr_max], div, total = self.train_iteration_per_epoch * epoch_num)
for epoch in range(epoch_num):
cur_epoch += 1
running_loss = 0.0
running_corrects = 0
for batch in tqdm(self.train_dl):
cur_lr = get_lr(optimizer)
self.lr_list.append(cur_lr)
cur_loss, cur_corrects = self.train(batch, optimizer, scheduler)
running_loss += cur_loss
running_corrects += cur_corrects
epoch_loss_train = running_loss / self.train_dset_size
epoch_acc_train = -1
if(self.no_acc == False):
epoch_acc_train = running_corrects / self.train_dset_size * 100.0
epoch_loss_val, epoch_acc_val = self.evaluate(self.val_dl, self.val_dset_size)
print('Epoch : {}, Train Loss : {:.6f}, Train Acc : {:.6f}, Val Loss : {:.6f}, Val Acc : {:.6f}'.format(
cur_epoch, epoch_loss_train, epoch_acc_train, epoch_loss_val, epoch_acc_val))
return self.model
def set_dl(self, train_dl, val_dl, sizes):
self.train_dl = train_dl
self.val_dl = val_dl
self.train_iteration_per_epoch = len(self.train_dl)
self.train_dset_size = sizes['train_dset_size']
self.val_dset_size = sizes['val_dset_size']
def lr_find(self, lr_start = 1e-6, lr_end = 10):
init_model_states = copy.deepcopy(self.model.state_dict())
children_num = len(list(self.model.children()))
freeze_until(self.model, children_num - 2)
t = (lr_end / lr_start) ** (1.0 / (len(self.train_dl) - 1))
optimizer = get_optimizer(self.model, [lr_start], None)
scheduler = StepLR(optimizer, step_size = 1, gamma = t)
records = []
for images, labels in tqdm(self.train_dl):
self.model.train()
scheduler.step()
images = images.to(self.device)
labels = labels.to(self.device)
optimizer.zero_grad()
with torch.set_grad_enabled(True):
outputs = self.model(images)
_, preds = torch.max(outputs, 1)
loss = self.criterion(outputs, labels)
loss.backward()
optimizer.step()
cur_lr = optimizer.param_groups[0]['lr']
cur_loss = loss.item()
records.append((cur_lr, cur_loss))
self.model.load_state_dict(init_model_states)
return records
def lr_find_plot(self, records):
lrs = [e[0] for e in records]
losses = [e[1] for e in records]
plt.figure(figsize = (6, 8))
plt.scatter(lrs, losses)
plt.xlabel('learning rates')
plt.ylabel('loss')
plt.xscale('log')
plt.yscale('log')
axes = plt.gca()
axes.set_xlim([lrs[0], lrs[-1]])
axes.set_ylim([min(losses) * 0.8, losses[0] * 4])
plt.show()