Beispiel #1
0
def main():
    # load net
    net_file = join(realpath(dirname(__file__)), 'SiamRPNBIG.model')
    net = SiamRPNBIG()
    net.load_state_dict(torch.load(net_file))
    net.eval().cuda()

    # warm up
    for i in range(10):
        net.temple(
            torch.autograd.Variable(torch.FloatTensor(1, 3, 127, 127)).cuda())
        net(torch.autograd.Variable(torch.FloatTensor(1, 3, 255, 255)).cuda())

    # start to track
    handle = vot.VOT("polygon")
    Polygon = handle.region()
    cx, cy, w, h = get_axis_aligned_bbox(Polygon)

    image_file = handle.frame()
    if not image_file:
        sys.exit(0)

    target_pos, target_sz = np.array([cx, cy]), np.array([w, h])
    im = cv2.imread(image_file)  # HxWxC
    state = SiamRPN_init(im, target_pos, target_sz, net)  # init tracker
    while True:
        image_file = handle.frame()
        if not image_file:
            break
        im = cv2.imread(image_file)  # HxWxC
        state = SiamRPN_track(state, im)  # track
        res = cxy_wh_2_rect(state['target_pos'], state['target_sz'])

        handle.report(Rectangle(res[0], res[1], res[2], res[3]))
Beispiel #2
0
 def track_vot2(self, imgtype):
     """Run tracker on a sequence."""
     
     handle = vot.VOT("rectangle", "rgbt")
     rect = list(handle.region())
     colorimage, thermalimage = handle.frame()
     startnum = 20        
     image_rgb = self._read_image(colorimage[startnum:len(colorimage)-2])
     image_ir = self._read_image(thermalimage[startnum:len(thermalimage)-2])
     if imgtype == 'rgb':
         self.initialize(image_ir, image_rgb, rect)
     elif imgtype == 'ir':
         self.initialize(image_rgb, image_ir, rect)
     while True:
         colorimage, thermalimage = handle.frame()
         if not colorimage:
             break
         image_rgb = self._read_image(colorimage[startnum:len(colorimage)-2])
         image_ir = self._read_image(thermalimage[startnum:len(thermalimage)-2])
         if imgtype == 'rgb':
             state = self.track(image_ir, image_rgb)
         elif imgtype == 'ir':
             state = self.track(image_rgb, image_ir)
         region = np.array(state).astype(int)
         handle.report(Rectangle(region[0], region[1], region[2], region[3]))
Beispiel #3
0
def run_vot_exp(tracker_name, para_name, vis=False):

    torch.set_num_threads(1)
    save_root = os.path.join(
        '/data/sda/v-yanbi/iccv21/LittleBoy/vot20_lt_debug', para_name)
    if vis and (not os.path.exists(save_root)):
        os.makedirs(save_root)
    tracker = stark_vot20_lt(tracker_name=tracker_name, para_name=para_name)
    handle = vot.VOT("rectangle")
    selection = handle.region()
    imagefile = handle.frame()
    init_box = [selection.x, selection.y, selection.width, selection.height]
    if not imagefile:
        sys.exit(0)
    if vis:
        '''for vis'''
        seq_name = imagefile.split('/')[-3]
        save_v_dir = os.path.join(save_root, seq_name)
        if not os.path.exists(save_v_dir):
            os.mkdir(save_v_dir)
        cur_time = int(time.time() % 10000)
        save_dir = os.path.join(save_v_dir, str(cur_time))
        if not os.path.exists(save_dir):
            os.makedirs(save_dir)

    image = cv2.cvtColor(cv2.imread(imagefile), cv2.COLOR_BGR2RGB)  # Right
    tracker.initialize(image, init_box)

    while True:
        imagefile = handle.frame()
        if not imagefile:
            break
        image = cv2.cvtColor(cv2.imread(imagefile), cv2.COLOR_BGR2RGB)  # Right
        b1, conf = tracker.track(image)
        x1, y1, w, h = b1
        handle.report(vot.Rectangle(x1, y1, w, h), conf)
        if vis:
            '''Visualization'''
            # original image
            image_ori = image[:, :, ::-1].copy()  # RGB --> BGR
            image_name = imagefile.split('/')[-1]
            save_path = os.path.join(save_dir, image_name)
            cv2.imwrite(save_path, image_ori)
            # tracker box
            image_b = image_ori.copy()
            cv2.rectangle(image_b, (int(b1[0]), int(b1[1])),
                          (int(b1[0] + b1[2]), int(b1[1] + b1[3])),
                          (0, 0, 255), 2)
            image_b_name = image_name.replace('.jpg', '_bbox.jpg')
            save_path = os.path.join(save_dir, image_b_name)
            cv2.imwrite(save_path, image_b)
