Skip to content
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
19 changes: 17 additions & 2 deletions tests/test_lr_finder.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
import random
import sys
import unittest
from typing import TYPE_CHECKING

import torch
from torch.utils.data import DataLoader
Expand All @@ -23,7 +24,14 @@
from monai.transforms import AddChanneld, Compose, LoadImaged, ScaleIntensityd, ToTensord
from monai.utils import optional_import, set_determinism

PILImage, has_pil = optional_import("PIL.Image")
if TYPE_CHECKING:
import matplotlib.pyplot as plt

has_matplotlib = True
has_pil = True
else:
plt, has_matplotlib = optional_import("matplotlib.pyplot")
_, has_pil = optional_import("PIL.Image")

RAND_SEED = 42
random.seed(RAND_SEED)
Expand Down Expand Up @@ -73,7 +81,14 @@ def test_lr_finder(self):
lr_finder = LearningRateFinder(model, optimizer, loss_function, device=device)
lr_finder.range_test(train_loader, val_loader=train_loader, end_lr=10, num_iter=5)
print(lr_finder.get_steepest_gradient(0, 0)[0])
lr_finder.plot(0, 0) # to inspect the loss-learning rate graph

if has_matplotlib:
ax = plt.subplot()
plt.show(block=False)
lr_finder.plot(0, 0, ax=ax) # to inspect the loss-learning rate graph
plt.pause(3)
plt.close()

lr_finder.reset() # to reset the model and optimizer to their initial state


Expand Down