Skip to content

Commit 388b19c

Browse files
camilodltNicolasHugdatumbox
authored
Explicitely store a distance value that is reused (#4341)
* Explicitely store a distance value that is reused I don't see a reason to calculate the value twice for each distance. Knowing that this code is going to be called at every epoch and that probably there is not a compiler that optimizes this (+ also code clarity), I think is best to store the value for x and y. * Update torchvision/models/detection/_utils.py Co-authored-by: Nicolas Hug <[email protected]> * removing spaces Co-authored-by: Nicolas Hug <[email protected]> Co-authored-by: Vasilis Vryniotis <[email protected]>
1 parent 5772590 commit 388b19c

File tree

1 file changed

+8
-4
lines changed

1 file changed

+8
-4
lines changed

torchvision/models/detection/_utils.py

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -216,10 +216,14 @@ def decode_single(self, rel_codes, boxes):
216216
pred_w = torch.exp(dw) * widths[:, None]
217217
pred_h = torch.exp(dh) * heights[:, None]
218218

219-
pred_boxes1 = pred_ctr_x - torch.tensor(0.5, dtype=pred_ctr_x.dtype, device=pred_w.device) * pred_w
220-
pred_boxes2 = pred_ctr_y - torch.tensor(0.5, dtype=pred_ctr_y.dtype, device=pred_h.device) * pred_h
221-
pred_boxes3 = pred_ctr_x + torch.tensor(0.5, dtype=pred_ctr_x.dtype, device=pred_w.device) * pred_w
222-
pred_boxes4 = pred_ctr_y + torch.tensor(0.5, dtype=pred_ctr_y.dtype, device=pred_h.device) * pred_h
219+
# Distance from center to box's corner.
220+
c_to_c_h = torch.tensor(0.5, dtype=pred_ctr_y.dtype, device=pred_h.device) * pred_h
221+
c_to_c_w = torch.tensor(0.5, dtype=pred_ctr_x.dtype, device=pred_w.device) * pred_w
222+
223+
pred_boxes1 = pred_ctr_x - c_to_c_w
224+
pred_boxes2 = pred_ctr_y - c_to_c_h
225+
pred_boxes3 = pred_ctr_x + c_to_c_w
226+
pred_boxes4 = pred_ctr_y + c_to_c_h
223227
pred_boxes = torch.stack((pred_boxes1, pred_boxes2, pred_boxes3, pred_boxes4), dim=2).flatten(1)
224228
return pred_boxes
225229

0 commit comments

Comments
 (0)