Beispiel #4
0
def run_vot_exp(tracker_name,
                para_name,
                refine_model_name,
                threshold,
                VIS=False):

    torch.set_num_threads(1)
    save_root = os.path.join(
        '/home/alphabin/Desktop/AlphaRefine_submit/vot20_debug', para_name)
    if VIS and (not os.path.exists(save_root)):
        os.mkdir(save_root)

    tracker = build_tracker(tracker_name, para_name, refine_model_name,
                            threshold)

    handle = vot.VOT("mask")
    selection = handle.region()
    imagefile = handle.frame()
    # if not imagefile:
    #     sys.exit(0)

    # if VIS:
    #     '''for vis'''
    #     seq_name = imagefile.split('/')[-3]
    #     save_v_dir = os.path.join(save_root,seq_name)
    #     if not os.path.exists(save_v_dir):
    #         os.mkdir(save_v_dir)
    #     cur_time = int(time.time() % 10000)
    #     save_dir = os.path.join(save_v_dir, str(cur_time))
    #     if not os.path.exists(save_dir):
    #         os.makedirs(save_dir)

    image = cv2.cvtColor(cv2.imread(imagefile), cv2.COLOR_BGR2RGB)  # Right
    # mask given by the toolkit ends with the target (zero-padding to the right and down is needed)
    mask = make_full_size(selection, (image.shape[1], image.shape[0]))
    tracker.initialize(image, mask)

    while True:
        imagefile = handle.frame()
        if not imagefile:
            break
        image = cv2.cvtColor(cv2.imread(imagefile), cv2.COLOR_BGR2RGB)  # Right
        b1, m, search, search_m = tracker.track(image)
        handle.report(m)
