Skip to content

Commit fe64019

Browse files
committed
Remove random_crop augmentation
1 parent 8218859 commit fe64019

File tree

8 files changed

+9
-429
lines changed

8 files changed

+9
-429
lines changed

docs/config.md

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -59,9 +59,6 @@ The config file has three main sections:
5959
- `mixup_lambda`: (float) min-max value of mixup strength. Default is 0-1. *Default*: `None`.
6060
- `mixup_p`: (float) Probability of applying random mixup v2. *Default*=0.0
6161
- `input_key`: (str) Can be `image` or `instance`. The input_key `instance` expects the KorniaAugmenter to follow the InstanceCropper else `image` otherwise for default.
62-
- `random_crop_p`: (float) Probability of applying random crop.
63-
- `random_crop_height`: (int) Desired output height of the random crop.
64-
- `random_crop_width`: (int) Desired output height of the random crop.
6562
- `model_config`:
6663
- `init_weight`: (str) model weights initialization method. "default" uses kaiming uniform initialization and "xavier" uses Xavier initialization method.
6764
- `pre_trained_weights`: (str) Pretrained weights file name supported only for ConvNext and SwinT backbones. For ConvNext, one of ["ConvNeXt_Base_Weights","ConvNeXt_Tiny_Weights", "ConvNeXt_Small_Weights", "ConvNeXt_Large_Weights"]. For SwinT, one of ["Swin_T_Weights", "Swin_S_Weights", "Swin_B_Weights"].

docs/config_centroid.yaml

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -36,9 +36,6 @@ data_config:
3636
erase_p: 0
3737
mixup_lambda: null
3838
mixup_p: 0
39-
random_crop_p: 0
40-
crop_height: 160
41-
crop_width: 160
4239

4340
model_config:
4441
init_weights: xavier

sleap_nn/data/augmentation.py

