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
  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 = {"max_epochs": 2, "lr_updates": [1], "lr_decay_factor": 0.1, "lr_mode": "step",
  18. "lr_warmup_epochs": 0, "initial_lr": 0.1, "loss": "cross_entropy", "optimizer": 'SGD',
  19. "criterion_params": {}, "optimizer_params": {"weight_decay": 1e-4, "momentum": 0.9},
  20. "train_metrics_list": [Accuracy(), Top5()], "valid_metrics_list": [Accuracy(), Top5()],
  21. "metric_to_watch": "Accuracy",
  22. "greater_metric_to_watch_is_better": True, "ema": True}
  23. def test_ema_ckpt_reload(self):
  24. # Define Model
  25. net = LeNet()
  26. trainer = Trainer("ema_ckpt_test")
  27. trainer.train(model=net, training_params=self.train_params,
  28. train_loader=classification_test_dataloader(),
  29. valid_loader=classification_test_dataloader())
  30. ema_model = trainer.ema_model.ema
  31. # TRAIN FOR 1 MORE EPOCH AND COMPARE THE NET AT THE BEGINNING OF EPOCH 3 AND THE END OF EPOCH NUMBER 2
  32. net = LeNet()
  33. trainer = Trainer("ema_ckpt_test")
  34. net_collector = PreTrainingEMANetCollector()
  35. self.train_params["resume"] = True
  36. self.train_params["max_epochs"] = 3
  37. self.train_params["phase_callbacks"] = [net_collector]
  38. trainer.train(model=net, training_params=self.train_params,
  39. train_loader=classification_test_dataloader(),
  40. valid_loader=classification_test_dataloader())
  41. reloaded_ema_model = net_collector.net.ema
  42. # ASSERT RELOADED EMA MODEL HAS THE SAME WEIGHTS AS THE EMA MODEL SAVED IN FIRST PART OF TRAINING
  43. assert check_models_have_same_weights(ema_model, reloaded_ema_model)
  44. if __name__ == '__main__':
  45. unittest.main()
Tip!

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

Comments

Loading...