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

#943 Add detection prediction tutorial

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

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