Lines changed: 0 additions & 43 deletions
Original file line numberDiff line numberDiff line change
@@ -123,9 +123,6 @@ def apply_geometric_augmentation(
123123
erase_p: float = 0.0,
124124
mixup_lambda: Union[Optional[float], Tuple[float, float], None] = None,
125125
mixup_p: float = 0.0,
126-
random_crop_height: int = 0,
127-
random_crop_width: int = 0,
128-
random_crop_p: float = 0.0,
129126
) -> Tuple[torch.Tensor]:
130127
"""Apply kornia geometric augmentation on image and instances.
131128
@@ -153,9 +150,6 @@ def apply_geometric_augmentation(
153150
erase_p: Probability of applying random erase.
154151
mixup_lambda: min-max value of mixup strength. Default is 0-1. Default: `None`.
155152
mixup_p: Probability of applying random mixup v2.
156-
random_crop_height: Desired output height of the crop. Must be int.
157-
random_crop_width: Desired output width of the crop. Must be int.
158-
random_crop_p: Probability of applying random crop.
159153
160154
161155
Returns:
@@ -195,19 +189,6 @@ def apply_geometric_augmentation(
195189
same_on_batch=True,
196190
)
197191
)
198-
if random_crop_p > 0:
199-
if random_crop_height > 0 and random_crop_width > 0:
200-
aug_stack.append(
201-
K.augmentation.RandomCrop(
202-
size=(random_crop_height, random_crop_width),
203-
pad_if_needed=True,
204-
p=random_crop_p,
205-
keepdim=True,
206-
same_on_batch=True,
207-
)
208-
)
209-
else:
210-
raise ValueError(f"random_crop_hw height and width must be greater than 0.")
211192

212193
augmenter = AugmentationSequential(
213194
*aug_stack,
@@ -350,9 +331,6 @@ class KorniaAugmenter(IterDataPipe):
350331
erase_p: Probability of applying random erase.
351332
mixup_lambda: min-max value of mixup strength. Default is 0-1. Default: `None`.
352333
mixup_p: Probability of applying random mixup v2.
353-
random_crop_height: Desired output height of the crop. Must be int.
354-
random_crop_width: Desired output width of the crop. Must be int.
355-
random_crop_p: Probability of applying random crop.
356334
input_key: Can be `image` or `instance`. The input_key `instance` expects the
357335
the KorniaAugmenter to follow the InstanceCropper else `image` otherwise
358336
for default.
@@ -398,9 +376,6 @@ def __init__(
398376
erase_p: float = 0.0,
399377
mixup_lambda: Union[Optional[float], Tuple[float, float], None] = None,
400378
mixup_p: float = 0.0,
401-
random_crop_height: int = 0,
402-
random_crop_width: int = 0,
403-
random_crop_p: float = 0.0,
404379
image_key: str = "image",
405380
instance_key: str = "instances",
406381
) -> None:
@@ -431,9 +406,6 @@ def __init__(
431406
self.erase_p = erase_p
432407
self.mixup_lambda = mixup_lambda
433408
self.mixup_p = mixup_p
434-
self.random_crop_height = random_crop_height
435-
self.random_crop_width = random_crop_width
436-
self.random_crop_p = random_crop_p
437409
self.image_key = image_key
438410
self.instance_key = instance_key
439411

@@ -505,21 +477,6 @@ def __init__(
505477
same_on_batch=True,
506478
)
507479
)
508-
if self.random_crop_p > 0:
509-
if self.random_crop_height > 0 and self.random_crop_width > 0:
510-
aug_stack.append(
511-
K.augmentation.RandomCrop(
512-
size=(self.random_crop_height, self.random_crop_width),
513-
pad_if_needed=True,
514-
p=self.random_crop_p,
515-
keepdim=True,
516-
same_on_batch=True,
517-
)
518-
)
519-
else:
520-
raise ValueError(
521-
f"random_crop_hw height and width must be greater than 0."
522-
)
523480

524481
self.augmenter = AugmentationSequential(
525482
*aug_stack,

sleap_nn/data/pipelines.py

Lines changed: 0 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -191,16 +191,6 @@ def make_training_pipeline(
191191
instance_key="instances",
192192
)
193193

194-
if "random_crop" in self.data_config.augmentation_config:
195-
datapipe = KorniaAugmenter(
196-
datapipe,
197-
random_crop_height=self.data_config.augmentation_config.random_crop.crop_height,
198-
random_crop_width=self.data_config.augmentation_config.random_crop.crop_width,
199-
random_crop_p=self.data_config.augmentation_config.random_crop.random_crop_p,
200-
image_key="image",
201-
instance_key="instances",
202-
)
203-
204194
datapipe = Resizer(datapipe, scale=self.data_config.preprocessing.scale)
205195
datapipe = PadToStride(datapipe, max_stride=self.max_stride)
206196

@@ -291,15 +281,6 @@ def make_training_pipeline(
291281
image_key="image",
292282
instance_key="instances",
293283
)
294-
if "random_crop" in self.data_config.augmentation_config:
295-
datapipe = KorniaAugmenter(
296-
datapipe,
297-
random_crop_height=self.data_config.augmentation_config.random_crop.crop_height,
298-
random_crop_width=self.data_config.augmentation_config.random_crop.crop_width,
299-
random_crop_p=self.data_config.augmentation_config.random_crop.random_crop_p,
300-
image_key="image",
301-
instance_key="instances",
302-
)
303284

304285
datapipe = Resizer(datapipe, scale=self.data_config.preprocessing.scale)
305286
datapipe = PadToStride(datapipe, max_stride=self.max_stride)
@@ -394,16 +375,6 @@ def make_training_pipeline(
394375
instance_key="instances",
395376
)
396377

397-
if "random_crop" in self.data_config.augmentation_config:
398-
datapipe = KorniaAugmenter(
399-
datapipe,
400-
random_crop_height=self.data_config.augmentation_config.random_crop.crop_height,
401-
random_crop_width=self.data_config.augmentation_config.random_crop.crop_width,
402-
random_crop_p=self.data_config.augmentation_config.random_crop.random_crop_p,
403-
image_key="image",
404-
instance_key="instances",
405-
)
406-
407378
datapipe = Resizer(datapipe, scale=self.data_config.preprocessing.scale)
408379
datapipe = PadToStride(datapipe, max_stride=self.max_stride)
409380

tests/data/test_augmentation.py

Lines changed: 1 addition & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -76,26 +76,16 @@ def test_apply_geometric_augmentation(minimal_instance):
7676
ex["instances"],
7777
scale=0.5,
7878
affine_p=1.0,
79-
random_crop_height=100,
80-
random_crop_width=100,
81-
random_crop_p=1.0,
8279
erase_p=1.0,
8380
mixup_p=1.0,
8481
)
8582
# Test all augmentations.
8683
assert torch.is_tensor(img)
8784
assert torch.is_tensor(pts)
8885
assert not torch.equal(img, ex["image"])
89-
assert img.shape == (1, 1, 100, 100)
86+
assert img.shape == (1, 1, 384, 384)
9087
assert pts.shape == (1, 2, 2, 2)
9188

92-
with pytest.raises(
93-
ValueError, match="crop_hw height and width must be greater than 0."
94-
):
95-
img, pts = apply_geometric_augmentation(
96-
ex["image"], ex["instances"], random_crop_p=1.0, random_crop_height=0
97-
)
98-
9989

10090
def test_kornia_augmentation(minimal_instance):
10191
"""Test KorniaAugmenter module."""
@@ -112,9 +102,6 @@ def test_kornia_augmentation(minimal_instance):
112102
erase_p=1.0,
113103
mixup_p=1.0,
114104
mixup_lambda=(0.0, 1.0),
115-
random_crop_height=384,
116-
random_crop_width=384,
117-
random_crop_p=1.0,
118105
)
119106

120107
# Test all augmentations.
@@ -125,16 +112,3 @@ def test_kornia_augmentation(minimal_instance):
125112
assert torch.is_tensor(pts)
126113
assert img.shape == (1, 1, 384, 384)
127114
assert pts.shape == (1, 2, 2, 2)
128-
129-
# Test RandomCrop value error.
130-
p = LabelsReaderDP.from_filename(minimal_instance)
131-
p = Normalizer(p)
132-
with pytest.raises(
133-
ValueError, match="crop_hw height and width must be greater than 0."
134-
):
135-
p = KorniaAugmenter(
136-
p,
137-
random_crop_height=0,
138-
random_crop_width=0,
139-
random_crop_p=1.0,
140-
)

tests/data/test_custom_datasets.py

Lines changed: 4 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -252,9 +252,6 @@ def test_centered_instance_dataset(minimal_instance):
252252
"erase_p": 0.5,
253253
"mixup_lambda": None,
254254
"mixup_p": 0.5,
255-
"random_crop_p": 0.0,
256-
"random_crop_height": 160,
257-
"random_crop_width": 160,
258255
},
259256
},
260257
}
@@ -327,9 +324,6 @@ def test_centered_instance_dataset(minimal_instance):
327324
"erase_p": 0.5,
328325
"mixup_lambda": None,
329326
"mixup_p": 0.5,
330-
"random_crop_p": 0.0,
331-
"random_crop_height": 160,
332-
"random_crop_width": 160,
333327
},
334328
},
335329
}
@@ -445,9 +439,6 @@ def test_centroid_dataset(minimal_instance):
445439
"erase_p": 0.5,
446440
"mixup_lambda": None,
447441
"mixup_p": 0.5,
448-
"random_crop_p": 1.0,
449-
"random_crop_height": 160,
450-
"random_crop_width": 160,
451442
},
452443
},
453444
}
@@ -478,8 +469,8 @@ def test_centroid_dataset(minimal_instance):
478469

479470
for gt_key, key in zip(sorted(gt_sample_keys), sorted(sample.keys())):
480471
assert gt_key == key
481-
assert sample["image"].shape == (1, 1, 160, 160)
482-
assert sample["centroids_confidence_maps"].shape == (1, 1, 80, 80)
472+
assert sample["image"].shape == (1, 1, 384, 384)
473+
assert sample["centroids_confidence_maps"].shape == (1, 1, 192, 192)
483474

484475

485476
def test_single_instance_dataset(minimal_instance):
@@ -568,9 +559,6 @@ def test_single_instance_dataset(minimal_instance):
568559
"erase_p": 0.5,
569560
"mixup_lambda": None,
570561
"mixup_p": 0.5,
571-
"random_crop_p": 1.0,
572-
"random_crop_height": 160,
573-
"random_crop_width": 160,
574562
},
575563
},
576564
}
@@ -598,6 +586,6 @@ def test_single_instance_dataset(minimal_instance):
598586

599587
for gt_key, key in zip(sorted(gt_sample_keys), sorted(sample.keys())):
600588
assert gt_key == key
601-
assert sample["image"].shape == (1, 1, 160, 160)
602-
assert sample["confidence_maps"].shape == (1, 2, 80, 80)
589+
assert sample["image"].shape == (1, 1, 384, 384)
590+
assert sample["confidence_maps"].shape == (1, 2, 192, 192)
603591
assert sample["instances"].shape == (1, 1, 2, 2)

0 commit comments

Comments
 (0)