def run_vot_exp(tracker_name,
                para_name,
                refine_model_name,
                threshold,
                VIS=False):
    torch.set_num_threads(1)
    # torch.cuda.set_device(CUDA_ID)  # set GPU id
    save_root = os.path.join('<SAVE_DIR>', para_name)
    if VIS and (not os.path.exists(save_root)):
        os.makedirs(save_root)
    tracker = STARK_ALPHA_SEG(tracker_name=tracker_name,
                              para_name=para_name,
                              refine_model_name=refine_model_name,
                              threshold=threshold)
    handle = vot.VOT("mask")
    selection = handle.region()
    imagefile = handle.frame()
    if not imagefile:
        sys.exit(0)
    if VIS:
        '''for vis'''
        seq_name = imagefile.split('/')[-3]
        save_v_dir = os.path.join(save_root, seq_name)
        if not os.path.exists(save_v_dir):
            os.mkdir(save_v_dir)
        cur_time = int(time.time() % 10000)
        save_dir = os.path.join(save_v_dir, str(cur_time))
        if not os.path.exists(save_dir):
            os.makedirs(save_dir)

    image = cv2.cvtColor(cv2.imread(imagefile), cv2.COLOR_BGR2RGB)  # Right
    # mask given by the toolkit ends with the target (zero-padding to the right and down is needed)
    mask = make_full_size(selection, (image.shape[1], image.shape[0]))
    tracker.initialize(image, mask)

    while True:
        imagefile = handle.frame()
        if not imagefile:
            break
        image = cv2.cvtColor(cv2.imread(imagefile), cv2.COLOR_BGR2RGB)  # Right
        b1, m, search, search_m = tracker.track(image)
        handle.report(m)
        if VIS:
            '''Visualization'''
            # original image
            image_ori = image[:, :, ::-1].copy()  # RGB --> BGR
            image_name = imagefile.split('/')[-1]
            save_path = os.path.join(save_dir, image_name)
            cv2.imwrite(save_path, image_ori)
            # dimp box
            image_b = image_ori.copy()
            cv2.rectangle(image_b, (int(b1[0]), int(b1[1])),
                          (int(b1[0] + b1[2]), int(b1[1] + b1[3])),
                          (0, 0, 255), 2)
            image_b_name = image_name.replace('.jpg', '_bbox.jpg')
            save_path = os.path.join(save_dir, image_b_name)
            cv2.imwrite(save_path, image_b)
            # search region
            search_bgr = search[:, :, ::-1].copy()
            search_name = image_name.replace('.jpg', '_search.jpg')
            save_path = os.path.join(save_dir, search_name)
            cv2.imwrite(save_path, search_bgr)
            # search region mask
            search_bgr_m = search_bgr.astype(np.float32)
            search_bgr_m[:, :, 1] += 127.0 * search_m
            search_bgr_m[:, :, 2] += 127.0 * search_m
            contours, _ = cv2.findContours(search_m, cv2.RETR_TREE,
                                           cv2.CHAIN_APPROX_SIMPLE)
            search_bgr_m = cv2.drawContours(search_bgr_m, contours, -1,
                                            (0, 255, 255), 4)
            search_bgr_m = search_bgr_m.clip(0, 255).astype(np.uint8)
            search_name_m = image_name.replace('.jpg', '_search_mask.jpg')
            save_path = os.path.join(save_dir, search_name_m)
            cv2.imwrite(save_path, search_bgr_m)
            # original image + mask
            image_m = image_ori.copy().astype(np.float32)
            image_m[:, :, 1] += 127.0 * m
            image_m[:, :, 2] += 127.0 * m
            contours, _ = cv2.findContours(m, cv2.RETR_TREE,
                                           cv2.CHAIN_APPROX_SIMPLE)
            image_m = cv2.drawContours(image_m, contours, -1, (0, 255, 255), 2)
            image_m = image_m.clip(0, 255).astype(np.uint8)
            image_mask_name_m = image_name.replace('.jpg', '_mask.jpg')
            save_path = os.path.join(save_dir, image_mask_name_m)
            cv2.imwrite(save_path, image_m)
from run_SiamRPN import SiamRPN_init, SiamRPN_track
from utils import get_axis_aligned_bbox, cxy_wh_2_rect

# load net
net_file = join(realpath(dirname(__file__)), 'SiamRPNBIG.model')
net = SiamRPNBIG()
net.load_state_dict(torch.load(net_file))
net.eval().cuda()

# warm up
for i in range(10):
    net.temple(torch.autograd.Variable(torch.FloatTensor(1, 3, 127, 127)).cuda())
    net(torch.autograd.Variable(torch.FloatTensor(1, 3, 255, 255)).cuda())

# start to track
handle = vot.VOT("polygon")
Polygon = handle.region()
cx, cy, w, h = get_axis_aligned_bbox(Polygon)

image_file = handle.frame()
if not image_file:
    sys.exit(0)

target_pos, target_sz = np.array([cx, cy]), np.array([w, h])
im = cv2.imread(image_file)  # HxWxC
state = SiamRPN_init(im, target_pos, target_sz, net)  # init tracker
while True:
    image_file = handle.frame()
    if not image_file:
        break
    im = cv2.imread(image_file)  # HxWxC
Beispiel #7
0
#!/usr/bin/python
#import torch

import vot
import sys
import time
from src.siamvggtracker import SiamVGGTracker
# *****************************************
# VOT: Create VOT handle at the beginning
#      Then get the initializaton region
#      and the first image
# *****************************************

handle = vot.VOT("rectangle")
selection = handle.region()

# Process the first frame
imagefile = handle.frame()
tracker = SiamVGGTracker(imagefile, selection)

if not imagefile:
    sys.exit(0)

while True:
    # *****************************************
    # VOT: Call frame method to get path of the
    #      current image frame. If the result is
    #      null, the sequence is over.
    # *****************************************
    imagefile = handle.frame()
    if not imagefile:
