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

load_ema_ckpt_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
  1. import unittest
  2. from super_gradients.training import Trainer
  3. from super_gradients.training.dataloaders.dataloaders import classification_test_dataloader
  4. from super_gradients.training.metrics import Accuracy, Top5
  5. from super_gradients.training.utils.callbacks import PhaseCallback, Phase, PhaseContext
  6. from super_gradients.training.utils.utils import check_models_have_same_weights
  7. from super_gradients.training.models import LeNet
  8. from copy import deepcopy
  9. class PreTrainingEMANetCollector(PhaseCallback):
  10. def __init__(self):
  11. super(PreTrainingEMANetCollector, self).__init__(phase=Phase.PRE_TRAINING)
  12. self.net = None
  13. def __call__(self, context: PhaseContext):
  14. self.net = deepcopy(context.ema_model)
  15. class LoadCheckpointWithEmaTest(unittest.TestCase):
  16. def setUp(self) -> None:
  17. self.train_params = {
  18. "max_epochs": 2,
  19. "lr_updates": [1],
  20. "lr_decay_factor": 0.1,
  21. "lr_mode": "StepLRScheduler",
  22. "lr_warmup_epochs": 0,
  23. "initial_lr": 0.1,
  24. "loss": "CrossEntropyLoss",
  25. "optimizer": "SGD",
  26. "optimizer_params": {"weight_decay": 1e-4, "momentum": 0.9},
  27. "train_metrics_list": [Accuracy(), Top5()],
  28. "valid_metrics_list": [Accuracy(), Top5()],
  29. "metric_to_watch": "Accuracy",
  30. "greater_metric_to_watch_is_better": True,
  31. "ema": True,
  32. }
  33. def test_ema_ckpt_reload(self):
  34. # Define Model
  35. net = LeNet()
  36. trainer = Trainer("ema_ckpt_test")
  37. trainer.train(
  38. model=net, training_params=self.train_params, train_loader=classification_test_dataloader(), valid_loader=classification_test_dataloader()
  39. )
  40. ema_model = trainer.ema_model.ema
  41. # TRAIN FOR 1 MORE EPOCH AND COMPARE THE NET AT THE BEGINNING OF EPOCH 3 AND THE END OF EPOCH NUMBER 2
  42. net = LeNet()
  43. trainer = Trainer("ema_ckpt_test")
  44. net_collector = PreTrainingEMANetCollector()
  45. self.train_params["resume"] = True
  46. self.train_params["max_epochs"] = 3
  47. self.train_params["phase_callbacks"] = [net_collector]
  48. trainer.train(
  49. model=net, training_params=self.train_params, train_loader=classification_test_dataloader(), valid_loader=classification_test_dataloader()
  50. )
  51. reloaded_ema_model = net_collector.net.ema
  52. # ASSERT RELOADED EMA MODEL HAS THE SAME WEIGHTS AS THE EMA MODEL SAVED IN FIRST PART OF TRAINING
  53. assert check_models_have_same_weights(ema_model, reloaded_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...