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

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

Comments

Loading...