Skip to content

Commit

Permalink
Renames RPNAnchorTarget -> RPNTarget, removes unused tensor in _build
Browse files Browse the repository at this point in the history
  • Loading branch information
vierja committed Aug 11, 2017
1 parent bcf2744 commit e278c7f
Show file tree
Hide file tree
Showing 3 changed files with 26 additions and 34 deletions.
9 changes: 4 additions & 5 deletions luminoth/models/fasterrcnn/rpn.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@

from sonnet.python.modules.conv import Conv2D

from .rpn_anchor_target import RPNAnchorTarget
from .rpn_target import RPNTarget
from .rpn_proposal import RPNProposal
from luminoth.utils.losses import smooth_l1_loss
from luminoth.utils.vars import variable_summaries, get_initializer
Expand Down Expand Up @@ -112,7 +112,7 @@ def _build(self, pretrained_feature_map, gt_boxes, image_shape,
# We start with a common conv layer applied to the feature map.
self._instantiate_layers()
self._proposal = RPNProposal(self._num_anchors, self._config.proposals)
self._anchor_target = RPNAnchorTarget(
self._anchor_target = RPNTarget(
self._num_anchors, self._config.target, debug=self._debug
)
rpn_feature = self._rpn_activation(self._rpn(pretrained_feature_map))
Expand Down Expand Up @@ -142,8 +142,7 @@ def _build(self, pretrained_feature_map, gt_boxes, image_shape,
# values we want to output.
(rpn_cls_target, rpn_bbox_target,
rpn_max_overlap) = self._anchor_target(
tf.shape(pretrained_feature_map), gt_boxes, image_shape,
all_anchors
all_anchors, gt_boxes, image_shape
)

# TODO: Better way to log variable summaries.
Expand Down Expand Up @@ -268,7 +267,7 @@ def loss(self, prediction_dict):

# Finally, we need to calculate the regression loss over
# `rpn_bbox_target` and `rpn_bbox_pred`.
# Since `rpn_bbox_target` is obtained from RPNAnchorTarget then we
# Since `rpn_bbox_target` is obtained from RPNTarget then we
# just need to apply SmoothL1Loss.
rpn_bbox_target = tf.reshape(rpn_bbox_target, [-1, 4])
rpn_bbox_pred = tf.reshape(rpn_bbox_pred, [-1, 4])
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,10 +6,10 @@
from luminoth.utils.bbox_transform import encode, unmap


class RPNAnchorTarget(snt.AbstractModule):
"""RPNAnchorTarget: Get RPN's classification and regression targets.
class RPNTarget(snt.AbstractModule):
"""RPNTarget: Get RPN's classification and regression targets.
RPNAnchorTarget is responsable for calculating the correct values for both
RPNTarget is responsable for calculating the correct values for both
classification and regression problems. It is also responsable for defining
which anchors and target values are going to be used for the RPN minibatch.
Expand All @@ -27,7 +27,7 @@ class RPNAnchorTarget(snt.AbstractModule):
to the ground truth box) only applies to some of the anchors, the ones that
we consider to be foreground.
RPNAnchorTarget is also responsable for selecting which ones of the anchors
RPNTarget is also responsable for selecting which ones of the anchors
are going to be used for the minibatch. This is a random process with some
restrictions on the ratio between foreground and background samples.
Expand All @@ -49,7 +49,7 @@ class RPNAnchorTarget(snt.AbstractModule):
bbox_targets: bbox regresion values for each anchor
"""
def __init__(self, num_anchors, config, debug=False, name='anchor_target'):
super(RPNAnchorTarget, self).__init__(name=name)
super(RPNTarget, self).__init__(name=name)
self._num_anchors = num_anchors

self._allowed_border = config.allowed_border
Expand All @@ -73,20 +73,18 @@ def __init__(self, num_anchors, config, debug=False, name='anchor_target'):
'to be fixed at 0.'
)

def _build(self, pretrained_shape, gt_boxes, im_size, all_anchors):
def _build(self, all_anchors, gt_boxes, im_size):
"""
Args:
pretrained_shape:
Shape of the pretrained feature map, (H, W).
all_anchors:
A Tensor with all the bounding boxes coords of the anchors.
gt_boxes:
A Tensor with the groundtruth bounding boxes of the image of
the batch being processed. It's dimensions should be
(num_gt, 5). The last dimension is used for the label.
im_size:
Shape of original image (height, width) in order to define
anchor targers in respect with gt_boxes.
all_anchors:
A Tensor with all the bounding boxes coords of the anchors.
We currently use the `anchor_target_layer` based on the code provided
in the original Caffe implementation by Ross Girshick. Ideally we
Expand All @@ -110,7 +108,7 @@ def _build(self, pretrained_shape, gt_boxes, im_size, all_anchors):
labels, bbox_targets, max_overlaps
) = tf.py_func(
self._anchor_target_layer_np,
[pretrained_shape, gt_boxes, im_size, all_anchors],
[all_anchors, gt_boxes, im_size],
[tf.float32, tf.float32, tf.float32],
stateful=False,
name='anchor_target_layer_np'
Expand All @@ -119,23 +117,21 @@ def _build(self, pretrained_shape, gt_boxes, im_size, all_anchors):

return labels, bbox_targets, max_overlaps

def _anchor_target_layer(self, pretrained_shape, gt_boxes, im_size, all_anchors):
def _anchor_target_layer(self, all_anchors, gt_boxes, im_size):
"""
Function working with Tensors instead of instances for proper
computing in the Tensorflow graph.
"""
raise NotImplemented()

def _anchor_target_layer_np(self, pretrained_shape, gt_boxes, im_size, all_anchors):
def _anchor_target_layer_np(self, all_anchors, gt_boxes, im_size):

if self._debug:
np.random.seed(0)

"""
Function to be executed with tf.py_func
"""

height, width = pretrained_shape[1:3]
# We have "W x H x k" anchors
total_anchors = all_anchors.shape[0]

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,13 @@
import tensorflow as tf

from easydict import EasyDict
from luminoth.models.fasterrcnn.rpn_anchor_target import RPNAnchorTarget
from luminoth.models.fasterrcnn.rpn_target import RPNTarget


class RPNAnchorTargetTest(tf.test.TestCase):
class RPNTargetTest(tf.test.TestCase):

def setUp(self):
super(RPNAnchorTargetTest, self).setUp()
super(RPNTargetTest, self).setUp()
# Setup
self.gt_boxes = np.array([[200, 0, 400, 400]])
self.im_size = (600, 600)
Expand All @@ -20,23 +20,20 @@ def setUp(self):
'foreground_fraction': 0.5,
'minibatch_size': 2
})
self.pretrained_shape = (1, 1, 1, 1)

