1 # --------------------------------------------------------
3 # Copyright (c) 2015 Microsoft
4 # Licensed under The MIT License [see LICENSE for details]
5 # Written by Ross Girshick
6 # --------------------------------------------------------
7 from __future__ import absolute_import
8 from __future__ import division
9 from __future__ import print_function
10 from libs.configs import cfgs
12 import numpy.random as npr
14 from libs.box_utils import encode_and_decode
15 from libs.box_utils.cython_utils.cython_bbox import bbox_overlaps
18 def proposal_target_layer(rpn_rois, gt_boxes):
20 Assign object detection proposals to ground-truth targets. Produces proposal
21 classification labels and bounding-box regression targets.
23 # Proposal ROIs (x1, y1, x2, y2) coming from RPN
24 # gt_boxes (x1, y1, x2, y2, label)
26 if cfgs.ADD_GTBOXES_TO_TRAIN:
27 all_rois = np.vstack((rpn_rois, gt_boxes[:, :-1]))
31 rois_per_image = np.inf if cfgs.FAST_RCNN_MINIBATCH_SIZE == -1 else cfgs.FAST_RCNN_MINIBATCH_SIZE
33 fg_rois_per_image = np.round(cfgs.FAST_RCNN_POSITIVE_RATE * rois_per_image)
35 # Sample rois with classification labels and bounding box regression
36 labels, rois, bbox_targets = _sample_rois(all_rois, gt_boxes, fg_rois_per_image,
37 rois_per_image, cfgs.CLASS_NUM+1)
39 rois = rois.reshape(-1, 4)
40 labels = labels.reshape(-1)
41 bbox_targets = bbox_targets.reshape(-1, (cfgs.CLASS_NUM+1) * 4)
43 return rois, labels, bbox_targets
46 def _get_bbox_regression_labels(bbox_target_data, num_classes):
47 """Bounding-box regression targets (bbox_target_data) are stored in a
48 compact form N x (class, tx, ty, tw, th)
50 This function expands those targets into the 4-of-4*K representation used
51 by the network (i.e. only one class has non-zero targets).
54 bbox_target (ndarray): N x 4K blob of regression targets
57 clss = bbox_target_data[:, 0]
58 bbox_targets = np.zeros((clss.size, 4 * num_classes), dtype=np.float32)
59 inds = np.where(clss > 0)[0]
64 bbox_targets[ind, start:end] = bbox_target_data[ind, 1:]
69 def _compute_targets(ex_rois, gt_rois, labels):
70 """Compute bounding-box regression targets for an image.
71 that is : [label, tx, ty, tw, th]
74 assert ex_rois.shape[0] == gt_rois.shape[0]
75 assert ex_rois.shape[1] == 4
76 assert gt_rois.shape[1] == 4
78 targets = encode_and_decode.encode_boxes(unencode_boxes=gt_rois,
79 reference_boxes=ex_rois,
80 scale_factors=cfgs.ROI_SCALE_FACTORS)
81 # targets = encode_and_decode.encode_boxes(ex_rois=ex_rois,
83 # scale_factor=cfgs.ROI_SCALE_FACTORS)
86 (labels[:, np.newaxis], targets)).astype(np.float32, copy=False)
89 def _sample_rois(all_rois, gt_boxes, fg_rois_per_image,
90 rois_per_image, num_classes):
91 """Generate a random sample of RoIs comprising foreground and background
94 all_rois shape is [-1, 4]
95 gt_boxes shape is [-1, 5]. that is [x1, y1, x2, y2, label]
97 # overlaps: (rois x gt_boxes)
98 overlaps = bbox_overlaps(
99 np.ascontiguousarray(all_rois, dtype=np.float),
100 np.ascontiguousarray(gt_boxes[:, :-1], dtype=np.float))
101 gt_assignment = overlaps.argmax(axis=1)
102 max_overlaps = overlaps.max(axis=1)
103 labels = gt_boxes[gt_assignment, -1]
105 # Select foreground RoIs as those with >= FG_THRESH overlap
106 fg_inds = np.where(max_overlaps >= cfgs.FAST_RCNN_IOU_POSITIVE_THRESHOLD)[0]
107 # Guard against the case when an image has fewer than fg_rois_per_image
108 # Select background RoIs as those within [BG_THRESH_LO, BG_THRESH_HI)
109 bg_inds = np.where((max_overlaps < cfgs.FAST_RCNN_IOU_POSITIVE_THRESHOLD) &
110 (max_overlaps >= cfgs.FAST_RCNN_IOU_NEGATIVE_THRESHOLD))[0]
111 # print("first fileter, fg_size: {} || bg_size: {}".format(fg_inds.shape, bg_inds.shape))
112 # Guard against the case when an image has fewer than fg_rois_per_image
114 fg_rois_per_this_image = min(fg_rois_per_image, fg_inds.size)
116 # Sample foreground regions without replacement
118 fg_inds = npr.choice(fg_inds, size=int(fg_rois_per_this_image), replace=False)
119 # Compute number of background RoIs to take from this image (guarding
120 # against there being fewer than desired)
121 bg_rois_per_this_image = rois_per_image - fg_rois_per_this_image
122 bg_rois_per_this_image = min(bg_rois_per_this_image, bg_inds.size)
123 # Sample background regions without replacement
125 bg_inds = npr.choice(bg_inds, size=int(bg_rois_per_this_image), replace=False)
127 # print("second fileter, fg_size: {} || bg_size: {}".format(fg_inds.shape, bg_inds.shape))
128 # The indices that we're selecting (both fg and bg)
129 keep_inds = np.append(fg_inds, bg_inds)
132 # Select sampled values from various arrays:
133 labels = labels[keep_inds]
135 # Clamp labels for the background RoIs to 0
136 labels[int(fg_rois_per_this_image):] = 0
137 rois = all_rois[keep_inds]
139 bbox_target_data = _compute_targets(
140 rois, gt_boxes[gt_assignment[keep_inds], :-1], labels)
143 _get_bbox_regression_labels(bbox_target_data, num_classes)
145 return labels, rois, bbox_targets