cost_op = rpn_bbox_loss_op + rpn_cls_loss_op rpn_cls_train_op = optimizer(rpn_cls_loss_op) rpn_bbox_train_op = optimizer(rpn_bbox_loss_op ) train_op = optimizer(rpn_bbox_loss_op ) sess=sess_start() for i in range(2,55000 * 100): src_img , src_gt_boxes =next_img_gtboxes(i) h,w=np.shape(src_img) src_im_dims = [(h,w)] anchor_scales = [3,4,5] rpn_cls_score=np.zeros([1,int(math.ceil(h/8.)),int(math.ceil(w/8.)),512]) rpn_labels, rpn_bbox_targets, rpn_bbox_inside_weights, rpn_bbox_outside_weights, bbox_targets, bbox_inside_weights, bbox_outside_weights = anchor_target( rpn_cls_score=rpn_cls_score, gt_boxes=src_gt_boxes, im_dims=src_im_dims, _feat_stride=8, anchor_scales=anchor_scales) """ rpn_labels : (1, 1, h*9 , w) rpn_bbox_targets : (1, 4*9, h, w) rpn_bbox_inside_weights : (1, 4*9, h, w) rpn_bbox_outside_weights: (1, 4*9, h, w) """ """ # delete me n, ch, h, w = np.shape(rpn_bbox_targets) rpn_bbox_targets = np.reshape(rpn_bbox_targets , [1,4, h*9 , w]) rpn_bbox_targets = np.transpose(rpn_bbox_targets , [0,2,3,1]) rpn_bbox_targets = np.reshape(rpn_bbox_targets, [-1 , 4]) for rpn_bbox in rpn_bbox_targets:
def eval(self, img, src_gt_boxes, src_im_dims, _feat_stride, anchor_scales): assert len(np.shape(img)) == 4 and np.shape(img)[0] == 1 rpn_cls_score = np.zeros([ 1, int(math.ceil(h / float(_feat_stride))), int(math.ceil(w / float(_feat_stride))), 512 ]) rpn_labels, rpn_bbox_targets, rpn_bbox_inside_weights, rpn_bbox_outside_weights, bbox_targets, bbox_inside_weights, bbox_outside_weights = anchor_target( rpn_cls_score=rpn_cls_score, gt_boxes=src_gt_boxes, im_dims=src_im_dims, _feat_stride=_feat_stride, anchor_scales=anchor_scales) indices = np.where([np.reshape(rpn_labels, [-1]) > 0])[1] feed_dict = { self.x_: src_img, self.im_dims: src_im_dims, self.gt_boxes: src_gt_boxes, self.phase_train: True, self.rpn_labels_op: rpn_labels, self.bbox_targets_op: bbox_targets, self.bbox_inside_weights_op: bbox_inside_weights, self.bbox_outside_weights_op: bbox_outside_weights, self.indice_op: indices } roi_blobs, roi_scores = self.sess.run( [self.roi_blobs_op, self.roi_scores_op], feed_dict=feed_dict) # draw pos , neg rectangles and roi_scores = np.reshape(roi_scores, [-1, 1]) keep = non_maximum_supression(dets=np.hstack([roi_blobs, roi_scores]), thresh=0.7) nms_roi_blobs = roi_blobs[keep] nms_roi_scores = roi_scores[keep] print 'NMS before {} ===> after {}'.format(len(roi_blobs), len(nms_roi_blobs)) draw_rectangles(src_img, nms_roi_blobs, nms_roi_scores, src_gt_boxes, fname='{}.png'.format('tmp'))
src_img, src_gt_boxes = next_img_gtboxes(i) h, w = np.shape(src_img)[:2] src_im_dims = [(h, w)] # rpn labels RPN Network 을 학습시킬 Label 이 있다 foreground 이면 1 , backgound = 0 , 학습 시키지 않을 box에 대해서는 -1 을 부여한다 # rpn_bbox_targets : anchor 들을 target 으로 변환했다 # src im dims : source image 의 원래 크기 # _feat stride : 얼마만큼 줄였는지 # _anchor scale : anchor scale rpn_cls_score = np.zeros([ 1, int(math.ceil(h / float(_feat_stride))), int(math.ceil(w / float(_feat_stride))), 512 ]) rpn_labels, rpn_bbox_targets, rpn_bbox_inside_weights, rpn_bbox_outside_weights, bbox_targets, bbox_inside_weights, bbox_outside_weights = \ anchor_target(rpn_cls_score, src_gt_boxes, src_im_dims, _feat_stride, anchor_scales) indices = np.where([np.reshape(rpn_labels, [-1]) > 0])[1] src_img = src_img.reshape([1] + list(np.shape(src_img)) + [1]) feed_dict = { x_: src_img, im_dims: src_im_dims, gt_boxes: src_gt_boxes, phase_train: True, rpn_labels_op: rpn_labels, bbox_targets_op: bbox_targets, bbox_inside_weights_op: bbox_inside_weights, bbox_outside_weights_op: bbox_outside_weights, indice_op: indices }
def _prepare(self, img, src_gt_boxes, src_im_dims, _feat_stride, anchor_scales): # save topconv caches assert len(np.shape(img)) == 4 and np.shape(img)[0] == 1 rpn_cls_score = np.zeros([ 1, int(math.ceil(h / float(_feat_stride))), int(math.ceil(w / float(_feat_stride))), 512 ]) rpn_labels, rpn_bbox_targets, rpn_bbox_inside_weights, rpn_bbox_outside_weights, bbox_targets, bbox_inside_weights, bbox_outside_weights = anchor_target( rpn_cls_score=rpn_cls_score, gt_boxes=src_gt_boxes, im_dims=src_im_dims, _feat_stride=_feat_stride, anchor_scales=anchor_scales) indices = np.where([np.reshape(rpn_labels, [-1]) > 0])[1] feed_dict = { self.x_: src_img, self.im_dims: src_im_dims, self.gt_boxes: src_gt_boxes, self.phase_train: True, self.rpn_labels_op: rpn_labels, self.bbox_targets_op: bbox_targets, self.bbox_inside_weights_op: bbox_inside_weights, self.bbox_outside_weights_op: bbox_outside_weights, self.indice_op: indices } return feed_dict