Skip to content

Commit 959b9b0

Browse files
authored
πŸ› [Fix] image size order inconsistencies (#118)
* πŸ› [Fix] image size order inconsistencies Fixes the expected order of image_size to be [width, height] in bounding_box_utils.py/generate_anchors function as in the rest of the repository. Additionally fixes the same issue in the create_auto_anchor functions of the Vec2Box and Anc2Box classes, where the order does not matter, but for the sake of consistency. * πŸ› [Fix] incorrect image_size order in ValidateModel Fixes an additional image_size order related bug in ValidateModel, in which the PostProcessor received the image_size in [height, width] instead of [width, height] order. * πŸ› [Fix] a variable spelling mistake Fixes a bug in Anc2Box, where its update function was updating "self.anchor_grid" instead of "self.anchor_grids" as initialized in the __init__() function. * πŸ› [Fix] test_anc2box_autoanchor The asserted Tensor shapes were wrong. The number of anchors should be half in each row as in the columns for the updated resolution of (320, 640) [width, height].
1 parent 2522f72 commit 959b9b0

File tree

3 files changed

+14
-11
lines changed

3 files changed

+14
-11
lines changed

β€Žtests/test_utils/test_bounding_box_utils.pyβ€Ž

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -138,9 +138,9 @@ def test_anc2box_autoanchor(inference_v7_cfg: Config):
138138
anc2box.update((320, 640))
139139
anchor_grids_shape = [anchor_grid.shape for anchor_grid in anc2box.anchor_grids]
140140
assert anchor_grids_shape == [
141-
torch.Size([1, 1, 80, 80, 2]),
142-
torch.Size([1, 1, 40, 40, 2]),
143-
torch.Size([1, 1, 20, 20, 2]),
141+
torch.Size([1, 1, 80, 40, 2]),
142+
torch.Size([1, 1, 40, 20, 2]),
143+
torch.Size([1, 1, 20, 10, 2]),
144144
]
145145
assert anc2box.anchor_scale.shape == torch.Size([3, 1, 3, 1, 1, 2])
146146

β€Žyolo/tools/solver.pyβ€Ž

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -45,7 +45,8 @@ def val_dataloader(self):
4545

4646
def validation_step(self, batch, batch_idx):
4747
batch_size, images, targets, rev_tensor, img_paths = batch
48-
predicts = self.post_process(self(images), image_size=images.shape[2:])
48+
H, W = images.shape[2:]
49+
predicts = self.post_process(self(images), image_size=[W, H])
4950
batch_metrics = self.metric(
5051
[to_metrics_format(predict) for predict in predicts], [to_metrics_format(target) for target in targets]
5152
)

β€Žyolo/utils/bounding_box_utils.pyβ€Ž

Lines changed: 9 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -122,7 +122,7 @@ def generate_anchors(image_size: List[int], strides: List[int]):
122122
all_anchors [HW x 2]:
123123
all_scalers [HW]: The index of the best targets for each anchors
124124
"""
125-
H, W = image_size
125+
W, H = image_size
126126
anchors = []
127127
scaler = []
128128
for stride in strides:
@@ -312,17 +312,18 @@ def __init__(self, model: YOLO, anchor_cfg: AnchorConfig, image_size, device):
312312
self.anchor_grid, self.scaler = anchor_grid.to(device), scaler.to(device)
313313

314314
def create_auto_anchor(self, model: YOLO, image_size):
315-
dummy_input = torch.zeros(1, 3, *image_size).to(self.device)
315+
W, H = image_size
316+
dummy_input = torch.zeros(1, 3, H, W).to(self.device)
316317
dummy_output = model(dummy_input)
317318
strides = []
318319
for predict_head in dummy_output["Main"]:
319320
_, _, *anchor_num = predict_head[2].shape
320-
strides.append(image_size[1] // anchor_num[1])
321+
strides.append(W // anchor_num[1])
321322
return strides
322323

323324
def update(self, image_size):
324325
"""
325-
image_size: H, W
326+
image_size: W, H
326327
"""
327328
if self.image_size == image_size:
328329
return
@@ -365,12 +366,13 @@ def __init__(self, model: YOLO, anchor_cfg: AnchorConfig, image_size, device):
365366
self.class_num = model.num_classes
366367

367368
def create_auto_anchor(self, model: YOLO, image_size):
368-
dummy_input = torch.zeros(1, 3, *image_size).to(self.device)
369+
W, H = image_size
370+
dummy_input = torch.zeros(1, 3, H, W).to(self.device)
369371
dummy_output = model(dummy_input)
370372
strides = []
371373
for predict_head in dummy_output["Main"]:
372374
_, _, *anchor_num = predict_head.shape
373-
strides.append(image_size[1] // anchor_num[1])
375+
strides.append(W // anchor_num[1])
374376
return strides
375377

376378
def generate_anchors(self, image_size: List[int]):
@@ -383,7 +385,7 @@ def generate_anchors(self, image_size: List[int]):
383385
return anchor_grids
384386

385387
def update(self, image_size):
386-
self.anchor_grid = self.generate_anchors(image_size)
388+
self.anchor_grids = self.generate_anchors(image_size)
387389

388390
def __call__(self, predicts: List[Tensor]):
389391
preds_box, preds_cls, preds_cnf = [], [], []

0 commit comments

Comments
Β (0)