-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathutils.py
73 lines (58 loc) · 2.46 KB
/
utils.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
# Copyright (C) 2021-2023, Mindee.
# This program is licensed under the Apache License 2.0.
# See LICENSE or go to <https://opensource.org/licenses/Apache-2.0> for full license details.
import math
import matplotlib.pyplot as plt
import numpy as np
def plot_samples(images, targets):
# Unnormalize image
num_samples = min(len(images), 12)
num_cols = min(len(images), 4)
num_rows = int(math.ceil(num_samples / num_cols))
_, axes = plt.subplots(num_rows, num_cols, figsize=(20, 5))
for idx in range(num_samples):
img = (255 * images[idx].numpy()).round().clip(0, 255).astype(np.uint8)
if img.shape[0] == 3 and img.shape[2] != 3:
img = img.transpose(1, 2, 0)
row_idx = idx // num_cols
col_idx = idx % num_cols
ax = axes[row_idx] if num_rows > 1 else axes
ax = ax[col_idx] if num_cols > 1 else ax
ax.imshow(img)
ax.set_title(targets[idx])
# Disable axis
for ax in axes.ravel():
ax.axis("off")
plt.show()
def plot_recorder(lr_recorder, loss_recorder, beta: float = 0.95, **kwargs) -> None:
"""Display the results of the LR grid search.
Adapted from https://github.com/frgfm/Holocron/blob/master/holocron/trainer/core.py.
Args:
lr_recorder: list of LR values
loss_recorder: list of loss values
beta (float, optional): smoothing factor
"""
if len(lr_recorder) != len(loss_recorder) or len(lr_recorder) == 0:
raise AssertionError("Both `lr_recorder` and `loss_recorder` should have the same length")
# Exp moving average of loss
smoothed_losses = []
avg_loss = 0.0
for idx, loss in enumerate(loss_recorder):
avg_loss = beta * avg_loss + (1 - beta) * loss
smoothed_losses.append(avg_loss / (1 - beta ** (idx + 1)))
# Properly rescale Y-axis
data_slice = slice(
min(len(loss_recorder) // 10, 10),
-min(len(loss_recorder) // 20, 5) if len(loss_recorder) >= 20 else len(loss_recorder),
)
vals = np.array(smoothed_losses[data_slice])
min_idx = vals.argmin()
max_val = vals.max() if min_idx is None else vals[: min_idx + 1].max() # type: ignore[misc]
delta = max_val - vals[min_idx]
plt.plot(lr_recorder[data_slice], smoothed_losses[data_slice])
plt.xscale("log")
plt.xlabel("Learning Rate")
plt.ylabel("Training loss")
plt.ylim(vals[min_idx] - 0.1 * delta, max_val + 0.2 * delta)
plt.grid(True, linestyle="--", axis="x")
plt.show(**kwargs)