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

ema_train_integration_test.py 2.8 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
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
  1. from super_gradients.common.object_names import Models
  2. from super_gradients.training import models
  3. from super_gradients.training import Trainer
  4. from super_gradients.training.dataloaders.dataloaders import classification_test_dataloader
  5. from super_gradients.training.metrics import Accuracy, Top5
  6. import unittest
  7. def do_nothing():
  8. pass
  9. class CallWrapper:
  10. def __init__(self, f, check_before=do_nothing):
  11. self.f = f
  12. self.check_before = check_before
  13. def __call__(self, *args, **kwargs):
  14. self.check_before()
  15. return self.f(*args, **kwargs)
  16. class EMAIntegrationTest(unittest.TestCase):
  17. def _init_model(self) -> None:
  18. self.trainer = Trainer("resnet18_cifar_ema_test")
  19. self.model = models.get(Models.RESNET18_CIFAR, arch_params={"num_classes": 5})
  20. @classmethod
  21. def tearDownClass(cls) -> None:
  22. pass
  23. def test_train_exp_decay(self):
  24. self._init_model()
  25. self._train({"decay_type": "exp", "beta": 15, "decay": 0.9999})
  26. def test_train_threshold_decay(self):
  27. self._init_model()
  28. self._train({"decay_type": "threshold", "decay": 0.9999})
  29. def test_train_constant_decay(self):
  30. self._init_model()
  31. self._train({"decay_type": "constant", "decay": 0.9999})
  32. def test_train_with_old_ema_params(self):
  33. self._init_model()
  34. self._train({"decay": 0.9999, "exp_activation": True, "beta": 10})
  35. def _train(self, ema_params):
  36. training_params = {
  37. "max_epochs": 4,
  38. "lr_updates": [4],
  39. "lr_mode": "step",
  40. "lr_decay_factor": 0.1,
  41. "lr_warmup_epochs": 0,
  42. "initial_lr": 0.1,
  43. "loss": "cross_entropy",
  44. "optimizer": "SGD",
  45. "criterion_params": {},
  46. "ema": True,
  47. "ema_params": ema_params,
  48. "optimizer_params": {"weight_decay": 1e-4, "momentum": 0.9},
  49. "train_metrics_list": [Accuracy(), Top5()],
  50. "valid_metrics_list": [Accuracy(), Top5()],
  51. "metric_to_watch": "Accuracy",
  52. "greater_metric_to_watch_is_better": True,
  53. }
  54. def before_test():
  55. self.assertEqual(self.trainer.net, self.trainer.ema_model.ema)
  56. def before_train_epoch():
  57. self.assertNotEqual(self.trainer.net, self.trainer.ema_model.ema)
  58. self.trainer.test = CallWrapper(self.trainer.test, check_before=before_test)
  59. self.trainer._train_epoch = CallWrapper(self.trainer._train_epoch, check_before=before_train_epoch)
  60. self.trainer.train(
  61. model=self.model, training_params=training_params, train_loader=classification_test_dataloader(), valid_loader=classification_test_dataloader()
  62. )
  63. self.assertIsNotNone(self.trainer.ema_model)
  64. if __name__ == "__main__":
  65. unittest.main()
Tip!

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

Comments

Loading...