henry000 commited on
Commit
4750cd0
·
1 Parent(s): 1ff7fa6

✨ [Add] Data augmentation of only pad and resize

Browse files
Files changed (1) hide show
  1. yolo/tools/data_augmentation.py +25 -0
yolo/tools/data_augmentation.py CHANGED
@@ -10,6 +10,7 @@ class AugmentationComposer:
10
  def __init__(self, transforms, image_size: int = 640):
11
  self.transforms = transforms
12
  self.image_size = image_size
 
13
 
14
  for transform in self.transforms:
15
  if hasattr(transform, "set_parent"):
@@ -18,9 +19,33 @@ class AugmentationComposer:
18
  def __call__(self, image, boxes):
19
  for transform in self.transforms:
20
  image, boxes = transform(image, boxes)
 
21
  return image, boxes
22
 
23
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
24
  class HorizontalFlip:
25
  """Randomly horizontally flips the image along with the bounding boxes."""
26
 
 
10
  def __init__(self, transforms, image_size: int = 640):
11
  self.transforms = transforms
12
  self.image_size = image_size
13
+ self.pad_resize = PadAndResize(self.image_size)
14
 
15
  for transform in self.transforms:
16
  if hasattr(transform, "set_parent"):
 
19
  def __call__(self, image, boxes):
20
  for transform in self.transforms:
21
  image, boxes = transform(image, boxes)
22
+ image, boxes = self.pad_resize(image, boxes)
23
  return image, boxes
24
 
25
 
26
+ class PadAndResize:
27
+ def __init__(self, image_size):
28
+ """Initialize the object with the target image size."""
29
+ self.image_size = image_size
30
+
31
+ def __call__(self, image, boxes):
32
+ original_size = max(image.size)
33
+ scale = self.image_size / original_size
34
+ square_img = Image.new("RGB", (original_size, original_size), (255, 255, 255))
35
+ left = (original_size - image.width) // 2
36
+ top = (original_size - image.height) // 2
37
+ square_img.paste(image, (left, top))
38
+
39
+ resized_img = square_img.resize((self.image_size, self.image_size))
40
+
41
+ boxes[:, 1] = (boxes[:, 1] + left) * scale # xmin
42
+ boxes[:, 2] = (boxes[:, 2] + top) * scale # ymin
43
+ boxes[:, 3] = (boxes[:, 3] + left) * scale # xmax
44
+ boxes[:, 4] = (boxes[:, 4] + top) * scale # ymax
45
+
46
+ return resized_img, boxes
47
+
48
+
49
  class HorizontalFlip:
50
  """Randomly horizontally flips the image along with the bounding boxes."""
51