Repository navigation
Expand file tree
/
Copy pathtest_dota.py
More file actions
executable file
·291 lines (240 loc) · 9.64 KB
/
Copy pathtest_dota.py
File metadata and controls
executable file
·291 lines (240 loc) · 9.64 KB
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
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
"""Tests for DOTA dataset loader."""
import math
from pathlib import Path
import tempfile
from PIL import Image
from oriented_det.data import DOTAAnnotation, DOTADataset, DOTASample, format_dota_line
from oriented_det.geometry import RBox
def test_format_dota_line_and_to_line():
"""Official DOTA format: comma-separated output."""
line = format_dota_line(100, 200, 300, 200, 300, 400, 100, 400, "plane", 0)
assert line == "100, 200, 300, 200, 300, 400, 100, 400, plane, 0"
ann = DOTAAnnotation.from_line(line)
assert ann.to_line() == line
def test_dota_annotation_parsing():
"""Test parsing of DOTA annotation lines (space-separated)."""
line = "100 200 300 200 300 400 100 400 plane 0"
ann = DOTAAnnotation.from_line(line)
assert ann.class_name == "plane"
assert ann.difficult == 0
assert len(ann.polygon) == 4
assert isinstance(ann.rbox, RBox)
def test_dota_annotation_parsing_comma_format():
"""Test parsing of official DOTA comma-separated format."""
line = "100, 200, 300, 200, 300, 400, 100, 400, plane, 0"
ann = DOTAAnnotation.from_line(line)
assert ann.class_name == "plane"
assert ann.difficult == 0
line_multiword = "0, 0, 10, 0, 10, 10, 0, 10, storage tank, 1"
ann2 = DOTAAnnotation.from_line(line_multiword)
assert ann2.class_name == "storage tank"
assert ann2.difficult == 1
def test_dota_from_line_space_separated_class_with_comma():
"""Space-separated lines keep commas inside class_name (Airbus synthetic line)."""
line = "0 0 10 0 10 10 0 10 car, van and pickup 0"
ann = DOTAAnnotation.from_line(line)
assert ann.class_name == "car, van and pickup"
assert ann.difficult == 0
def test_dota_from_line_does_not_use_comma_path_for_space_coords():
"""A comma in the category must not force the official 10-field comma grammar."""
line = "10.0 20.0 30.0 20.0 30.0 40.0 10.0 40.0 Partially Hidden, car 1"
ann = DOTAAnnotation.from_line(line)
assert ann.class_name == "Partially Hidden, car"
assert ann.difficult == 1
def test_dota_from_corners_builds_annotation():
coords = [0.0, 0.0, 10.0, 0.0, 10.0, 10.0, 0.0, 10.0]
ann = DOTAAnnotation.from_corners(coords, "car, van and pickup", difficult=1)
assert ann.class_name == "car, van and pickup"
assert ann.difficult == 1
assert len(ann.polygon) == 4
assert isinstance(ann.rbox, RBox)
def test_dota_from_line_official_comma_with_comma_in_category():
"""Official comma lines with extra category fields join category parts."""
line = "0, 0, 10, 0, 10, 10, 0, 10, car, van and pickup, 0"
ann = DOTAAnnotation.from_line(line)
assert ann.class_name == "car, van and pickup"
assert ann.difficult == 0
def test_dota_sample_filtering():
"""Test filtering of DOTA samples by class and difficulty."""
annotations = [
DOTAAnnotation.from_line("0 0 10 0 10 10 0 10 plane 0"),
DOTAAnnotation.from_line("20 20 30 20 30 30 20 30 ship 0"),
DOTAAnnotation.from_line("40 40 50 40 50 50 40 50 plane 1"),
]
sample = DOTASample(
image_path=Path("test.png"),
width=100,
height=100,
annotations=tuple(annotations)
)
# Filter by class
filtered = sample.filter_by_class(allowed_classes=["plane"])
assert len(filtered.annotations) == 2
# Filter by difficulty
filtered = sample.filter_by_class(drop_difficult=True)
assert len(filtered.annotations) == 2
# Combined filter
filtered = sample.filter_by_class(allowed_classes=["plane"], drop_difficult=True)
assert len(filtered.annotations) == 1
def test_dota_dataset_structure():
"""Test DOTA dataset structure (without requiring actual files)."""
# This test validates the dataset structure without file I/O
with tempfile.TemporaryDirectory() as tmpdir:
root = Path(tmpdir)
(root / "labelTxt").mkdir()
(root / "images").mkdir()
# Create a mock annotation file
ann_file = root / "labelTxt" / "P0001_train.txt"
ann_file.write_text("100, 200, 300, 200, 300, 400, 100, 400, plane, 0\n")
# Create a mock image (we'll skip actual image creation for unit tests)
# In real usage, PIL would load the image
# Dataset initialization should work
try:
dataset = DOTADataset(root, split="train")
assert len(dataset._annotation_files) == 1
except FileNotFoundError:
# Expected if image file doesn't exist
pass
def test_dota_dataset_separate_folders():
"""Test DOTA dataset with train/val/test in separate folders."""
with tempfile.TemporaryDirectory() as tmpdir:
root = Path(tmpdir)
# Create separate folder structure
train_label_dir = root / "train" / "labelTxt"
train_image_dir = root / "train" / "images"
train_label_dir.mkdir(parents=True)
train_image_dir.mkdir(parents=True)
val_label_dir = root / "val" / "labelTxt"
val_image_dir = root / "val" / "images"
val_label_dir.mkdir(parents=True)
val_image_dir.mkdir(parents=True)
# Create annotation files in separate folders
train_ann_file = train_label_dir / "P0001.txt"
train_ann_file.write_text("100, 200, 300, 200, 300, 400, 100, 400, plane, 0\n")
val_ann_file = val_label_dir / "P0002.txt"
val_ann_file.write_text("50, 50, 150, 50, 150, 150, 50, 150, ship, 0\n")
# Test train dataset with custom directories
try:
train_dataset = DOTADataset(
root_dir=root,
split="train",
label_dir=train_label_dir,
image_dir=train_image_dir
)
assert len(train_dataset._annotation_files) == 1
assert train_dataset.label_dir == train_label_dir
assert train_dataset.image_dir == train_image_dir
except FileNotFoundError:
# Expected if image file doesn't exist
pass
# Test val dataset with custom directories
try:
val_dataset = DOTADataset(
root_dir=root,
split="val",
label_dir=val_label_dir,
image_dir=val_image_dir
)
assert len(val_dataset._annotation_files) == 1
assert val_dataset.label_dir == val_label_dir
assert val_dataset.image_dir == val_image_dir
except FileNotFoundError:
# Expected if image file doesn't exist
pass
def test_dota_dataset_difficult_strategy_controls_read_time_filtering(tmp_path):
label_dir = tmp_path / "labelTxt"
image_dir = tmp_path / "images"
label_dir.mkdir()
image_dir.mkdir()
Image.new("RGB", (32, 32)).save(image_dir / "P0001_train.png")
(label_dir / "P0001_train.txt").write_text(
"0, 0, 10, 0, 10, 10, 0, 10, plane, 0\n"
"12, 12, 20, 12, 20, 20, 12, 20, ship, 1\n",
encoding="utf-8",
)
drop_ds = DOTADataset(
tmp_path,
split="train",
label_dir=label_dir,
image_dir=image_dir,
difficult_strategy="drop",
)
keep_ds = DOTADataset(
tmp_path,
split="train",
label_dir=label_dir,
image_dir=image_dir,
difficult_strategy="keep",
)
ignore_ds = DOTADataset(
tmp_path,
split="train",
label_dir=label_dir,
image_dir=image_dir,
difficult_strategy="ignore",
)
assert [a.class_name for a in drop_ds[0].annotations] == ["plane"]
assert [a.class_name for a in keep_ds[0].annotations] == ["plane", "ship"]
assert [a.class_name for a in ignore_ds[0].annotations] == ["plane", "ship"]
def test_dota_dataset_filter_empty_gt(tmp_path):
label_dir = tmp_path / "labels"
image_dir = tmp_path / "images"
label_dir.mkdir()
image_dir.mkdir()
Image.new("RGB", (32, 32)).save(image_dir / "empty.png")
Image.new("RGB", (32, 32)).save(image_dir / "has_obj.png")
(label_dir / "empty.txt").write_text("", encoding="utf-8")
(label_dir / "has_obj.txt").write_text(
"0, 0, 10, 0, 10, 10, 0, 10, plane, 0\n",
encoding="utf-8",
)
all_ds = DOTADataset(
tmp_path,
split="train",
label_dir=label_dir,
image_dir=image_dir,
filter_empty_gt=False,
)
filtered_ds = DOTADataset(
tmp_path,
split="train",
label_dir=label_dir,
image_dir=image_dir,
filter_empty_gt=True,
)
assert len(all_ds) == 2
assert all_ds.annotation_files_discovered_count == 2
assert all_ds.empty_gt_filtered_count == 0
assert len(filtered_ds) == 1
assert filtered_ds.annotation_files_discovered_count == 2
assert filtered_ds.empty_gt_filtered_count == 1
assert filtered_ds[0].num_objects == 1
def test_dota_dataset_filter_empty_gt_respects_difficult_drop(tmp_path):
label_dir = tmp_path / "labels"
image_dir = tmp_path / "images"
label_dir.mkdir()
image_dir.mkdir()
Image.new("RGB", (32, 32)).save(image_dir / "only_difficult.png")
(label_dir / "only_difficult.txt").write_text(
"0, 0, 10, 0, 10, 10, 0, 10, plane, 1\n",
encoding="utf-8",
)
keep_ds = DOTADataset(
tmp_path,
split="train",
label_dir=label_dir,
image_dir=image_dir,
difficult_strategy="keep",
filter_empty_gt=True,
)
drop_ds = DOTADataset(
tmp_path,
split="train",
label_dir=label_dir,
image_dir=image_dir,
difficult_strategy="drop",
filter_empty_gt=True,
)
assert len(keep_ds) == 1
assert len(drop_ds) == 0
assert drop_ds.empty_gt_filtered_count == 1