Skip to content

Commit 494e02f

Browse files
committed
apply anchor transformations beforehand to make inference slightly faster
1 parent 42c0bcd commit 494e02f

File tree

1 file changed

+12
-4
lines changed

1 file changed

+12
-4
lines changed

efficientdet/utils.py

Lines changed: 12 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -16,10 +16,10 @@ def forward(self, anchors, regression):
1616
Returns:
1717
1818
"""
19-
y_centers_a = (anchors[..., 0] + anchors[..., 2]) / 2
20-
x_centers_a = (anchors[..., 1] + anchors[..., 3]) / 2
21-
ha = anchors[..., 2] - anchors[..., 0]
22-
wa = anchors[..., 3] - anchors[..., 1]
19+
x_centers_a = anchors[..., 0]
20+
y_centers_a = anchors[..., 1]
21+
wa = anchors[..., 2]
22+
ha = anchors[..., 3]
2323

2424
w = regression[..., 3].exp() * wa
2525
h = regression[..., 2].exp() * ha
@@ -130,6 +130,14 @@ def forward(self, image, dtype=torch.float32):
130130
anchor_boxes = np.vstack(boxes_all)
131131

132132
anchor_boxes = torch.from_numpy(anchor_boxes.astype(dtype)).to(image.device)
133+
134+
# transform
135+
y_centers_a = (anchor_boxes[..., 0] + anchor_boxes[..., 2]) / 2
136+
x_centers_a = (anchor_boxes[..., 1] + anchor_boxes[..., 3]) / 2
137+
ha = anchor_boxes[..., 2] - anchor_boxes[..., 0]
138+
wa = anchor_boxes[..., 3] - anchor_boxes[..., 1]
139+
140+
anchor_boxes = torch.stack([x_centers_a, y_centers_a, wa, ha], 1)
133141
anchor_boxes = anchor_boxes.unsqueeze(0)
134142

135143
# save it for later use to reduce overhead

0 commit comments

Comments
 (0)