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.4 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
  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(self):
  24. self._init_model()
  25. self._train({})
  26. self._init_model()
  27. self._train({"exp_activation": False})
  28. def _train(self, ema_params):
  29. training_params = {
  30. "max_epochs": 4,
  31. "lr_updates": [4],
  32. "lr_mode": "step",
  33. "lr_decay_factor": 0.1,
  34. "lr_warmup_epochs": 0,
  35. "initial_lr": 0.1,
  36. "loss": "cross_entropy",
  37. "optimizer": "SGD",
  38. "criterion_params": {},
  39. "ema": True,
  40. "ema_params": ema_params,
  41. "optimizer_params": {"weight_decay": 1e-4, "momentum": 0.9},
  42. "train_metrics_list": [Accuracy(), Top5()],
  43. "valid_metrics_list": [Accuracy(), Top5()],
  44. "metric_to_watch": "Accuracy",
  45. "greater_metric_to_watch_is_better": True,
  46. }
  47. def before_test():
  48. self.assertEqual(self.trainer.net, self.trainer.ema_model.ema)
  49. def before_train_epoch():
  50. self.assertNotEqual(self.trainer.net, self.trainer.ema_model.ema)
  51. self.trainer.test = CallWrapper(self.trainer.test, check_before=before_test)
  52. self.trainer._train_epoch = CallWrapper(self.trainer._train_epoch, check_before=before_train_epoch)
  53. self.trainer.train(
  54. model=self.model, training_params=training_params, train_loader=classification_test_dataloader(), valid_loader=classification_test_dataloader()
  55. )
  56. self.assertIsNotNone(self.trainer.ema_model)
  57. if __name__ == "__main__":
  58. unittest.main()
Tip!

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

Comments

Loading...