-
Notifications
You must be signed in to change notification settings - Fork 0
/
find_lr.py
executable file
·702 lines (581 loc) · 27.5 KB
/
find_lr.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
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
# adapted from https://github.com/davidtvs/pytorch-lr-finder
import copy
import os
import torch
import numpy as np
from tqdm.autonotebook import tqdm
from torch.optim.lr_scheduler import _LRScheduler
import matplotlib.pyplot as plt
from torch.utils.data import DataLoader
from monai.losses import DiceLoss
import torch.nn as nn
from packaging import version
PYTORCH_VERSION = version.parse(torch.__version__)
# Check available backends for mixed precision training
AVAILABLE_AMP_BACKENDS = []
try:
import apex.amp
AVAILABLE_AMP_BACKENDS.append("apex")
except ImportError:
pass
try:
import torch.amp
AVAILABLE_AMP_BACKENDS.append("torch")
except ImportError:
pass
def cal_iou(result, reference):
intersection = torch.count_nonzero(torch.logical_and(result, reference), dim=[i for i in range(1, result.ndim)])
union = torch.count_nonzero(torch.logical_or(result, reference), dim=[i for i in range(1, result.ndim)])
iou = intersection.float() / union.float()
return iou.unsqueeze(1)
class DataLoaderIter(object):
def __init__(self, data_loader):
self.data_loader = data_loader
self._iterator = iter(data_loader)
@property
def dataset(self):
return self.data_loader.dataset
def inputs_labels_from_batch(self, batch_data):
images = batch_data['image']
gt2D = batch_data['gt2D']
boxes = batch_data['bboxes']
return images, gt2D, boxes
def __iter__(self):
return self
def __next__(self):
batch = next(self._iterator)
return self.inputs_labels_from_batch(batch)
class TrainDataLoaderIter(DataLoaderIter):
def __init__(self, data_loader, auto_reset=True):
super().__init__(data_loader)
self.auto_reset = auto_reset
def __next__(self):
try:
batch = next(self._iterator)
images, gt2D, boxes = self.inputs_labels_from_batch(batch)
except StopIteration:
if not self.auto_reset:
raise
self._iterator = iter(self.data_loader)
batch = next(self._iterator)
images, gt2D, boxes = self.inputs_labels_from_batch(batch)
return images, gt2D, boxes
class ValDataLoaderIter(DataLoaderIter):
"""This iterator will reset itself **only** when it is acquired by
the syntax of normal `iterator`. That is, this iterator just works
like a `torch.data.DataLoader`. If you want to restart it, you
should use it like:
```
loader_iter = ValDataLoaderIter(data_loader)
for batch in loader_iter:
...
# `loader_iter` should run out of values now, you can restart it by:
# 1. the way we use a `torch.data.DataLoader`
for batch in loader_iter: # __iter__ is called implicitly
...
# 2. passing it into `iter()` manually
loader_iter = iter(loader_iter) # __iter__ is called by `iter()`
```
"""
def __init__(self, data_loader):
super().__init__(data_loader)
self.run_limit = len(self.data_loader)
self.run_counter = 0
def __iter__(self):
if self.run_counter >= self.run_limit:
self._iterator = iter(self.data_loader)
self.run_counter = 0
return self
def __next__(self):
self.run_counter += 1
return super(ValDataLoaderIter, self).__next__()
class LRFinder(object):
"""Learning rate range test.
The learning rate range test increases the learning rate in a pre-training run
between two boundaries in a linear or exponential manner. It provides valuable
information on how well the network can be trained over a range of learning rates
and what is the optimal learning rate.
Arguments:
model (torch.nn.Module): wrapped model.
optimizer (torch.optim.Optimizer): wrapped optimizer where the defined learning
is assumed to be the lower boundary of the range test.
criterion (torch.nn.Module): wrapped loss function.
device (str or torch.device, optional): a string ("cpu" or "cuda") with an
optional ordinal for the device type (e.g. "cuda:X", where is the ordinal).
Alternatively, can be an object representing the device on which the
computation will take place. Default: None, uses the same device as `model`.
memory_cache (boolean, optional): if this flag is set to True, `state_dict` of
model and optimizer will be cached in memory. Otherwise, they will be saved
to files under the `cache_dir`.
cache_dir (string, optional): path for storing temporary files. If no path is
specified, system-wide temporary directory is used. Notice that this
parameter will be ignored if `memory_cache` is True.
amp_backend (string, optional): backend for mixed precision training. Currently
only `torch.amp` and `apex.amp` are supported. If it's not specified,
mixed precision training is disabled.
amp_config (dict, optional): config for `torch.amp.autocast()` only.
grad_scaler (torch.cuda.amp.GradScaler, optional): gradient scaler for
`torch.amp` only.
Example:
>>> lr_finder = LRFinder(net, optimizer, criterion, device="cuda")
>>> lr_finder.range_test(dataloader, end_lr=100, num_iter=100)
>>> lr_finder.plot() # to inspect the loss-learning rate graph
>>> lr_finder.reset() # to reset the model and optimizer to their initial state
Reference:
Cyclical Learning Rates for Training Neural Networks: https://arxiv.org/abs/1506.01186
fastai/lr_find: https://github.com/fastai/fastai
"""
def __init__(
self,
model,
optimizer,
device=None,
memory_cache=True,
cache_dir=None,
amp_backend=None,
amp_config=None,
grad_scaler=None,
):
# Check if the optimizer is already attached to a scheduler
self.optimizer = optimizer
self._check_for_scheduler()
self.model = model
self.seg_loss = DiceLoss(sigmoid=True, squared_pred=True, reduction='mean')
self.ce_loss = nn.BCEWithLogitsLoss(reduction='mean')
self.iou_loss = nn.MSELoss(reduction='mean')
self.history = {"lr": [], "loss": []}
self.best_loss = None
self.memory_cache = memory_cache
self.cache_dir = cache_dir
# Settings related to mixed precision training
if amp_backend and (amp_backend not in AVAILABLE_AMP_BACKENDS):
raise ValueError("Unknown amp backend: {}".format(amp_backend))
if amp_backend == "torch":
if grad_scaler is None:
raise ValueError("`grad_scaler` is required when using `torch.amp`")
self.amp_backend = amp_backend
self.amp_config = amp_config
self.grad_scaler = grad_scaler
# Save the original state of the model and optimizer so they can be restored if
# needed
self.model_device = next(self.model.parameters()).device
self.state_cacher = StateCacher(memory_cache, cache_dir=cache_dir)
self.state_cacher.store("model", self.model.state_dict())
self.state_cacher.store("optimizer", self.optimizer.state_dict())
# If device is None, use the same as the model
if device:
self.device = device
else:
self.device = self.model_device
def reset(self):
"""Restores the model and optimizer to their initial states."""
self.model.load_state_dict(self.state_cacher.retrieve("model"))
self.optimizer.load_state_dict(self.state_cacher.retrieve("optimizer"))
self.model.to(self.model_device)
def range_test(
self,
train_loader,
val_loader=None,
start_lr=None,
end_lr=10,
num_iter=100,
step_mode="exp",
smooth_f=0.05,
diverge_th=5,
accumulation_steps=1,
non_blocking_transfer=True,
):
"""Performs the learning rate range test.
Arguments:
train_loader (`torch.utils.data.DataLoader`
or child of `TrainDataLoaderIter`, optional):
the training set data loader.
If your dataset (data loader) returns a tuple (inputs, labels,*) then
Pytorch data loader object can be provided. However, if a dataset
returns different outputs e.g. dicts, then you should inherit
from `TrainDataLoaderIter` class and redefine `inputs_labels_from_batch`
method so that it outputs (inputs, labels).
val_loader (`torch.utils.data.DataLoader`
or child of `ValDataLoaderIter`, optional): if `None` the range test
will only use the training loss. When given a data loader, the model is
evaluated after each iteration on that dataset and the evaluation loss
is used. Note that in this mode the test takes significantly longer but
generally produces more precise results.
Similarly to `train_loader`, if your dataset outputs are not standard
you should inherit from `ValDataLoaderIter` class and
redefine method `inputs_labels_from_batch` so that
it outputs (inputs, labels). Default: None.
start_lr (float, optional): the starting learning rate for the range test.
Default: None (uses the learning rate from the optimizer).
end_lr (float, optional): the maximum learning rate to test. Default: 10.
num_iter (int, optional): the number of iterations over which the test
occurs. Default: 100.
step_mode (str, optional): one of the available learning rate policies,
linear or exponential ("linear", "exp"). Default: "exp".
smooth_f (float, optional): the loss smoothing factor within the [0, 1[
interval. Disabled if set to 0, otherwise the loss is smoothed using
exponential smoothing. Default: 0.05.
diverge_th (int, optional): the test is stopped when the loss surpasses the
threshold: diverge_th * best_loss. Default: 5.
accumulation_steps (int, optional): steps for gradient accumulation. If it
is 1, gradients are not accumulated. Default: 1.
non_blocking_transfer (bool, optional): when non_blocking_transfer is set,
tries to convert/move data to the device asynchronously if possible,
e.g., moving CPU Tensors with pinned memory to CUDA devices. Default: True.
Example (fastai approach):
>>> lr_finder = LRFinder(net, optimizer, criterion, device="cuda")
>>> lr_finder.range_test(dataloader, end_lr=100, num_iter=100)
Example (Leslie Smith's approach):
>>> lr_finder = LRFinder(net, optimizer, criterion, device="cuda")
>>> lr_finder.range_test(trainloader, val_loader=val_loader, end_lr=1, num_iter=100, step_mode="linear")
Gradient accumulation is supported; example:
>>> train_data = ... # prepared dataset
>>> desired_bs, real_bs = 32, 4 # batch size
>>> accumulation_steps = desired_bs // real_bs # required steps for accumulation
>>> dataloader = torch.utils.data.DataLoader(train_data, batch_size=real_bs, shuffle=True)
>>> acc_lr_finder = LRFinder(net, optimizer, criterion, device="cuda")
>>> acc_lr_finder.range_test(dataloader, end_lr=10, num_iter=100, accumulation_steps=accumulation_steps)
If your DataLoader returns e.g. dict, or other non standard output, intehit from TrainDataLoaderIter,
redefine method `inputs_labels_from_batch` so that it outputs (inputs, lables) data:
>>> import torch_lr_finder
>>> class TrainIter(torch_lr_finder.TrainDataLoaderIter):
>>> def inputs_labels_from_batch(self, batch_data):
>>> return (batch_data['user_features'], batch_data['user_history']), batch_data['y_labels']
>>> train_data_iter = TrainIter(train_dl)
>>> finder = torch_lr_finder.LRFinder(model, optimizer, partial(model._train_loss, need_one_hot=False))
>>> finder.range_test(train_data_iter, end_lr=10, num_iter=300, diverge_th=10)
Reference:
[Training Neural Nets on Larger Batches: Practical Tips for 1-GPU, Multi-GPU & Distributed setups](
https://medium.com/huggingface/ec88c3e51255)
[thomwolf/gradient_accumulation](https://gist.github.com/thomwolf/ac7a7da6b1888c2eeac8ac8b9b05d3d3)
"""
# Reset test results
self.history = {"lr": [], "loss": []}
self.best_loss = None
# Move the model to the proper device
self.model.to(self.device)
# Check if the optimizer is already attached to a scheduler
self._check_for_scheduler()
# Set the starting learning rate
if start_lr:
self._set_learning_rate(start_lr)
# Initialize the proper learning rate policy
if step_mode.lower() == "exp":
lr_schedule = ExponentialLR(self.optimizer, end_lr, num_iter)
elif step_mode.lower() == "linear":
lr_schedule = LinearLR(self.optimizer, end_lr, num_iter)
else:
raise ValueError("expected one of (exp, linear), got {}".format(step_mode))
if smooth_f < 0 or smooth_f >= 1:
raise ValueError("smooth_f is outside the range [0, 1[")
# Create an iterator to get data batch by batch
if isinstance(train_loader, DataLoader):
train_iter = TrainDataLoaderIter(train_loader)
elif isinstance(train_loader, TrainDataLoaderIter):
train_iter = train_loader
else:
raise ValueError(
"`train_loader` has unsupported type: {}."
"Expected types are `torch.utils.data.DataLoader`"
"or child of `TrainDataLoaderIter`.".format(type(train_loader))
)
if val_loader:
if isinstance(val_loader, DataLoader):
val_iter = ValDataLoaderIter(val_loader)
elif isinstance(val_loader, ValDataLoaderIter):
val_iter = val_loader
else:
raise ValueError(
"`val_loader` has unsupported type: {}."
"Expected types are `torch.utils.data.DataLoader`"
"or child of `ValDataLoaderIter`.".format(type(val_loader))
)
for iteration in tqdm(range(num_iter)):
# Train on batch and retrieve loss
loss = self._train_batch(
train_iter,
accumulation_steps,
non_blocking_transfer=non_blocking_transfer,
)
if val_loader:
loss = self._validate(
val_iter, non_blocking_transfer=non_blocking_transfer
)
# Update the learning rate
self.history["lr"].append(lr_schedule.get_lr()[0])
lr_schedule.step()
# Track the best loss and smooth it if smooth_f is specified
if iteration == 0:
self.best_loss = loss
else:
if smooth_f > 0:
loss = smooth_f * loss + (1 - smooth_f) * self.history["loss"][-1]
if loss < self.best_loss:
self.best_loss = loss
# Check if the loss has diverged; if it has, stop the test
self.history["loss"].append(loss)
if loss > diverge_th * self.best_loss:
print("Stopping early, the loss has diverged")
break
print("Learning rate search finished. See the graph with {finder_name}.plot()")
def _set_learning_rate(self, new_lrs):
if not isinstance(new_lrs, list):
new_lrs = [new_lrs] * len(self.optimizer.param_groups)
if len(new_lrs) != len(self.optimizer.param_groups):
raise ValueError(
"Length of `new_lrs` is not equal to the number of parameter groups "
+ "in the given optimizer"
)
for param_group, new_lr in zip(self.optimizer.param_groups, new_lrs):
param_group["lr"] = new_lr
def _check_for_scheduler(self):
for param_group in self.optimizer.param_groups:
if "initial_lr" in param_group:
raise RuntimeError("Optimizer already has a scheduler attached to it")
def _train_batch(self, train_iter, accumulation_steps, non_blocking_transfer=True):
self.model.train()
total_loss = None # for late initialization
self.optimizer.zero_grad()
for i in range(accumulation_steps):
image, gt2D, boxes = next(train_iter)
image, gt2D, boxes = image.to(self.device), gt2D.to(self.device), boxes.to(self.device)
# Forward pass
if self.amp_backend == "torch":
with torch.amp.autocast(**self.amp_config):
outputs = self.model(inputs)
loss = self.criterion(outputs, labels)
else:
logits_pred, iou_pred = self.model(image, boxes)
l_seg = self.seg_loss(logits_pred, gt2D)
l_ce = self.ce_loss(logits_pred, gt2D.float())
iou_gt = cal_iou(torch.sigmoid(logits_pred) > 0.5, gt2D.bool())
l_iou = self.iou_loss(iou_pred, iou_gt)
loss = l_ce * l_ce + l_seg * l_seg + l_iou * l_iou
# Loss should be averaged in each step
loss /= accumulation_steps
# Backward pass
if self.amp_backend == "torch":
self.grad_scaler.scale(loss).backward()
elif self.amp_backend == "apex" and hasattr(self.optimizer, "_amp_stash"):
with apex.amp.scale_loss(loss, self.optimizer) as scaled_loss:
scaled_loss.backward()
else:
loss.backward()
if total_loss is None:
total_loss = loss
else:
total_loss += loss
if self.amp_backend == "torch":
self.grad_scaler.step(self.optimizer)
self.grad_scaler.update()
else:
self.optimizer.step()
return total_loss.item()
def _move_to_device(self, inputs, labels, non_blocking=True):
def move(obj, device, non_blocking=True):
if hasattr(obj, "to"):
return obj.to(device, non_blocking=non_blocking)
elif isinstance(obj, tuple):
return tuple(move(o, device, non_blocking) for o in obj)
elif isinstance(obj, list):
return [move(o, device, non_blocking) for o in obj]
elif isinstance(obj, dict):
return {k: move(o, device, non_blocking) for k, o in obj.items()}
else:
return obj
inputs = move(inputs, self.device, non_blocking=non_blocking)
labels = move(labels, self.device, non_blocking=non_blocking)
return inputs, labels
def _validate(self, val_iter, non_blocking_transfer=True):
# Set model to evaluation mode and disable gradient computation
running_loss = 0
self.model.eval()
with torch.no_grad():
for inputs, labels in val_iter:
# Move data to the correct device
inputs, labels = self._move_to_device(
inputs, labels, non_blocking=non_blocking_transfer
)
# Forward pass and loss computation
outputs = self.model(inputs)
loss = self.criterion(outputs, labels)
running_loss += loss.item() * len(labels)
return running_loss / len(val_iter.dataset)
def plot(
self,
skip_start=10,
skip_end=5,
log_lr=True,
show_lr=None,
ax=None,
suggest_lr=True,
):
"""Plots the learning rate range test.
Arguments:
skip_start (int, optional): number of batches to trim from the start.
Default: 10.
skip_end (int, optional): number of batches to trim from the start.
Default: 5.
log_lr (bool, optional): True to plot the learning rate in a logarithmic
scale; otherwise, plotted in a linear scale. Default: True.
show_lr (float, optional): if set, adds a vertical line to visualize the
specified learning rate. Default: None.
ax (matplotlib.axes.Axes, optional): the plot is created in the specified
matplotlib axes object and the figure is not be shown. If `None`, then
the figure and axes object are created in this method and the figure is
shown . Default: None.
suggest_lr (bool, optional): suggest a learning rate by
- 'steepest': the point with steepest gradient (minimal gradient)
you can use that point as a first guess for an LR. Default: True.
Returns:
The matplotlib.axes.Axes object that contains the plot,
and the suggested learning rate (if set suggest_lr=True).
"""
if skip_start < 0:
raise ValueError("skip_start cannot be negative")
if skip_end < 0:
raise ValueError("skip_end cannot be negative")
if show_lr is not None and not isinstance(show_lr, float):
raise ValueError("show_lr must be float")
# Get the data to plot from the history dictionary. Also, handle skip_end=0
# properly so the behaviour is the expected
lrs = self.history["lr"]
losses = self.history["loss"]
if skip_end == 0:
lrs = lrs[skip_start:]
losses = losses[skip_start:]
else:
lrs = lrs[skip_start:-skip_end]
losses = losses[skip_start:-skip_end]
# Create the figure and axes object if axes was not already given
fig = None
if ax is None:
fig, ax = plt.subplots()
# Plot loss as a function of the learning rate
ax.plot(lrs, losses)
# Plot the suggested LR
if suggest_lr:
# 'steepest': the point with steepest gradient (minimal gradient)
print("LR suggestion: steepest gradient")
min_grad_idx = None
try:
min_grad_idx = (np.gradient(np.array(losses))).argmin()
except ValueError:
print(
"Failed to compute the gradients, there might not be enough points."
)
if min_grad_idx is not None:
print("Suggested LR: {:.2E}".format(lrs[min_grad_idx]))
ax.scatter(
lrs[min_grad_idx],
losses[min_grad_idx],
s=75,
marker="o",
color="red",
zorder=3,
label="steepest gradient",
)
ax.legend()
if log_lr:
ax.set_xscale("log")
ax.set_xlabel("Learning rate")
ax.set_ylabel("Loss")
if show_lr is not None:
ax.axvline(x=show_lr, color="red")
# Show only if the figure was created internally
if fig is not None:
plt.show()
plt.savefig('./workdir/rep_m1_0/finetune/lr.png')
if suggest_lr and min_grad_idx is not None:
return ax, lrs[min_grad_idx]
else:
return ax
class LinearLR(_LRScheduler):
"""Linearly increases the learning rate between two boundaries over a number of
iterations.
Arguments:
optimizer (torch.optim.Optimizer): wrapped optimizer.
end_lr (float): the final learning rate.
num_iter (int): the number of iterations over which the test occurs.
last_epoch (int, optional): the index of last epoch. Default: -1.
"""
def __init__(self, optimizer, end_lr, num_iter, last_epoch=-1):
self.end_lr = end_lr
if num_iter <= 1:
raise ValueError("`num_iter` must be larger than 1")
self.num_iter = num_iter
super(LinearLR, self).__init__(optimizer, last_epoch)
def get_lr(self):
# In earlier Pytorch versions last_epoch starts at -1, while in recent versions
# it starts at 0. We need to adjust the math a bit to handle this. See
# discussion at: https://github.com/davidtvs/pytorch-lr-finder/pull/42
if PYTORCH_VERSION < version.parse("1.1.0"):
curr_iter = self.last_epoch + 1
r = curr_iter / (self.num_iter - 1)
else:
r = self.last_epoch / (self.num_iter - 1)
return [base_lr + r * (self.end_lr - base_lr) for base_lr in self.base_lrs]
class ExponentialLR(_LRScheduler):
"""Exponentially increases the learning rate between two boundaries over a number of
iterations.
Arguments:
optimizer (torch.optim.Optimizer): wrapped optimizer.
end_lr (float): the final learning rate.
num_iter (int): the number of iterations over which the test occurs.
last_epoch (int, optional): the index of last epoch. Default: -1.
"""
def __init__(self, optimizer, end_lr, num_iter, last_epoch=-1):
self.end_lr = end_lr
if num_iter <= 1:
raise ValueError("`num_iter` must be larger than 1")
self.num_iter = num_iter
super(ExponentialLR, self).__init__(optimizer, last_epoch)
def get_lr(self):
# In earlier Pytorch versions last_epoch starts at -1, while in recent versions
# it starts at 0. We need to adjust the math a bit to handle this. See
# discussion at: https://github.com/davidtvs/pytorch-lr-finder/pull/42
if PYTORCH_VERSION < version.parse("1.1.0"):
curr_iter = self.last_epoch + 1
r = curr_iter / (self.num_iter - 1)
else:
r = self.last_epoch / (self.num_iter - 1)
return [base_lr * (self.end_lr / base_lr) ** r for base_lr in self.base_lrs]
class StateCacher(object):
def __init__(self, in_memory, cache_dir=None):
self.in_memory = in_memory
self.cache_dir = cache_dir
if self.cache_dir is None:
import tempfile
self.cache_dir = tempfile.gettempdir()
else:
if not os.path.isdir(self.cache_dir):
raise ValueError("Given `cache_dir` is not a valid directory.")
self.cached = {}
def store(self, key, state_dict):
if self.in_memory:
self.cached.update({key: copy.deepcopy(state_dict)})
else:
fn = os.path.join(self.cache_dir, "state_{}_{}.pt".format(key, id(self)))
self.cached.update({key: fn})
torch.save(state_dict, fn)
def retrieve(self, key):
if key not in self.cached:
raise KeyError("Target {} was not cached.".format(key))
if self.in_memory:
return self.cached.get(key)
else:
fn = self.cached.get(key)
if not os.path.exists(fn):
raise RuntimeError(
"Failed to load state in {}. File doesn't exist anymore.".format(fn)
)
state_dict = torch.load(fn, map_location=lambda storage, location: storage)
return state_dict
def __del__(self):
"""Check whether there are unused cached files existing in `cache_dir` before
this instance being destroyed."""
if self.in_memory:
return
for k in self.cached:
if os.path.exists(self.cached[k]):
os.remove(self.cached[k])