Optimize CPU GenerateProposals op by lazily generating anchors (3-5x faster) (#15103)
authorViswanath Sivakumar <viswanath@fb.com>
Wed, 12 Dec 2018 23:48:03 +0000 (15:48 -0800)
committerFacebook Github Bot <facebook-github-bot@users.noreply.github.com>
Wed, 12 Dec 2018 23:53:52 +0000 (15:53 -0800)
commitf34d82700743e2bf6c0d163fc046d1454ccc4c4c
tree5294cb2f0b4909ffa708d5739fe3fc505d3f5a9f
parent90f9e8103c9b1108ae923f5ab67bcc566e942699
Optimize CPU GenerateProposals op by lazily generating anchors (3-5x faster) (#15103)

Summary:
Pull Request resolved: https://github.com/pytorch/pytorch/pull/15103

There are two main optimizations in this diff:
1. We generate all anchors for every single spatial grid first, and then apply
NMS to pick 2000 anchors according to RPN_PRE_NMS_TOP_N. By first sorting the
score and picking the 2000 top ones and then lazily generating only the
corresponding anchors is much faster.
2. Transposing bbox_deltas from (num_anchors * 4, H, W) to
(H, W, num_anchors * 4) was also quite slow - taking about 20ms in the RRPN
case when there are lots of anchors which it's negligible for RPN case (like
0.1 ms). Instead of transponsing, performing all operations in the
(num_anchors, H, W) format speeds things up.

For regular RPN scenario, this gives 5x speedup from 5.84ms to 1.18ms a case
with 35 anchors over a 600x600 image.

For rotated boxes with 245 anchors, the runtime down from 80ms to 27ms per
iter.

Reviewed By: newstzpz

Differential Revision: D13428688

fbshipit-source-id: 6006b332925e01a7c9433ded2ff5dc9e6d96f7d3
caffe2/operators/generate_proposals_op.cc
caffe2/operators/generate_proposals_op.h
caffe2/operators/generate_proposals_op_test.cc