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

trainer_test.py 3.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
75
76
77
78
79
80
81
82
83
  1. import shutil
  2. import unittest
  3. from super_gradients.training import models
  4. import super_gradients
  5. import torch
  6. import os
  7. from super_gradients import Trainer
  8. from super_gradients.training.dataloaders.dataloaders import classification_test_dataloader
  9. from super_gradients.training.metrics import Accuracy, Top5
  10. class TestTrainer(unittest.TestCase):
  11. @classmethod
  12. def setUp(cls):
  13. super_gradients.init_trainer()
  14. # NAMES FOR THE EXPERIMENTS TO LATER DELETE
  15. cls.folder_names = ["test_train", "test_save_load", "test_load_w", "test_load_w2", "test_load_w3", "test_checkpoint_content", "analyze"]
  16. cls.training_params = {
  17. "max_epochs": 1,
  18. "silent_mode": True,
  19. "lr_decay_factor": 0.1,
  20. "initial_lr": 0.1,
  21. "lr_updates": [4],
  22. "lr_mode": "step",
  23. "loss": "cross_entropy",
  24. "train_metrics_list": [Accuracy(), Top5()],
  25. "valid_metrics_list": [Accuracy(), Top5()],
  26. "metric_to_watch": "Accuracy",
  27. "greater_metric_to_watch_is_better": True,
  28. }
  29. @classmethod
  30. def tearDownClass(cls) -> None:
  31. # ERASE ALL THE FOLDERS THAT WERE CREATED DURING THIS TEST
  32. for folder in cls.folder_names:
  33. if os.path.isdir(os.path.join("checkpoints", folder)):
  34. shutil.rmtree(os.path.join("checkpoints", folder))
  35. @staticmethod
  36. def get_classification_trainer(name=""):
  37. trainer = Trainer(name)
  38. model = models.get("resnet18", num_classes=5)
  39. return trainer, model
  40. def test_train(self):
  41. trainer, model = self.get_classification_trainer(self.folder_names[0])
  42. trainer.train(
  43. model=model, training_params=self.training_params, train_loader=classification_test_dataloader(), valid_loader=classification_test_dataloader()
  44. )
  45. def test_save_load(self):
  46. trainer, model = self.get_classification_trainer(self.folder_names[1])
  47. trainer.train(
  48. model=model, training_params=self.training_params, train_loader=classification_test_dataloader(), valid_loader=classification_test_dataloader()
  49. )
  50. resume_training_params = self.training_params.copy()
  51. resume_training_params["resume"] = True
  52. resume_training_params["max_epochs"] = 2
  53. trainer, model = self.get_classification_trainer(self.folder_names[1])
  54. trainer.train(
  55. model=model, training_params=resume_training_params, train_loader=classification_test_dataloader(), valid_loader=classification_test_dataloader()
  56. )
  57. def test_checkpoint_content(self):
  58. """VERIFY THAT ALL CHECKPOINTS ARE SAVED AND CONTAIN ALL THE EXPECTED KEYS"""
  59. trainer, model = self.get_classification_trainer(self.folder_names[5])
  60. params = self.training_params.copy()
  61. params["save_ckpt_epoch_list"] = [1]
  62. trainer.train(model=model, training_params=params, train_loader=classification_test_dataloader(), valid_loader=classification_test_dataloader())
  63. ckpt_filename = ["ckpt_best.pth", "ckpt_latest.pth", "ckpt_epoch_1.pth"]
  64. ckpt_paths = [os.path.join(trainer.checkpoints_dir_path, suf) for suf in ckpt_filename]
  65. for ckpt_path in ckpt_paths:
  66. ckpt = torch.load(ckpt_path)
  67. self.assertListEqual(["net", "acc", "epoch", "optimizer_state_dict", "scaler_state_dict"], list(ckpt.keys()))
  68. trainer._save_checkpoint()
  69. weights_only = torch.load(os.path.join(trainer.checkpoints_dir_path, "ckpt_latest_weights_only.pth"))
  70. self.assertListEqual(["net"], list(weights_only.keys()))
  71. if __name__ == "__main__":
  72. unittest.main()
Tip!

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

Comments

Loading...