Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add fix for using cpu device #72

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 7 additions & 4 deletions code/demo.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,10 +13,13 @@
from run_SiamRPN import SiamRPN_init, SiamRPN_track
from utils import get_axis_aligned_bbox, cxy_wh_2_rect

# get supported device
device = torch.device("cuda:0" if torch.cuda.is_available() else "cpu")

# load net
net = SiamRPNvot()
net.load_state_dict(torch.load(join(realpath(dirname(__file__)), 'SiamRPNVOT.model')))
net.eval().cuda()
net.load_state_dict(torch.load(join(realpath(dirname(__file__)), 'SiamRPNVOT.model'), map_location=device))
net.eval().to(device)

# image and init box
image_files = sorted(glob.glob('./bag/*.jpg'))
Expand All @@ -26,14 +29,14 @@
# tracker init
target_pos, target_sz = np.array([cx, cy]), np.array([w, h])
im = cv2.imread(image_files[0]) # HxWxC
state = SiamRPN_init(im, target_pos, target_sz, net)
state = SiamRPN_init(im, target_pos, target_sz, net, device)

# tracking and visualization
toc = 0
for f, image_file in enumerate(image_files):
im = cv2.imread(image_file)
tic = cv2.getTickCount()
state = SiamRPN_track(state, im) # track
state = SiamRPN_track(state, im, device) # track
toc += cv2.getTickCount()-tic
res = cxy_wh_2_rect(state['target_pos'], state['target_sz'])
res = [int(l) for l in res]
Expand Down
8 changes: 4 additions & 4 deletions code/run_SiamRPN.py
Original file line number Diff line number Diff line change
Expand Up @@ -114,7 +114,7 @@ def sz_wh(wh):
return target_pos, target_sz, score[best_pscore_id]


def SiamRPN_init(im, target_pos, target_sz, net):
def SiamRPN_init(im, target_pos, target_sz, net, device="cuda:0"):
state = dict()
p = TrackerConfig()
p.update(net.cfg)
Expand All @@ -140,7 +140,7 @@ def SiamRPN_init(im, target_pos, target_sz, net):
z_crop = get_subwindow_tracking(im, target_pos, p.exemplar_size, s_z, avg_chans)

z = Variable(z_crop.unsqueeze(0))
net.temple(z.cuda())
net.temple(z.to(device))

if p.windowing == 'cosine':
window = np.outer(np.hanning(p.score_size), np.hanning(p.score_size))
Expand All @@ -157,7 +157,7 @@ def SiamRPN_init(im, target_pos, target_sz, net):
return state


def SiamRPN_track(state, im):
def SiamRPN_track(state, im, device="cuda:0"):
p = state['p']
net = state['net']
avg_chans = state['avg_chans']
Expand All @@ -176,7 +176,7 @@ def SiamRPN_track(state, im):
# extract scaled crops for search region x at previous target position
x_crop = Variable(get_subwindow_tracking(im, target_pos, p.instance_size, round(s_x), avg_chans).unsqueeze(0))

target_pos, target_sz, score = tracker_eval(net, x_crop.cuda(), target_pos, target_sz * scale_z, window, scale_z, p)
target_pos, target_sz, score = tracker_eval(net, x_crop.to(device), target_pos, target_sz * scale_z, window, scale_z, p)
target_pos[0] = max(0, min(state['im_w'], target_pos[0]))
target_pos[1] = max(0, min(state['im_h'], target_pos[1]))
target_sz[0] = max(10, min(state['im_w'], target_sz[0]))
Expand Down