EG version upgrade to 1.3
[ealt-edge.git] / example-apps / PDD / pcb-defect-detection / libs / detection_oprations / proposal_target_layer.py
1 # --------------------------------------------------------
2 # Faster R-CNN
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
11 import numpy as np
12 import numpy.random as npr
13
14 from libs.box_utils import encode_and_decode
15 from libs.box_utils.cython_utils.cython_bbox import bbox_overlaps
16
17
18 def proposal_target_layer(rpn_rois, gt_boxes):
19     """
20     Assign object detection proposals to ground-truth targets. Produces proposal
21     classification labels and bounding-box regression targets.
22     """
23     # Proposal ROIs (x1, y1, x2, y2) coming from RPN
24     # gt_boxes (x1, y1, x2, y2, label)
25
26     if cfgs.ADD_GTBOXES_TO_TRAIN:
27         all_rois = np.vstack((rpn_rois, gt_boxes[:, :-1]))
28     else:
29         all_rois = rpn_rois
30
31     rois_per_image = np.inf if cfgs.FAST_RCNN_MINIBATCH_SIZE == -1 else cfgs.FAST_RCNN_MINIBATCH_SIZE
32
33     fg_rois_per_image = np.round(cfgs.FAST_RCNN_POSITIVE_RATE * rois_per_image)
34
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)
38
39     rois = rois.reshape(-1, 4)
40     labels = labels.reshape(-1)
41     bbox_targets = bbox_targets.reshape(-1, (cfgs.CLASS_NUM+1) * 4)
42
43     return rois, labels, bbox_targets
44
45
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)
49
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).
52
53     Returns:
54         bbox_target (ndarray): N x 4K blob of regression targets
55     """
56
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]
60     for ind in inds:
61         cls = clss[ind]
62         start = int(4 * cls)
63         end = start + 4
64         bbox_targets[ind, start:end] = bbox_target_data[ind, 1:]
65
66     return bbox_targets
67
68
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]
72     """
73
74     assert ex_rois.shape[0] == gt_rois.shape[0]
75     assert ex_rois.shape[1] == 4
76     assert gt_rois.shape[1] == 4
77
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,
82     #                                          gt_rois=gt_rois,
83     #                                          scale_factor=cfgs.ROI_SCALE_FACTORS)
84
85     return np.hstack(
86         (labels[:, np.newaxis], targets)).astype(np.float32, copy=False)
87
88
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
92     examples.
93
94     all_rois shape is [-1, 4]
95     gt_boxes shape is [-1, 5]. that is [x1, y1, x2, y2, label]
96     """
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]
104
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
113     # foreground RoIs
114     fg_rois_per_this_image = min(fg_rois_per_image, fg_inds.size)
115
116     # Sample foreground regions without replacement
117     if fg_inds.size > 0:
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
124     if bg_inds.size > 0:
125         bg_inds = npr.choice(bg_inds, size=int(bg_rois_per_this_image), replace=False)
126
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)
130
131
132     # Select sampled values from various arrays:
133     labels = labels[keep_inds]
134
135     # Clamp labels for the background RoIs to 0
136     labels[int(fg_rois_per_this_image):] = 0
137     rois = all_rois[keep_inds]
138
139     bbox_target_data = _compute_targets(
140         rois, gt_boxes[gt_assignment[keep_inds], :-1], labels)
141
142     bbox_targets = \
143         _get_bbox_regression_labels(bbox_target_data, num_classes)
144
145     return labels, rois, bbox_targets