forked from aharley/pips
-
Notifications
You must be signed in to change notification settings - Fork 0
/
chain_demo.py
193 lines (153 loc) · 6.48 KB
/
chain_demo.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
import time
import numpy as np
import io
import os
from PIL import Image
import cv2
import saverloader
import imageio.v2 as imageio
from nets.pips import Pips
import utils.improc
import random
import glob
from utils.basic import print_, print_stats
import torch
from tensorboardX import SummaryWriter
import torch.nn.functional as F
random.seed(125)
np.random.seed(125)
def run_model(model, rgbs, N, sw):
rgbs = rgbs.cuda().float() # B, S, C, H, W
B, S, C, H, W = rgbs.shape
rgbs_ = rgbs.reshape(B*S, C, H, W)
H_, W_ = 360, 640
rgbs_ = F.interpolate(rgbs_, (H_, W_), mode='bilinear')
H, W = H_, W_
rgbs = rgbs_.reshape(B, S, C, H, W)
# try to pick a point on the dog, so we get an interesting trajectory
# x = torch.randint(-10, 10, size=(1, N), device=torch.device('cuda')) + 468
# y = torch.randint(-10, 10, size=(1, N), device=torch.device('cuda')) + 118
x = torch.ones((1, N), device=torch.device('cuda')) * 450.0
y = torch.ones((1, N), device=torch.device('cuda')) * 100.0
xy0 = torch.stack([x, y], dim=-1) # B, N, 2
_, S, C, H, W = rgbs.shape
trajs_e = torch.zeros((B, S, N, 2), dtype=torch.float32, device='cuda')
for n in range(N):
# print('working on keypoint %d/%d' % (n+1, N))
cur_frame = 0
done = False
traj_e = torch.zeros((B, S, 2), dtype=torch.float32, device='cuda')
traj_e[:,0] = xy0[:,n] # B, 1, 2 # set first position
feat_init = None
while not done:
end_frame = cur_frame + 8
rgb_seq = rgbs[:,cur_frame:end_frame]
S_local = rgb_seq.shape[1]
rgb_seq = torch.cat([rgb_seq, rgb_seq[:,-1].unsqueeze(1).repeat(1,8-S_local,1,1,1)], dim=1)
outs = model(traj_e[:,cur_frame].reshape(1, -1, 2), rgb_seq, iters=6, feat_init=feat_init, return_feat=True)
preds = outs[0]
vis = outs[2] # B, S, 1
feat_init = outs[3]
vis = torch.sigmoid(vis) # visibility confidence
xys = preds[-1].reshape(1, 8, 2)
traj_e[:,cur_frame:end_frame] = xys[:,:S_local]
found_skip = False
thr = 0.9
si_last = 8-1 # last frame we are willing to take
si_earliest = 1 # earliest frame we are willing to take
si = si_last
while not found_skip:
if vis[0,si] > thr:
found_skip = True
else:
si -= 1
if si == si_earliest:
# print('decreasing thresh')
thr -= 0.02
si = si_last
# print('found skip at frame %d, where we have' % si, vis[0,si].detach().item())
cur_frame = cur_frame + si
if cur_frame >= S:
done = True
trajs_e[:,:,n] = traj_e
pad = 50
rgbs = F.pad(rgbs.reshape(B*S, 3, H, W), (pad, pad, pad, pad), 'constant', 0).reshape(B, S, 3, H+pad*2, W+pad*2)
trajs_e = trajs_e + pad
prep_rgbs = utils.improc.preprocess_color(rgbs)
gray_rgbs = torch.mean(prep_rgbs, dim=2, keepdim=True).repeat(1, 1, 3, 1, 1)
if sw is not None and sw.save_this:
linewidth = 2
for n in range(N):
# print('visualizing kp %d' % n)
kp_vis = sw.summ_traj2ds_on_rgbs('video_%d/kp_%d_trajs_e_on_rgbs' % (sw.global_step, n), trajs_e[0:1,:,n:n+1], gray_rgbs[0:1,:S], cmap='spring', linewidth=linewidth)
# write to disk, in case that's more convenient
kp_list = list(kp_vis.unbind(1))
kp_list = [kp[0].permute(1,2,0).cpu().numpy() for kp in kp_list]
kp_list = [Image.fromarray(kp) for kp in kp_list]
out_fn = './chain_out_%d.gif' % sw.global_step
kp_list[0].save(out_fn, save_all=True, append_images=kp_list[1:])
print('saved %s' % out_fn)
sw.summ_traj2ds_on_rgb('outputs/trajs_e_on_rgb', trajs_e[0:1], prep_rgbs[0:1,0], cmap='spring')
sw.summ_traj2ds_on_rgb('outputs/trajs_e_on_rgb2', trajs_e[0:1], torch.mean(prep_rgbs[0:1], dim=1), cmap='spring')
return trajs_e-pad
def main():
# the idea in this file is to chain together pips from a long sequence, and return some visualizations
exp_name = '00' # (exp_name is used for logging notes that correspond to different runs)
init_dir = 'reference_model'
## choose hyps
B = 1
S = 50
N = 1 # number of points to track
filenames = glob.glob('./demo_images/*.jpg')
filenames = sorted(filenames)
print('filenames', filenames)
max_iters = len(filenames)//(S//2)-1 # run slightly overlapping subseqs
log_freq = 1 # when to produce visualizations
## autogen a name
model_name = "%02d_%d_%d" % (B, S, N)
model_name += "_%s" % exp_name
import datetime
model_date = datetime.datetime.now().strftime('%H:%M:%S')
model_name = model_name + '_' + model_date
print('model_name', model_name)
log_dir = 'logs_chain_demo'
writer_t = SummaryWriter(log_dir + '/' + model_name + '/t', max_queue=10, flush_secs=60)
global_step = 0
model = Pips(stride=4).cuda()
parameters = list(model.parameters())
if init_dir:
_ = saverloader.load(init_dir, model)
global_step = 0
model.eval()
while global_step < max_iters:
read_start_time = time.time()
global_step += 1
sw_t = utils.improc.Summ_writer(
writer=writer_t,
global_step=global_step,
log_freq=log_freq,
fps=12,
scalar_freq=int(log_freq/2),
just_gif=True)
try:
rgbs = []
for s in range(S):
fn = filenames[(global_step-1)*S//2+s]
if s==0:
print('start frame', fn)
im = imageio.imread(fn)
im = im.astype(np.uint8)
rgbs.append(torch.from_numpy(im).permute(2,0,1))
rgbs = torch.stack(rgbs, dim=0).unsqueeze(0) # 1, S, C, H, W
read_time = time.time()-read_start_time
iter_start_time = time.time()
with torch.no_grad():
trajs_e = run_model(model, rgbs, N, sw_t)
iter_time = time.time()-iter_start_time
print('%s; step %06d/%d; rtime %.2f; itime %.2f' % (
model_name, global_step, max_iters, read_time, iter_time))
except FileNotFoundError as e:
print('error', e)
writer_t.close()
if __name__ == '__main__':
main()