Skip to content

Commit 4bef13d

Browse files
change ther refactoring of transform and collate function
1 parent b43d5b6 commit 4bef13d

7 files changed

Lines changed: 623 additions & 457 deletions

File tree

src/lightly_train/_task_models/ltdetr_object_detection/transforms.py

Lines changed: 123 additions & 40 deletions
Original file line numberDiff line numberDiff line change
@@ -15,14 +15,6 @@
1515
from lightly_train._task_models.object_detection_components.ltdetr_geometry import (
1616
ltdetr_image_size_divisor,
1717
)
18-
from lightly_train._transforms.ltdetr_transforms.base import (
19-
LTDETRMosaicArgs,
20-
LTDETRRandomFlipArgs,
21-
LTDETRRandomIoUCropArgs,
22-
LTDETRRandomPhotometricDistortArgs,
23-
LTDETRRandomZoomOutArgs,
24-
LTDETRResizeArgs,
25-
)
2618
from lightly_train._transforms.ltdetr_transforms.object_detection import (
2719
LTDETRObjectDetectionTransform,
2820
LTDETRObjectDetectionTransformArgs,
@@ -51,6 +43,63 @@
5143
from lightly_train.types import ImageSizeTuple
5244

5345

46+
class LTDETRObjectDetectionRandomPhotometricDistortArgs(RandomPhotometricDistortArgs):
47+
prob: float = 0.5
48+
49+
brightness: tuple[float, float] = (0.875, 1.125)
50+
contrast: tuple[float, float] = (0.5, 1.5)
51+
saturation: tuple[float, float] = (0.5, 1.5)
52+
hue: tuple[float, float] = (-0.05, 0.05)
53+
54+
# "auto" resolves to epoch 4, or to floor(total_epochs / 3) for runs
55+
# with <= 12 epochs.
56+
step_start: int | Literal["auto"] = "auto"
57+
# "auto" resolves to epoch total_epochs - no_aug_epoch. For shorter runs,
58+
# no_aug_epoch is scaled following a certain rule. See :func:`resolve_ltdetr_step_schedule` for the full algorithm.
59+
# None means photometric distort is always on.
60+
step_stop: int | Literal["auto"] | None = "auto"
61+
62+
63+
class LTDETRObjectDetectionRandomZoomOutArgs(RandomZoomOutArgs):
64+
prob: float = 0.5
65+
66+
fill: float = 0.0
67+
side_range: tuple[float, float] = (1.0, 4.0)
68+
69+
# "auto" resolves to epoch 4, or to floor(total_epochs / 3) for runs
70+
# with <= 12 epochs.
71+
step_start: int | Literal["auto"] = "auto"
72+
# "auto" resolves to epoch total_epochs - no_aug_epoch. For shorter runs,
73+
# no_aug_epoch is scaled following a certain rule. See :func:`resolve_ltdetr_step_schedule` for the full algorithm.
74+
# None means random zoom out is always on.
75+
step_stop: int | Literal["auto"] | None = "auto"
76+
77+
78+
class LTDETRObjectDetectionRandomIoUCropArgs(RandomIoUCropArgs):
79+
prob: float = 0.8
80+
81+
min_scale: float = 0.3
82+
max_scale: float = 1.0
83+
min_aspect_ratio: float = 0.5
84+
max_aspect_ratio: float = 2.0
85+
sampler_options: Sequence[float] | None = None
86+
crop_trials: int = 40
87+
iou_trials: int = 1000
88+
89+
# "auto" resolves to epoch 4, or to floor(total_epochs / 3) for runs
90+
# with <= 12 epochs.
91+
step_start: int | Literal["auto"] = "auto"
92+
# "auto" resolves to epoch total_epochs - no_aug_epoch. For shorter runs,
93+
# no_aug_epoch is scaled following a certain rule. See :func:`resolve_ltdetr_step_schedule` for the full algorithm.
94+
# None means random IoU crop is always on.
95+
step_stop: int | Literal["auto"] | None = "auto"
96+
97+
98+
class LTDETRObjectDetectionRandomFlipArgs(RandomFlipArgs):
99+
horizontal_prob: float = 0.5
100+
vertical_prob: float = 0.0
101+
102+
54103
class LTDETRObjectDetectionScaleJitterArgs(ScaleJitterArgs):
55104
sizes: Sequence[tuple[int, int]] | None = [
56105
(480, 480),
@@ -124,29 +173,57 @@ class LTDETRObjectDetectionCopyBlendArgs(CopyBlendArgs):
124173
step_stop: int | Literal["auto"] | None = "auto"
125174

126175

176+
class LTDETRObjectDetectionMosaicArgs(MosaicArgs):
177+
prob: float = 0.5
178+
179+
output_size: int = 320
180+
max_size: int | None = None
181+
rotation_range: float = 10.0
182+
translation_range: tuple[float, float] = (0.1, 0.1)
183+
scaling_range: tuple[float, float] = (0.5, 1.5)
184+
fill_value: int | float = 0
185+
max_cached_images: int = 50
186+
random_pop: bool = True
187+
188+
# "auto" resolves to epoch 4, or to floor(total_epochs / 3) for runs
189+
# with <= 12 epochs.
190+
step_start: int | Literal["auto"] = "auto"
191+
# "auto" uses a compressed short-run schedule for <= 12 epochs and
192+
# transitions to the midpoint rule on longer runs.
193+
# None means mosaic is always on.
194+
step_stop: int | Literal["auto"] | None = "auto"
195+
196+
197+
class LTDETRObjectDetectionResizeArgs(ResizeArgs):
198+
height: int | Literal["auto"] = "auto"
199+
width: int | Literal["auto"] = "auto"
200+
201+
127202
class LTDETRObjectDetectionTrainTransformArgs(LTDETRObjectDetectionTransformArgs):
128203
channel_drop: ChannelDropArgs | None = None
129204
num_channels: int | Literal["auto"] = "auto"
130-
photometric_distort: LTDETRRandomPhotometricDistortArgs | None = Field(
131-
default_factory=LTDETRRandomPhotometricDistortArgs
205+
photometric_distort: LTDETRObjectDetectionRandomPhotometricDistortArgs | None = (
206+
Field(default_factory=LTDETRObjectDetectionRandomPhotometricDistortArgs)
132207
)
133-
random_zoom_out: LTDETRRandomZoomOutArgs | None = Field(
134-
default_factory=LTDETRRandomZoomOutArgs
208+
random_zoom_out: LTDETRObjectDetectionRandomZoomOutArgs | None = Field(
209+
default_factory=LTDETRObjectDetectionRandomZoomOutArgs
135210
)
136-
random_iou_crop: LTDETRRandomIoUCropArgs | None = Field(
137-
default_factory=LTDETRRandomIoUCropArgs
211+
random_iou_crop: LTDETRObjectDetectionRandomIoUCropArgs | None = Field(
212+
default_factory=LTDETRObjectDetectionRandomIoUCropArgs
138213
)
139-
random_flip: LTDETRRandomFlipArgs | None = Field(
140-
default_factory=LTDETRRandomFlipArgs
214+
random_flip: LTDETRObjectDetectionRandomFlipArgs | None = Field(
215+
default_factory=LTDETRObjectDetectionRandomFlipArgs
141216
)
142217
random_rotate_90: RandomRotate90Args | None = None
143218
random_rotate: RandomRotationArgs | None = None
144219
image_size: ImageSizeTuple | Literal["auto"] = "auto"
145-
resize: ResizeArgs | None = Field(default_factory=LTDETRResizeArgs)
220+
resize: ResizeArgs | None = Field(default_factory=LTDETRObjectDetectionResizeArgs)
146221
scale_jitter: LTDETRObjectDetectionScaleJitterArgs | None = Field(
147222
default_factory=LTDETRObjectDetectionScaleJitterArgs
148223
)
149-
mosaic: LTDETRMosaicArgs | None = Field(default_factory=LTDETRMosaicArgs)
224+
mosaic: LTDETRObjectDetectionMosaicArgs | None = Field(
225+
default_factory=LTDETRObjectDetectionMosaicArgs
226+
)
150227
mixup: LTDETRObjectDetectionMixUpArgs | None = Field(
151228
default_factory=LTDETRObjectDetectionMixUpArgs
152229
)
@@ -253,9 +330,9 @@ class LTDETRObjectDetectionValTransformArgs(LTDETRObjectDetectionTransformArgs):
253330
random_rotate_90: RandomRotate90Args | None = None
254331
random_rotate: RandomRotationArgs | None = None
255332
image_size: ImageSizeTuple | Literal["auto"] = "auto"
256-
resize: ResizeArgs | None = Field(default_factory=LTDETRResizeArgs)
333+
resize: ResizeArgs | None = Field(default_factory=LTDETRObjectDetectionResizeArgs)
257334
scale_jitter: ScaleJitterArgs | None = None
258-
mosaic: LTDETRMosaicArgs | None = None
335+
mosaic: LTDETRObjectDetectionMosaicArgs | None = None
259336
mixup: LTDETRObjectDetectionMixUpArgs | None = None
260337
copyblend: LTDETRObjectDetectionCopyBlendArgs | None = None
261338
bbox_params: BboxParams = Field(
@@ -324,7 +401,9 @@ class LTDETRObjectDetectionValTransform(LTDETRObjectDetectionTransform):
324401

325402
# TODO (Lionel, 06/26): Remove all the `v2` naming once the DINOv2 LT-DETR models are
326403
# completely migrated to the generic LTDETR pipeline.
327-
class DINOv2LTDETRRandomPhotometricDistortArgsV2(RandomPhotometricDistortArgs):
404+
class DINOv2LTDETRObjectDetectionRandomPhotometricDistortArgsV2(
405+
RandomPhotometricDistortArgs
406+
):
328407
prob: float = 0.5
329408

330409
brightness: tuple[float, float] = (0.875, 1.125)
@@ -341,7 +420,7 @@ class DINOv2LTDETRRandomPhotometricDistortArgsV2(RandomPhotometricDistortArgs):
341420
step_stop: int | Literal["auto"] | None = "auto"
342421

343422

344-
class DINOv2LTDETRRandomZoomOutArgsV2(RandomZoomOutArgs):
423+
class DINOv2LTDETRObjectDetectionRandomZoomOutArgsV2(RandomZoomOutArgs):
345424
prob: float = 0.5
346425

347426
fill: float = 0.0
@@ -356,7 +435,7 @@ class DINOv2LTDETRRandomZoomOutArgsV2(RandomZoomOutArgs):
356435
step_stop: int | Literal["auto"] | None = "auto"
357436

358437

359-
class DINOv2LTDETRRandomIoUCropArgsV2(RandomIoUCropArgs):
438+
class DINOv2LTDETRObjectDetectionRandomIoUCropArgsV2(RandomIoUCropArgs):
360439
prob: float = 0.8
361440

362441
min_scale: float = 0.3
@@ -376,7 +455,7 @@ class DINOv2LTDETRRandomIoUCropArgsV2(RandomIoUCropArgs):
376455
step_stop: int | Literal["auto"] | None = "auto"
377456

378457

379-
class DINOv2LTDETRRandomFlipArgsV2(RandomFlipArgs):
458+
class DINOv2LTDETRObjectDetectionRandomFlipArgsV2(RandomFlipArgs):
380459
horizontal_prob: float = 0.5
381460
vertical_prob: float = 0.0
382461

@@ -457,7 +536,7 @@ class DINOv2LTDETRObjectDetectionCopyBlendArgsV2(CopyBlendArgs):
457536
step_stop: int | Literal["auto"] | None = "auto"
458537

459538

460-
class DINOv2LTDETRMosaicArgsV2(MosaicArgs):
539+
class DINOv2LTDETRObjectDetectionMosaicArgsV2(MosaicArgs):
461540
prob: float = 0.5
462541

463542
output_size: int = 320
@@ -478,7 +557,7 @@ class DINOv2LTDETRMosaicArgsV2(MosaicArgs):
478557
step_stop: int | Literal["auto"] | None = "auto"
479558

480559

481-
class DINOv2LTDETRResizeArgsV2(ResizeArgs):
560+
class DINOv2LTDETRObjectDetectionResizeArgsV2(ResizeArgs):
482561
height: int | Literal["auto"] = "auto"
483562
width: int | Literal["auto"] = "auto"
484563

@@ -488,27 +567,29 @@ class DINOv2LTDETRObjectDetectionTrainTransformArgsV2(
488567
):
489568
channel_drop: None = None
490569
num_channels: int | Literal["auto"] = "auto"
491-
photometric_distort: DINOv2LTDETRRandomPhotometricDistortArgsV2 | None = Field(
492-
default_factory=DINOv2LTDETRRandomPhotometricDistortArgsV2
493-
)
494-
random_zoom_out: DINOv2LTDETRRandomZoomOutArgsV2 | None = Field(
495-
default_factory=DINOv2LTDETRRandomZoomOutArgsV2
570+
photometric_distort: (
571+
DINOv2LTDETRObjectDetectionRandomPhotometricDistortArgsV2 | None
572+
) = Field(default_factory=DINOv2LTDETRObjectDetectionRandomPhotometricDistortArgsV2)
573+
random_zoom_out: DINOv2LTDETRObjectDetectionRandomZoomOutArgsV2 | None = Field(
574+
default_factory=DINOv2LTDETRObjectDetectionRandomZoomOutArgsV2
496575
)
497-
random_iou_crop: DINOv2LTDETRRandomIoUCropArgsV2 | None = Field(
498-
default_factory=DINOv2LTDETRRandomIoUCropArgsV2
576+
random_iou_crop: DINOv2LTDETRObjectDetectionRandomIoUCropArgsV2 | None = Field(
577+
default_factory=DINOv2LTDETRObjectDetectionRandomIoUCropArgsV2
499578
)
500-
random_flip: DINOv2LTDETRRandomFlipArgsV2 | None = Field(
501-
default_factory=DINOv2LTDETRRandomFlipArgsV2
579+
random_flip: DINOv2LTDETRObjectDetectionRandomFlipArgsV2 | None = Field(
580+
default_factory=DINOv2LTDETRObjectDetectionRandomFlipArgsV2
502581
)
503582
random_rotate_90: RandomRotate90Args | None = None
504583
random_rotate: RandomRotationArgs | None = None
505584
image_size: ImageSizeTuple | Literal["auto"] = "auto"
506-
resize: ResizeArgs | None = Field(default_factory=DINOv2LTDETRResizeArgsV2)
585+
resize: ResizeArgs | None = Field(
586+
default_factory=DINOv2LTDETRObjectDetectionResizeArgsV2
587+
)
507588
scale_jitter: DINOv2LTDETRObjectDetectionScaleJitterArgsV2 | None = Field(
508589
default_factory=DINOv2LTDETRObjectDetectionScaleJitterArgsV2
509590
)
510-
mosaic: DINOv2LTDETRMosaicArgsV2 | None = Field(
511-
default_factory=DINOv2LTDETRMosaicArgsV2
591+
mosaic: DINOv2LTDETRObjectDetectionMosaicArgsV2 | None = Field(
592+
default_factory=DINOv2LTDETRObjectDetectionMosaicArgsV2
512593
)
513594
mixup: DINOv2LTDETRObjectDetectionMixUpArgsV2 | None = Field(
514595
default_factory=DINOv2LTDETRObjectDetectionMixUpArgsV2
@@ -595,9 +676,11 @@ class DINOv2LTDETRObjectDetectionValTransformArgsV2(LTDETRObjectDetectionTransfo
595676
random_rotate_90: RandomRotate90Args | None = None
596677
random_rotate: RandomRotationArgs | None = None
597678
image_size: ImageSizeTuple | Literal["auto"] = "auto"
598-
resize: ResizeArgs | None = Field(default_factory=DINOv2LTDETRResizeArgsV2)
679+
resize: ResizeArgs | None = Field(
680+
default_factory=DINOv2LTDETRObjectDetectionResizeArgsV2
681+
)
599682
scale_jitter: ScaleJitterArgs | None = None
600-
mosaic: DINOv2LTDETRMosaicArgsV2 | None = None
683+
mosaic: DINOv2LTDETRObjectDetectionMosaicArgsV2 | None = None
601684
mixup: DINOv2LTDETRObjectDetectionMixUpArgsV2 | None = None
602685
copyblend: DINOv2LTDETRObjectDetectionCopyBlendArgsV2 | None = None
603686
bbox_params: BboxParams = Field(

0 commit comments

Comments
 (0)