def _run_rpn_anchor_target(self, anchors, config):
pretrained_shape = tf.placeholder(tf.float32, shape=(4,))
def _run_rpn_target(self, anchors, config):
gt_boxes = tf.placeholder(tf.float32, shape=self.gt_boxes.shape)
im_size = tf.placeholder(tf.float32, shape=(2,))
all_anchors = tf.placeholder(tf.float32, shape=anchors.shape)

model = RPNAnchorTarget(anchors.shape[0], config)
model = RPNTarget(anchors.shape[0], config)
labels, bbox_targets, max_overlaps = model(
pretrained_shape, gt_boxes, im_size, all_anchors
all_anchors, gt_boxes, im_size
)

with self.test_session() as sess:
labels_val, bbox_targets_val, max_overlaps_val = sess.run(
[labels, bbox_targets, max_overlaps], feed_dict={
pretrained_shape: self.pretrained_shape,
gt_boxes: self.gt_boxes,
im_size: self.im_size,
all_anchors: anchors,
Expand All @@ -52,7 +49,7 @@ def testBaseCase(self):
[300, 300, 400, 400], # background
[200, 380, 300, 500], # background
])
labels, bbox_targets, max_overlaps = self._run_rpn_anchor_target(
labels, bbox_targets, max_overlaps = self._run_rpn_target(
all_anchors, self.config
)

Expand Down Expand Up @@ -95,7 +92,7 @@ def testWithNoClearMatch(self):
[200, 380, 300, 500], # background
])

labels, bbox_targets, max_overlaps = self._run_rpn_anchor_target(
labels, bbox_targets, max_overlaps = self._run_rpn_target(
all_anchors, self.config
)

Expand All @@ -108,7 +105,7 @@ def testWithNoClearMatch(self):
config = self.config
config['clobber_positives'] = True

labels, bbox_targets, max_overlaps = self._run_rpn_anchor_target(
labels, bbox_targets, max_overlaps = self._run_rpn_target(
all_anchors, config
)

Expand All @@ -133,7 +130,7 @@ def testBorderOutsiders(self):

config = self.config
config['minibatch_size'] = 5
labels, bbox_targets, max_overlaps = self._run_rpn_anchor_target(
labels, bbox_targets, max_overlaps = self._run_rpn_target(
all_anchors, config
)

Expand All @@ -147,7 +144,7 @@ def testBorderOutsiders(self):

# Test with a different foreground_fraction value.
config['foreground_fraction'] = 0.2
labels, bbox_targets, max_overlaps = self._run_rpn_anchor_target(
labels, bbox_targets, max_overlaps = self._run_rpn_target(
all_anchors, config
)

Expand Down

0 comments on commit e278c7f

Please sign in to comment.