6179bc0290c67c45171f21bd8ba1fe4fef903235
[ealt-edge.git] / example-apps / PDD / pcb-defect-detection / libs / detection_oprations / anchor_target_layer_without_boxweight.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 and Xinlei Chen
6 # --------------------------------------------------------
7 from __future__ import absolute_import
8 from __future__ import division
9 from __future__ import print_function
10
11 import os
12 from libs.configs import cfgs
13 import numpy as np
14 import numpy.random as npr
15 from libs.box_utils.cython_utils.cython_bbox import bbox_overlaps
16 from libs.box_utils import encode_and_decode
17
18
19 def anchor_target_layer(
20         gt_boxes, img_shape, all_anchors, is_restrict_bg=False):
21     """Same as the anchor target layer in original Fast/er RCNN """
22
23     total_anchors = all_anchors.shape[0]
24     img_h, img_w = img_shape[1], img_shape[2]
25     gt_boxes = gt_boxes[:, :-1]  # remove class label
26
27
28     # allow boxes to sit over the edge by a small amount
29     _allowed_border = 0
30
31     # only keep anchors inside the image
32     if cfgs.IS_FILTER_OUTSIDE_BOXES:
33         inds_inside = np.where(
34             (all_anchors[:, 0] >= -_allowed_border) &
35             (all_anchors[:, 1] >= -_allowed_border) &
36             (all_anchors[:, 2] < img_w + _allowed_border) &  # width
37             (all_anchors[:, 3] < img_h + _allowed_border)  # height
38         )[0]
39     else:
40         inds_inside = range(all_anchors.shape[0])
41
42     anchors = all_anchors[inds_inside, :]
43
44     # label: 1 is positive, 0 is negative, -1 is dont care
45     labels = np.empty((len(inds_inside),), dtype=np.float32)
46     labels.fill(-1)
47
48     # overlaps between the anchors and the gt boxes
49     overlaps = bbox_overlaps(
50         np.ascontiguousarray(anchors, dtype=np.float),
51         np.ascontiguousarray(gt_boxes, dtype=np.float))
52
53     argmax_overlaps = overlaps.argmax(axis=1)
54     max_overlaps = overlaps[np.arange(len(inds_inside)), argmax_overlaps]
55     gt_argmax_overlaps = overlaps.argmax(axis=0)
56     gt_max_overlaps = overlaps[
57         gt_argmax_overlaps, np.arange(overlaps.shape[1])]
58     gt_argmax_overlaps = np.where(overlaps == gt_max_overlaps)[0]
59
60     if not cfgs.TRAIN_RPN_CLOOBER_POSITIVES:
61         labels[max_overlaps < cfgs.RPN_IOU_NEGATIVE_THRESHOLD] = 0
62
63     labels[gt_argmax_overlaps] = 1
64     labels[max_overlaps >= cfgs.RPN_IOU_POSITIVE_THRESHOLD] = 1
65
66     if cfgs.TRAIN_RPN_CLOOBER_POSITIVES:
67         labels[max_overlaps < cfgs.RPN_IOU_NEGATIVE_THRESHOLD] = 0
68
69     num_fg = int(cfgs.RPN_MINIBATCH_SIZE * cfgs.RPN_POSITIVE_RATE)
70     fg_inds = np.where(labels == 1)[0]
71     if len(fg_inds) > num_fg:
72         disable_inds = npr.choice(
73             fg_inds, size=(len(fg_inds) - num_fg), replace=False)
74         labels[disable_inds] = -1
75
76     num_bg = cfgs.RPN_MINIBATCH_SIZE - np.sum(labels == 1)
77     if is_restrict_bg:
78         num_bg = max(num_bg, num_fg * 1.5)
79     bg_inds = np.where(labels == 0)[0]
80     if len(bg_inds) > num_bg:
81         disable_inds = npr.choice(
82             bg_inds, size=(len(bg_inds) - num_bg), replace=False)
83         labels[disable_inds] = -1
84
85     bbox_targets = _compute_targets(anchors, gt_boxes[argmax_overlaps, :])
86
87     # map up to original set of anchors
88     labels = _unmap(labels, total_anchors, inds_inside, fill=-1)
89     bbox_targets = _unmap(bbox_targets, total_anchors, inds_inside, fill=0)
90
91     # labels = labels.reshape((1, height, width, A))
92     rpn_labels = labels.reshape((-1, 1))
93
94     # bbox_targets
95     bbox_targets = bbox_targets.reshape((-1, 4))
96     rpn_bbox_targets = bbox_targets
97
98     return rpn_labels, rpn_bbox_targets
99
100
101 def _unmap(data, count, inds, fill=0):
102     """ Unmap a subset of item (data) back to the original set of items (of
103     size count) """
104     if len(data.shape) == 1:
105         ret = np.empty((count,), dtype=np.float32)
106         ret.fill(fill)
107         ret[inds] = data
108     else:
109         ret = np.empty((count,) + data.shape[1:], dtype=np.float32)
110         ret.fill(fill)
111         ret[inds, :] = data
112     return ret
113
114
115 def _compute_targets(ex_rois, gt_rois):
116     """Compute bounding-box regression targets for an image."""
117     # targets = bbox_transform(ex_rois, gt_rois[:, :4]).astype(
118     #     np.float32, copy=False)
119     targets = encode_and_decode.encode_boxes(unencode_boxes=gt_rois,
120                                              reference_boxes=ex_rois,
121                                              scale_factors=cfgs.ANCHOR_SCALE_FACTORS)
122     # targets = encode_and_decode.encode_boxes(ex_rois=ex_rois,
123     #                                          gt_rois=gt_rois,
124     #                                          scale_factor=None)
125     return targets