Register
Login
Resources
Docs Blog Datasets Glossary Case Studies Tutorials & Webinars
Product
Data Engine LLMs Platform Enterprise
Pricing Explore
Connect to our Discord channel

forward_pass_prep_fn_test.py 2.5 KB

You have to be logged in to leave a comment. Sign In
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
  1. import unittest
  2. from super_gradients.training import SgModel
  3. from super_gradients.training.metrics import Accuracy
  4. from super_gradients.training.datasets import ClassificationTestDatasetInterface
  5. from super_gradients.training.utils.callbacks import PhaseCallback, Phase, PhaseContext
  6. import torch
  7. class TestInputSizesCallback(PhaseCallback):
  8. """
  9. Phase callback that collects the input shapes rates in lr_placeholder at the end of each forward pass.
  10. """
  11. def __init__(self, shapes_placeholder):
  12. super(TestInputSizesCallback, self).__init__(Phase.TRAIN_BATCH_END)
  13. self.shapes_placeholder = shapes_placeholder
  14. def __call__(self, context: PhaseContext):
  15. self.shapes_placeholder.append(context.inputs.shape)
  16. def test_forward_pass_prep_fn(inputs, targets, *args, **kwargs):
  17. inputs = torch.nn.functional.interpolate(
  18. inputs, size=(50, 50), mode="bilinear", align_corners=False
  19. )
  20. return inputs, targets
  21. class ForwardpassPrepFNTest(unittest.TestCase):
  22. def setUp(self) -> None:
  23. self.dataset_params = {"batch_size": 4}
  24. self.dataset = ClassificationTestDatasetInterface(dataset_params=self.dataset_params)
  25. self.arch_params = {'num_classes': 10}
  26. def test_resizing_with_forward_pass_prep_fn(self):
  27. # Define Model
  28. model = SgModel("ForwardpassPrepFNTest")
  29. model.connect_dataset_interface(self.dataset)
  30. model.build_model("resnet18", arch_params=self.arch_params)
  31. sizes = []
  32. phase_callbacks = [TestInputSizesCallback(sizes)]
  33. train_params = {"max_epochs": 2, "cosine_final_lr_ratio": 0.2, "lr_mode": "cosine",
  34. "lr_cooldown_epochs": 2,
  35. "lr_warmup_epochs": 3, "initial_lr": 1, "loss": "cross_entropy", "optimizer": 'SGD',
  36. "criterion_params": {}, "optimizer_params": {"weight_decay": 1e-4, "momentum": 0.9},
  37. "train_metrics_list": [Accuracy()], "valid_metrics_list": [Accuracy()],
  38. "loss_logging_items_names": ["Loss"], "metric_to_watch": "Accuracy",
  39. "greater_metric_to_watch_is_better": True, "ema": False, "phase_callbacks": phase_callbacks,
  40. "pre_prediction_callback": test_forward_pass_prep_fn}
  41. model.train(train_params)
  42. # ALTHOUGH NOT SEEN IN HERE, THE 4TH EPOCH USES LR=1, SO THIS IS THE EXPECTED LIST AS WE COLLECT
  43. # THE LRS AFTER THE UPDATE
  44. sizes = list(map(lambda size: size[2], sizes))
  45. self.assertTrue(all(map(lambda size: size == 50, sizes)))
Tip!

Press p or to see the previous file or, n or to see the next file

Comments

Loading...