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

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

Comments

Loading...