七扭八歪解faster rcnn(keras版)(二)

来源:互联网 发布:唯品会类似淘宝客 编辑:程序博客网 时间:2024/05/16 08:24

if img_data['bboxes'][bbox_num]['class'] != 'bg':      # all GT boxes should be mapped to an anchor box, so we keep track of which anchor box was best      if curr_iou > best_iou_for_bbox[bbox_num]:         best_anchor_for_bbox[bbox_num] = [jy, ix, anchor_ratio_idx, anchor_size_idx]         best_iou_for_bbox[bbox_num] = curr_iou         best_x_for_bbox[bbox_num,:] = [x1_anc, x2_anc, y1_anc, y2_anc]         best_dx_for_bbox[bbox_num,:] = [tx, ty, tw, th]      # we set the anchor to positive if the IOU is >0.7 (it does not matter if there was another better box, it just indicates overlap)      if curr_iou > C.rpn_max_overlap:         bbox_type = 'pos'         num_anchors_for_bbox[bbox_num] += 1         # we update the regression layer target if this IOU is the best for the current (x,y) and anchor position         if curr_iou > best_iou_for_loc:            best_iou_for_loc = curr_iou            best_regr = (tx, ty, tw, th)      # if the IOU is >0.3 and <0.7, it is ambiguous and no included in the objective      if C.rpn_min_overlap < curr_iou < C.rpn_max_overlap:         # gray zone between neg and pos         if bbox_type != 'pos':            bbox_type = 'neutral'# turn on or off outputs depending on IOUsif bbox_type == 'neg':   y_is_box_valid[jy, ix, anchor_ratio_idx + n_anchratios * anchor_size_idx] = 1   y_rpn_overlap[jy, ix, anchor_ratio_idx + n_anchratios * anchor_size_idx] = 0elif bbox_type == 'neutral':   y_is_box_valid[jy, ix, anchor_ratio_idx + n_anchratios * anchor_size_idx] = 0   y_rpn_overlap[jy, ix, anchor_ratio_idx + n_anchratios * anchor_size_idx] = 0elif bbox_type == 'pos':   y_is_box_valid[jy, ix, anchor_ratio_idx + n_anchratios * anchor_size_idx] = 1   y_rpn_overlap[jy, ix, anchor_ratio_idx + n_anchratios * anchor_size_idx] = 1   start = 4 * (anchor_ratio_idx + n_anchratios * anchor_size_idx)   y_rpn_regr[jy, ix, start:start+4] = best_regr
如果标注样本不是背景,那么更新一系列变量,bbox_num对应于标注样本,如果iou大于C.rpn_max_overlap(初始设定为0.7),则将bbox_type设定为pos,这个变量对应于每一个anchor。如果iou还大于best_iou_for_loc,这里每一点的每一个anchor(在该代码中是4*3个),都会跟该标注样本里面所有的bounding box做对比,如果大于则将best_regress设为该anchor的tx,ty,tw,th。

如果在iou值在min和max中间,那么type设为neutral中性。

y_is_box_valid,y_rpn_overlap在该点(output的宽高)标记,哪个anchor

一个点的anchor循环结束以后,根据type值在标记y_is_box_valid是否y是有效的(背景和pos的有效)和y_rpn_overlaprpn和y是否重叠。


下边做循环,保证每一个样本的bbox都至少有一个best anchor

# we ensure that every bbox has at least one positive RPN regionfor idx in range(num_anchors_for_bbox.shape[0]):   if num_anchors_for_bbox[idx] == 0:      # no box with an IOU greater than zero ...      if best_anchor_for_bbox[idx, 0] == -1:         continue


# one issue is that the RPN has many more negative than positive regions, so we turn off some of the negative# regions. We also limit it to 256 regions.num_regions = 256
看注释,因为。。所以设了regions不能超过256

if len(pos_locs[0]) > num_regions/2:   val_locs = random.sample(range(len(pos_locs[0])), len(pos_locs[0]) - num_regions/2)   y_is_box_valid[0, pos_locs[0][val_locs], pos_locs[1][val_locs], pos_locs[2][val_locs]] = 0   num_pos = num_regions/2if len(neg_locs[0]) + num_pos > num_regions:   val_locs = random.sample(range(len(neg_locs[0])), len(neg_locs[0]) - num_pos)   y_is_box_valid[0, neg_locs[0][val_locs], neg_locs[1][val_locs], neg_locs[2][val_locs]] = 0


y_rpn_cls = np.concatenate([y_is_box_valid, y_rpn_overlap], axis=1)y_rpn_regr = np.concatenate([np.repeat(y_rpn_overlap, 4, axis=1), y_rpn_regr], axis=1)return np.copy(y_rpn_cls), np.copy(y_rpn_regr)
把很多数据都合在了一起,返回。训练时候是每张图每张图训练的


参考文章链接:

https://zhuanlan.zhihu.com/p/28585873

https://zhuanlan.zhihu.com/p/24916624

阅读全文
0 0
原创粉丝点击