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

kd_module_test.py 3.0 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
  1. import unittest
  2. import os
  3. from super_gradients.training import SgModel
  4. import torch
  5. from super_gradients.training.utils.utils import check_models_have_same_weights
  6. from super_gradients.training.datasets.dataset_interfaces.dataset_interface import ClassificationTestDatasetInterface
  7. from super_gradients.training.metrics import Accuracy
  8. class KDModuleTest(unittest.TestCase):
  9. def test_build_kd_module_with_pretrained_teacher(self):
  10. sg_model = SgModel("build_kd_module_with_pretrained_teacher", device='cpu')
  11. sg_model.build_kd_model(student_architecture='resnet18',
  12. teacher_architecture='resnet50',
  13. student_arch_params={'num_classes': 1000},
  14. teacher_arch_params={'pretrained_weights': "imagenet"},
  15. )
  16. imagenet_resnet50_sg_model = SgModel("pretrained_resnet50")
  17. imagenet_resnet50_sg_model.build_model('resnet50', arch_params={'pretrained_weights': "imagenet",
  18. 'num_classes': 1000})
  19. self.assertTrue(check_models_have_same_weights(sg_model.net.module.teacher,
  20. imagenet_resnet50_sg_model.net.module))
  21. def test_build_kd_module_with_sg_trained_teacher(self):
  22. sg_model = SgModel("sg_trained_teacher", device='cpu')
  23. dataset_params = {"batch_size": 10}
  24. dataset = ClassificationTestDatasetInterface(dataset_params=dataset_params)
  25. sg_model.connect_dataset_interface(dataset)
  26. sg_model.build_model('resnet50', arch_params={'num_classes': 5})
  27. train_params = {"max_epochs": 3, "lr_updates": [1], "lr_decay_factor": 0.1, "lr_mode": "step",
  28. "lr_warmup_epochs": 0, "initial_lr": 0.1, "loss": torch.nn.CrossEntropyLoss(),
  29. "optimizer": "SGD",
  30. "criterion_params": {}, "optimizer_params": {"weight_decay": 1e-4, "momentum": 0.9},
  31. "train_metrics_list": [Accuracy()], "valid_metrics_list": [Accuracy()],
  32. "metric_to_watch": "Accuracy",
  33. "greater_metric_to_watch_is_better": True}
  34. sg_model.train(train_params)
  35. teacher_path = os.path.join(sg_model.checkpoints_dir_path, 'ckpt_latest.pth')
  36. sg_kd_model = SgModel('test_build_kd_module_with_sg_trained_teacher', device='cpu')
  37. sg_kd_model.build_kd_model(student_architecture='resnet18',
  38. teacher_architecture='resnet50',
  39. student_arch_params={'num_classes': 5},
  40. teacher_arch_params={'num_classes': 5},
  41. teacher_checkpoint_path=teacher_path
  42. )
  43. self.assertTrue(check_models_have_same_weights(sg_model.net.module, sg_kd_model.net.module.teacher))
  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...