Skip to content

Commit

Permalink
regulate formats
Browse files Browse the repository at this point in the history
  • Loading branch information
MasterBin-IIAU committed Apr 9, 2021
1 parent a9fe2ee commit 20de637
Show file tree
Hide file tree
Showing 12 changed files with 41 additions and 49 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -3,21 +3,18 @@
from __future__ import print_function
from __future__ import unicode_literals


import cv2
import torch
import vot
import sys
import time

'''Refine module & Pytracking base trackers'''
# from common_path import *
import os
'''2020.4.24 Use new pytracking library(New DiMP)'''
from pytracking.evaluation import Tracker
'''2020.4.15 ARcm_seg model'''
from pytracking.ARcm_seg import ARcm_seg
'''other utils'''
from pytracking.vot20_utils import *

''''''
'''DiMP-alpha class'''

Expand All @@ -35,11 +32,11 @@ def __init__(self, tracker_name='dimp', para_name='dimp50_vot19',
params.visdom_info = {'use_visdom': False, 'server': '127.0.0.1', 'port': 8097}
self.dimp = tracker_info.tracker_class(params)
'''Alpha-Refine'''
project_path = os.path.join(os.path.dirname(__file__),'..','..')
project_path = os.path.join(os.path.dirname(__file__), '..', '..')
refine_root = os.path.join(project_path, 'ltr/checkpoints/ltr/ARcm_seg/')
refine_path = os.path.join(refine_root, refine_model_name)
'''2020.4.25 input size: 384x384'''
self.alpha = ARcm_seg(refine_path,input_sz=384)
self.alpha = ARcm_seg(refine_path, input_sz=384)

def initialize(self, img_RGB, mask):
region = rect_from_mask(mask)
Expand Down Expand Up @@ -71,25 +68,23 @@ def track(self, img_RGB):
self.dimp.pos = new_pos.clone()
self.dimp.target_sz = new_target_sz
self.dimp.target_scale = new_scale
bbox_new = [x1,y1,w,h]
bbox_new = [x1, y1, w, h]
'''Step2: Mask report'''
pred_mask, search, search_mask = self.alpha.get_mask(img_RGB, np.array(bbox_new),vis=True)
pred_mask, search, search_mask = self.alpha.get_mask(img_RGB, np.array(bbox_new), vis=True)
final_mask = (pred_mask > self.THRES).astype(np.uint8)
search_region = search.astype(np.uint8)
search_mask = (search_mask > self.THRES).astype(np.uint8)
return bbox_new, final_mask, search_region, search_mask


def run_vot_exp(tracker_name,para_name,refine_model_name,threshold,VIS=False):

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('/media/masterbin-iiau/WIN_SSD/vot20_debug',para_name)
save_root = os.path.join('/home/alphabin/Desktop/AlphaRefine_submit/vot20_debug',para_name)
save_root = os.path.join('<SAVE_DIR>', para_name)
if VIS and (not os.path.exists(save_root)):
os.mkdir(save_root)
tracker = DIMP_ALPHA(tracker_name=tracker_name,para_name=para_name,
refine_model_name=refine_model_name,threshold=threshold)
tracker = DIMP_ALPHA(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()
Expand All @@ -98,15 +93,15 @@ def run_vot_exp(tracker_name,para_name,refine_model_name,threshold,VIS=False):
if VIS:
'''for vis'''
seq_name = imagefile.split('/')[-3]
save_v_dir = os.path.join(save_root,seq_name)
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
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)
Expand All @@ -121,19 +116,19 @@ def run_vot_exp(tracker_name,para_name,refine_model_name,threshold,VIS=False):
if VIS:
'''Visualization'''
# original image
image_ori = image[:,:,::-1].copy() # RGB --> BGR
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')
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_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)
Expand All @@ -143,7 +138,7 @@ def run_vot_exp(tracker_name,para_name,refine_model_name,threshold,VIS=False):
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_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)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@
from __future__ import print_function
from __future__ import unicode_literals


