♻️ [Refactor] the code of data augment and rename
Browse files
config/data/augmentation.yaml
CHANGED
@@ -1,3 +1,3 @@
|
|
1 |
Mosaic: 1
|
2 |
-
MixUp: 1
|
3 |
-
|
|
|
1 |
Mosaic: 1
|
2 |
+
# MixUp: 1
|
3 |
+
HorizontalFlip: 0.5
|
tests/test_utils/test_dataaugment.py
CHANGED
@@ -6,7 +6,7 @@ from PIL import Image
|
|
6 |
from torchvision.transforms import functional as TF
|
7 |
|
8 |
sys.path.append("./")
|
9 |
-
from utils.
|
10 |
|
11 |
|
12 |
def test_random_horizontal_flip():
|
|
|
6 |
from torchvision.transforms import functional as TF
|
7 |
|
8 |
sys.path.append("./")
|
9 |
+
from utils.data_augment import Compose, Mosaic, RandomHorizontalFlip
|
10 |
|
11 |
|
12 |
def test_random_horizontal_flip():
|
utils/data_augment.py
CHANGED
@@ -2,7 +2,6 @@ import numpy as np
|
|
2 |
import torch
|
3 |
from PIL import Image
|
4 |
from torchvision.transforms import functional as TF
|
5 |
-
from torchvision.transforms.functional import to_pil_image, to_tensor
|
6 |
|
7 |
|
8 |
class Compose:
|
@@ -22,7 +21,7 @@ class Compose:
|
|
22 |
return image, boxes
|
23 |
|
24 |
|
25 |
-
class
|
26 |
"""Randomly horizontally flips the image along with the bounding boxes."""
|
27 |
|
28 |
def __init__(self, prob=0.5):
|
@@ -35,7 +34,7 @@ class RandomHorizontalFlip:
|
|
35 |
return image, boxes
|
36 |
|
37 |
|
38 |
-
class
|
39 |
"""Randomly vertically flips the image along with the bounding boxes."""
|
40 |
|
41 |
def __init__(self, prob=0.5):
|
@@ -88,6 +87,7 @@ class Mosaic:
|
|
88 |
all_labels.append(adjusted_boxes)
|
89 |
|
90 |
all_labels = torch.cat(all_labels, dim=0)
|
|
|
91 |
return mosaic_image, all_labels
|
92 |
|
93 |
|
@@ -116,10 +116,10 @@ class MixUp:
|
|
116 |
lam = np.random.beta(self.alpha, self.alpha) if self.alpha > 0 else 0.5
|
117 |
|
118 |
# Mix images
|
119 |
-
image1, image2 = to_tensor(image), to_tensor(image2)
|
120 |
mixed_image = lam * image1 + (1 - lam) * image2
|
121 |
|
122 |
# Mix bounding boxes
|
123 |
mixed_boxes = torch.cat([lam * boxes, (1 - lam) * boxes2])
|
124 |
|
125 |
-
return to_pil_image(mixed_image), mixed_boxes
|
|
|
2 |
import torch
|
3 |
from PIL import Image
|
4 |
from torchvision.transforms import functional as TF
|
|
|
5 |
|
6 |
|
7 |
class Compose:
|
|
|
21 |
return image, boxes
|
22 |
|
23 |
|
24 |
+
class HorizontalFlip:
|
25 |
"""Randomly horizontally flips the image along with the bounding boxes."""
|
26 |
|
27 |
def __init__(self, prob=0.5):
|
|
|
34 |
return image, boxes
|
35 |
|
36 |
|
37 |
+
class VerticalFlip:
|
38 |
"""Randomly vertically flips the image along with the bounding boxes."""
|
39 |
|
40 |
def __init__(self, prob=0.5):
|
|
|
87 |
all_labels.append(adjusted_boxes)
|
88 |
|
89 |
all_labels = torch.cat(all_labels, dim=0)
|
90 |
+
mosaic_image = mosaic_image.resize((img_sz, img_sz))
|
91 |
return mosaic_image, all_labels
|
92 |
|
93 |
|
|
|
116 |
lam = np.random.beta(self.alpha, self.alpha) if self.alpha > 0 else 0.5
|
117 |
|
118 |
# Mix images
|
119 |
+
image1, image2 = TF.to_tensor(image), TF.to_tensor(image2)
|
120 |
mixed_image = lam * image1 + (1 - lam) * image2
|
121 |
|
122 |
# Mix bounding boxes
|
123 |
mixed_boxes = torch.cat([lam * boxes, (1 - lam) * boxes2])
|
124 |
|
125 |
+
return TF.to_pil_image(mixed_image), mixed_boxes
|