def _get_all_ious(bbox_coords, conv_rows, conv_cols, anchor_dims, img_width, img_height, stride): # not used anymore, might be useful to keep around as a reference num_boxes = conv_rows * conv_cols * len(anchor_dims) num_gt_boxes = len(bbox_coords) result = np.zeros((num_boxes, num_gt_boxes)) out_of_bounds_idxs = [] num_boxes = conv_rows * conv_cols * len(anchor_dims) for i in range(num_boxes): y, x, anchor_idx = _idx_to_conv(i, conv_cols, len(anchor_dims)) x_center, y_center = _get_conv_center(x, y, stride) anchor_height, anchor_width = anchor_dims[anchor_idx] anchor_box = Box.from_center_dims_int(x_center, y_center, anchor_width, anchor_height) if _out_of_bounds(anchor_box, img_width, img_height): out_of_bounds_idxs.append(i) continue for bbox_idx in range(num_gt_boxes): iou = calc_iou(bbox_coords[bbox_idx], anchor_box.corners) result[i, bbox_idx] = iou return result, out_of_bounds_idxs
def _process(self, image): # internal method, performs the expensive calculations needed to produce training inputs. conv_rows, conv_cols = self.calc_conv_dims(image.height, image.width) num_anchors = conv_rows * conv_cols * len(self.anchor_dims) bbreg_targets = np.zeros((num_anchors, 4), dtype=np.float32) can_use = np.zeros(num_anchors, dtype=np.bool) is_pos = np.zeros(num_anchors, dtype=np.bool) gt_box_coords = get_bbox_coords(image.gt_boxes) anchor_coords = _get_all_anchor_coords(conv_rows, conv_cols, self.anchor_dims, self.stride) out_of_bounds_idxs = _get_out_of_bounds_idxs(anchor_coords, image.width, image.height) all_ious = cross_ious(anchor_coords, gt_box_coords) # all_ious, out_of_bounds_idxs = get_all_ious_faster(gt_box_coords, conv_rows, conv_cols, ANCHORS_PER_LOC, image.width, image.height, self.stride) max_iou_by_anchor = np.amax(all_ious, axis=1) max_idx_by_anchor = np.argmax(all_ious, axis=1) max_iou_by_gt_box = np.amax(all_ious, axis=0) max_idx_by_gt_box = np.argmax(all_ious, axis=0) # anchors with more than 0.7 IOU with a gt box are positives pos_box_idxs = np.where(max_iou_by_anchor > POS_OVERLAP)[0] # for each gt box, the highest non-zero IOU anchor is a positive eligible_idxs = np.where(max_iou_by_gt_box > 0.0) more_pos_box_idxs = max_idx_by_gt_box[eligible_idxs] total_pos_idxs = np.unique( np.concatenate((pos_box_idxs, more_pos_box_idxs))) can_use[total_pos_idxs] = 1 is_pos[total_pos_idxs] = 1 # don't bother optimizing, profiling showed this loop's runtime is negligible for box_idx in total_pos_idxs: y, x, anchor_idx = _idx_to_conv(box_idx, conv_cols, len(self.anchor_dims)) x_center, y_center = _get_conv_center(x, y, self.stride) anchor_height, anchor_width = self.anchor_dims[anchor_idx] anchor_box = Box.from_center_dims_int(x_center, y_center, anchor_width, anchor_height) gt_box_idx = max_idx_by_anchor[box_idx] reg_params = get_reg_params(anchor_box.corners, gt_box_coords[gt_box_idx]) bbreg_targets[box_idx, :] = BBREG_MULTIPLIERS * reg_params neg_box_idxs = np.where( np.logical_and(is_pos == 0, max_iou_by_anchor < NEG_OVERLAP))[0] can_use[neg_box_idxs] = 1 can_use[out_of_bounds_idxs] = 0 self._cache[image.cache_key] = { 'can_use': can_use, 'is_pos': is_pos, 'bbreg_targets': bbreg_targets }