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

detection_utils_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
  1. import os
  2. import unittest
  3. from super_gradients.training import SgModel, utils as core_utils
  4. from super_gradients.training.datasets import CoCoDetectionDatasetInterface
  5. from super_gradients.training.datasets.datasets_conf import COCO_DETECTION_CLASSES_LIST
  6. from super_gradients.training.models.detection_models.yolov5 import YoloV5PostPredictionCallback
  7. from super_gradients.training.utils.detection_utils import base_detection_collate_fn, DetectionVisualization
  8. class TestDetectionUtils(unittest.TestCase):
  9. def test_visualization(self):
  10. # Create dataset
  11. dataset_params = {"batch_size": 4, "val_batch_size": 4, "train_image_size": 320, "val_image_size": 320,
  12. "val_collate_fn": base_detection_collate_fn,
  13. "train_collate_fn": base_detection_collate_fn,
  14. "val_sample_loading_method": "default"
  15. }
  16. dataset = CoCoDetectionDatasetInterface(dataset_params)
  17. # Create Yolo model
  18. model = SgModel('visualization_test',
  19. model_checkpoints_location='local',
  20. post_prediction_callback=YoloV5PostPredictionCallback())
  21. model.connect_dataset_interface(dataset, data_loader_num_workers=8)
  22. model.build_model("yolo_v5s")
  23. # Simulate one iteration of validation subset
  24. valid_loader = model.valid_loader
  25. batch_i, (imgs, targets) = 0, next(iter(valid_loader))
  26. imgs = core_utils.tensor_container_to_device(imgs, model.device)
  27. targets = core_utils.tensor_container_to_device(targets, model.device)
  28. output = model.net(imgs)
  29. output = model.post_prediction_callback(output)
  30. # Visualize the batch
  31. DetectionVisualization.visualize_batch(imgs, output, targets, batch_i,
  32. COCO_DETECTION_CLASSES_LIST, model.checkpoints_dir_path)
  33. # Assert images ware created and delete them
  34. img_name = '{}/{}_{}.jpg'
  35. for i in range(dataset_params['val_batch_size']):
  36. img_path = img_name.format(model.checkpoints_dir_path, batch_i, i)
  37. self.assertTrue(os.path.exists(img_path))
  38. os.remove(img_path)
  39. if __name__ == '__main__':
  40. unittest.main()
Tip!

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

Comments

Loading...