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 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
  1. import os
  2. import unittest
  3. from super_gradients.training import Trainer, utils as core_utils
  4. from super_gradients.training.datasets.dataset_interfaces.dataset_interface import CoCoDetectionDatasetInterface
  5. from super_gradients.training.datasets.datasets_conf import COCO_DETECTION_CLASSES_LIST
  6. from super_gradients.training.models.detection_models.yolo_base import YoloPostPredictionCallback
  7. from super_gradients.training.utils.detection_utils import DetectionVisualization, DetectionCollateFN, DetectionTargetsFormat
  8. class TestDetectionUtils(unittest.TestCase):
  9. def test_visualization(self):
  10. # Create dataset
  11. dataset = CoCoDetectionDatasetInterface(dataset_params={"data_dir": "/data/coco",
  12. "train_subdir": "images/train2017",
  13. "val_subdir": "images/val2017",
  14. "train_json_file": "instances_train2017.json",
  15. "val_json_file": "instances_val2017.json",
  16. "batch_size": 16,
  17. "val_batch_size": 4,
  18. "val_image_size": 640,
  19. "train_image_size": 640,
  20. "hgain": 5,
  21. "sgain": 30,
  22. "vgain": 30,
  23. "mixup_prob": 1.0,
  24. "degrees": 10.,
  25. "shear": 2.0,
  26. "flip_prob": 0.5,
  27. "hsv_prob": 1.0,
  28. "mosaic_scale": [0.1, 2],
  29. "mixup_scale": [0.5, 1.5],
  30. "mosaic_prob": 1.,
  31. "translate": 0.1,
  32. "val_collate_fn": DetectionCollateFN(),
  33. "train_collate_fn": DetectionCollateFN(),
  34. "cache_dir_path": None,
  35. "cache_train_images": False,
  36. "cache_val_images": False,
  37. "targets_format": DetectionTargetsFormat.LABEL_NORMALIZED_CXCYWH,
  38. "with_crowd": False,
  39. "filter_box_candidates": False,
  40. "wh_thr": 0,
  41. "ar_thr": 0,
  42. "area_thr": 0
  43. })
  44. # Create Yolo model
  45. trainer = Trainer('visualization_test',
  46. model_checkpoints_location='local',
  47. post_prediction_callback=YoloPostPredictionCallback())
  48. trainer.connect_dataset_interface(dataset, data_loader_num_workers=8)
  49. trainer.build_model("yolox_n", checkpoint_params={"pretrained_weights": "coco"})
  50. # Simulate one iteration of validation subset
  51. valid_loader = trainer.valid_loader
  52. batch_i, (imgs, targets) = 0, next(iter(valid_loader))
  53. imgs = core_utils.tensor_container_to_device(imgs, trainer.device)
  54. targets = core_utils.tensor_container_to_device(targets, trainer.device)
  55. output = trainer.net(imgs)
  56. output = trainer.post_prediction_callback(output)
  57. # Visualize the batch
  58. DetectionVisualization.visualize_batch(imgs, output, targets, batch_i,
  59. COCO_DETECTION_CLASSES_LIST, trainer.checkpoints_dir_path)
  60. # Assert images ware created and delete them
  61. img_name = '{}/{}_{}.jpg'
  62. for i in range(4):
  63. img_path = img_name.format(trainer.checkpoints_dir_path, batch_i, i)
  64. self.assertTrue(os.path.exists(img_path))
  65. os.remove(img_path)
  66. if __name__ == '__main__':
  67. unittest.main()
Tip!

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

Comments

Loading...