Skip to content

Commit b3dcb20

Browse files
authored
Merge pull request #186 from rwightman/bbox_aug_fix
Fix for augmentation bbox clipping issue
2 parents f1032c5 + 2ac9f92 commit b3dcb20

File tree

1 file changed

+12
-10
lines changed

1 file changed

+12
-10
lines changed

effdet/data/transforms.py

Lines changed: 12 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -2,11 +2,13 @@
22
33
Hacked together by Ross Wightman
44
"""
5-
import torch
6-
from PIL import Image
7-
import numpy as np
85
import random
96
import math
7+
from copy import deepcopy
8+
9+
from PIL import Image
10+
import numpy as np
11+
import torch
1012

1113
IMAGENET_DEFAULT_MEAN = (0.485, 0.456, 0.406)
1214
IMAGENET_DEFAULT_STD = (0.229, 0.224, 0.225)
@@ -91,13 +93,13 @@ def __call__(self, img, anno: dict):
9193
new_img = Image.new("RGB", (self.target_size[1], self.target_size[0]), color=self.fill_color)
9294
interp_method = _pil_interp(self.interpolation)
9395
img = img.resize((scaled_w, scaled_h), interp_method)
94-
new_img.paste(img)
96+
new_img.paste(img) # pastes at 0,0 (upper-left corner)
9597

9698
if 'bbox' in anno:
97-
# FIXME haven't tested this path since not currently using dataset annotations for train/eval
9899
bbox = anno['bbox']
99100
bbox[:, :4] *= img_scale
100-
clip_boxes_(bbox, (scaled_h, scaled_w))
101+
bbox_bound = (min(scaled_h, self.target_size[0]), min(scaled_w, self.target_size[1]))
102+
clip_boxes_(bbox, bbox_bound) # crop to bounds of target image or letter-box, whichever is smaller
101103
valid_indices = (bbox[:, :2] < bbox[:, 2:4]).all(axis=1)
102104
anno['bbox'] = bbox[valid_indices, :]
103105
anno['cls'] = anno['cls'][valid_indices]
@@ -151,15 +153,15 @@ def __call__(self, img, anno: dict):
151153
right, lower = min(scaled_w, offset_x + self.target_size[1]), min(scaled_h, offset_y + self.target_size[0])
152154
img = img.crop((offset_x, offset_y, right, lower))
153155
new_img = Image.new("RGB", (self.target_size[1], self.target_size[0]), color=self.fill_color)
154-
new_img.paste(img)
156+
new_img.paste(img) # pastes at 0,0 (upper-left corner)
155157

156158
if 'bbox' in anno:
157-
# FIXME not fully tested
158-
bbox = anno['bbox'].copy() # FIXME copy for debugger inspection, back to inplace
159+
bbox = anno['bbox'] # for convenience, modifies in-place
159160
bbox[:, :4] *= img_scale
160161
box_offset = np.stack([offset_y, offset_x] * 2)
161162
bbox -= box_offset
162-
clip_boxes_(bbox, (scaled_h, scaled_w))
163+
bbox_bound = (min(scaled_h, self.target_size[0]), min(scaled_w, self.target_size[1]))
164+
clip_boxes_(bbox, bbox_bound) # crop to bounds of target image or letter-box, whichever is smaller
163165
valid_indices = (bbox[:, :2] < bbox[:, 2:4]).all(axis=1)
164166
anno['bbox'] = bbox[valid_indices, :]
165167
anno['cls'] = anno['cls'][valid_indices]

0 commit comments

Comments
 (0)