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

test_without_train_test.py 4.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
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
87
88
89
90
91
92
93
94
95
96
97
98
99
  1. import shutil
  2. import unittest
  3. import os
  4. from super_gradients import SgModel, \
  5. ClassificationTestDatasetInterface, \
  6. SegmentationTestDatasetInterface, DetectionTestDatasetInterface
  7. from super_gradients.training.metrics import Accuracy, Top5
  8. from super_gradients.training import MultiGPUMode
  9. from super_gradients.training.utils.detection_utils import base_detection_collate_fn
  10. from super_gradients.training.datasets.datasets_utils import ComposedCollateFunction, MultiScaleCollateFunction
  11. from super_gradients.training.utils.detection_utils import YoloV3NonMaxSuppression
  12. from super_gradients.training.metrics.detection_metrics import DetectionMetrics
  13. from super_gradients.training.metrics.segmentation_metrics import PixelAccuracy, IoU
  14. class TestWithoutTrainTest(unittest.TestCase):
  15. @classmethod
  16. def setUp(cls):
  17. # NAMES FOR THE EXPERIMENTS TO LATER DELETE
  18. cls.folder_names = ['test_classification_model', 'test_detection_model', 'test_segmentation_model']
  19. @classmethod
  20. def tearDownClass(cls) -> None:
  21. # ERASE ALL THE FOLDERS THAT WERE CREATED DURING THIS TEST
  22. for folder in cls.folder_names:
  23. if os.path.isdir(os.path.join('checkpoints', folder)):
  24. shutil.rmtree(os.path.join('checkpoints', folder))
  25. @staticmethod
  26. def get_classification_trainer(name=''):
  27. model = SgModel(name, model_checkpoints_location='local')
  28. dataset_params = {"batch_size": 4}
  29. dataset = ClassificationTestDatasetInterface(dataset_params=dataset_params)
  30. model.connect_dataset_interface(dataset)
  31. model.build_model("resnet18_cifar", load_checkpoint=False)
  32. return model
  33. @staticmethod
  34. def get_detection_trainer(name=''):
  35. collate_fn_holder = ComposedCollateFunction([base_detection_collate_fn, MultiScaleCollateFunction(320)])
  36. dataset_params = {"batch_size": 4,
  37. "test_batch_size": 4,
  38. "dataset_dir": "/data/coco/",
  39. "s3_link": None,
  40. "image_size": 320,
  41. "test_collate_fn": base_detection_collate_fn,
  42. "train_collate_fn": collate_fn_holder,
  43. }
  44. yolo_v3_arch_params = {"image_size": 320, "iou_t": 0.225, "multi_gpu_mode": "distributed_data_parallel"}
  45. post_prediction_callback = YoloV3NonMaxSuppression()
  46. model = SgModel(name, model_checkpoints_location='local',
  47. multi_gpu=MultiGPUMode.OFF,
  48. post_prediction_callback=post_prediction_callback)
  49. dataset_interface = DetectionTestDatasetInterface(dataset_params=dataset_params)
  50. model.connect_dataset_interface(dataset_interface, data_loader_num_workers=4)
  51. model.build_model('yolo_v3', arch_params=yolo_v3_arch_params, load_checkpoint=False)
  52. return model
  53. @staticmethod
  54. def get_segmentation_trainer(name=''):
  55. shelfnet_lw_arch_params = {"num_classes": 5, "load_checkpoint": False}
  56. model = SgModel(name, model_checkpoints_location='local', multi_gpu=False)
  57. dataset_interface = SegmentationTestDatasetInterface()
  58. model.connect_dataset_interface(dataset_interface, data_loader_num_workers=8)
  59. model.build_model('shelfnet34_lw', arch_params=shelfnet_lw_arch_params)
  60. return model
  61. def test_test_without_train(self):
  62. model = self.get_classification_trainer(self.folder_names[0])
  63. assert isinstance(model.test(silent_mode=True, test_metrics_list=[Accuracy(), Top5()]), tuple)
  64. model = self.get_detection_trainer(self.folder_names[1])
  65. test_metrics = [DetectionMetrics(post_prediction_callback=model.post_prediction_callback, num_cls=5)]
  66. assert isinstance(model.test(silent_mode=True, test_metrics_list=test_metrics), tuple)
  67. model = self.get_segmentation_trainer(self.folder_names[2])
  68. assert isinstance(model.test(silent_mode=True, test_metrics_list=[IoU(21), PixelAccuracy()]), tuple)
  69. def test_test_on_valid_loader_without_train(self):
  70. model = self.get_classification_trainer(self.folder_names[0])
  71. assert isinstance(model.test(test_loader=model.valid_loader, silent_mode=True, test_metrics_list=[Accuracy(), Top5()]), tuple)
  72. model = self.get_detection_trainer(self.folder_names[1])
  73. test_metrics = [DetectionMetrics(post_prediction_callback=model.post_prediction_callback, num_cls=5)]
  74. assert isinstance(model.test(test_loader=model.valid_loader, silent_mode=True, test_metrics_list=test_metrics), tuple)
  75. model = self.get_segmentation_trainer(self.folder_names[2])
  76. assert isinstance(model.test(test_loader=model.valid_loader, silent_mode=True, test_metrics_list=[IoU(21), PixelAccuracy()]), tuple)
  77. if __name__ == '__main__':
  78. unittest.main()
Tip!

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

Comments

Loading...