forked from google-deepmind/tapnet
-
Notifications
You must be signed in to change notification settings - Fork 0
/
tapir_model.py
1051 lines (940 loc) · 35.9 KB
/
tapir_model.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
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
# Copyright 2023 DeepMind Technologies Limited
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
# ==============================================================================
"""TAPIR model definition."""
import functools
from typing import Any, Dict, List, Mapping, Optional, Tuple, NamedTuple, Sequence
import chex
from einshape import jax_einshape as einshape
import haiku as hk
import jax
import jax.numpy as jnp
from tapnet.models import resnet
from tapnet.utils import model_utils
from tapnet.utils import transforms
TRAIN_SIZE = (24, 256, 256, 3) # (num_frames, height, width, channels)
def layernorm(x):
return hk.LayerNorm(axis=-1, create_scale=True, create_offset=False)(x)
def depthwise_conv_residual(
x: chex.Array,
kernel_shape: int = 3,
use_causal_conv: bool = False,
causal_context: Optional[Mapping[str, chex.Array]] = None,
get_causal_context: bool = False,
) -> Tuple[chex.Array, Dict[str, chex.Array]]:
"""First mlp in mixer."""
cur_name = hk.experimental.current_name()
name1 = cur_name + '_causal_1'
if causal_context is not None:
x = jnp.concatenate([causal_context[name1], x], axis=-2)
# Because we're adding extra frames, the output of this convolution will
# also have extra (invalid) frames that need to be removed afterward.
num_extra = causal_context[name1].shape[-2]
new_causal_context = {}
if get_causal_context:
# Keep only as many frames of x as needed for future frames. This may be up
# to (kernel_shape - 1) frames.
new_causal_context[name1] = x[..., -(kernel_shape - 1) :, :]
x = hk.DepthwiseConv1D(
channel_multiplier=4,
kernel_shape=kernel_shape,
data_format='NWC',
stride=1,
padding=[(kernel_shape - 1, 0)] if use_causal_conv else 'SAME',
name='mlp1_up',
)(x)
x = jax.nn.gelu(x)
name2 = cur_name + '_causal_2'
if causal_context is not None:
x = jnp.concatenate([causal_context[name2], x[..., num_extra:, :]], axis=-2)
num_extra = causal_context[name2].shape[-2]
if get_causal_context:
new_causal_context[name2] = x[..., -(kernel_shape - 1) :, :]
x = hk.DepthwiseConv1D(
channel_multiplier=1,
kernel_shape=kernel_shape,
data_format='NWC',
stride=1,
padding=[(kernel_shape - 1, 0)] if use_causal_conv else 'SAME',
name='mlp1_up',
)(x)
if causal_context is not None:
x = x[..., num_extra:, :]
return (
x[..., 0::4] + x[..., 1::4] + x[..., 2::4] + x[..., 3::4],
new_causal_context,
)
def conv_channels_mixer(x):
"""Second mlp in mixer."""
in_channels = x.shape[-1]
x = hk.Linear(in_channels * 4, name='mlp2_up')(x)
x = jax.nn.gelu(x)
x = hk.Linear(in_channels, name='mlp2_down')(x)
return x
class PIPsConvBlock(hk.Module):
"""Transformer block (mha and ffw)."""
def __init__(self, name='block', kernel_shape=3, use_causal_conv=False):
super().__init__(name=name)
self.kernel_shape = kernel_shape
self.use_causal_conv = use_causal_conv
def __call__(self, x, causal_context=None, get_causal_context=False):
to_skip = x
x = layernorm(x)
x, new_causal_context = depthwise_conv_residual(
x,
self.kernel_shape,
self.use_causal_conv,
causal_context,
get_causal_context,
)
x = x + to_skip
to_skip = x
x = layernorm(x)
x = conv_channels_mixer(x)
x = x + to_skip
return x, new_causal_context
class PIPSMLPMixer(hk.Module):
"""Depthwise-conv version of PIPs's MLP Mixer."""
def __init__(
self,
output_channels,
hidden_dim=512,
num_blocks=12,
kernel_shape=3,
use_causal_conv=False,
name='pips_mlp_mixer',
):
super().__init__(name=name)
self._output_channels = output_channels
self.hidden_dim = hidden_dim
self._num_blocks = num_blocks
self.kernel_shape = kernel_shape
self.use_causal_conv = use_causal_conv
def __call__(self, x, causal_context=None, get_causal_context=False):
x = hk.Linear(self.hidden_dim)(x)
all_causal_context = {}
for _ in range(self._num_blocks):
x, new_causal_context = PIPsConvBlock(
kernel_shape=self.kernel_shape, use_causal_conv=self.use_causal_conv
)(x, causal_context, get_causal_context)
if get_causal_context:
all_causal_context.update(new_causal_context)
x = layernorm(x)
return hk.Linear(self._output_channels)(x), all_causal_context
def construct_patch_kernel(pos, grid_size, patch_size=7):
"""A conv kernel that performs bilinear interpolation for a point."""
# pos is n-by-2, [x,y]
# grid_size is [heigh,width]
# result is [1,n,kernel_width,kernel_height]
pos = pos + (patch_size) / 2 - 1
def gen_bump(pos, num):
# pos is shape [n]
# result is shape [n,num]
res = jnp.arange(num)
return jnp.maximum(
0, 1 - jnp.abs(res[jnp.newaxis, :] - pos[:, jnp.newaxis])
)
x_bump = gen_bump(pos[:, 0], grid_size[1] - patch_size + 1)
y_bump = gen_bump(pos[:, 1], grid_size[0] - patch_size + 1)
# because it's a conv we need to reverse the spatial axis
kernel = (
x_bump[:, jnp.newaxis, :, jnp.newaxis]
* y_bump[:, jnp.newaxis, jnp.newaxis, :]
)
return kernel
def extract_patch_depthwise_conv(pos, corrs, patch_size=7):
"""Use a depthwise conv to extract a patch via bilinear interpolation."""
# pos is n-by-2, [x,y], raster coordinates
# arr is [num_points, height, width]
# result is [num_points, height, width]
# add an extra batch axis because conv needs it
corrs = jnp.pad(
corrs,
(
(0, 0),
(patch_size - 1, patch_size - 1),
(patch_size - 1, patch_size - 1),
),
)[jnp.newaxis, ...]
kernel = construct_patch_kernel(pos, corrs.shape[2:4], patch_size)
dim_nums = jax.lax.ConvDimensionNumbers(
lhs_spec=(0, 1, 2, 3), rhs_spec=(0, 1, 2, 3), out_spec=(0, 1, 2, 3)
)
# the [0] gets rid of the extra batch axis
res = jax.lax.conv_general_dilated(
corrs,
kernel,
(1, 1),
'VALID',
(1, 1),
(1, 1),
dim_nums,
feature_group_count=kernel.shape[0],
)[0]
return res
def is_same_res(r1, r2):
"""Test if two image resolutions are the same."""
return all([x == y for x, y in zip(r1, r2)])
class FeatureGrids(NamedTuple):
"""Feature grids for a video, used to compute trajectories.
These are per-frame outputs of the encoding resnet.
Attributes:
lowres: Low-resolution features, one for each resolution; 256 channels.
hires: High-resolution features, one for each resolution; 64 channels.
resolutions: Resolutions used for trajectory computation. There will be one
entry for the initialization, and then an entry for each PIPs refinement
resolution.
"""
lowres: Sequence[chex.Array]
hires: Sequence[chex.Array]
resolutions: Sequence[Tuple[int, int]]
class QueryFeatures(NamedTuple):
"""Query features used to compute trajectories.
These are sampled from the query frames and are a full descriptor of the
tracked points. They can be acquired from a query image and then reused in a
separate video.
Attributes:
lowres: Low-resolution features, one for each resolution; each has shape
[batch, num_query_points, 256]
hires: High-resolution features, one for each resolution; each has shape
[batch, num_query_points, 64]
resolutions: Resolutions used for trajectory computation. There will be one
entry for the initialization, and then an entry for each PIPs refinement
resolution.
"""
lowres: Sequence[chex.Array]
hires: Sequence[chex.Array]
resolutions: Sequence[Tuple[int, int]]
class TAPIR(hk.Module):
"""TAPIR model."""
def __init__(
self,
bilinear_interp_with_depthwise_conv: bool = False,
num_pips_iter: int = 4,
pyramid_level: int = 1,
mixer_hidden_dim: int = 512,
num_mixer_blocks: int = 12,
mixer_kernel_shape: int = 3,
patch_size: int = 7,
softmax_temperature: float = 20.0,
use_causal_conv: bool = False,
parallelize_query_extraction: bool = False,
name: str = 'tapir',
):
super().__init__(name=name)
self.highres_dim = 128
self.lowres_dim = 256
self.resnet = resnet.ResNet(
resnet_v2=True,
normalization='instancenorm',
strides=(1, 2, 2, 1),
blocks_per_group=(2, 2, 2, 2),
channels_per_group=(64, self.highres_dim, 256, self.lowres_dim),
use_projection=(True, True, True, True),
use_max_pool=False,
name='resnet',
)
self.cost_volume_track_mods = {
'hid1': hk.Conv2D(
16,
[3, 3],
name='cost_volume_regression_1',
stride=[1, 1],
),
'hid2': hk.Conv2D(
1,
[3, 3],
name='cost_volume_regression_2',
stride=[1, 1],
),
'hid3': hk.Conv2D(
32,
[3, 3],
name='cost_volume_occlusion_1',
stride=[2, 2],
),
'hid4': hk.Linear(16, name='cost_volume_occlusion_2'),
'occ_out': hk.Linear(2, name='occlusion_out'),
'regression_hid': hk.Linear(128, name='regression_hid'),
'regression_out': hk.Linear(2, name='regression_out'),
'conv_stats_conv1': hk.Conv2D(
256,
[5, 5],
name='conv_stats_conv1',
stride=[1, 1],
),
'conv_stats_conv2': hk.Conv2D(
256,
[3, 3],
name='conv_stats_conv2',
stride=[1, 1],
),
'conv_stats_linear': hk.Linear(32, name='conv_stats_linear'),
}
self.pips_mixer = PIPSMLPMixer(
4 + self.highres_dim + self.lowres_dim,
hidden_dim=mixer_hidden_dim,
num_blocks=num_mixer_blocks,
kernel_shape=mixer_kernel_shape,
use_causal_conv=use_causal_conv,
)
self.bilinear_interp_with_depthwise_conv = (
bilinear_interp_with_depthwise_conv
)
self.parallelize_query_extraction = parallelize_query_extraction
self.num_pips_iter = num_pips_iter
self.pyramid_level = pyramid_level
self.patch_size = patch_size
self.softmax_temperature = softmax_temperature
def tracks_from_cost_volume(
self,
interp_feature: chex.Array,
feature_grid: chex.Array,
query_points: Optional[chex.Array],
im_shp: Optional[chex.Shape] = None,
) -> Tuple[chex.Array, chex.Array, chex.Array]:
"""Converts features into tracks by computing a cost volume.
The computed cost volume will have shape
[batch, num_queries, time, height, width], which can be very
memory intensive.
Args:
interp_feature: A tensor of features for each query point, of shape
[batch, num_queries, channels, heads].
feature_grid: A tensor of features for the video, of shape [batch, time,
height, width, channels, heads].
query_points: When computing tracks, we assume these points are given as
ground truth and we reproduce them exactly. This is a set of points of
shape [batch, num_points, 3], where each entry is [t, y, x] in frame/
raster coordinates.
im_shp: The shape of the original image, i.e., [batch, num_frames, time,
height, width, 3].
Returns:
A 2-tuple of the inferred points (of shape
[batch, num_points, num_frames, 2] where each point is [x, y]) and
inferred occlusion (of shape [batch, num_points, num_frames], where
each is a logit where higher means occluded)
"""
mods = self.cost_volume_track_mods
# Note: time is first axis to prevent the TPU from padding
cost_volume = jnp.einsum(
'bnc,bthwc->tbnhw',
interp_feature,
feature_grid,
)
shape = cost_volume.shape
batch_size, num_points = cost_volume.shape[1:3]
cost_volume = einshape('tbnhw->(tbn)hw1', cost_volume)
occlusion = mods['hid1'](cost_volume)
occlusion = jax.nn.relu(occlusion)
pos = mods['hid2'](occlusion)
pos_rshp = einshape('(tb)hw1->t(b)hw1', pos, t=shape[0])
pos = einshape('t(bn)hw1->bnthw', pos_rshp, b=batch_size, n=num_points)
pos = jax.nn.softmax(pos * self.softmax_temperature, axis=(-2, -1))
points = model_utils.heatmaps_to_points(
pos, im_shp, query_points=query_points
)
occlusion = mods['hid3'](occlusion)
occlusion = jax.nn.relu(occlusion)
occlusion = jnp.mean(occlusion, axis=(-2, -3))
occlusion = mods['hid4'](occlusion)
occlusion = jax.nn.relu(occlusion)
occlusion = mods['occ_out'](occlusion)
expected_dist = einshape(
'(tbn)1->bnt', occlusion[..., 1:2], n=shape[2], t=shape[0]
)
occlusion = einshape(
'(tbn)1->bnt', occlusion[..., 0:1], n=shape[2], t=shape[0]
)
return points, occlusion, expected_dist
def refine_pips(
self,
target_feature,
frame_features,
pyramid,
pos_guess,
occ_guess,
expd_guess,
orig_hw,
last_iter=None,
mixer_iter=0.0,
resize_hw=None,
causal_context=None,
get_causal_context=False,
):
# frame_features is batch, num_frames, height, width, channels
# target_features is batch, num_points, channels
# pos_guess is batch, num_points, num_frames,2
orig_h, orig_w = orig_hw
resized_h, resized_w = resize_hw
corrs_pyr = []
assert len(target_feature) == len(pyramid)
for pyridx, (query, grid) in enumerate(zip(target_feature, pyramid)):
# note: interp needs [y,x]
coords = transforms.convert_grid_coordinates(
pos_guess, (orig_w, orig_h), grid.shape[-2:-4:-1]
)[..., ::-1]
last_iter_query = None
if last_iter is not None:
if pyridx == 0:
last_iter_query = last_iter[..., : self.highres_dim]
else:
last_iter_query = last_iter[..., self.highres_dim :]
if not self.bilinear_interp_with_depthwise_conv:
# on CPU, gathers are cheap and matmuls are expensive
ctxx, ctxy = jnp.meshgrid(jnp.arange(-3, 4), jnp.arange(-3, 4))
ctx = jnp.stack([ctxy, ctxx], axis=-1)
ctx = jnp.reshape(ctx, [-1, 2])
coords2 = (
coords[:, :, :, jnp.newaxis, :]
+ ctx[jnp.newaxis, jnp.newaxis, jnp.newaxis, ...]
)
# grid is batch, frames, height, width, channels
# coords is batch, num_points, frames, spatial, x/y
# neighborhood = batch, num_points, frames, patch_height, patch_width,
# channels
neighborhood = jax.vmap( # across batch
jax.vmap( # across frames
jax.vmap( # across patch context size
jax.vmap( # across channels
functools.partial(model_utils.interp, mode='constant'),
in_axes=(-1, None),
out_axes=-1,
),
in_axes=(None, -2),
out_axes=-2,
),
in_axes=(0, 1),
out_axes=1,
)
)(grid, coords2)
# s is spatial context size
if last_iter_query is None:
patches = jnp.einsum('bnfsc,bnc->bnfs', neighborhood, query)
else:
patches = jnp.einsum(
'bnfsc,bnfc->bnfs', neighborhood, last_iter_query
)
else:
# on TPU, matmul is cheap and gather is expensive, so we rewrite
# the interpolation with a depthwise conv.
if last_iter_query is None:
corrs = jnp.einsum('bfhwc,bnc->bnfhw', grid, query)
else:
corrs = jnp.einsum('bfhwc,bnfc->bnfhw', grid, last_iter_query)
n = corrs.shape[1]
# coords is bnfs2
# patches is batch,n,frames,height,width
# vmap across batch dimension (because we have different points across
# the batch) and across the frame axis (this could potentially be rolled
# into the depthwise conv)
extract_patch_depthwise_conv_ = functools.partial(
extract_patch_depthwise_conv, patch_size=self.patch_size
)
patches = jax.vmap(extract_patch_depthwise_conv_)(
einshape('bnfc->b(nf)c', coords), einshape('bnfhw->b(nf)hw', corrs)
)
patches = einshape('b(nf)hw->bnf(hw)', patches, n=n)
corrs_pyr.append(patches)
corrs_pyr = jnp.concatenate(corrs_pyr, axis=-1)
corrs_chunked = corrs_pyr
pos_guess_input = pos_guess
occ_guess_input = occ_guess[..., jnp.newaxis]
expd_guess_input = expd_guess[..., jnp.newaxis]
# mlp_input is batch, num_points, num_chunks, frames_per_chunk, channels
if last_iter is None:
both_feature = jnp.concatenate(
[target_feature[0], target_feature[1]], axis=-1
)
mlp_input_features = jnp.tile(
both_feature[:, :, jnp.newaxis, :],
(1, 1) + corrs_chunked.shape[-2:-1] + (1,),
)
else:
mlp_input_features = last_iter
pos_guess_input = jnp.zeros_like(pos_guess_input)
mlp_input = jnp.concatenate(
[
pos_guess_input,
occ_guess_input,
expd_guess_input,
mlp_input_features,
corrs_chunked,
],
axis=-1,
)
x = einshape('bnfc->(bn)fc', mlp_input)
if causal_context is not None:
causal_context = jax.tree_map(
lambda x: einshape('bn...->(bn)...', x), causal_context
)
res, new_causal_context = self.pips_mixer(
x, causal_context, get_causal_context
)
res = einshape('(bn)fc->bnfc', res, b=mlp_input.shape[0])
if get_causal_context:
new_causal_context = jax.tree_map(
lambda x: einshape('(bn)...->bn...', x, b=mlp_input.shape[0]),
new_causal_context,
)
pos_update = transforms.convert_grid_coordinates(
res[..., :2],
(resized_w, resized_h),
(orig_w, orig_h),
)
return (
pos_update + pos_guess,
res[..., 2] + occ_guess,
res[..., 3] + expd_guess,
res[..., 4:] + (mlp_input_features if last_iter is None else last_iter),
new_causal_context,
)
def get_feature_grids(
self,
video: chex.Array,
is_training: bool,
refinement_resolutions: Optional[List[Tuple[int, int]]] = None,
) -> FeatureGrids:
"""Computes feature grids.
Args:
video: A 5-D tensor representing a batch of sequences of images.
is_training: Whether we are training.
refinement_resolutions: A list of (height, width) tuples. Refinement will
be repeated at each specified resolution, in order to achieve high
accuracy on resolutions higher than what TAPIR was trained on. If None,
reasonable refinement resolutions will be inferred from the input video
size.
Returns:
A FeatureGrids object which contains the required features for every
required resolution. Note that there will be one more feature grid
than there are refinement_resolutions, because there is always a
feature grid computed for TAP-Net initialization.
"""
if refinement_resolutions is None:
refinement_resolutions = model_utils.generate_default_resolutions(
video.shape[2:4], TRAIN_SIZE[1:3]
)
all_required_resolutions = [(TRAIN_SIZE[1], TRAIN_SIZE[2])]
all_required_resolutions.extend(refinement_resolutions)
feature_grid = []
hires_feats = []
resize_im_shape = []
curr_resolution = (-1, -1)
for resolution in all_required_resolutions:
if resolution[0] % 8 != 0 or resolution[1] % 8 != 0:
raise ValueError('Image resolution must be a multiple of 8.')
if not is_same_res(curr_resolution, resolution):
if is_same_res(curr_resolution, video.shape[-3:-1]):
video_resize = video
else:
video_resize = jax.image.resize(
video, video.shape[0:2] + resolution + (3,), method='bilinear'
)
curr_resolution = resolution
resnet_out = hk.BatchApply(self.resnet)(
video_resize, is_training=is_training
)
latent = resnet_out['resnet_unit_3']
hires = resnet_out['resnet_unit_1']
latent = latent / jnp.sqrt(
jnp.maximum(
jnp.sum(jnp.square(latent), axis=-1, keepdims=True),
1e-12,
)
)
hires = hires / jnp.sqrt(
jnp.maximum(
jnp.sum(jnp.square(hires), axis=-1, keepdims=True),
1e-12,
)
)
feature_grid.append(latent)
hires_feats.append(hires)
resize_im_shape.append(video_resize.shape[2:4])
return FeatureGrids(
tuple(feature_grid), tuple(hires_feats), tuple(resize_im_shape)
)
def get_query_features(
self,
video: chex.Array,
is_training: bool,
query_points: chex.Array,
feature_grids: Optional[FeatureGrids] = None,
refinement_resolutions: Optional[List[Tuple[int, int]]] = None,
) -> QueryFeatures:
"""Computes query features, which can be used for estimate_trajectories.
Args:
video: A 5-D tensor representing a batch of sequences of images.
is_training: Whether we are training.
query_points: The query points for which we compute tracks.
feature_grids: If passed, we'll use these feature grids rather than
computing new ones.
refinement_resolutions: A list of (height, width) tuples. Refinement will
be repeated at each specified resolution, in order to achieve high
accuracy on resolutions higher than what TAPIR was trained on. If None,
reasonable refinement resolutions will be inferred from the input video
size.
Returns:
A QueryFeatures object which contains the required features for every
required resolution.
"""
if feature_grids is None:
feature_grids = self.get_feature_grids(
video,
is_training=is_training,
refinement_resolutions=refinement_resolutions,
)
feature_grid = feature_grids.lowres
hires_feats = feature_grids.hires
resize_im_shape = feature_grids.resolutions
shape = video.shape
# shape is [batch_size, time, height, width, channels]; conversion needs
# [time, width, height]
curr_resolution = (-1, -1)
query_feats = []
hires_query_feats = []
for i, resolution in enumerate(resize_im_shape):
if is_same_res(curr_resolution, resolution):
query_feats.append(query_feats[-1])
hires_query_feats.append(hires_query_feats[-1])
continue
position_in_grid = transforms.convert_grid_coordinates(
query_points,
shape[1:4],
feature_grid[i].shape[1:4],
coordinate_format='tyx',
)
position_in_grid_hires = transforms.convert_grid_coordinates(
query_points,
shape[1:4],
hires_feats[i].shape[1:4],
coordinate_format='tyx',
)
if self.parallelize_query_extraction:
# Extracting query features involves gathering features across frames;
# a naive implementation will cause the model to do an all gather of
# the full video feature tensor, which consumes lots of memory.
# Therefore, we instead perform the x,y gather for every point on every
# single frame, and then mask out the gathers on the incorrect frames.
# This could be made more efficient by gathering exactly one query
# feature per device (rather than per frame).
#
# interp_features is now [batch, time, num_points, features]
interp_features = jax.vmap(
jax.vmap(
jax.vmap(
model_utils.interp,
in_axes=(2, None),
out_axes=-1,
),
in_axes=(0, None),
)
)(feature_grid[i], position_in_grid[..., 1:])
# is_correct_frame is [batch, time, num_points]
frame_id = jnp.array(
jnp.round(position_in_grid[:, jnp.newaxis, :, 0]), jnp.int32
)
is_correct_frame = jax.nn.one_hot(
frame_id, feature_grid[i].shape[1], axis=1
)
interp_features = jnp.sum(
interp_features * is_correct_frame[..., jnp.newaxis], axis=1
)
hires_interp = jax.vmap(
jax.vmap(
jax.vmap(
model_utils.interp,
in_axes=(2, None),
out_axes=-1,
),
in_axes=(0, None),
)
)(hires_feats[i], position_in_grid_hires[..., 1:])
hires_interp = jnp.sum(
hires_interp * is_correct_frame[..., jnp.newaxis], axis=1
)
else:
interp_features = jax.vmap(
jax.vmap(
model_utils.interp,
in_axes=(3, None),
out_axes=1,
)
)(feature_grid[i], position_in_grid)
hires_interp = jax.vmap(
jax.vmap(
model_utils.interp,
in_axes=(3, None),
out_axes=1,
)
)(hires_feats[i], position_in_grid_hires)
hires_query_feats.append(hires_interp)
query_feats.append(interp_features)
return QueryFeatures(
tuple(query_feats), tuple(hires_query_feats), tuple(resize_im_shape)
)
def estimate_trajectories(
self,
video_size: Tuple[int, int],
is_training: bool,
feature_grids: FeatureGrids,
query_features: QueryFeatures,
query_points_in_video: Optional[chex.Array],
query_chunk_size: Optional[int] = None,
causal_context: Optional[Sequence[Mapping[str, chex.Array]]] = None,
get_causal_context: bool = False,
) -> Mapping[str, Any]:
"""Estimates trajectories given features for a video and query features.
Args:
video_size: A 2-tuple containing the original [height, width] of the
video. Predictions will be scaled with respect to this resolution.
is_training: Whether we are training.
feature_grids: a FeatureGrids object computed for the given video.
query_features: a QueryFeatures object computed for the query points.
query_points_in_video: If provided, assume that the query points come from
the same video as feature_grids, and therefore constrain the resulting
trajectories to (approximately) pass through them.
query_chunk_size: When computing cost volumes, break the queries into
chunks of this size to save memory.
causal_context: if running online, this will be features computed for each
trajectory on earlier frames. If None, no context is assumed.
get_causal_context: if True, the output dict will also include features
that can be passed as causal_context to future frames when running
online.
Returns:
A dict of outputs, including:
occlusion: Occlusion logits, of shape [batch, num_queries, num_frames]
where higher indicates more likely to be occluded.
tracks: predicted point locations, of shape
[batch, num_queries, num_frames, 2], where each point is [x, y]
in raster coordinates
expected_dist: uncertainty estimate logits, of shape
[batch, num_queries, num_frames], where higher indicates more likely
to be far from the correct answer.
causal_context: (if get_causal_context is True) a pytree which can
be passed as causal_context to subsequent calls if running the model
online. In the current model, it is a list of dicts, one per
PIPs refinement iteration, where for each dict the values are hidden
units from the temporal depthwise conv layers, and the keys are names
derived from Haiku's layer names.
"""
def train2orig(x):
return transforms.convert_grid_coordinates(
x,
TRAIN_SIZE[2:0:-1],
video_size[::-1],
coordinate_format='xy',
)
occ_iters = []
pts_iters = []
expd_iters = []
new_causal_context = []
num_iters = self.num_pips_iter * (len(feature_grids.lowres) - 1)
# This contains both middle step points and final step points.
for _ in range(num_iters + 1):
occ_iters.append([])
pts_iters.append([])
expd_iters.append([])
new_causal_context.append([])
del new_causal_context[-1]
infer = functools.partial(
self.tracks_from_cost_volume,
im_shp=feature_grids.lowres[0].shape[0:2] + TRAIN_SIZE[1:],
)
num_queries = query_features.lowres[0].shape[1]
# Note: the permutation is required in order to randomize which tracks
# get the stop_gradient.
if causal_context is None:
perm = jax.random.permutation(hk.next_rng_key(), num_queries)
else:
if is_training:
# Need to handle permutation if we want to train with causal context.
raise ValueError('Training with causal context is not supported.')
perm = jnp.arange(num_queries, dtype=jnp.int32)
inv_perm = jnp.zeros_like(perm)
inv_perm = inv_perm.at[perm].set(jnp.arange(num_queries))
for ch in range(0, num_queries, query_chunk_size):
perm_chunk = perm[ch : ch + query_chunk_size]
chunk = query_features.lowres[0][:, perm_chunk]
if causal_context is not None:
cc_chunk = jax.tree_map(lambda x: x[:, perm_chunk], causal_context) # pylint: disable=cell-var-from-loop
if query_points_in_video is not None:
infer_query_points = query_points_in_video[
:, perm[ch : ch + query_chunk_size]
]
num_frames = feature_grids.lowres[0].shape[1]
infer_query_points = transforms.convert_grid_coordinates(
infer_query_points,
(num_frames,) + video_size,
(num_frames,) + TRAIN_SIZE[1:-1],
coordinate_format='tyx',
)
else:
infer_query_points = None
points, occlusion, expected_dist = infer(
chunk,
feature_grids.lowres[0],
infer_query_points,
)
pts_iters[0].append(train2orig(points))
occ_iters[0].append(occlusion)
expd_iters[0].append(expected_dist)
mixer_feats = None
for i in range(num_iters):
feature_level = i // self.num_pips_iter + 1
queries = [
query_features.hires[feature_level][:, perm_chunk],
query_features.lowres[feature_level][:, perm_chunk],
]
for _ in range(self.pyramid_level):
queries.append(queries[-1])
pyramid = [
feature_grids.hires[feature_level],
feature_grids.lowres[feature_level],
]
for _ in range(self.pyramid_level):
pyramid.append(
hk.avg_pool(
pyramid[-1], [1, 1, 2, 2, 1], [1, 1, 2, 2, 1], 'VALID'
)
)
# Note: even when the pyramids are higher resolution, the points are
# all scaled according to the original resolution. This is because
# the raw points are input into the model, and need to be scaled that
# way.
#
# TODO(doersch): this should constrain the output to match the query
# points.
cc = cc_chunk[i] if causal_context is not None else None
refined = self.refine_pips(
queries,
None,
pyramid,
points,
occlusion,
expected_dist,
orig_hw=TRAIN_SIZE[1:3],
last_iter=mixer_feats,
mixer_iter=i,
resize_hw=feature_grids.resolutions[feature_level],
causal_context=cc,
get_causal_context=get_causal_context,
)
if ch > 0:
refined = jax.lax.stop_gradient(refined)
points = refined[0]
occlusion = refined[1]
expected_dist = refined[2]
mixer_feats = refined[3]
new_causal_context[i].append(refined[4])
pts_iters[i + 1].append(train2orig(points))
occ_iters[i + 1].append(occlusion)
expd_iters[i + 1].append(expected_dist)
if (i + 1) % self.num_pips_iter == 0:
mixer_feats = None
expected_dist = expd_iters[0][-1]
occlusion = occ_iters[0][-1]
occlusion = []
points = []
expd = []
for i in range(len(occ_iters)):
occlusion.append(jnp.concatenate(occ_iters[i], axis=1)[:, inv_perm])
points.append(jnp.concatenate(pts_iters[i], axis=1)[:, inv_perm])
expd.append(jnp.concatenate(expd_iters[i], axis=1)[:, inv_perm])
for i in range(len(new_causal_context)):
new_causal_context[i] = jax.tree_map(
lambda *x: jnp.concatenate(x, axis=1)[:, inv_perm],
*new_causal_context[i],
)
out = dict(
occlusion=occlusion,
tracks=points,
expected_dist=expd,
)
if get_causal_context:
out['causal_context'] = new_causal_context
return out
def __call__(
self,
video: chex.Array,
is_training: bool,
query_points: chex.Array,
query_chunk_size: Optional[int] = None,
get_query_feats: bool = False,
refinement_resolutions: Optional[List[Tuple[int, int]]] = None,
) -> Mapping[str, chex.Array]:
"""Runs a forward pass of the model.
Args:
video: A 5-D tensor representing a batch of sequences of images.
is_training: Whether we are training.
query_points: The query points for which we compute tracks.
query_chunk_size: When computing cost volumes, break the queries into
chunks of this size to save memory.
get_query_feats: Return query features for other losses like contrastive.
Not supported in the current version.
refinement_resolutions: A list of (height, width) tuples. Refinement will
be repeated at each specified resolution, in order to achieve high
accuracy on resolutions higher than what TAPIR was trained on. If None,
reasonable refinement resolutions will be inferred from the input video
size.
Returns:
A dict of outputs, including:
occlusion: Occlusion logits, of shape [batch, num_queries, num_frames]
where higher indicates more likely to be occluded.