Beispiel #8
0
def run_vot_exp(tracker_name, para_name, vis=False):

    torch.set_num_threads(1)
    save_root = os.path.join('/data/sda/v-yanbi/iccv21/LittleBoy/vot20_debug',
                             para_name)
    if vis and (not os.path.exists(save_root)):
        os.mkdir(save_root)
    tracker = stark_vot20(tracker_name=tracker_name, para_name=para_name)
    handle = vot.VOT("mask")
    selection = handle.region()
    imagefile = handle.frame()
    if not imagefile:
        sys.exit(0)
    if vis:
        '''for vis'''
        seq_name = imagefile.split('/')[-3]
        save_v_dir = os.path.join(save_root, seq_name)
        if not os.path.exists(save_v_dir):
            os.mkdir(save_v_dir)
        cur_time = int(time.time() % 10000)
        save_dir = os.path.join(save_v_dir, str(cur_time))
        if not os.path.exists(save_dir):
            os.makedirs(save_dir)

    image = cv2.cvtColor(cv2.imread(imagefile), cv2.COLOR_BGR2RGB)  # Right
    # mask given by the toolkit ends with the target (zero-padding to the right and down is needed)
    mask = make_full_size(selection, (image.shape[1], image.shape[0]))
    tracker.initialize(image, mask)

    while True:
        imagefile = handle.frame()
        if not imagefile:
            break
        image = cv2.cvtColor(cv2.imread(imagefile), cv2.COLOR_BGR2RGB)  # Right
        b1, m = tracker.track(image)
        handle.report(m)
        if vis:
            '''Visualization'''
            # original image
            image_ori = image[:, :, ::-1].copy()  # RGB --> BGR
            image_name = imagefile.split('/')[-1]
            save_path = os.path.join(save_dir, image_name)
            cv2.imwrite(save_path, image_ori)
            # tracker box
            image_b = image_ori.copy()
            cv2.rectangle(image_b, (int(b1[0]), int(b1[1])),
                          (int(b1[0] + b1[2]), int(b1[1] + b1[3])),
                          (0, 0, 255), 2)
            image_b_name = image_name.replace('.jpg', '_bbox.jpg')
            save_path = os.path.join(save_dir, image_b_name)
            cv2.imwrite(save_path, image_b)
            # original image + mask
            image_m = image_ori.copy().astype(np.float32)
            image_m[:, :, 1] += 127.0 * m
            image_m[:, :, 2] += 127.0 * m
            contours, _ = cv2.findContours(m, cv2.RETR_TREE,
                                           cv2.CHAIN_APPROX_SIMPLE)
            image_m = cv2.drawContours(image_m, contours, -1, (0, 255, 255), 2)
            image_m = image_m.clip(0, 255).astype(np.uint8)
            image_mask_name_m = image_name.replace('.jpg', '_mask.jpg')
            save_path = os.path.join(save_dir, image_mask_name_m)
            cv2.imwrite(save_path, image_m)
Beispiel #9
0
sys.path.append("/home/guo/zpy/Mypysot")
import os
os.environ['CUDA_VISIBLE_DEVICES'] = '1'
from os.path import realpath, dirname, join
del os.environ['MKL_NUM_THREADS']

import time
import cv2
import torch
import numpy as np

from mypysot.tracker.TSDMTrack import TSDMTracker
from mypysot.tools.bbox import get_axis_aligned_bbox

# start to track
handle = vot.VOT("rectangle", 'rgbd')
region = handle.region()
image_file1, image_file2 = handle.frame()
if not image_file1:
    sys.exit(0)

image_rgb = cv2.imread(image_file1)
image_depth = cv2.imread(image_file2, -1)
SiamRes_dir = '/home/guo/zpy/Mypysot/mypysot/data_and_result/weight/modelMob.pth'  #modelRes
SiamMask_dir = '/home/guo/zpy/Mypysot/mypysot/data_and_result/weight/Mob20.pth'  #Res20.pth'
Dr_dir = '/home/guo/zpy/Mypysot/mypysot/data_and_result/weight/High-Low-two.pth.tar'
tracker = TSDMTracker(SiamRes_dir, SiamMask_dir, Dr_dir, image_rgb,
                      image_depth, region)

