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.6 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
  1. from super_gradients import ClassificationTestDatasetInterface
  2. from super_gradients.training import MultiGPUMode
  3. from super_gradients.training import SgModel
  4. from super_gradients.training.metrics import Accuracy, Top5
  5. import unittest
  6. def do_nothing():
  7. pass
  8. class CallWrapper:
  9. def __init__(self, f, check_before=do_nothing):
  10. self.f = f
  11. self.check_before = check_before
  12. def __call__(self, *args, **kwargs):
  13. self.check_before()
  14. return self.f(*args, **kwargs)
  15. class EMAIntegrationTest(unittest.TestCase):
  16. def _init_model(self) -> None:
  17. self.model = SgModel("resnet18_cifar_ema_test", model_checkpoints_location='local',
  18. device='cpu', multi_gpu=MultiGPUMode.OFF)
  19. dataset_interface = ClassificationTestDatasetInterface({"batch_size": 32})
  20. self.model.connect_dataset_interface(dataset_interface, 8)
  21. self.model.build_model("resnet18_cifar")
  22. @classmethod
  23. def tearDownClass(cls) -> None:
  24. pass
  25. def test_train(self):
  26. self._init_model()
  27. self._train({})
  28. self._init_model()
  29. self._train({"exp_activation": False})
  30. def _train(self, ema_params):
  31. training_params = {"max_epochs": 4,
  32. "lr_updates": [4],
  33. "lr_mode": "step",
  34. "lr_decay_factor": 0.1,
  35. "lr_warmup_epochs": 0,
  36. "initial_lr": 0.1,
  37. "loss": "cross_entropy",
  38. "optimizer": "SGD",
  39. "criterion_params": {},
  40. "ema": True,
  41. "ema_params": ema_params,
  42. "optimizer_params": {"weight_decay": 1e-4, "momentum": 0.9},
  43. "train_metrics_list": [Accuracy(), Top5()], "valid_metrics_list": [Accuracy(), Top5()],
  44. "loss_logging_items_names": ["Loss"], "metric_to_watch": "Accuracy",
  45. "greater_metric_to_watch_is_better": True}
  46. def before_test():
  47. self.assertEqual(self.model.net, self.model.ema_model.ema)
  48. def before_train_epoch():
  49. self.assertNotEqual(self.model.net, self.model.ema_model.ema)
  50. self.model.test = CallWrapper(self.model.test, check_before=before_test)
  51. self.model._train_epoch = CallWrapper(self.model._train_epoch, check_before=before_train_epoch)
  52. self.model.train(training_params=training_params)
  53. self.assertIsNotNone(self.model.ema_model)
  54. if __name__ == '__main__':
  55. unittest.main()
Tip!

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

Comments

Loading...