def sample_rois(rois, gt_boxes, num_classes, rois_per_image, fg_rois_per_image, fg_overlap, box_stds): """ generate random sample of ROIs comprising foreground and background examples :param rois: [n, 5] (batch_index, x1, y1, x2, y2) :param gt_boxes: [n, 5] (x1, y1, x2, y2, cls) :param num_classes: number of classes :param rois_per_image: total roi number :param fg_rois_per_image: foreground roi number :param fg_overlap: overlap threshold for fg rois :param box_stds: std var of bbox reg :return: (rois, labels, bbox_targets, bbox_weights) """ overlaps = bbox_overlaps(rois[:, 1:], gt_boxes[:, :4]) gt_assignment = overlaps.argmax(axis=1) labels = gt_boxes[gt_assignment, 4] max_overlaps = overlaps.max(axis=1) # select foreground RoI with FG_THRESH overlap fg_indexes = np.where(max_overlaps >= fg_overlap)[0] # guard against the case when an image has fewer than fg_rois_per_image foreground RoIs fg_rois_this_image = min(fg_rois_per_image, len(fg_indexes)) # sample foreground regions without replacement if len(fg_indexes) > fg_rois_this_image: fg_indexes = np.random.choice(fg_indexes, size=fg_rois_this_image, replace=False) # select background RoIs as those within [0, FG_THRESH) bg_indexes = np.where(max_overlaps < fg_overlap)[0] # compute number of background RoIs to take from this image (guarding against there being fewer than desired) bg_rois_this_image = rois_per_image - fg_rois_this_image bg_rois_this_image = min(bg_rois_this_image, len(bg_indexes)) # sample bg rois without replacement if len(bg_indexes) > bg_rois_this_image: bg_indexes = np.random.choice(bg_indexes, size=bg_rois_this_image, replace=False) # indexes selected keep_indexes = np.append(fg_indexes, bg_indexes) # pad more bg rois to ensure a fixed minibatch size while len(keep_indexes) < rois_per_image: gap = min(len(bg_indexes), rois_per_image - len(keep_indexes)) gap_indexes = np.random.choice(range(len(bg_indexes)), size=gap, replace=False) keep_indexes = np.append(keep_indexes, bg_indexes[gap_indexes]) # sample rois and labels rois = rois[keep_indexes] labels = labels[keep_indexes] # set labels of bg rois to be 0 labels[fg_rois_this_image:] = 0 # load or compute bbox_target targets = bbox_transform(rois[:, 1:], gt_boxes[gt_assignment[keep_indexes], :4], box_stds=box_stds) bbox_targets = np.zeros((rois_per_image, 4 * num_classes), dtype=np.float32) bbox_weights = np.zeros((rois_per_image, 4 * num_classes), dtype=np.float32) for i in range(fg_rois_this_image): cls_ind = int(labels[i]) bbox_targets[i, cls_ind * 4:(cls_ind + 1) * 4] = targets[i] bbox_weights[i, cls_ind * 4:(cls_ind + 1) * 4] = 1 return rois, labels, bbox_targets, bbox_weights
def sample_rois(rois, gt_boxes, num_classes, rois_per_image, fg_rois_per_image, fg_overlap, box_stds): """ generate random sample of ROIs comprising foreground and background examples :param rois: [n, 5] (batch_index, x1, y1, x2, y2) :param gt_boxes: [n, 5] (x1, y1, x2, y2, cls) :param num_classes: number of classes :param rois_per_image: total roi number :param fg_rois_per_image: foreground roi number :param fg_overlap: overlap threshold for fg rois :param box_stds: std var of bbox reg :return: (labels, rois, bbox_targets, bbox_weights) """ overlaps = bbox_overlaps(rois[:, 1:], gt_boxes[:, :4]) gt_assignment = overlaps.argmax(axis=1) labels = gt_boxes[gt_assignment, 4] max_overlaps = overlaps.max(axis=1) # select foreground RoI with FG_THRESH overlap fg_indexes = np.where(max_overlaps >= fg_overlap)[0] # guard against the case when an image has fewer than fg_rois_per_image foreground RoIs fg_rois_this_image = min(fg_rois_per_image, len(fg_indexes)) # sample foreground regions without replacement if len(fg_indexes) > fg_rois_this_image: fg_indexes = np.random.choice(fg_indexes, size=fg_rois_this_image, replace=False) # select background RoIs as those within [0, FG_THRESH) bg_indexes = np.where(max_overlaps < fg_overlap)[0] # compute number of background RoIs to take from this image (guarding against there being fewer than desired) bg_rois_this_image = rois_per_image - fg_rois_this_image bg_rois_this_image = min(bg_rois_this_image, len(bg_indexes)) # sample bg rois without replacement if len(bg_indexes) > bg_rois_this_image: bg_indexes = np.random.choice(bg_indexes, size=bg_rois_this_image, replace=False) # indexes selected keep_indexes = np.append(fg_indexes, bg_indexes) # pad more bg rois to ensure a fixed minibatch size while len(keep_indexes) < rois_per_image: gap = min(len(bg_indexes), rois_per_image - len(keep_indexes)) gap_indexes = np.random.choice(range(len(bg_indexes)), size=gap, replace=False) keep_indexes = np.append(keep_indexes, bg_indexes[gap_indexes]) # sample rois and labels rois = rois[keep_indexes] labels = labels[keep_indexes] # set labels of bg rois to be 0 labels[fg_rois_this_image:] = 0 # load or compute bbox_target targets = bbox_transform(rois[:, 1:], gt_boxes[gt_assignment[keep_indexes], :4], box_stds=box_stds) bbox_targets = np.zeros((rois_per_image, 4 * num_classes), dtype=np.float32) bbox_weights = np.zeros((rois_per_image, 4 * num_classes), dtype=np.float32) for i in range(fg_rois_this_image): cls_ind = int(labels[i]) bbox_targets[i, cls_ind * 4:(cls_ind + 1) * 4] = targets[i] bbox_weights[i, cls_ind * 4:(cls_ind + 1) * 4] = 1 return rois, labels, bbox_targets, bbox_weights
def assign(self, anchors, gt_boxes, im_height, im_width): # print(anchors) num_anchors = anchors.shape[0] # filter out padded gt_boxes valid_labels = np.where(gt_boxes[:, -1] > 0)[0] gt_boxes = gt_boxes[valid_labels] # filter out anchors outside the region #BIYUJI # print(anchors[0]) # print('anchor shape are:\n', anchors.shape, '\n') # print('im info\n', self._allowed_border, im_height, im_width) inds_inside = np.where( (anchors[:, 0] >= -self._allowed_border) & (anchors[:, 2] < im_width + self._allowed_border) & (anchors[:, 1] >= -self._allowed_border) & (anchors[:, 3] < im_height + self._allowed_border))[0] anchors = anchors[inds_inside, :] num_valid = len(inds_inside) # raise Exception('?') # label: 1 is positive, 0 is negative, -1 is dont care labels = np.ones((num_valid, ), dtype=np.float32) * -1 bbox_targets = np.zeros((num_valid, 4), dtype=np.float32) bbox_weights = np.zeros((num_valid, 4), dtype=np.float32) # sample for positive labels if num_valid: if gt_boxes.size > 0: # overlap between the anchors and the gt boxes # overlaps (ex, gt) overlaps = bbox_overlaps(anchors.astype(np.float), gt_boxes.astype(np.float)) # fg anchors: anchor with highest overlap for each gt gt_max_overlaps = overlaps.max(axis=0) argmax_inds = np.where(overlaps == gt_max_overlaps)[0] labels[argmax_inds] = 1 # fg anchors: anchor with overlap > iou thresh max_overlaps = overlaps.max(axis=1) labels[max_overlaps >= self._fg_overlap] = 1 # bg anchors: anchor with overlap < iou thresh labels[max_overlaps < self._bg_overlap] = 0 # sanity check fg_inds = np.where(labels == 1)[0] bg_inds = np.where(labels == 0)[0] assert len(np.intersect1d(fg_inds, bg_inds)) == 0 # subsample positive anchors cur_fg = len(fg_inds) if cur_fg > self._num_fg: disable_inds = np.random.choice(fg_inds, size=(cur_fg - self._num_fg), replace=False) labels[disable_inds] = -1 # subsample negative anchors cur_bg = len(bg_inds) max_neg = self._num_batch - min(self._num_fg, cur_fg) if cur_bg > max_neg: disable_inds = np.random.choice(bg_inds, size=(cur_bg - max_neg), replace=False) labels[disable_inds] = -1 # assign to argmax overlap fg_inds = np.where(labels == 1)[0] argmax_overlaps = overlaps.argmax(axis=1) bbox_targets[fg_inds, :] = bbox_transform( anchors[fg_inds, :], gt_boxes[argmax_overlaps[fg_inds], :], box_stds=(1.0, 1.0, 1.0, 1.0)) # only fg anchors has bbox_targets bbox_weights[fg_inds, :] = 1 else: # randomly draw bg anchors bg_inds = np.random.choice(np.arange(num_valid), size=self._num_batch, replace=False) labels[bg_inds] = 0 all_labels = np.ones((num_anchors, ), dtype=np.float32) * -1 all_labels[inds_inside] = labels all_bbox_targets = np.zeros((num_anchors, 4), dtype=np.float32) all_bbox_targets[inds_inside, :] = bbox_targets all_bbox_weights = np.zeros((num_anchors, 4), dtype=np.float32) all_bbox_weights[inds_inside, :] = bbox_weights return all_labels, all_bbox_targets, all_bbox_weights
def assign(self, anchors, gt_boxes, im_height, im_width): num_anchors = anchors.shape[0] # filter out padded gt_boxes valid_labels = np.where(gt_boxes[:, -1] > 0)[0] gt_boxes = gt_boxes[valid_labels] # filter out anchors outside the region inds_inside = np.where((anchors[:, 0] >= -self._allowed_border) & (anchors[:, 2] < im_width + self._allowed_border) & (anchors[:, 1] >= -self._allowed_border) & (anchors[:, 3] < im_height + self._allowed_border))[0] anchors = anchors[inds_inside, :] num_valid = len(inds_inside) # label: 1 is positive, 0 is negative, -1 is dont care labels = np.ones((num_valid,), dtype=np.float32) * -1 bbox_targets = np.zeros((num_valid, 4), dtype=np.float32) bbox_weights = np.zeros((num_valid, 4), dtype=np.float32) # sample for positive labels if gt_boxes.size > 0: # overlap between the anchors and the gt boxes # overlaps (ex, gt) overlaps = bbox_overlaps(anchors.astype(np.float), gt_boxes.astype(np.float)) # fg anchors: anchor with highest overlap for each gt gt_max_overlaps = overlaps.max(axis=0) argmax_inds = np.where(overlaps == gt_max_overlaps)[0] labels[argmax_inds] = 1 # fg anchors: anchor with overlap > iou thresh max_overlaps = overlaps.max(axis=1) labels[max_overlaps >= self._fg_overlap] = 1 # bg anchors: anchor with overlap < iou thresh labels[max_overlaps < self._bg_overlap] = 0 # sanity check fg_inds = np.where(labels == 1)[0] bg_inds = np.where(labels == 0)[0] assert len(np.intersect1d(fg_inds, bg_inds)) == 0 # subsample positive anchors cur_fg = len(fg_inds) if cur_fg > self._num_fg: disable_inds = np.random.choice(fg_inds, size=(cur_fg - self._num_fg), replace=False) labels[disable_inds] = -1 # subsample negative anchors cur_bg = len(bg_inds) max_neg = self._num_batch - min(self._num_fg, cur_fg) if cur_bg > max_neg: disable_inds = np.random.choice(bg_inds, size=(cur_bg - max_neg), replace=False) labels[disable_inds] = -1 # assign to argmax overlap fg_inds = np.where(labels == 1)[0] argmax_overlaps = overlaps.argmax(axis=1) bbox_targets[fg_inds, :] = bbox_transform(anchors[fg_inds, :], gt_boxes[argmax_overlaps[fg_inds], :], box_stds=(1.0, 1.0, 1.0, 1.0)) # only fg anchors has bbox_targets bbox_weights[fg_inds, :] = 1 else: # randomly draw bg anchors bg_inds = np.random.choice(np.arange(num_valid), size=self._num_batch, replace=False) labels[bg_inds] = 0 all_labels = np.ones((num_anchors,), dtype=np.float32) * -1 all_labels[inds_inside] = labels all_bbox_targets = np.zeros((num_anchors, 4), dtype=np.float32) all_bbox_targets[inds_inside, :] = bbox_targets all_bbox_weights = np.zeros((num_anchors, 4), dtype=np.float32) all_bbox_weights[inds_inside, :] = bbox_weights return all_labels, all_bbox_targets, all_bbox_weights
def assign(self, anchors, gt_boxes, im_height, im_width): num_anchors = anchors.shape[0] # filter out padded gt_boxes valid_labels = np.where(gt_boxes[:, -1] > 0)[0] gt_boxes = gt_boxes[valid_labels] # filter out anchors outside the region inds_inside = np.where( (anchors[:, 0] >= -self._allowed_border) & (anchors[:, 2] < im_width + self._allowed_border) & (anchors[:, 1] >= -self._allowed_border) & (anchors[:, 3] < im_height + self._allowed_border))[0] anchors = anchors[inds_inside, :] num_valid = len(inds_inside) # label: 1 is positive, 0 is negative, -1 is dont care labels = np.ones((num_valid, ), dtype=np.float32) * -1 bbox_targets = np.zeros((num_valid, 4), dtype=np.float32) bbox_weights = np.zeros((num_valid, 4), dtype=np.float32) # sample for positive labels if gt_boxes.size > 0: # overlap between the anchors and the gt boxes # overlaps (ex, gt) overlaps = bbox_overlaps(anchors.astype(np.float), gt_boxes.astype(np.float)) gt_max_overlaps = overlaps.max(axis=0) # fg anchors: anchor with highest overlap for each gt; or overlap > iou thresh fg_inds = np.where((overlaps >= self._fg_overlap) | (overlaps == gt_max_overlaps))[0] # subsample to num_fg if len(fg_inds) > self._num_fg: fg_inds = np.random.choice(fg_inds, size=self._num_fg, replace=False) # bg anchor: anchor with overlap < iou thresh but not highest overlap for some gt bg_inds = np.where((overlaps < self._bg_overlap) & (overlaps < gt_max_overlaps))[0] if len(bg_inds) > self._num_batch - len(fg_inds): bg_inds = np.random.choice(bg_inds, size=self._num_batch - len(fg_inds), replace=False) # assign label labels[fg_inds] = 1 labels[bg_inds] = 0 # assign to argmax overlap argmax_overlaps = overlaps.argmax(axis=1) bbox_targets[fg_inds, :] = bbox_transform( anchors[fg_inds, :], gt_boxes[argmax_overlaps[fg_inds], :], box_stds=(1.0, 1.0, 1.0, 1.0)) # only fg anchors has bbox_targets bbox_weights[fg_inds, :] = 1 else: # randomly draw bg anchors bg_inds = np.random.choice(np.arange(num_valid), size=self._num_batch, replace=False) labels[bg_inds] = 0 all_labels = np.ones((num_anchors, ), dtype=np.float32) * -1 all_labels[inds_inside] = labels all_bbox_targets = np.zeros((num_anchors, 4), dtype=np.float32) all_bbox_targets[inds_inside, :] = bbox_targets all_bbox_weights = np.zeros((num_anchors, 4), dtype=np.float32) all_bbox_weights[inds_inside, :] = bbox_weights return all_labels, all_bbox_targets, all_bbox_weights
def assign(self, anchors, gt_boxes, im_height, im_width): num_anchors = anchors.shape[0] #print("gt_boxes=",gt_boxes) # filter out padded gt_boxes valid_labels = np.where(gt_boxes[:, -1] > 0)[0] #判断gt_boxes是否是目标 #print("valid_labels=",valid_labels) gt_boxes = gt_boxes[valid_labels] #得到目标gt_boxes的索引 #print("gt_boxes2=", gt_boxes) # filter out anchors outside the region #过滤超出边界的anchor inds_inside = np.where( (anchors[:, 0] >= -self._allowed_border) & (anchors[:, 2] < im_width + self._allowed_border) & (anchors[:, 1] >= -self._allowed_border) & (anchors[:, 3] < im_height + self._allowed_border))[0] anchors = anchors[inds_inside, :] num_valid = len(inds_inside) #print('num_valid=',num_valid) # label: 1 is positive, 0 is negative, -1 is dont care labels = np.ones((num_valid, ), dtype=np.float32) * -1 bbox_targets = np.zeros((num_valid, 4), dtype=np.float32) bbox_weights = np.zeros((num_valid, 4), dtype=np.float32) # sample for positive labels if gt_boxes.size > 0: # overlap between the anchors and the gt boxes # overlaps (ex, gt) #print('anchor=',anchors) #print('anchor_gt_boxes=', gt_boxes)####问题就在这,不带标签 gt_boxes_rec = np.zeros((gt_boxes.shape[0], 4), dtype=np.float32) gt_boxes_rec[:, 0] = np.min(gt_boxes[:, 0:8:2]) # x_min gt_boxes_rec[:, 1] = np.min(gt_boxes[:, 1:8:2]) # y_min gt_boxes_rec[:, 2] = np.max(gt_boxes[:, 0:8:2]) # x_max gt_boxes_rec[:, 3] = np.max(gt_boxes[:, 1:8:2]) # y_max overlaps = bbox_overlaps(anchors.astype(np.float), gt_boxes_rec.astype(np.float)) # fg anchors: anchor with highest overlap for each gt gt_max_overlaps = overlaps.max(axis=0) #print("gt_max_overlaps=",gt_max_overlaps) argmax_inds = np.where(overlaps == gt_max_overlaps)[0] labels[argmax_inds] = 1 ################################问题就在这里########################################################### # fg anchors: anchor with overlap > iou thresh max_overlaps = overlaps.max(axis=1) #print('max_overlaps=',max_overlaps) # bg anchors: anchor with overlap < iou thresh labels[max_overlaps < self._bg_overlap] = 0 #print("labels_0=", labels.shape) labels[max_overlaps >= self._fg_overlap] = 1 #print("labels_1=",labels.shape) # sanity check fg_inds = np.where(labels == 1)[0] #print(" fg_inds=", fg_inds.shape) bg_inds = np.where(labels == 0)[0] #print("bg_inds=",bg_inds.shape) assert len(np.intersect1d(fg_inds, bg_inds)) == 0 # subsample positive anchors cur_fg = len(fg_inds) if cur_fg > self._num_fg: disable_inds = np.random.choice(fg_inds, size=(cur_fg - self._num_fg), replace=False) labels[disable_inds] = -1 # subsample negative anchors cur_bg = len(bg_inds) max_neg = self._num_batch - min(self._num_fg, cur_fg) #print("max_neg=",max_neg) if cur_bg > max_neg: disable_inds = np.random.choice(bg_inds, size=(cur_bg - max_neg), replace=False) labels[disable_inds] = -1 # assign to argmax overlap fg_inds = np.where(labels == 1)[0] #print(" fg_inds=",fg_inds) argmax_overlaps = overlaps.argmax(axis=1) #print("anchors[fg_inds, :]=",anchors[fg_inds, :]) #print("gt_boxes[argmax_overlaps[fg_inds], :]=",gt_boxes[argmax_overlaps[fg_inds], :]) bbox_targets[fg_inds, :] = bbox_transform( anchors[fg_inds, :], gt_boxes_rec[argmax_overlaps[fg_inds], :], box_stds=(1.0, 1.0, 1.0, 1.0)) # only fg anchors has bbox_targets bbox_weights[fg_inds, :] = 1 ###前面和tensorflow都一样,但是缺少_unmap函数 else: # randomly draw bg anchors bg_inds = np.random.choice(np.arange(num_valid), size=self._num_batch, replace=False) labels[bg_inds] = 0 all_labels = np.ones((num_anchors, ), dtype=np.float32) * -1 all_labels[inds_inside] = labels all_bbox_targets = np.zeros((num_anchors, 4), dtype=np.float32) all_bbox_targets[inds_inside, :] = bbox_targets all_bbox_weights = np.zeros((num_anchors, 4), dtype=np.float32) all_bbox_weights[inds_inside, :] = bbox_weights return all_labels, all_bbox_targets, all_bbox_weights
def assign(self, anchors, gt_boxes, im_height, im_width): num_anchors = anchors.shape[0] # filter out padded gt_boxes valid_labels = np.where(gt_boxes[:, -1] > 0)[0] gt_boxes = gt_boxes[valid_labels] # filter out anchors outside the region inds_inside = np.where((anchors[:, 0] >= -self._allowed_border) & (anchors[:, 2] < im_width + self._allowed_border) & (anchors[:, 1] >= -self._allowed_border) & (anchors[:, 3] < im_height + self._allowed_border))[0] anchors = anchors[inds_inside, :] num_valid = len(inds_inside) # label: 1 is positive, 0 is negative, -1 is dont care labels = np.ones((num_valid,), dtype=np.float32) * -1 bbox_targets = np.zeros((num_valid, 4), dtype=np.float32) bbox_weights = np.zeros((num_valid, 4), dtype=np.float32) # sample for positive labels if gt_boxes.size > 0: # overlap between the anchors and the gt boxes # overlaps (ex, gt) overlaps = bbox_overlaps(anchors.astype(np.float), gt_boxes.astype(np.float)) gt_max_overlaps = overlaps.max(axis=0) # fg anchors: anchor with highest overlap for each gt; or overlap > iou thresh fg_inds = np.where((overlaps >= self._fg_overlap) | (overlaps == gt_max_overlaps))[0] # subsample to num_fg if len(fg_inds) > self._num_fg: fg_inds = np.random.choice(fg_inds, size=self._num_fg, replace=False) # bg anchor: anchor with overlap < iou thresh but not highest overlap for some gt bg_inds = np.where((overlaps < self._bg_overlap) & (overlaps < gt_max_overlaps))[0] if len(bg_inds) > self._num_batch - len(fg_inds): bg_inds = np.random.choice(bg_inds, size=self._num_batch - len(fg_inds), replace=False) # assign label labels[fg_inds] = 1 labels[bg_inds] = 0 # assign to argmax overlap argmax_overlaps = overlaps.argmax(axis=1) bbox_targets[fg_inds, :] = bbox_transform(anchors[fg_inds, :], gt_boxes[argmax_overlaps[fg_inds], :], box_stds=(1.0, 1.0, 1.0, 1.0)) # only fg anchors has bbox_targets bbox_weights[fg_inds, :] = 1 else: # randomly draw bg anchors bg_inds = np.random.choice(np.arange(num_valid), size=self._num_batch, replace=False) labels[bg_inds] = 0 all_labels = np.ones((num_anchors,), dtype=np.float32) * -1 all_labels[inds_inside] = labels all_bbox_targets = np.zeros((num_anchors, 4), dtype=np.float32) all_bbox_targets[inds_inside, :] = bbox_targets all_bbox_weights = np.zeros((num_anchors, 4), dtype=np.float32) all_bbox_weights[inds_inside, :] = bbox_weights return all_labels, all_bbox_targets, all_bbox_weights