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_dataloader_adapter.py 7.3 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
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
  1. import unittest
  2. import tempfile
  3. import shutil
  4. import torch
  5. from torchvision.datasets import VOCDetection, VOCSegmentation, Caltech101
  6. from torchvision.transforms import Compose, ToTensor, Resize, InterpolationMode
  7. from data_gradients.managers.detection_manager import DetectionAnalysisManager
  8. from data_gradients.managers.segmentation_manager import SegmentationAnalysisManager
  9. from data_gradients.managers.classification_manager import ClassificationAnalysisManager
  10. from data_gradients.dataset_adapters.config.data_config import SegmentationDataConfig
  11. from super_gradients.training.dataloaders.adapters import (
  12. DetectionDataloaderAdapterFactory,
  13. SegmentationDataloaderAdapterFactory,
  14. ClassificationDataloaderAdapterFactory,
  15. )
  16. class DataloaderAdapterTest(unittest.TestCase):
  17. def setUp(self) -> None:
  18. self.tmp_dir = tempfile.mkdtemp()
  19. def tearDown(self):
  20. shutil.rmtree(self.tmp_dir)
  21. def test_torch_classification(self):
  22. class ToRGB:
  23. def __call__(self, pic):
  24. return pic.convert("RGB")
  25. train_set = Caltech101(root=self.tmp_dir, download=True, transform=Compose([ToRGB(), ToTensor(), Resize((512, 512))]))
  26. analyzer = ClassificationAnalysisManager(
  27. train_data=train_set,
  28. val_data=train_set,
  29. log_dir=self.tmp_dir,
  30. report_title="Caltech101",
  31. class_names=train_set.categories,
  32. batches_early_stop=4,
  33. n_image_channels=3,
  34. use_cache=True,
  35. )
  36. analyzer.run()
  37. train_loader = ClassificationDataloaderAdapterFactory.from_dataset(
  38. dataset=train_set,
  39. config_path=analyzer.data_config.cache_path,
  40. batch_size=20,
  41. )
  42. images, labels = next(iter(train_loader))
  43. self.assertTrue(images.shape == torch.Size([20, 3, 512, 512]))
  44. self.assertTrue(labels.shape == torch.Size([20]))
  45. def test_torchvision_detection(self):
  46. train_set = VOCDetection(
  47. root=self.tmp_dir,
  48. image_set="train",
  49. download=True,
  50. year="2007",
  51. transform=Compose([Resize(size=(720, 720))]),
  52. )
  53. val_set = VOCDetection(
  54. root=self.tmp_dir,
  55. image_set="val",
  56. download=True,
  57. year="2007",
  58. transform=Compose([Resize(size=(720, 720))]),
  59. )
  60. import numpy as np
  61. PASCAL_VOC_CLASS_NAMES = (
  62. "aeroplane",
  63. "bicycle",
  64. "bird",
  65. "boat",
  66. "bottle",
  67. "bus",
  68. "car",
  69. "cat",
  70. "chair",
  71. "cow",
  72. "diningtable",
  73. "dog",
  74. "horse",
  75. "motorbike",
  76. "person",
  77. "pottedplant",
  78. "sheep",
  79. "sofa",
  80. "train",
  81. "tvmonitor",
  82. )
  83. def voc_format_to_bbox(sample: tuple) -> np.ndarray:
  84. target_annotations = sample[1]
  85. targets = []
  86. for target in target_annotations["annotation"]["object"]:
  87. target_bbox = target["bndbox"]
  88. target_np = np.array(
  89. [
  90. PASCAL_VOC_CLASS_NAMES.index(target["name"]),
  91. float(target_bbox["xmin"]),
  92. float(target_bbox["ymin"]),
  93. float(target_bbox["xmax"]),
  94. float(target_bbox["ymax"]),
  95. ]
  96. )
  97. targets.append(target_np)
  98. return np.array(targets, dtype=float)
  99. from data_gradients.dataset_adapters.config.data_config import DetectionDataConfig
  100. analyzer = DetectionAnalysisManager(
  101. report_title="VOC_from_torch",
  102. log_dir=self.tmp_dir,
  103. train_data=train_set,
  104. val_data=val_set,
  105. labels_extractor=voc_format_to_bbox,
  106. class_names=PASCAL_VOC_CLASS_NAMES,
  107. # class_names=train_set,
  108. batches_early_stop=20,
  109. use_cache=True, # With this we will be asked about the dataset information only once
  110. is_label_first=True,
  111. bbox_format="cxcywh",
  112. )
  113. analyzer.run()
  114. config = DetectionDataConfig(labels_extractor=voc_format_to_bbox, cache_path=analyzer.data_config.cache_path)
  115. train_loader = DetectionDataloaderAdapterFactory.from_dataset(
  116. dataset=train_set,
  117. config=config,
  118. batch_size=20,
  119. num_workers=0,
  120. drop_last=True,
  121. )
  122. val_loader = DetectionDataloaderAdapterFactory.from_dataset(
  123. dataset=train_set,
  124. config=config,
  125. batch_size=20,
  126. num_workers=0,
  127. drop_last=True,
  128. )
  129. for images, labels in train_loader:
  130. self.assertTrue(images.ndim == 4)
  131. self.assertTrue(images.shape[:2] == torch.Size([20, 3]))
  132. self.assertTrue(labels.ndim == 2)
  133. self.assertTrue(labels.shape[-1] == 6)
  134. for images, labels in val_loader:
  135. self.assertTrue(images.ndim == 4)
  136. self.assertTrue(images.shape[:2] == torch.Size([20, 3]))
  137. self.assertTrue(labels.ndim == 2)
  138. self.assertTrue(labels.shape[-1] == 6)
  139. def test_torchvision_segmentation(self):
  140. train_set = VOCSegmentation(
  141. root=self.tmp_dir,
  142. image_set="train",
  143. download=True,
  144. year="2007",
  145. transform=Compose([Resize(size=(720, 720))]),
  146. target_transform=Compose([Resize((720, 720), interpolation=InterpolationMode.NEAREST)]),
  147. )
  148. val_set = VOCSegmentation(
  149. root=self.tmp_dir,
  150. image_set="val",
  151. download=True,
  152. year="2007",
  153. transform=Compose([Resize(size=(720, 720))]),
  154. target_transform=Compose([Resize((720, 720), interpolation=InterpolationMode.NEAREST)]),
  155. )
  156. analyzer = SegmentationAnalysisManager(
  157. report_title="VOC_SEG_from_torch2",
  158. log_dir=self.tmp_dir,
  159. train_data=train_set,
  160. val_data=val_set,
  161. class_names=list(range(256)),
  162. # class_names=train_set,
  163. batches_early_stop=20,
  164. use_cache=True, # With this we will be asked about the dataset information only once
  165. )
  166. analyzer.run()
  167. config = SegmentationDataConfig(cache_path=analyzer.data_config.cache_path)
  168. train_loader = SegmentationDataloaderAdapterFactory.from_dataset(
  169. dataset=train_set,
  170. config=config,
  171. batch_size=20,
  172. num_workers=0,
  173. drop_last=True,
  174. )
  175. val_loader = SegmentationDataloaderAdapterFactory.from_dataset(
  176. dataset=train_set,
  177. config=config,
  178. batch_size=20,
  179. num_workers=0,
  180. drop_last=True,
  181. )
  182. for images, labels in train_loader:
  183. self.assertTrue(images.shape == torch.Size([20, 3, 720, 720]))
  184. self.assertTrue(labels.shape == torch.Size([20, 720, 720]))
  185. for images, labels in val_loader:
  186. self.assertTrue(images.shape == torch.Size([20, 3, 720, 720]))
  187. self.assertTrue(labels.shape == torch.Size([20, 720, 720]))
  188. if __name__ == "__main__":
  189. DataloaderAdapterTest()
Tip!

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

Comments

Loading...