import cv2
import torch
import vot
Expand Down Expand Up @@ -58,15 +57,13 @@ def track(self, img_RGB):


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('/media/masterbin-iiau/WIN_SSD/vot20_debug',para_name)
save_root = os.path.join('/data/sda/v-yanbi/iccv21/LittleBoy/vot20/vot20_debug', para_name)
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)
refine_model_name=refine_model_name, threshold=threshold)
handle = vot.VOT("mask")
selection = handle.region()
imagefile = handle.frame()
Expand All @@ -75,15 +72,15 @@ def run_vot_exp(tracker_name, para_name, refine_model_name, threshold, VIS=False
if VIS:
'''for vis'''
seq_name = imagefile.split('/')[-3]
save_v_dir = os.path.join(save_root,seq_name)
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
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)
Expand All @@ -98,19 +95,19 @@ def run_vot_exp(tracker_name, para_name, refine_model_name, threshold, VIS=False
if VIS:
'''Visualization'''
# original image
image_ori = image[:,:,::-1].copy() # RGB --> BGR
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')
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_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)
Expand All @@ -120,7 +117,7 @@ def run_vot_exp(tracker_name, para_name, refine_model_name, threshold, VIS=False
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_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)
Expand Down
2 changes: 1 addition & 1 deletion external/vot20/stark_s50/trackers.ini
Original file line number Diff line number Diff line change
Expand Up @@ -3,4 +3,4 @@ label = stark_s50
protocol = traxpython
command = stark_s50
# Specify a path to trax python wrapper if it is not visible (separate by ; if using multiple paths)
paths = /data/sda/v-yanbi/iccv21/MSRA/Stark/lib/test/vot20
paths = <PATH_OF_STARK>/lib/test/vot20
2 changes: 1 addition & 1 deletion external/vot20/stark_s50_ar/trackers.ini
Original file line number Diff line number Diff line change
Expand Up @@ -3,4 +3,4 @@ label = stark_s50_ar
protocol = traxpython
command = stark_s50_ar
# Specify a path to trax python wrapper if it is not visible (separate by ; if using multiple paths)
paths = /data/sda/v-yanbi/iccv21/MSRA/Stark/external/AR/pytracking/VOT2020_super_only_mask_384_HP
paths = <PATH_OF_STARK>/external/AR/pytracking/VOT2020_super_only_mask_384_HP
2 changes: 1 addition & 1 deletion external/vot20/stark_st101/trackers.ini
Original file line number Diff line number Diff line change
Expand Up @@ -3,4 +3,4 @@ label = stark_st101
protocol = traxpython
command = stark_st101
# Specify a path to trax python wrapper if it is not visible (separate by ; if using multiple paths)
paths = /data/sda/v-yanbi/iccv21/MSRA/Stark/lib/test/vot20
paths = <PATH_OF_STARK>/lib/test/vot20
2 changes: 1 addition & 1 deletion external/vot20/stark_st101_ar/trackers.ini
Original file line number Diff line number Diff line change
Expand Up @@ -3,4 +3,4 @@ label = stark_st101_ar
protocol = traxpython
command = stark_st101_ar
# Specify a path to trax python wrapper if it is not visible (separate by ; if using multiple paths)
paths = /data/sda/v-yanbi/iccv21/MSRA/Stark/external/AR/pytracking/VOT2020_super_only_mask_384_HP
paths = <PATH_OF_STARK>/external/AR/pytracking/VOT2020_super_only_mask_384_HP
2 changes: 1 addition & 1 deletion external/vot20/stark_st50/trackers.ini
Original file line number Diff line number Diff line change
Expand Up @@ -3,4 +3,4 @@ label = stark_st50
protocol = traxpython
command = stark_st50
# Specify a path to trax python wrapper if it is not visible (separate by ; if using multiple paths)
paths = /data/sda/v-yanbi/iccv21/MSRA/Stark/lib/test/vot20
paths = <PATH_OF_STARK>/lib/test/vot20
2 changes: 1 addition & 1 deletion external/vot20/stark_st50_ar/trackers.ini
Original file line number Diff line number Diff line change
Expand Up @@ -3,4 +3,4 @@ label = stark_st50_ar
protocol = traxpython
command = stark_st50_ar
# Specify a path to trax python wrapper if it is not visible (separate by ; if using multiple paths)
paths = /data/sda/v-yanbi/iccv21/MSRA/Stark/external/AR/pytracking/VOT2020_super_only_mask_384_HP
paths = <PATH_OF_STARK>/external/AR/pytracking/VOT2020_super_only_mask_384_HP
2 changes: 1 addition & 1 deletion external/vot20_lt/stark_st101_lt/trackers.ini
Original file line number Diff line number Diff line change
Expand Up @@ -3,4 +3,4 @@ label = stark_st101_lt
protocol = traxpython
command = stark_st101_lt
# Specify a path to trax python wrapper if it is not visible (separate by ; if using multiple paths)
paths = /data/sda/v-yanbi/iccv21/MSRA/Stark/lib/test/vot20
paths = <PATH_OF_STARK>/lib/test/vot20
2 changes: 1 addition & 1 deletion external/vot20_lt/stark_st50_lt/trackers.ini
Original file line number Diff line number Diff line change
Expand Up @@ -3,4 +3,4 @@ label = stark_st50_lt
protocol = traxpython
command = stark_st50_lt
# Specify a path to trax python wrapper if it is not visible (separate by ; if using multiple paths)
paths = /data/sda/v-yanbi/iccv21/MSRA/Stark/lib/test/vot20
paths = <PATH_OF_STARK>/lib/test/vot20
14 changes: 7 additions & 7 deletions lib/test/vot20/vot20_utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,11 @@


