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]))
def __init__(self, path_seq, num_frames, gt_first): self.path_seq = path_seq self.num_frames = num_frames self.gt_first = gt_first # load net self.net = SiamRPNBIG() # self.net.load_state_dict(torch.load("./SiamRPNBIG.model")) self.net.eval().cuda() #self.testing_config = testing_config self.cur_seq_name = os.path.split(path_seq)[1] self.cur_frame = None
class Tracker(object): def __init__(self, path_seq, num_frames, gt_first): self.path_seq = path_seq self.num_frames = num_frames self.gt_first = gt_first # load net self.net = SiamRPNBIG() # self.net.load_state_dict(torch.load("./SiamRPNBIG.model")) self.net.eval().cuda() #self.testing_config = testing_config self.cur_seq_name = os.path.split(path_seq)[1] self.cur_frame = None def on_tracking(self): # warm up for i in range(10): self.net.template( torch.autograd.Variable(torch.FloatTensor(1, 3, 127, 127)).cuda()) self.net( torch.autograd.Variable(torch.FloatTensor(1, 3, 255, 255)).cuda()) i = 1 pred_bbx = self.gt_first print("{}th frame: {} {} {} {}".format(i, pred_bbx[0], pred_bbx[1], pred_bbx[2], pred_bbx[3])) cx, cy, w, h = pred_bbx[0] + pred_bbx[2] / 2.0, pred_bbx[ 1] + pred_bbx[3] / 2.0, pred_bbx[2], pred_bbx[3] i += 1 target_pos, target_sz = np.array([cx, cy]), np.array([w, h]) im = cv2.imread(self.path_seq + '/imgs/0001.jpg') # HxWxC state = SiamRPN_init(im, target_pos, target_sz, self.net) # init tracker while i <= self.num_frames: self.index_frame = i im = cv2.imread(self.path_seq + '/imgs/' + str(i).zfill(4) + '.jpg') state = SiamRPN_track(state, im) # convert cx, cy, w, h into rect res = cxy_wh_2_rect(state['target_pos'], state['target_sz']) print(f"{i}th frame: ", res) i += 1
def main(imagedir, gtdir): # 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 # get the first frame groundtruth gt_file = os.path.join(gtdir, 'gt.txt') with open(gt_file, 'r') as f: lines = f.readlines() gt = [] for line in lines: line = line.split(' ') gt.append([int(float(x)) for x in line]) init_bbox = gt[0] # top-left x y,w,h target_pos, target_sz = rect_2_cxy_wh( init_bbox) # top-left x y,w,h --> center x y,w,h image_list = glob.glob(os.path.join(imagedir, '*.jpg')) image_list.sort() im = cv2.imread(image_list[0]) # HxWxC state = SiamRPN_init(im, target_pos, target_sz, net) # init tracker bboxes = [] for i in range(1, len(gt)): im = cv2.imread(image_list[i]) # HxWxC state = SiamRPN_track(state, im) # track res = cxy_wh_2_rect( state['target_pos'], state['target_sz']) # center x y,w,h --> top-left x y,w,h bboxes.append(res.tolist()) _, precision, precision_auc, iou = _compile_results(gt[1:], bboxes) print(' -- Precision ' + "(20 px)" + ': ' + "%.2f" % precision +\ ' -- Precision AUC: ' + "%.2f" % precision_auc + \ ' -- IOU: ' + "%.2f" % iou + ' --') isSavebbox = True if isSavebbox: print('saving bbox...') res_bbox_file = os.path.join('results_bbox.json') json.dump(bboxes, open(res_bbox_file, 'w'), indent=2) isSavevideo = True if isSavevideo: print('saving video...') save_video(image_list, bboxes) print('done')
def main(): # load net net = SiamRPNBIG() net.load_state_dict(torch.load('SiamRPNBIG.model')) 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 cap = cv2.VideoCapture(0) while True: success,im=cap.read() if success is not True: break cv2.imshow('cam',im) k = cv2.waitKey(1) if k==27: x,y,w,h = cv2.selectROI('cam',im) cx,cy = (x+w/2,y+h/2) break target_pos, target_sz = np.array([cx, cy]), np.array([w, h]) state = SiamRPN_init(im, target_pos, target_sz, net) # init tracker while True: success,im=cap.read() # HxWxC if success is not True: break state = SiamRPN_track(state, im) # track bbox = np.array([state['target_pos'][0]-state['target_sz'][0]/2, state['target_pos'][1]-state['target_sz'][1]/2, state['target_pos'][0]+state['target_sz'][0]/2, state['target_pos'][1]+state['target_sz'][1]/2]).astype(int) pt = (state['target_pos'],state['target_sz']) im_bbox = cv2.rectangle(im,(bbox[0],bbox[1]),(bbox[2],bbox[3]),(0,255,0),3) cv2.imshow('cam',im_bbox) k = cv2.waitKey(1) if k==27: cap.release() cv2.destroyAllWindows() break
import vot from vot import Rectangle import sys import cv2 # imread import torch import numpy as np from os.path import realpath, dirname, join from net import SiamRPNBIG 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:
inter_rect_y2 = torch.min(b1_y2, b2_y2) # Intersection area inter_area = torch.clamp(inter_rect_x2 - inter_rect_x1 + 1, min=0) * \ torch.clamp(inter_rect_y2 - inter_rect_y1 + 1, min=0) # Union Area b1_area = (b1_x2 - b1_x1 + 1) * (b1_y2 - b1_y1 + 1) b2_area = (b2_x2 - b2_x1 + 1) * (b2_y2 - b2_y1 + 1) iou = inter_area / (b1_area + b2_area - inter_area + 1e-16) return iou # load net net_file = join(realpath(dirname(__file__)), 'SiamRPNBIG.model') net = SiamRPNBIG() net.train() config = TRAINING_PARAMS optimizer = _get_optimizer(config, net) net = net.cuda() net.load_state_dict(torch.load(net_file)) lr_scheduler = optim.lr_scheduler.CosineAnnealingLR(optimizer, T_max=15, eta_min=1e-6) # warm up for i in range(10): net.temple(
# print(data2[1]) data4=[] for k in data2: data3=np.array([0.0 if y=='' else float(y) for y in k]) data4.append(data3) data_rect=np.array(data4) # print(data_rect[0][0]) x1=data_rect[0][0] y1=data_rect[0][1] x2=data_rect[0][2] y2=data_rect[0][3] x3=data_rect[0][4] y3=data_rect[0][5] x4=data_rect[0][6] y4=data_rect[0][7] net = SiamRPNBIG() net.load_state_dict(torch.load('/home/mars/Documents/Subject2018/vot2018-SiamRPN/DaSiamRPN-master/code/SiamRPNBIG.model')) net.eval().cuda() 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()) cx = (x1+x2+x3+x4)/4 cy = (y1+y2+y3+y4)/4 w = ((x2-x1)+(x3-x4))/2 h = ((y3-y2)+(y4-y1))/2 target_pos, target_sz = np.array([cx, cy]), np.array([w, h]) image_file =image_path + img_name[0]
if __name__ == '__main__': args = parse_args() gpu_id = args.gpu_id if gpu_id is None: DEVICE = torch.device(f'cpu') else: DEVICE = torch.device(f'cuda:{gpu_id}') z_size = (127, 127) x_size = (255, 255) batch_size = num_domains = 5 num_epoches = 100 loader = dataset.load_data(batch_size, z_size, x_size)['train'] net = SiamRPNBIG() net.train().to(DEVICE) # load_pretrained_weights(net, "./SiamRPNBIG.model") optimizer = torch.optim.Adam(net.parameters(), weight_decay=0.001, lr=0.001) for i_ep in range(num_epoches): for i_iter, sample in enumerate(loader): zs = sample['template'].to(DEVICE) xs = sample['search_region'].to(DEVICE) gt_boxes = sample['gt_box'] # .to(DEVICE) optimizer.zero_grad() net.temple(zs) reg_output, cls_output = net.forward(xs) # of shape (50, 4*5, 17, 17), (50, 2*5, 17, 17) feat_h, feat_w = cls_output.size(-2), cls_output.size(-1)
import vot from vot import Rectangle import sys import cv2 # imread import torch import numpy as np from os.path import realpath, dirname, join from net import SiamRPNBIG 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, map_location="cpu")) # net.eval().cpu() net.eval().cpu() # warm up for i in range(10): net.temple(torch.autograd.Variable(torch.FloatTensor(1, 3, 127, 127)).cpu()) net(torch.autograd.Variable(torch.FloatTensor(1, 3, 255, 255)).cpu()) # start to track handle = vot.VOT("polygon") Polygon = handle.region() cx, cy, w, h = get_axis_aligned_bbox(Polygon) image_file = handle.frame()
def loadNet(): net = SiamRPNBIG() net.load_state_dict( torch.load('/home/zhenglong/DaSiamRPN/code/SiamRPNBIG.model')) net.eval().cuda() return net
import vot from vot import Rectangle import sys import cv2 # imread import torch import numpy as np from os.path import realpath, dirname, join from net import SiamRPNBIG 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() # 对模型进行验证,cuda(device=None) 将所有模型参数和缓冲区移动到GPU # warm up # 预热,运行模板和检测分支10次,先屏蔽掉 # for i in range(10): # net.temple(torch.autograd.Variable(torch.FloatTensor(1, 3, 127, 127)).cuda()) # selonsy:cuda()表示使用GPU进行计算,FloatTensor(1, 3, 127, 127):浮点型四维张量 # net(torch.autograd.Variable(torch.FloatTensor(1, 3, 255, 255)).cuda()) # start to track handle = vot.VOT("polygon") Polygon = handle.region() # region:将配置消息发送到客户端并接收初始化区域和第一个图像的路径。其返回值为初始化区域。 cx, cy, w, h = get_axis_aligned_bbox(Polygon) # get_axis_aligned_bbox:将坐标数据转换成 RPN 的格式 image_file = handle.frame() # frame 函数从客户端获取帧(图像路径)