def get_sample(self, idx): """ Returns sample without transformation for visualization. Sample consists of resized previous and current frame with target which is passed to the network. Bounding box values are normalized between 0 and 1 with respect to the target frame and then scaled by factor of 10. """ opts_curr = {} curr_sample = {} curr_img = self.get_orig_sample(idx, 1)['image'] currbb = self.get_orig_sample(idx, 1)['bb'] prevbb = self.get_orig_sample(idx, 0)['bb'] bbox_curr_shift = BoundingBox(prevbb[0], prevbb[1], prevbb[2], prevbb[3]) (rand_search_region, rand_search_location, edge_spacing_x, edge_spacing_y) = cropPadImage(bbox_curr_shift, curr_img) bbox_curr_gt = BoundingBox(currbb[0], currbb[1], currbb[2], currbb[3]) bbox_gt_recentered = BoundingBox(0, 0, 0, 0) bbox_gt_recentered = bbox_curr_gt.recenter(rand_search_location, edge_spacing_x, edge_spacing_y, bbox_gt_recentered) # get larger context bbox_curr_shift.kContextFactor = 4 (rand_search_region_x2, rand_search_location_x2, edge_spacing_x_x2, edge_spacing_y_x2) = cropPadImage(bbox_curr_shift, curr_img) curr_sample['image'] = rand_search_region curr_sample['image_x2'] = rand_search_region_x2 curr_sample['bb'] = bbox_gt_recentered.get_bb_list() # additional options for visualization opts_curr['edge_spacing_x'] = edge_spacing_x opts_curr['edge_spacing_y'] = edge_spacing_y opts_curr['search_location'] = rand_search_location opts_curr['search_region'] = rand_search_region # build prev sample prev_sample = self.get_orig_sample(idx, 0) prev_sample_x2 = self.get_orig_sample(idx, 0) prev_sample, opts_prev = crop_sample(prev_sample) prev_sample_x2, opts_prev_x2 = crop_sample(prev_sample_x2, 4) prev_sample['image_x2'] = prev_sample_x2['image'] # scale scale = Rescale((self.input_size, self.input_size)) scaled_curr_obj = scale(curr_sample, opts_curr) scaled_prev_obj = scale(prev_sample, opts_prev) training_sample = { 'previmg': scaled_prev_obj['image'], 'currimg': scaled_curr_obj['image'], 'previmg_x2': scaled_prev_obj['image_x2'], 'currimg_x2': scaled_curr_obj['image_x2'], 'currbb': scaled_curr_obj['bb'] } return training_sample, opts_curr
def __init__(self, model_path, device): # model_path = os.path.join(model_path, "/checkpoints/pytroch_goturn.pth.tar") if (not os.path.isfile(model_path)) or ( not os.path.exists(model_path)): fileName = input( "Whhoops! No such file! Please enter the name of the file you'd like to use." ) # Ros params self.init_variables_hard() self.init_publisher() self.init_subscribers() # GOTURN Model params self.device = device self.transform = NormalizeToTensor() self.scale = Rescale((224, 224)) self.model_path = model_path self.model = GoNet() self.opts = None self.prev_img = None self.curr_img = None checkpoint = torch.load(model_path, map_location=lambda storage, loc: storage) self.model.load_state_dict(checkpoint['state_dict']) self.model.to(device)
def make_transformed_samples(dataset, args): ''' Given a dataset, it picks a random sample from it and returns a batch of (kGeneratedExamplesPerImage+1) samples. The batch contains true sample from dataset and kGeneratedExamplesPerImage samples, which are created artifically with augmentation by GOTURN smooth motion model. ''' idx = np.random.randint(dataset.len, size=1)[0] # unscaled original sample (single image and bb) orig_sample = dataset.get_orig_sample(idx) # cropped scaled sample (two frames and bb) true_sample, _ = dataset.get_sample(idx) true_tensor = transform(true_sample) x1_batch = torch.Tensor(kGeneratedExamplesPerImage + 1, 3, input_size, input_size) x2_batch = torch.Tensor(kGeneratedExamplesPerImage + 1, 3, input_size, input_size) x1_batch_x2 = torch.Tensor(kGeneratedExamplesPerImage + 1, 3, input_size * 2, input_size * 2) x2_batch_x2 = torch.Tensor(kGeneratedExamplesPerImage + 1, 3, input_size * 2, input_size * 2) y_batch = torch.Tensor(kGeneratedExamplesPerImage + 1, 4) # initialize batch with the true sample x1_batch[0] = true_tensor['previmg'] x2_batch[0] = true_tensor['currimg'] x1_batch_x2[0] = true_tensor['previmg_x2'] x2_batch_x2[0] = true_tensor['currimg_x2'] y_batch[0] = true_tensor['currbb'] scale = Rescale((input_size, input_size)) for i in range(kGeneratedExamplesPerImage): sample = orig_sample # unscaled current image crop with box curr_sample, opts_curr = shift_crop_training_sample(sample, bb_params) # unscaled previous image crop with box prev_sample, opts_prev = crop_sample(sample) prev_sample_x2, opts_prev_x2 = crop_sample(sample, contextFactor=4) prev_sample['image_x2'] = prev_sample_x2['image'] scaled_curr_obj = scale(curr_sample, opts_curr) scaled_prev_obj = scale(prev_sample, opts_prev) training_sample = { 'previmg': scaled_prev_obj['image'], 'currimg': scaled_curr_obj['image'], 'previmg_x2': scaled_prev_obj['image_x2'], 'currimg_x2': scaled_curr_obj['image_x2'], 'currbb': scaled_curr_obj['bb'] } sample = transform(training_sample) x1_batch[i + 1] = sample['previmg'] x2_batch[i + 1] = sample['currimg'] x1_batch_x2[i + 1] = sample['previmg_x2'] x2_batch_x2[i + 1] = sample['currimg_x2'] y_batch[i + 1] = sample['currbb'] return x1_batch, x2_batch, x1_batch_x2, x2_batch_x2, y_batch
def __init__(self, root_dir, model_path, device): self.root_dir = root_dir self.device = device self.transform = NormalizeToTensor() self.scale = Rescale((224, 224)) self.model_path = model_path self.model = GoNet() self.gt = [] self.opts = None self.curr_img = None checkpoint = torch.load( model_path, map_location=lambda storage, loc: storage) self.model.load_state_dict(checkpoint['state_dict']) self.model.to(device) frames = [file for file in os.listdir(root_dir + '/img') if'jpeg' in file or 'jpg' in file] frames = [root_dir + "/img/" + frame for frame in frames] self.len = len(frames)-1 frames = np.array(frames) frames.sort() self.x = [] try: f = open(root_dir + '/groundtruth_rect.txt') except: f = open(root_dir + '/groundtruth.txt') lines = f.readlines() #print('frame length is', self.len) #print('gt length is', len(lines)) #choose min between gt and the frame for evaluation self.len = min(self.len, len(lines)-1) lines[0] = re.sub('\t', ',', lines[0]) lines[0] = re.sub(' +', ',', lines[0]) init_bbox = lines[0].strip().split(',') init_bbox = [float(x) for x in init_bbox] init_bbox = [init_bbox[0], init_bbox[1], init_bbox[0]+init_bbox[2], init_bbox[1] + init_bbox[3]] init_bbox = np.array(init_bbox) self.prev_rect = init_bbox self.img = [] for i in range(self.len): self.x.append([frames[i], frames[i+1]]) img_prev = cv2.imread(frames[i]) img_prev = bgr2rgb(img_prev) img_curr = cv2.imread(frames[i+1]) img_curr = bgr2rgb(img_curr) self.img.append([img_prev, img_curr]) lines[i+1] = re.sub('\t', ',', lines[i+1]) lines[i+1] = re.sub(' +', ',', lines[i+1]) bb = lines[i+1].strip().split(',') bb = [float(x) for x in bb] bb = [bb[0], bb[1], bb[0]+bb[2], bb[1]+bb[3]] self.gt.append(bb) self.x = np.array(self.x) print(init_bbox)
def make_transformed_samples(dataset, args): idx = np.random.randint(dataset.len, size=1)[0] # unscaled original sample (single image and bb) orig_sample = dataset.get_orig_sample(idx) # cropped scaled sample (two frames and bb) true_sample, _ = dataset.get_sample(idx) true_tensor = transform(true_sample) x1_batch = torch.Tensor(kGeneratedExamplesPerImage + 1, 3, input_size, input_size) x2_batch = torch.Tensor(kGeneratedExamplesPerImage + 1, 3, input_size, input_size) y_batch = torch.Tensor(kGeneratedExamplesPerImage + 1, 4) # initialize batch with the true sample x1_batch[0, :, :, :] = true_tensor['previmg'] x2_batch[0, :, :, :] = true_tensor['currimg'] y_batch[0, :] = true_tensor['currbb'] scale = Rescale((input_size, input_size)) for i in range(kGeneratedExamplesPerImage): sample = orig_sample # unscaled current image crop with box curr_sample, opts_curr = shift_crop_training_sample(sample, bb_params) # unscaled previous image crop with box prev_sample, opts_prev = crop_sample(sample) scaled_curr_obj = scale(curr_sample, opts_curr) scaled_prev_obj = scale(prev_sample, opts_prev) training_sample = { 'previmg': scaled_prev_obj['image'], 'currimg': scaled_curr_obj['image'], 'currbb': scaled_curr_obj['bb'] } sample = transform(training_sample) x1_batch[i + 1, :, :, :] = sample['previmg'] x2_batch[i + 1, :, :, :] = sample['currimg'] y_batch[i + 1, :] = sample['currbb'] return x1_batch, x2_batch, y_batch
def __init__(self, net_path=None, **kargs): super(TrackerGOTURN, self).__init__(name='PyTorchGOTURN', is_deterministic=True) # setup GPU device if available self.cuda = torch.cuda.is_available() self.device = torch.device('cuda:0' if self.cuda else 'cpu') # setup model self.net = GoNet() if net_path is not None: checkpoint = torch.load(net_path, map_location=lambda storage, loc: storage) self.net.load_state_dict(checkpoint['state_dict']) self.net.eval() self.net = self.net.to(self.device) # setup transforms self.prev_img = None # previous image in numpy format self.prev_box = None # follows format: [xmin, ymin, xmax, ymax] self.scale = Rescale((224, 224)) self.transform_tensor = transforms.Compose([NormalizeToTensor()]) self.opts = None
def get_sample(self, idx): """ Returns sample without transformation for visualization. Sample consists of resized previous and current frame with target which is passed to the network. Bounding box values are normalized between 0 and 1 with respect to the target frame and then scaled by factor of 10. """ sample = self.get_orig_sample(idx) # unscaled current image crop with box curr_sample, opts_curr = shift_crop_training_sample( sample, self.bb_params) # unscaled previous image crop with box prev_sample, opts_prev = crop_sample(sample) scale = Rescale((self.sz, self.sz)) scaled_curr_obj = scale(curr_sample, opts_curr) scaled_prev_obj = scale(prev_sample, opts_prev) training_sample = { 'previmg': scaled_prev_obj['image'], 'currimg': scaled_curr_obj['image'], 'currbb': scaled_curr_obj['bb'] } return training_sample, opts_curr
def __init__(self, root_dir, model_path, device, live=False): self.root_dir = root_dir self.device = device self.transform = NormalizeToTensor() self.scale = Rescale((224, 224)) self.model_path = model_path self.model = GoNet() self.gt = [] self.opts = None self.curr_img = None checkpoint = torch.load(model_path, map_location=lambda storage, loc: storage) self.model.load_state_dict(checkpoint['state_dict']) self.model.to(device) if live: # contains only img. frames = glob.glob(os.path.join(root_dir, '*.jpg')) frames = sorted(frames) self.len = len(frames) - 1 frames = np.array(frames) frames.sort() self.x = [] self.img = [] for i in range(self.len): self.x.append([frames[i], frames[i + 1]]) img_prev = cv2.imread(frames[i]) # img_prev = bgr2rgb(img_prev) img_curr = cv2.imread(frames[i + 1]) # img_curr = bgr2rgb(img_curr) self.img.append([img_prev, img_curr]) self.x = np.array(self.x) else: frames = os.listdir(root_dir + '/img') frames = [root_dir + "/img/" + frame for frame in frames] self.len = len(frames) - 1 frames = np.array(frames) frames.sort() self.x = [] f = open(root_dir + '/groundtruth_rect.txt') lines = f.readlines() lines[0] = re.sub('\t', ',', lines[0]) lines[0] = re.sub(' +', ',', lines[0]) init_bbox = lines[0].strip().split(',') init_bbox = [float(x) for x in init_bbox] init_bbox = [ init_bbox[0], init_bbox[1], init_bbox[0] + init_bbox[2], init_bbox[1] + init_bbox[3] ] init_bbox = np.array(init_bbox) self.prev_rect = init_bbox self.img = [] for i in range(self.len): self.x.append([frames[i], frames[i + 1]]) img_prev = cv2.imread(frames[i]) img_prev = bgr2rgb(img_prev) img_curr = cv2.imread(frames[i + 1]) img_curr = bgr2rgb(img_curr) self.img.append([img_prev, img_curr]) lines[i + 1] = re.sub('\t', ',', lines[i + 1]) lines[i + 1] = re.sub(' +', ',', lines[i + 1]) bb = lines[i + 1].strip().split(',') bb = [float(x) for x in bb] bb = [bb[0], bb[1], bb[0] + bb[2], bb[1] + bb[3]] self.gt.append(bb) self.x = np.array(self.x) print(init_bbox)