#track
while True:
Beispiel #10
0
net = net.cuda()

# warm up
print('==== warm up ====')
for i in range(10):
    net.template(
        torch.rand(1, 3, 127, 127).cuda(),
        torch.rand(1, 127, 127).cuda())
    net.track(torch.rand(1, 3, 255, 255).cuda())

tracker = OceanPlus(info)

# vot2020 settings

if mask_vot:
    handle = vot.VOT("mask")
else:
    handle = vot.VOT("rectangle")

image_file = handle.frame()

if not image_file:
    sys.exit(0)

im = cv2.imread(image_file)  # HxWxC

if mask_vot:
    print('the input is a binary mask')
    selection = handle.region()
    mask = make_full_size(selection, (im.shape[1], im.shape[0]))
    bbox = rect_from_mask(mask)  # [cx,cy,w,h] TODO: use cv.minmaxRect here
Beispiel #11
0
    if x.shape[0] == output_sz[1] and x.shape[1] == output_sz[0]:
        return x
    pad_x = output_sz[0] - x.shape[1]
    if pad_x < 0:
        x = x[:, :x.shape[1] + pad_x]
        # padding has to be set to zero, otherwise pad function fails
        pad_x = 0
    pad_y = output_sz[1] - x.shape[0]
    if pad_y < 0:
        x = x[:x.shape[0] + pad_y, :]
        # padding has to be set to zero, otherwise pad function fails
        pad_y = 0
    return np.pad(x, ((0, pad_y), (0, pad_x)), 'constant', constant_values=0)


handle = vot.VOT("mask")
selection = handle.region()

imagefile = handle.frame()
if not imagefile:
    sys.exit(0)

image = cv2.imread(imagefile, cv2.IMREAD_GRAYSCALE)

# mask given by the toolkit ends with the target (zero-padding to the right and down is needed)
mask = make_full_size(selection, (image.shape[1], image.shape[0]))

tracker = NCCTracker(image, mask)
while True:
    imagefile = handle.frame()
    if not imagefile:
Beispiel #12
0
        top_left, bottom_right = box[:2].tolist(), box[2:].tolist()
        image = cv2.rectangle(image, tuple(top_left), tuple(bottom_right),
                              [0, 0, 255], 2)

    save_path = os.path.join("/media/tangjiuqi097/ext", tracker_name)
    os.makedirs(save_path, exist_ok=True)
    split_image_file = image_file.split('/')
    seq_name = split_image_file[-3]
    im_name = split_image_file[-1]
    os.makedirs(os.path.join(save_path, seq_name), exist_ok=True)
    im_path = os.path.join(save_path, seq_name, im_name)
    cv2.imwrite(im_path, image[:, :, ::-1])


# handle = vot.VOT("rectangle")
handle = vot.VOT("polygon", 'rgbd')
selection = handle.region()

if isinstance(selection, Polygon):
    selection = np.array(selection).reshape(-1)
    cx = np.mean(selection[0::2])
    cy = np.mean(selection[1::2])
    x1 = np.min(selection[0::2])
    x2 = np.max(selection[0::2])
    y1 = np.min(selection[1::2])
    y2 = np.max(selection[1::2])
    A1 = np.linalg.norm(selection[0:2] -
                        selection[2:4]) * np.linalg.norm(selection[2:4] -
                                                         selection[4:6])
    A2 = (x2 - x1) * (y2 - y1)
    s = np.sqrt(A1 / A2)
