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

replace_head_test.py 1.8 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
  1. import os
  2. import shutil
  3. import unittest
  4. import torch
  5. import super_gradients
  6. from super_gradients.common.object_names import Models
  7. from super_gradients.training import models
  8. class ReplaceHeadUnitTest(unittest.TestCase):
  9. def setUp(self) -> None:
  10. self.device = "cuda" if torch.cuda.is_available() and torch.cuda.device_count() > 0 else "cpu"
  11. super_gradients.init_trainer()
  12. def test_ppyolo_replace_head(self):
  13. input = torch.randn(1, 3, 640, 640).to(self.device)
  14. for model in [Models.PP_YOLOE_S, Models.PP_YOLOE_M, Models.PP_YOLOE_L, Models.PP_YOLOE_X]:
  15. model = models.get(model, pretrained_weights="coco").to(self.device).eval()
  16. model.replace_head(new_num_classes=100)
  17. (_, pred_scores), _ = model.forward(input)
  18. self.assertEqual(pred_scores.size(2), 100)
  19. def test_yolo_nas_replace_head(self):
  20. input = torch.randn(1, 3, 640, 640).to(self.device)
  21. for model in [Models.YOLO_NAS_S, Models.YOLO_NAS_M, Models.YOLO_NAS_L]:
  22. model = models.get(model, pretrained_weights="coco").to(self.device).eval()
  23. model.replace_head(new_num_classes=100)
  24. (_, pred_scores), _ = model.forward(input)
  25. self.assertEqual(pred_scores.size(2), 100)
  26. def test_dekr_replace_head(self):
  27. input = torch.randn(1, 3, 640, 640).to(self.device)
  28. model = models.get(Models.DEKR_W32_NO_DC, num_classes=20, pretrained_weights="coco_pose").to(self.device).eval()
  29. heatmap, offsets = model.forward(input)
  30. self.assertEqual(heatmap.size(1), 20 + 1)
  31. self.assertEqual(offsets.size(1), 20 * 2)
  32. def tearDown(self) -> None:
  33. if os.path.exists("~/.cache/torch/hub/"):
  34. shutil.rmtree("~/.cache/torch/hub/")
  35. if __name__ == "__main__":
  36. unittest.main()
Tip!

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

Comments

Loading...