Skip to content

Commit

Permalink
Merge pull request broadinstitute#94 from jhung0/fix_clip
Browse files Browse the repository at this point in the history
Fix clip
  • Loading branch information
jhung0 authored Aug 23, 2017
2 parents 711e44d + f145a6c commit d782b80
Show file tree
Hide file tree
Showing 2 changed files with 30 additions and 12 deletions.
29 changes: 17 additions & 12 deletions keras_rcnn/backend/common.py
Original file line number Diff line number Diff line change
Expand Up @@ -51,18 +51,23 @@ def clip(boxes, shape):
Clips box coordinates to be within the width and height as defined in shape
"""
proposals = [
keras.backend.maximum(
keras.backend.minimum(boxes[:, 0::4], shape[1] - 1), 0),
keras.backend.maximum(
keras.backend.minimum(boxes[:, 1::4], shape[0] - 1), 0),
keras.backend.maximum(
keras.backend.minimum(boxes[:, 2::4], shape[1] - 1), 0),
keras.backend.maximum(
keras.backend.minimum(boxes[:, 3::4], shape[0] - 1), 0)
]

return keras.backend.concatenate(proposals, axis=1)
indices = keras.backend.tile(keras.backend.arange(0, keras.backend.shape(boxes)[0]), [4])
indices = keras.backend.reshape(indices, (-1, 1))
indices = keras.backend.tile(indices, [1, keras.backend.shape(boxes)[1] // 4])
indices = keras.backend.reshape(indices, (-1, 1))

indices_coords = keras.backend.tile(keras.backend.arange(0, keras.backend.shape(boxes)[1], step = 4), [keras.backend.shape(boxes)[0]])
indices_coords = keras.backend.concatenate([indices_coords, indices_coords + 1, indices_coords + 2, indices_coords + 3], 0)
indices = keras.backend.concatenate([indices, keras.backend.expand_dims(indices_coords)], axis=1)

updates = keras.backend.concatenate([keras.backend.maximum(keras.backend.minimum(boxes[:, 0::4], shape[1] - 1), 0),
keras.backend.maximum(keras.backend.minimum(boxes[:, 1::4], shape[0] - 1), 0),
keras.backend.maximum(keras.backend.minimum(boxes[:, 2::4], shape[1] - 1), 0),
keras.backend.maximum(keras.backend.minimum(boxes[:, 3::4], shape[0] - 1), 0)], axis=0)
updates = keras.backend.reshape(updates, (-1,))
pred_boxes = keras_rcnn.backend.scatter_add_tensor(keras.backend.zeros_like(boxes), indices, updates)

return pred_boxes


def _mkanchors(ws, hs, x_ctr, y_ctr):
Expand Down
13 changes: 13 additions & 0 deletions tests/backend/test_common.py
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,19 @@ def test_clip():
expected = numpy.array([[0,0,0,0], [1,2,3,4], [0,2,223,223], [3, 0, 223, 223]])
numpy.testing.assert_array_almost_equal(results, expected)

boxes = numpy.reshape(numpy.arange(200, 200+12*5), (-1, 12))
shape = [224, 224]
boxes = keras.backend.variable(boxes)
results = keras_rcnn.backend.clip(boxes, shape)
results = keras.backend.eval(results)
expected = numpy.array(
[[200, 201, 202, 203, 204, 205, 206, 207, 208, 209, 210, 211],
[212, 213, 214, 215, 216, 217, 218, 219, 220, 221, 222, 223],
[223, 223, 223, 223, 223, 223, 223, 223, 223, 223, 223, 223],
[223, 223, 223, 223, 223, 223, 223, 223, 223, 223, 223, 223],
[223, 223, 223, 223, 223, 223, 223, 223, 223, 223, 223, 223]])
numpy.testing.assert_array_almost_equal(results, expected, 0)


def test_bbox_transform():
gt_rois = numpy.array([[ -84., -40., 99., 55.], [-176., -88., 191., 103.], [-360., -184., 375., 199.], [ -56., -56., 71., 71.], [-120., -120., 135., 135.], [-248., -248., 263., 263.], [ -36., -80., 51., 95.], [ -80., -168., 95., 183.], [-168., -344., 183., 359.]])
Expand Down

0 comments on commit d782b80

Please sign in to comment.