def run_meta_tracker():
    handle = vot.VOT("rectangle")
    selection = handle.region()
    imagefile = handle.frame()
    if not imagefile:
        sys.exit(0)

    # Init bbox
    target_bbox = np.array([selection.x, selection.y, selection.width, selection.height])
    prev_result = target_bbox
    prev_result_bb = target_bbox

    ## Initialize Tracker Net ##
    init_net = MetaSDNet(opts['init_model_path'])
    if opts['use_gpu']:
        init_net = init_net.cuda()
    init_net.set_learnable_params(['features','fc'])
    states = torch.load(opts['meta_tracker_path'])
    meta_init = states['meta_init']
    meta_alpha = states['meta_alpha']
    init_net.copy_meta_weights(meta_init)
    
    # Init criterion
    criterion = BinaryLoss()
    evaluator = Accuracy() # or Precision

    # Load first image1
    image1 = Image.open(imagefile).convert('RGB')
    
    # Train bbox regressor
    bbreg_examples = gen_samples(SampleGenerator('uniform', image1.size, 0.3, 1.5, 1.1),
                                 target_bbox, opts['n_bbreg'], opts['overlap_bbreg'], opts['scale_bbreg'])
    bbreg_feats = forward_samples(init_net, image1, bbreg_examples, out_layer='features')
    bbreg = BBRegressor(image1.size)
    bbreg.train(bbreg_feats, bbreg_examples, target_bbox)

    # Initial training(for the first frame)
    pos_feats, neg_feats, init_acc, first_acc = train_init(init_net, meta_alpha, criterion, image1, target_bbox, evaluator)
    init_params = OrderedDict()
    for k, p in init_net.named_parameters():
            init_params[k] = p
    online_net = MetaSDNetOnline()
    online_net.copy_meta_weights(init_params)
    online_net.set_learnable_params(['fc'])
    if opts['use_gpu']:
        online_net = online_net.cuda()
    online_optimizer = set_optimizer(online_net, opts['lr_update'], opts['lr_mult'])
    #print("\t[Init] init_acc:%.4f, acc:%.4f\n"%(init_acc, first_acc))
    
    # Init sample generators
    sample_generator = SampleGenerator('gaussian', image1.size, opts['trans_f'], opts['scale_f'], valid=True)
    pos_generator = SampleGenerator('gaussian', image1.size, 0.1, 1.2)
    neg_generator = SampleGenerator('uniform', image1.size, 1.5, 1.2)

    # Init pos/neg features for update
    pos_feats_all = [pos_feats]
    neg_feats_all = [neg_feats]

    # Main loop
    iters = 1
    while True:
        # Load image
        imagefile = handle.frame()
        if not imagefile:
            break
        image = Image.open(imagefile).convert('RGB')

        # Estimate target bbox,
        # Using top-scored 5 bboxes, average bbox # regression
        samples = gen_samples(sample_generator, target_bbox, opts['n_samples'])
        sample_scores = forward_samples(online_net, image, samples)
        top_scores, top_idx = sample_scores[:,1].topk(5)
        top_idx = top_idx.cpu().numpy()
        target_score = top_scores.mean()
        target_bbox = samples[top_idx].mean(axis=0)

        success = target_score > opts['success_thr']
        
        # Expand search area at failure
        if success:
            sample_generator.set_trans_f(opts['trans_f'])
        else:
            sample_generator.set_trans_f(opts['trans_f_expand'])

        # Bbox regression
        if success:
            bbreg_samples = samples[top_idx]
            bbreg_feats = forward_samples(online_net, image, bbreg_samples, out_layer='features')
            bbreg_samples = bbreg.predict(bbreg_feats, bbreg_samples)
            bbreg_bbox = bbreg_samples.mean(axis=0)
        else:
            bbreg_bbox = target_bbox
        
        # Copy previous result at failure
        if not success:
            target_bbox = prev_result
            bbreg_bbox = prev_result_bb
        
        # Save result
        prev_result = target_bbox
        prev_result_bb = bbreg_bbox
        vot_result = vot.Rectangle(bbreg_bbox[0],bbreg_bbox[1],bbreg_bbox[2],bbreg_bbox[3])
        handle.report(vot_result)

        # Data collect
        if success:
            # Draw pos/neg samples
            pos_examples = gen_samples(pos_generator, target_bbox, 
                                       opts['n_pos_update'],
                                       opts['overlap_pos_update'])
            neg_examples = gen_samples(neg_generator, target_bbox, 
                                       opts['n_neg_update'],
                                       opts['overlap_neg_update'])
            # Extract pos/neg features
            pos_feats = forward_samples(online_net, image, pos_examples, out_layer='features')
            neg_feats = forward_samples(online_net, image, neg_examples, out_layer='features')
            pos_feats_all.append(pos_feats)
            neg_feats_all.append(neg_feats)
            if len(pos_feats_all) > opts['n_frames_long']:
                del pos_feats_all[0]
            if len(neg_feats_all) > opts['n_frames_short']:
                del neg_feats_all[0]

        # Short term update
        if not success:
            #print("Short term update!")
            nframes = min(opts['n_frames_short'],len(pos_feats_all))
            pos_feats_short = pos_feats_all[-nframes:]
            pos_data = pos_feats_short[0]
            for pos_feat in pos_feats_short[1:]:
                pos_data = torch.cat([pos_data,pos_feat],0)
            #neg_feats_short = neg_feats_all
            neg_feats_short = neg_feats_all[-nframes:]
            neg_data = neg_feats_short[0]
            for neg_feat in neg_feats_short[1:]:
                neg_data = torch.cat([neg_data,neg_feat],0)
            train_online(online_net, online_optimizer, criterion, pos_data, neg_data, opts['n_tracker_updates'])
        # Long term update
        elif iters % opts['long_interval'] == 0:
            #print("Long term update!")
            pos_data = pos_feats_all[0]
            for pos_feat in pos_feats_all[1:]:
                pos_data = torch.cat([pos_data,pos_feat],0)
            neg_data = neg_feats_all[0]
            for neg_feat in neg_feats_all[1:]:
                neg_data = torch.cat([neg_data,neg_feat],0)
            train_online(online_net, online_optimizer, criterion, pos_data, neg_data, opts['n_tracker_updates'])
        iters = iters+1
        
    return prev_result, prev_result_bb 