def make_full_size(x, output_sz):
'''
"""
zero-pad input x (right and down) to match output_sz
x: numpy array e.g., binary mask
output_sz: size of the output [width, height]
'''
"""
if x.shape[0] == output_sz[1] and x.shape[1] == output_sz[0]:
return x
pad_x = output_sz[0] - x.shape[1]
Expand All @@ -23,10 +23,10 @@ def make_full_size(x, output_sz):


def rect_from_mask(mask):
'''
"""
create an axis-aligned rectangle from a given binary mask
mask in created as a minimal rectangle containing all non-zero pixels
'''
"""
x_ = np.sum(mask, axis=0)
y_ = np.sum(mask, axis=1)
x0 = np.min(np.nonzero(x_))
Expand All @@ -37,11 +37,11 @@ def rect_from_mask(mask):


def mask_from_rect(rect, output_sz):
'''
"""
create a binary mask from a given rectangle
rect: axis-aligned rectangle [x0, y0, width, height]
output_sz: size of the output [width, height]
'''
"""
mask = np.zeros((output_sz[1], output_sz[0]), dtype=np.uint8)
x0 = max(int(round(rect[0])), 0)
y0 = max(int(round(rect[1])), 0)
Expand All @@ -52,7 +52,7 @@ def mask_from_rect(rect, output_sz):


def bbox_clip(x1, y1, x2, y2, boundary, min_sz=10):
'''boundary (H,W)'''
"""boundary (H,W)"""
x1_new = max(0, min(x1, boundary[1] - min_sz))
y1_new = max(0, min(y1, boundary[0] - min_sz))
x2_new = max(min_sz, min(x2, boundary[1]))
Expand Down
4 changes: 2 additions & 2 deletions tracking/profile_model.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
import time
import importlib


def parse_args():
"""
args for training.
Expand All @@ -25,7 +26,7 @@ def parse_args():


def evaluate(model, search, seq_dict, run_box_head, run_cls_head):
'''Compute FLOPs, Params, and Speed'''
"""Compute FLOPs, Params, and Speed"""
# # backbone
macs1, params1 = profile(model, inputs=(search, None, "backbone", False, False),
custom_ops=None, verbose=False)
Expand Down Expand Up @@ -128,4 +129,3 @@ def get_data(bs, sz):
seq_dict = merge_template_search([oup_t1, oup_t2, oup_s])
# evaluate the model properties
evaluate(model, search, seq_dict, run_box_head=True, run_cls_head=True)

0 comments on commit 20de637

Please sign in to comment.