def _compute_targets(ex_rois, gt_rois, gt_rrois, gt_quadrois, labels): """Compute bounding-box regression targets for an image.""" assert ex_rois.shape[0] == gt_rois.shape[0] assert ex_rois.shape[1] == 4 assert gt_rois.shape[1] == 4 assert gt_rrois.shape[1] == 5 assert gt_quadrois.shape[1] == 8 targets = bbox_transform(ex_rois, gt_rois) rtargets = rbox_transform(ex_rois, gt_rrois) quadtargets = quadbox_transform(ex_rois, gt_quadrois) if cfg.TRAIN.BBOX_NORMALIZE_TARGETS_PRECOMPUTED: # Optionally normalize targets by a precomputed mean and stdev targets = ((targets - np.array(cfg.TRAIN.BBOX_NORMALIZE_MEANS)) / np.array(cfg.TRAIN.BBOX_NORMALIZE_STDS)) if cfg.TRAIN.RBOX_NORMALIZE_TARGETS_PRECOMPUTED: # Optionally normalize targets by a precomputed mean and stdev rtargets = ((rtargets - np.array(cfg.TRAIN.RBOX_NORMALIZE_MEANS)) / np.array(cfg.TRAIN.RBOX_NORMALIZE_STDS)) if cfg.TRAIN.QUADBOX_NORMALIZE_TARGETS_PRECOMPUTED: # Optionally normalize targets by a precomputed mean and stdev quadtargets = ((quadtargets - np.array(cfg.TRAIN.QUADBOX_NORMALIZE_MEANS)) / np.array(cfg.TRAIN.QUADBOX_NORMALIZE_STDS)) return np.hstack( (labels[:, np.newaxis], targets)).astype(np.float32, copy=False), \ np.hstack( (labels[:, np.newaxis], rtargets)).astype(np.float32, copy=False), \ np.hstack( (labels[:, np.newaxis], quadtargets)).astype(np.float32, copy=False)
def _compute_targets(ex_rois, gt_rois): # 计算一张照片的边界框回归目标 assert ex_rois.shape[0] == gt_rois.shape[0] assert ex_rois.shape[1] == 4 assert gt_rois.shape[1] == 5 return bbox_transform(ex_rois, gt_rois[:, :4]).astype(np.float32, copy=False)
def _compute_targets(ex_rois, gt_rois): """Compute bounding-box regression targets for an image.""" assert ex_rois.shape[0] == gt_rois.shape[0] assert ex_rois.shape[1] == 4 assert gt_rois.shape[1] == 5 return bbox_transform(torch.from_numpy(ex_rois), torch.from_numpy(gt_rois[:, :4])).numpy()
def _compute_targets(ex_rois, gt_rois): """Compute bounding-box regression targets for an image.""" assert ex_rois.shape[0] == gt_rois.shape[0] assert ex_rois.shape[1] == 4 assert gt_rois.shape[1] == 5 return bbox_transform(ex_rois, gt_rois[:, :4]).astype(np.float32, copy=False)
def _compute_targets(ex_rois, gt_rois): """Compute bounding-box regression targets for an image.""" assert ex_rois.shape[0] == gt_rois.shape[0] assert ex_rois.shape[1] == 4 assert gt_rois.shape[1] == 5 return bbox_transform(ex_rois, gt_rois[:, :4]).astype(np.float32, copy=False)
def _compute_targets(ex_rois, gt_rois): """Compute bounding-box regression targets for an image.""" assert ex_rois.shape[0] == gt_rois.shape[0] assert ex_rois.shape[1] == 4 assert gt_rois.shape[1] == 5 return bbox_transform( torch.from_numpy(ex_rois), torch.from_numpy(gt_rois[:, :4])).numpy()
def _compute_targets(ex_rois,gt_rois,labels): # 计算图像的边界框回归目标 assert ex_rois.shape[0] == gt_rois.shape[0] assert ex_rois.shape[1] == 4 assert gt_rois.shape[1] == 4 targets = bbox_transform(ex_rois,gt_rois) if cfg.TRAIN.BBOX_NORMALIZE_TARGETS_PRECOMPUTED: targets = ((targets-np.array(cfg.TRAIN.BBOX_NORMALIZE_MEANS))/np.array(cfg.TRAIN.BBOX_NORMALIZE_STDS)) return np.hstack((labels[:,np.newaxis],targets)).astype(np.float32,copy=False)
def _compute_targets(ex_rois, gt_rois): """Compute bounding-box regression targets for an image.""" assert ex_rois.shape[0] == gt_rois.shape[0] assert ex_rois.shape[1] == 4 assert gt_rois.shape[1] == 5 targets = bbox_transform(ex_rois, gt_rois) return targets
def _compute_targets(ex_rois, gt_rois): """Compute bounding-box regression targets for an image.""" ## 要求anchor与对应匹配最好GT个数相同 assert ex_rois.shape[0] == gt_rois.shape[0] ## 要有anchor左上角与右下角坐标,有4个元素 assert ex_rois.shape[1] == 4 ## GT有标签位,所以为5个 assert gt_rois.shape[1] == 5 ## 返回一个用于anchor回归成target的包含每个anchor回归值(dx、dy、dw、dh)的array return bbox_transform(ex_rois, gt_rois[:, :4]).astype(np.float32, copy=False)
def _compute_targets(ex_rois, gt_rois): """Compute bounding-box regression targets for an image.""" assert ex_rois.shape[0] == gt_rois.shape[0] assert ex_rois.shape[1] == 4 assert gt_rois.shape[1] == 5 targets = bbox_transform(ex_rois, gt_rois) return targets
def _compute_targets(rois, overlaps, labels): """Compute bounding-box regression targets for an image.""" # We are sampling relations from fg rois, hence each # fg box must be assigned to an gt box assert(cfg.TRAIN.FG_THRESH >= cfg.TRAIN.BBOX_THRESH) # Indices of ground-truth ROIs gt_inds = np.where(overlaps == 1)[0] if len(gt_inds) == 0: # Bail if the image has no ground-truth ROIs return np.zeros((rois.shape[0], 5), dtype=np.float32) else: # sanity check assert(gt_inds[0] == 0) for i in range(1, len(gt_inds)): assert(gt_inds[i] - gt_inds[i-1] == 1) # Indices of examples for which we try to make predictions ex_inds = np.where(overlaps >= cfg.TRAIN.BBOX_THRESH)[0] # Get IoU overlap between each ex ROI and gt ROI ex_gt_overlaps = bbox_overlaps( np.ascontiguousarray(rois[ex_inds, :], dtype=np.float), np.ascontiguousarray(rois[gt_inds, :], dtype=np.float)) # Find which gt ROI each ex ROI has max overlap with: # this will be the ex ROI's gt target gt_assignment = ex_gt_overlaps.argmax(axis=1) # guarding against the case where a gt box doesn't get assigned to itself gt_to_ex_inds = [np.where(ex_inds == g)[0][0] for g in gt_inds] for i, g in enumerate(gt_to_ex_inds): gt_assignment[g] = gt_inds[i] # assign rois gt_rois = rois[gt_inds[gt_assignment], :] ex_rois = rois[ex_inds, :] # record target assignments for all foreground rois fg_gt_ind_assignment = {} for i, e in enumerate(ex_inds): if overlaps[e] >= cfg.TRAIN.FG_THRESH: fg_gt_ind_assignment[e] = gt_inds[gt_assignment[i]] # check if all gt has been assigned for g in gt_inds: assert(g in list(fg_gt_ind_assignment.values())) targets = np.zeros((rois.shape[0], 5), dtype=np.float32) targets[ex_inds, 0] = labels[ex_inds] # transfer to center and log targets[ex_inds, 1:] = bbox_transform(ex_rois, gt_rois) return targets, fg_gt_ind_assignment
def _compute_targets(ex_rois, gt_rois): """Compute bounding-box regression targets for an image.""" assert ex_rois.shape[0] == gt_rois.shape[0] assert ex_rois.shape[1] == 4 assert gt_rois.shape[1] == 5 if isinstance(ex_rois, np.ndarray): ex_rois = torch.from_numpy(ex_rois) if isinstance(gt_rois, np.ndarray): gt_rois = torch.from_numpy(gt_rois) return bbox_transform(ex_rois, gt_rois[:, :4])
def _compute_targets(ex_rois, gt_rois): """Compute bounding-box regression targets for an image.""" assert ex_rois.shape[0] == gt_rois.shape[0] assert ex_rois.shape[1] == 4 assert gt_rois.shape[1] == 4 targets = bbox_transform(ex_rois, gt_rois) if cfg.TRAIN.BBOX_NORMALIZE_TARGETS_PRECOMPUTED: # Optionally normalize targets by a precomputed mean and stdev targets = ((targets - np.array(cfg.TRAIN.BBOX_NORMALIZE_MEANS)) / np.array(cfg.TRAIN.BBOX_NORMALIZE_STDS)) return targets
def _compute_targets(ex_rois, gt_rois, labels): """Compute bounding-box regression targets for an image.""" # Inputs are tensor assert ex_rois.shape[0] == gt_rois.shape[0] assert ex_rois.shape[1] == 4 assert gt_rois.shape[1] == 4 targets = bbox_transform(ex_rois, gt_rois) if cfg.TRAIN.BBOX_NORMALIZE_TARGETS_PRECOMPUTED: # Optionally normalize targets by a precomputed mean and stdev targets = ((targets - targets.new(cfg.TRAIN.BBOX_NORMALIZE_MEANS)) / targets.new(cfg.TRAIN.BBOX_NORMALIZE_STDS)) return torch.cat([labels.unsqueeze(1), targets], 1)
def _compute_targets(ex_rois, gt_rois, labels): """Compute bounding-box regression targets for an image.""" # Inputs are tensor assert ex_rois.shape[0] == gt_rois.shape[0] assert ex_rois.shape[1] == 4 assert gt_rois.shape[1] == 4 targets = bbox_transform(ex_rois, gt_rois) if cfg.TRAIN.BBOX_NORMALIZE_TARGETS_PRECOMPUTED: # Optionally normalize targets by a precomputed mean and stdev targets = ((targets - targets.new(cfg.TRAIN.BBOX_NORMALIZE_MEANS)) / targets.new(cfg.TRAIN.BBOX_NORMALIZE_STDS)) return torch.cat([labels.unsqueeze(1), targets], 1)
def _compute_targets(ex_rois, gt_rois, labels): """Compute bounding-box regression targets for an image.""" assert ex_rois.shape[0] == gt_rois.shape[0] assert ex_rois.shape[1] == 4 assert gt_rois.shape[1] == 4 targets = bbox_transform(ex_rois, gt_rois) if cfg.TRAIN.BBOX_NORMALIZE_TARGETS_PRECOMPUTED: # Optionally normalize targets by a precomputed mean and stdev targets = ((targets - np.array(cfg.TRAIN.BBOX_NORMALIZE_MEANS)) / np.array(cfg.TRAIN.BBOX_NORMALIZE_STDS)) return np.hstack( (labels[:, np.newaxis], targets)).astype(np.float32, copy=False)
def _compute_targets(ex_rois, gt_rois, labels): """Compute bounding-box regression targets for an image.""" assert ex_rois.shape[0] == gt_rois.shape[ 0] #confirm that the ex_rois and the gt_rois are the same shape[0],so as to compute the results from proposals to gt boxes. assert ex_rois.shape[1] == 4 assert gt_rois.shape[1] == 4 targets = bbox_transform(ex_rois, gt_rois) if cfg.TRAIN.BBOX_NORMALIZE_TARGETS_PRECOMPUTED: # Optionally normalize targets by a precomputed mean and stdev targets = ((targets - np.array(cfg.TRAIN.BBOX_NORMALIZE_MEANS)) / np.array(cfg.TRAIN.BBOX_NORMALIZE_STDS)) return np.hstack((labels[:, np.newaxis], targets)).astype(np.float32, copy=False)
def _compute_targets(ex_rois, gt_rois, labels): """Compute bounding-box regression targets for an image.""" # Inputs are tensor assert ex_rois.shape[0] == gt_rois.shape[0] assert ex_rois.shape[1] == 4 assert gt_rois.shape[1] == 4 # 返回roi相对与其匹配的gt (dx,dy,dw,dh)四个回归值,shape(len(rois),4) targets = bbox_transform(ex_rois, gt_rois) if cfg.TRAIN.BBOX_NORMALIZE_TARGETS_PRECOMPUTED: # 是否归一化 # Optionally normalize targets by a precomputed mean and stdev targets = ((targets - targets.new(cfg.TRAIN.BBOX_NORMALIZE_MEANS)) / targets.new(cfg.TRAIN.BBOX_NORMALIZE_STDS)) # labels.unsqueeze(1) -> (128, 1) return torch.cat( [labels.unsqueeze(1), targets], 1) # (128, 5) 类别和4个回归值
def _compute_targets(ex_rois, gt_rois, labels): """Compute bounding-box regression targets for an image.""" assert ex_rois.shape[0] == gt_rois.shape[0] assert ex_rois.shape[1] == 4 assert gt_rois.shape[1] == 4 #targets:是anchor距离gt_box的 :[dx, dy,dw, dh] targets = bbox_transform(ex_rois, gt_rois) #cfg.TRAIN.BBOX_NORMALIZE_TARGETS_PRECOMPUTED=True if cfg.TRAIN.BBOX_NORMALIZE_TARGETS_PRECOMPUTED: # Optionally normalize targets by a precomputed mean and stdev ''' 下面这条语句的作用不懂??? ''' targets = ((targets - np.array(cfg.TRAIN.BBOX_NORMALIZE_MEANS)) / np.array(cfg.TRAIN.BBOX_NORMALIZE_STDS)) return np.hstack((labels[:, np.newaxis], targets)).astype(np.float32, copy=False)
def forward(self, gt_boxes, gt_ellipses): batch_size = gt_boxes.size(0) labels = gt_boxes.new(batch_size, self._inds_inside.size(0)).fill_(-1) overlaps = bbox_overlaps_batch(self._anchors, gt_boxes) max_overlaps, argmax_overlaps = torch.max(overlaps, 2) gt_max_overlaps, _ = torch.max(overlaps, 1) # assign bg labels first so that positive labels can clobber them labels[max_overlaps < self._cfg['TRAIN.RPN_NEGATIVE_OVERLAP']] = 0 # mark max overlap of 0, which are padded gt_boxes gt_max_overlaps[gt_max_overlaps == 0] = 1e-5 # mark the max overlap of each ground truth keep = torch.sum(overlaps.eq( gt_max_overlaps.view(batch_size, 1, -1).expand_as(overlaps)), 2) if torch.sum(keep) > 0: labels[keep > 0] = 1 labels[max_overlaps >= self._cfg['TRAIN.RPN_POSITIVE_OVERLAP']] = 1 # subsample positive labels if we have too many num_fg = int(self._cfg['TRAIN.RPN_FG_FRACTION'] * self._cfg['TRAIN.RPN_BATCHSIZE']) sum_fg = torch.sum((labels == 1).int(), 1) sum_bg = torch.sum((labels == 0).int(), 1) bbox_targets = gt_boxes.new( batch_size, self._inds_inside.size(0), 4).fill_(0) ellipse_targets = gt_ellipses.new( batch_size, self._inds_inside.size(0), 5).fill_(0) for i in range(batch_size): # subsample positive labels if we have too many if sum_fg[i] > num_fg: fg_inds = torch.nonzero(labels[i] == 1).view(-1) rand_num = torch.from_numpy( np.random.permutation(fg_inds.size(0)) ).type_as(gt_boxes).long() disable_inds = fg_inds[rand_num[:fg_inds.size(0)-num_fg]] labels[i][disable_inds] = -1 num_bg = self._cfg['TRAIN.RPN_BATCHSIZE'] - \ torch.sum((labels == 1).int(), 1)[i] # subsample negative labels if we have too many if sum_bg[i] > num_bg: bg_inds = torch.nonzero(labels[i] == 0).view(-1) rand_num = torch.from_numpy( np.random.permutation(bg_inds.size(0)) ).type_as(gt_boxes).long() disable_inds = bg_inds[rand_num[:bg_inds.size(0)-num_bg]] labels[i][disable_inds] = -1 bbox_targets[i] = bbox_transform( self._anchors, gt_boxes[i][argmax_overlaps[i], :4]) ellipse_targets[i] = ellipse_transform( self._anchors, gt_ellipses[i][argmax_overlaps[i], :8]) # map up to original set of anchors labels = _unmap( labels, self._total_anchors, self._inds_inside, batch_size, fill=-1) bbox_targets = _unmap( bbox_targets, self._total_anchors, self._inds_inside, batch_size, fill=0) ellipse_targets = _unmap( ellipse_targets, self._total_anchors, self._inds_inside, batch_size, fill=0) labels = labels.view( batch_size, self._feat_height, self._feat_width, self._A, 1 ).contiguous() bbox_targets = bbox_targets.view( batch_size, self._feat_height, self._feat_width, self._A, 4 ).contiguous() ellipse_targets = ellipse_targets.view( batch_size, self._feat_height, self._feat_width, self._A, 5 ).contiguous() return (labels, bbox_targets, ellipse_targets)
sub_image = image[10*j:10*j+200,10*i:10*i+200,:]#[y:y+h,x:x+w] sub_image = cv2.resize(sub_image,(32,32)) sub_image = sub_image[np.newaxis,:,:,:] area = np.array([10*i,10*j,10*i+200,10*j+200])#[x,y] area = area[np.newaxis,:] # print(area) # print(sub_image.shape) image_batch.append(sub_image) image_area.append(area) image_batch = np.concatenate(image_batch,axis=0) anchors = np.concatenate(image_area,axis=0) # print(anchors.shape) print(image_batch.shape) print(image_batch.dtype) targets = bbox_transform(anchors, label) # print('11111111111') # print(targets,targets.shape,targets.dtype) print(targets.shape) # print(targets) overlaps = bbox_overlaps( np.ascontiguousarray(anchors, dtype=np.float), np.ascontiguousarray(label_sub, dtype=np.float)) # print(overlaps) n = np.where(overlaps[:,0] > 0.7) print(n) face_label_1 = np.ones([231,1]) face_label_2 = np.zeros([231,1]) face_label_2[n,0]=1 face_label_1[n,0]=0