Skip to content

Commit

Permalink
refactor bbox_transform_inv
Browse files Browse the repository at this point in the history
  • Loading branch information
jhung0 committed Aug 24, 2017
1 parent e55e598 commit 64f76f2
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 16 deletions.
30 changes: 15 additions & 15 deletions keras_rcnn/backend/common.py
Original file line number Diff line number Diff line change
Expand Up @@ -195,23 +195,23 @@ def smooth_l1_loss(y_true, y_pred):
return keras.backend.sum(x)


def bbox_transform_inv(shifted, boxes):
def bbox_transform_inv(boxes, deltas):
def shape_zero():
x = keras.backend.int_shape(boxes)[-1]
x = keras.backend.int_shape(deltas)[-1]

return keras.backend.zeros_like(x, dtype=keras.backend.floatx())

def shape_non_zero():
a = shifted[:, 2] - shifted[:, 0] + 1.0
b = shifted[:, 3] - shifted[:, 1] + 1.0
a = boxes[:, 2] - boxes[:, 0] + 1.0
b = boxes[:, 3] - boxes[:, 1] + 1.0

ctr_x = shifted[:, 0] + 0.5 * a
ctr_y = shifted[:, 1] + 0.5 * b
ctr_x = boxes[:, 0] + 0.5 * a
ctr_y = boxes[:, 1] + 0.5 * b

dx = boxes[:, 0::4]
dy = boxes[:, 1::4]
dw = boxes[:, 2::4]
dh = boxes[:, 3::4]
dx = deltas[:, 0::4]
dy = deltas[:, 1::4]
dw = deltas[:, 2::4]
dh = deltas[:, 3::4]

pred_ctr_x = dx * a[:, keras_rcnn.backend.newaxis] + ctr_x[:, keras_rcnn.backend.newaxis]
pred_ctr_y = dy * b[:, keras_rcnn.backend.newaxis] + ctr_y[:, keras_rcnn.backend.newaxis]
Expand All @@ -220,11 +220,11 @@ def shape_non_zero():
pred_h = keras.backend.exp(dh) * b[:, keras_rcnn.backend.newaxis]


indices = keras.backend.tile(keras.backend.arange(0, keras.backend.shape(boxes)[0]), [4])
indices = keras.backend.tile(keras.backend.arange(0, keras.backend.shape(deltas)[0]), [4])
indices = keras.backend.reshape(indices, (-1, 1))
indices = keras.backend.tile(indices, [1, keras.backend.shape(boxes)[-1] // 4])
indices = keras.backend.tile(indices, [1, keras.backend.shape(deltas)[-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.tile(keras.backend.arange(0, keras.backend.shape(deltas)[1], step = 4), [keras.backend.shape(deltas)[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)

Expand All @@ -233,11 +233,11 @@ def shape_non_zero():
keras.backend.reshape(pred_ctr_y - 0.5 * pred_h, (-1,)),
keras.backend.reshape(pred_ctr_x + 0.5 * pred_w, (-1,)),
keras.backend.reshape(pred_ctr_y + 0.5 * pred_h, (-1,))], axis=0)
pred_boxes = keras_rcnn.backend.scatter_add_tensor(keras.backend.zeros_like(boxes), indices, updates)
pred_boxes = keras_rcnn.backend.scatter_add_tensor(keras.backend.zeros_like(deltas), indices, updates)
return pred_boxes


zero_boxes = keras.backend.equal(keras.backend.shape(boxes)[0], 0)
zero_boxes = keras.backend.equal(keras.backend.shape(deltas)[0], 0)

pred_boxes = keras.backend.switch(zero_boxes, shape_zero, shape_non_zero)

Expand Down
2 changes: 1 addition & 1 deletion keras_rcnn/layers/box_regression.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@

class BoxRegression(keras.engine.topology.Layer):
"""
Get final detections + labels by unscaling back to image space, performing box regression, choosing box coordinates, and removing extra detections via NMS
Get final detections + labels by unscaling back to image space, applying regression deltas, choosing box coordinates, and removing extra detections via NMS
"""
def __init__(self, threshold = 0.05, test_nms = 0.5, **kwargs):
self.threshold = threshold
Expand Down

0 comments on commit 64f76f2

Please sign in to comment.