def run_crest():
    handle = vot.VOT("rectangle")
    selection = handle.region()
    imagefile = handle.frame()
    if not imagefile:
        sys.exit(0)

    # Generate sequence config
    max_object_sz = 100

    target_bbox1 = np.array([selection.x, selection.y, selection.width, selection.height])

    ## initialize feature extractor ##
    feature_net = FeatureExtractor(feat_extractor_path)
    if use_gpu:
        feature_net = feature_net.cuda()
    feature_net.eval()

    ## resize target object size - computational issue ##
    scale = 1
    if target_bbox1[2:].prod() > max_object_sz**2:
        scale = max_object_sz/(target_bbox1[2:].max())

    # load first image1
    image1 = Image.open(imagefile).convert('RGB')
    image_sz_resized = (int(image1.size[0]*scale), int(image1.size[1]*scale))
    image1_resized = image1.resize(image_sz_resized,Image.BILINEAR)
    
    # scale target bbox
    resized_target_bbox1 = np.round(target_bbox1*scale)
    target_sz1 = resized_target_bbox1[2:]
    target_center1 = resized_target_bbox1[:2] + np.floor(target_sz1/2)

    # get cosine window(square shape)
    window_sz = get_search_size(target_sz1)
    window_cell_sz = np.ceil(window_sz/cell_sz)
    window_cell_sz = window_cell_sz - (window_cell_sz%2) + 1
    cos_window = np.outer(np.hanning(window_cell_sz),np.hanning(window_cell_sz))
    cos_window = cos_window[None,None,:,:].repeat(vgg_num_channels,axis=1)
    cos_window = Variable(torch.from_numpy(cos_window).float(),requires_grad=False)
    if use_gpu:
        cos_window = cos_window.cuda()

    # get search patch and extract the feature
    patch = get_search_patch(image1_resized, target_center1, window_sz)
    patch = Variable(torch.from_numpy(patch[None,:]))
    if use_gpu:
        patch = patch.cuda()
    feat = feature_net.forward(patch)

    # resizing features as same size as cos_window
    feat = resize_feat_cos_window(feat, cos_window)
    feat_sz=feat.size(3)

    ## get correlation filter label(gaussian shape) ##
    target_cell_sz1 = np.ceil(target_sz1/cell_sz)
    rw = int(np.ceil(target_cell_sz1[0]/2)) # padding for CF
    rh = int(np.ceil(target_cell_sz1[1]/2)) # padding for CF
    output_sigma = target_cell_sz1*output_sigma_factor
    label1_np = gaussian_shaped_labels(output_sigma, feat_sz, target_sz1)
    label1 = Variable(torch.from_numpy(label1_np).float())
    label1 = label1.view(1,1,label1.size(0),label1.size(1))
    if use_gpu:
        label1 = label1.cuda()
    
    ## MetaCrest init ##
    state = torch.load(tracker_path)
    meta_init = state['meta_init']
    meta_alpha = state['meta_alpha']
    criterion = L2normLoss()
    if use_gpu:
        criterion = criterion.cuda()
    
    ## training first frame ##
    tracker_net = train_init(meta_init, meta_alpha, target_cell_sz1, criterion, feat, label1)
    online_optimizer = set_optimizer(tracker_net, online_lr_base, online_w_decay)
    tracker_net.eval()
    
    ## online prediction ##
    target_center = target_center1
    target_sz = target_sz1
    feat_update = []
    label_update = []
    iters = 0
    while True:
        imagefile = handle.frame()
        if not imagefile:
            break
        image = Image.open(imagefile).convert('RGB')
        image_resized = image.resize(image_sz_resized,Image.BILINEAR)
        patch = get_search_patch(image_resized, target_center, window_sz)
        #plt.imshow(patch[0].data.cpu().numpy().transpose(1,2,0)); plt.colorbar(); plt.show()
        patch = Variable(torch.from_numpy(patch[None,:]))
        if use_gpu:
            patch = patch.cuda()
        feat = feature_net.forward(patch)
        feat = resize_feat_cos_window(feat, cos_window)
        
        response = tracker_net.forward(feat)

        score = response.max().data[0]
        #plt.imshow(response.data.cpu().numpy()[0][0]); plt.colorbar(); plt.show()

        motion_sigma = target_cell_sz1*motion_sigma_factor
        motion_map = gaussian_shaped_labels(motion_sigma, window_cell_sz, target_sz)
        #plt.imshow(motion_map); plt.colorbar(); plt.show()
        motion_map = Variable(torch.from_numpy(motion_map).float())
        motion_map = motion_map.view(1,1,motion_map.size(0),motion_map.size(1))
        if use_gpu:
            motion_map = motion_map.cuda()
        
        response = response*motion_map
        response_np = response.data[0][0].cpu().numpy()
        #plt.imshow(response_np); plt.colorbar(); plt.show()
        delta = np.argwhere(response_np.max()==response_np)[0]+1 #zero-index -> original-index
        delta = delta-np.ceil(window_cell_sz/2)
        target_center[0] = target_center[0] + delta[1]*cell_sz
        target_center[1] = target_center[1] + delta[0]*cell_sz
        target_center = valid_pos(target_center, image_sz_resized)
        target_sz = scale_estimation(image_resized, feature_net, tracker_net, \
                                     target_sz, target_center, window_sz, cos_window)
        
        resized_target_bbox = np.zeros(4)
        resized_target_bbox[:2] = target_center - target_sz/2
        resized_target_bbox[2:] = target_sz
        result = np.round(resized_target_bbox/scale)
        vot_result = vot.Rectangle(result[0],result[1],result[2],result[3])
        handle.report(vot_result)
      
        if score > score_thres:
            temp_label = np.roll(label1_np, int(delta[0]), axis=0) # circular shift y-axis
            temp_label = np.roll(temp_label, int(delta[1]), axis=1) # x-axis
            if len(label_update) == max_db_size:
                label_update.pop(0)
                feat_update.pop(0)
            temp_label = torch.from_numpy(temp_label[None,:]).float()
            if use_gpu:
                temp_label = temp_label.cuda()
            label_update.append(temp_label)
            feat_update.append(feat[0].data.clone().float())
        
        if (iters+1)%update_interval==0:
            if len(label_update) >= update_interval:
                tracker_net.train()
                model_update(tracker_net, online_optimizer, criterion, torch.stack(feat_update), torch.stack(label_update))
        iters = iters+1

    return result, gt