def _convert_bbox(self, delta, point): delta = delta.permute(1, 2, 3, 0).contiguous().view(4, -1) delta = delta.detach().cpu().numpy() delta[0, :] = point[:, 0] - delta[0, :] delta[1, :] = point[:, 1] - delta[1, :] delta[2, :] = point[:, 0] + delta[2, :] delta[3, :] = point[:, 1] + delta[3, :] delta[0, :], delta[1, :], delta[2, :], delta[3, :] = corner2center(delta) return delta
def __call__(self, target, size, neg=False): # -1 ignore; 0 negative; 1 positive # 使用-1初始化cls, 之后负采样会设置0, 正采样会设置1 cls = -1 * np.ones((size, size), dtype=np.int64) delta = np.zeros((4, size, size), dtype=np.float32) tcx, tcy, tw, th = corner2center(target) points = self.points.points def select(position, keep_num=16): num = position[0].shape[0] if num <= keep_num: return position, num slt = np.arange(num) np.random.shuffle(slt) slt = slt[:keep_num] return tuple(p[slt] for p in position), keep_num if neg: neg = np.where( np.square(tcx - points[0]) / np.square(tw / 4) + np.square(tcy - points[1]) / np.square(th / 4) < 1) neg, neg_num = select(neg, cfg.TRAIN.NEG_NUM) cls[neg] = 0 return cls, delta delta[0] = points[0] - target[0] delta[1] = points[1] - target[1] delta[2] = target[2] - points[0] delta[3] = target[3] - points[1] # 椭圆标签 # ellipse label pos = np.where( np.square(tcx - points[0]) / np.square(tw / 4) + np.square(tcy - points[1]) / np.square(th / 4) < 1) neg = np.where( np.square(tcx - points[0]) / np.square(tw / 2) + np.square(tcy - points[1]) / np.square(th / 2) > 1) # sampling pos, pos_num = select(pos, cfg.TRAIN.POS_NUM) neg, neg_num = select(neg, cfg.TRAIN.TOTAL_NUM - cfg.TRAIN.POS_NUM) cls[pos] = 1 cls[neg] = 0 return cls, delta
def main(): # load config cfg.merge_from_file(args.config) cur_dir = os.path.dirname(os.path.realpath(__file__)) # create model model = ModelBuilder() # load model model = load_pretrain(model, args.snapshot).cuda().eval() # build tracker tracker = build_tracker(model) handle = vot.VOT("rectangle") region = handle.region() imagefile = handle.frame() if not imagefile: sys.exit(0) img = cv2.imread(imagefile) left = max(region.x, 0) top = max(region.y, 0) right = min(region.x + region.width, img.shape[1] - 1) bottom = min(region.y + region.height, img.shape[0] - 1) cx, cy, w, h = corner2center(Corner(left, top, right, bottom)) gt_bbox_ = [cx - (w - 1) / 2, cy - (h - 1) / 2, w, h] tracker.init(img, gt_bbox_) while True: imagefile = handle.frame() if not imagefile: break image = cv2.imread(imagefile) outputs = tracker.track(image) pred_bbox = outputs['bbox'] conf = outputs['best_score'] handle.report(vot.Rectangle(*pred_bbox), conf)
def _shift_scale_aug(self, image, bbox, crop_bbox, size): im_h, im_w = image.shape[:2] # adjust crop bounding box crop_bbox_center = corner2center(crop_bbox) if self.scale: scale_x = (1.0 + Augmentation.random() * self.scale) scale_y = (1.0 + Augmentation.random() * self.scale) h, w = crop_bbox_center.h, crop_bbox_center.w scale_x = min(scale_x, float(im_w) / w) scale_y = min(scale_y, float(im_h) / h) crop_bbox_center = Center(crop_bbox_center.x, crop_bbox_center.y, crop_bbox_center.w * scale_x, crop_bbox_center.h * scale_y) crop_bbox = center2corner(crop_bbox_center) if self.shift: sx = Augmentation.random() * self.shift sy = Augmentation.random() * self.shift x1, y1, x2, y2 = crop_bbox sx = max(-x1, min(im_w - 1 - x2, sx)) sy = max(-y1, min(im_h - 1 - y2, sy)) crop_bbox = Corner(x1 + sx, y1 + sy, x2 + sx, y2 + sy) # adjust target bounding box x1, y1 = crop_bbox.x1, crop_bbox.y1 bbox = Corner(bbox.x1 - x1, bbox.y1 - y1, bbox.x2 - x1, bbox.y2 - y1) if self.scale: bbox = Corner(bbox.x1 / scale_x, bbox.y1 / scale_y, bbox.x2 / scale_x, bbox.y2 / scale_y) image = self._crop_roi(image, crop_bbox, size) return image, bbox