Skip to content
This repository has been archived by the owner on Jul 2, 2021. It is now read-only.

Commit

Permalink
change argument order of delta_decode
Browse files Browse the repository at this point in the history
  • Loading branch information
yuyu2172 committed May 18, 2017
1 parent 943aefa commit 7cb46cb
Show file tree
Hide file tree
Showing 5 changed files with 37 additions and 41 deletions.
2 changes: 1 addition & 1 deletion chainercv/links/model/faster_rcnn/faster_rcnn.py
Original file line number Diff line number Diff line change
Expand Up @@ -276,7 +276,7 @@ def predict(self, imgs):
std = self.xp.tile(self.xp.asarray(self.bbox_normalize_std),
self.n_class)
roi_bbox = (roi_bbox * std + mean).astype(np.float32)
raw_bbox = delta_decode(roi, roi_bbox)
raw_bbox = delta_decode(roi_bbox, roi)
# clip bounding box
raw_bbox[:, slice(0, 4, 2)] = self.xp.clip(
raw_bbox[:, slice(0, 4, 2)], 0, W / scale)
Expand Down
5 changes: 3 additions & 2 deletions chainercv/links/model/faster_rcnn/utils/delta_decode.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
from chainer import cuda


def delta_decode(base_raw_bbox, bbox):
def delta_decode(bbox, base_raw_bbox):
"""Decode bounding boxes from bounding box offsets and scales.
Given bounding box offsets and scales (deltas) computed by
Expand All @@ -28,8 +28,9 @@ def delta_decode(base_raw_bbox, bbox):
The output is same type as the type of the inputs.
Args:
base_raw_bbox (array): An array whose shape is :math:`(R, 4)`.
bbox (array): An array with offsets and scales.
Its shape is :math:`(R, 4)`.
base_raw_bbox (array): A coordinates of bounding boxes.
The shapes of :obj:`bbox` and :obj:`gt_bbox` should be same.
Returns:
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -106,7 +106,7 @@ def __call__(self, bbox, score,
anchor = cuda.to_cpu(anchor)

# Convert anchors into proposal via bbox transformations.
roi = delta_decode(anchor, bbox)
roi = delta_decode(bbox, anchor)

# Clip predicted boxes to image.
roi[:, slice(0, 4, 2)] = np.clip(
Expand Down
2 changes: 0 additions & 2 deletions examples/faster_rcnn/demo.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,12 +14,10 @@
def main():
parser = argparse.ArgumentParser()
parser.add_argument('gpu')
parser.add_argument('model')
parser.add_argument('image')
args = parser.parse_args()

model = FasterRCNNVGG16(n_class=21, pretrained_model='voc07')
serializers.load_npz(args.model, model)
if args.gpu >= 0:
model.to_gpu(args.gpu)
chainer.cuda.get_device(args.gpu).use()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,74 +23,71 @@ def generate_bbox(n, img_size, min_length, max_length):
class TestDeltaEncodeDecode(unittest.TestCase):

def setUp(self):
self.bbox = np.array([[0, 0, 49, 29]], dtype=np.float32)
self.gt_bbox = np.array([[0, 0, 49, 29]], dtype=np.float32)
self.target = np.array([[0, 0, 0, 0]], dtype=np.float32)
self.raw_bbox = np.array([[0, 0, 49, 29]], dtype=np.float32)
self.base_raw_bbox = np.array([[0, 0, 49, 29]], dtype=np.float32)
self.expected_enc = np.array([[0, 0, 0, 0]], dtype=np.float32)

def check_delta_encode(self, bbox, gt_bbox, target):
out_target = delta_encode(bbox, gt_bbox)
def check_delta_encode(self, raw_bbox, base_raw_bbox, expected_enc):
bbox = delta_encode(raw_bbox, base_raw_bbox)

xp = cuda.get_array_module(target)
self.assertEqual(xp, cuda.get_array_module(out_target))

np.testing.assert_equal(cuda.to_cpu(out_target),
cuda.to_cpu(target))
self.assertIsInstance(bbox, type(expected_enc))
np.testing.assert_equal(cuda.to_cpu(bbox),
cuda.to_cpu(expected_enc))

def test_delta_encode_cpu(self):
self.check_delta_encode(self.bbox, self.gt_bbox, self.target)
self.check_delta_encode(
self.raw_bbox, self.base_raw_bbox, self.expected_enc)

@attr.gpu
def test_delta_encode_gpu(self):
self.check_delta_encode(
cuda.to_gpu(self.bbox),
cuda.to_gpu(self.gt_bbox),
cuda.to_gpu(self.target))

def check_delta_decode(self, bbox, gt_bbox, target):
out_bbox = delta_decode(bbox, target)
cuda.to_gpu(self.raw_bbox),
cuda.to_gpu(self.base_raw_bbox),
cuda.to_gpu(self.expected_enc))

xp = cuda.get_array_module(gt_bbox)
self.assertEqual(xp, cuda.get_array_module(out_bbox))
def check_delta_decode(self, bbox, base_raw_bbox, expected):
raw_bbox = delta_decode(bbox, base_raw_bbox)

self.assertIsInstance(raw_bbox, type(expected))
np.testing.assert_equal(
cuda.to_cpu(out_bbox), cuda.to_cpu(gt_bbox))
cuda.to_cpu(raw_bbox), cuda.to_cpu(expected))

def test_delta_decode_cpu(self):
self.check_delta_decode(
self.bbox,
self.gt_bbox,
self.target)
self.expected_enc,
self.raw_bbox,
self.base_raw_bbox)

@attr.gpu
def test_delta_decode_gpu(self):
self.check_delta_decode(
cuda.to_gpu(self.bbox),
cuda.to_gpu(self.gt_bbox),
cuda.to_gpu(self.target))
cuda.to_gpu(self.expected_enc),
cuda.to_gpu(self.raw_bbox),
cuda.to_gpu(self.base_raw_bbox))


class TestDeltaEncodeDecodeConsistency(unittest.TestCase):

def setUp(self):
self.bbox = generate_bbox(8, (32, 64), 4, 16)
self.gt_bbox = self.bbox + 1
self.raw_bbox = generate_bbox(8, (32, 64), 4, 16)
self.base_raw_bbox = self.raw_bbox + 1

def check_delta_encode_decode_consistency(self, bbox, gt_bbox):
target = delta_encode(bbox, gt_bbox)
out_bbox = delta_decode(bbox, target)
def check_delta_encode_decode_consistency(self, raw_bbox, base_raw_bbox):
bbox = delta_encode(raw_bbox, base_raw_bbox)
out_raw_bbox = delta_decode(bbox, raw_bbox)

np.testing.assert_almost_equal(
cuda.to_cpu(out_bbox), cuda.to_cpu(gt_bbox), decimal=5)
cuda.to_cpu(out_raw_bbox), cuda.to_cpu(base_raw_bbox), decimal=5)

def test_delta_encde_decode_consistency_cpu(self):
self.check_delta_encode_decode_consistency(
self.bbox, self.gt_bbox)
self.raw_bbox, self.base_raw_bbox)

@attr.gpu
def test_delta_encode_decode_consistency_gpu(self):
self.check_delta_encode_decode_consistency(
cuda.to_gpu(self.bbox),
cuda.to_gpu(self.gt_bbox))
cuda.to_gpu(self.raw_bbox),
cuda.to_gpu(self.base_raw_bbox))


testing.run_module(__name__, __file__)

0 comments on commit 7cb46cb

Please sign in to comment.