-
Notifications
You must be signed in to change notification settings - Fork 356
Expand file tree
/
Copy pathsamurai.py
More file actions
1499 lines (1321 loc) · 59.2 KB
/
samurai.py
File metadata and controls
1499 lines (1321 loc) · 59.2 KB
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
import os
import sys
from collections import OrderedDict
from logging import getLogger
import cv2
import numpy as np
from tqdm import tqdm
from PIL import Image
import ailia
# import original modules
sys.path.append("../../util")
from arg_utils import get_base_parser, update_parser, get_savepath # noqa
from image_utils import normalize_image
from model_utils import check_and_download_models # noqa
from kalman_filter import KalmanFilter
logger = getLogger(__name__)
# ======================
# Parameters
# ======================
WEIGHT_BACKBONE_PATH = "backbone.onnx"
WEIGHT_SAM_PATH = "sam_heads.onnx"
WEIGHT_ENC_PATH = "memory_encoder.onnx"
WEIGHT_ATN_PATH = "memory_attention.onnx"
MODEL_BACKBONE_PATH = "backbone.onnx.prototxt"
MODEL_SAM_PATH = "sam_heads.onnx.prototxt"
MODEL_ENC_PATH = "memory_encoder.onnx.prototxt"
MODEL_ATN_PATH = "memory_attention.onnx.prototxt"
REMOTE_PATH = "https://storage.googleapis.com/ailia-models/samurai/"
VIDEO_PATH = "demo.mp4"
SAVE_VIDEO_PATH = "output.mp4"
IMAGE_SIZE = 1024
# a large negative value as a placeholder score for missing objects
NO_OBJ_SCORE = -1024.0
this_dir = os.path.dirname(os.path.abspath(__file__))
weight_dir = os.path.join(this_dir, "weights")
# ======================
# Arguemnt Parser Config
# ======================
parser = get_base_parser("SAMURAI", VIDEO_PATH, SAVE_VIDEO_PATH)
parser.add_argument(
"--txt_path",
type=str,
default="bbox.txt",
help="the .txt file contains a single line with the bounding box of the first frame in x,y,w,h format",
)
parser.add_argument("--onnx", action="store_true", help="execute onnxruntime version.")
args = update_parser(parser)
# ======================
# Secondary Functions
# ======================
def load_txt(gt_path):
with open(gt_path, "r") as f:
gt = f.readlines()
prompts = {}
for fid, line in enumerate(gt):
x, y, w, h = map(float, line.split(","))
x, y, w, h = int(x), int(y), int(w), int(h)
prompts[fid] = ((x, y, x + w, y + h), 0)
return prompts
def load_video_frames(input_path):
if isinstance(input_path, list):
loaded_frames = [cv2.imread(frame_path) for frame_path in input_path]
height, width = loaded_frames[0].shape[:2]
else:
cap = cv2.VideoCapture(input_path)
frame_rate = cap.get(cv2.CAP_PROP_FPS)
loaded_frames = []
while True:
ret, frame = cap.read()
if not ret:
break
loaded_frames.append(frame)
cap.release()
height, width = loaded_frames[0].shape[:2]
if len(loaded_frames) == 0:
raise ValueError("No frames were loaded from the video.")
return loaded_frames, height, width
def preprocess_image(img):
img = img[:, :, ::-1] # BGR -> RGB
img = np.array(Image.fromarray(img).resize((IMAGE_SIZE, IMAGE_SIZE)))
img = normalize_image(img, normalize_type="ImageNet")
img = img.transpose(2, 0, 1) # HWC -> CHW
img = np.expand_dims(img, axis=0)
img = img.astype(np.float32)
return img
def select_closest_cond_frames(frame_idx, cond_frame_outputs, max_cond_frame_num):
"""
Select up to `max_cond_frame_num` conditioning frames from `cond_frame_outputs`
that are temporally closest to the current frame at `frame_idx`.
"""
if max_cond_frame_num == -1 or len(cond_frame_outputs) <= max_cond_frame_num:
selected_outputs = cond_frame_outputs
unselected_outputs = {}
else:
selected_outputs = {}
# the closest conditioning frame before `frame_idx` (if any)
idx_before = max((t for t in cond_frame_outputs if t < frame_idx), default=None)
if idx_before is not None:
selected_outputs[idx_before] = cond_frame_outputs[idx_before]
# the closest conditioning frame after `frame_idx` (if any)
idx_after = min((t for t in cond_frame_outputs if t >= frame_idx), default=None)
if idx_after is not None:
selected_outputs[idx_after] = cond_frame_outputs[idx_after]
# add other temporally closest conditioning frames until reaching a total
# of `max_cond_frame_num` conditioning frames.
num_remain = max_cond_frame_num - len(selected_outputs)
inds_remain = sorted(
(t for t in cond_frame_outputs if t not in selected_outputs),
key=lambda x: abs(x - frame_idx),
)[:num_remain]
selected_outputs.update((t, cond_frame_outputs[t]) for t in inds_remain)
unselected_outputs = {
t: v for t, v in cond_frame_outputs.items() if t not in selected_outputs
}
return selected_outputs, unselected_outputs
def get_1d_sine_pe(pos_inds, dim, temperature=10000):
"""
Get 1D sine positional embedding as in the original Transformer paper.
"""
pe_dim = dim // 2
dim_t = np.arange(pe_dim, dtype=np.float32)
dim_t = temperature ** (2 * (dim_t // 2) / pe_dim)
pos_embed = np.expand_dims(pos_inds, axis=-1) / dim_t
pos_embed = np.concatenate([np.sin(pos_embed), np.cos(pos_embed)], axis=-1)
return pos_embed
# ======================
# Main functions
# ======================
class SAM2VideoPredictor:
"""The predictor class to handle user interactions and manage inference states."""
def __init__(
self, backbone, sam_heads, memory_encoder, memory_attn, flg_onnx=False
):
self.backbone = backbone
self.sam_heads = sam_heads
self.memory_encoder = memory_encoder
self.memory_attn = memory_attn
self.flg_onnx = flg_onnx
self.num_feature_levels = 3
self.hidden_dim = 256
self.maskmem_tpos_enc = np.load(
os.path.join(weight_dir, "maskmem_tpos_enc.npy")
)
self.no_mem_embed = np.load(os.path.join(weight_dir, "no_mem_embed.npy"))
self.obj_ptr_tpos_proj_weight = np.load(
os.path.join(weight_dir, "obj_ptr_tpos_proj_weight.npy")
)
self.obj_ptr_tpos_proj_bias = np.load(
os.path.join(weight_dir, "obj_ptr_tpos_proj_bias.npy")
)
# Init Kalman Filter
self.kf = KalmanFilter()
self.kf_mean = None
self.kf_covariance = None
self.stable_frames = 0
self.stable_frames_threshold = 15
### SAM2Base
def _forward_sam_heads(
self,
backbone_features,
point_inputs,
mask_inputs,
high_res_features,
multimask_output,
):
B = backbone_features.shape[0]
if point_inputs is not None:
sam_point_coords = point_inputs["point_coords"]
sam_point_labels = point_inputs["point_labels"]
else:
sam_point_coords = np.zeros((B, 1, 2), dtype=np.float32)
sam_point_labels = -np.ones((B, 1), dtype=np.int64)
if mask_inputs is not None:
sam_mask_prompt = np.zeros((B, 1, 256, 256), dtype=np.float32)
for i in len(mask_inputs):
x = cv2.resize(
mask_inputs[i].squeeze(0),
dsize=(256, 256),
interpolation=cv2.INTER_LINEAR,
)
sam_mask_prompt[i] = x[np.newaxis, :, :]
else:
sam_mask_prompt = np.zeros((0, 1, 256, 256), dtype=np.float32)
# feedforward
if not self.flg_onnx:
output = self.sam_heads.predict(
[
backbone_features,
sam_point_coords,
sam_point_labels,
sam_mask_prompt,
high_res_features[0],
high_res_features[1],
np.array(multimask_output, dtype=np.int64),
]
)
else:
output = self.sam_heads.run(
None,
{
"backbone_features": backbone_features,
"point_coords": sam_point_coords,
"point_labels": sam_point_labels,
"mask_inputs": sam_mask_prompt,
"high_res_features_0": high_res_features[0],
"high_res_features_1": high_res_features[1],
"multimask_output": np.array(multimask_output, dtype=np.int64),
},
)
(
low_res_multimasks,
high_res_multimasks,
ious,
obj_ptrs,
object_score_logits,
) = output
obj_ptr = obj_ptrs[:, 0]
kf_ious = None
if multimask_output:
if (
self.kf_mean is None
and self.kf_covariance is None
or self.stable_frames == 0
):
best_iou_inds = np.argmax(ious, axis=-1)
batch_inds = np.arange(B)
low_res_masks = np.expand_dims(
low_res_multimasks[batch_inds, best_iou_inds], axis=1
)
high_res_masks = np.expand_dims(
high_res_multimasks[batch_inds, best_iou_inds], axis=1
)
non_zero_indices = np.argwhere(high_res_masks[0, 0] > 0.0)
if len(non_zero_indices) == 0:
high_res_bbox = [0, 0, 0, 0]
else:
y_min, x_min = non_zero_indices.min(axis=0)
y_max, x_max = non_zero_indices.max(axis=0)
high_res_bbox = [x_min, y_min, x_max, y_max]
self.kf_mean, self.kf_covariance = self.kf.initiate(
self.kf.xyxy_to_xyah(high_res_bbox)
)
if obj_ptrs.shape[1] > 1:
obj_ptr = obj_ptrs[batch_inds, best_iou_inds]
self.stable_frames += 1
elif self.stable_frames < self.stable_frames_threshold:
self.kf_mean, self.kf_covariance = self.kf.predict(
self.kf_mean, self.kf_covariance
)
best_iou_inds = np.argmax(ious, axis=-1)
batch_inds = np.arange(B)
low_res_masks = np.expand_dims(
low_res_multimasks[batch_inds, best_iou_inds], axis=1
)
high_res_masks = np.expand_dims(
high_res_multimasks[batch_inds, best_iou_inds], axis=1
)
non_zero_indices = np.argwhere(high_res_masks[0][0] > 0.0)
if len(non_zero_indices) == 0:
high_res_bbox = [0, 0, 0, 0]
else:
y_min, x_min = non_zero_indices.min(axis=0)
y_max, x_max = non_zero_indices.max(axis=0)
high_res_bbox = [x_min, y_min, x_max, y_max]
stable_ious_threshold = 0.3
if ious[0][best_iou_inds] > stable_ious_threshold:
self.kf_mean, self.kf_covariance = self.kf.update(
self.kf_mean,
self.kf_covariance,
self.kf.xyxy_to_xyah(high_res_bbox),
)
self.stable_frames += 1
else:
self.stable_frames = 0
if obj_ptrs.shape[1] > 1:
obj_ptr = obj_ptrs[batch_inds, best_iou_inds]
else:
self.kf_mean, self.kf_covariance = self.kf.predict(
self.kf_mean, self.kf_covariance
)
high_res_multibboxes = []
batch_inds = np.arange(B)
for i in range(ious.shape[1]):
non_zero_indices = np.argwhere(
np.expand_dims(high_res_multimasks[batch_inds, i], axis=1)[0][0]
> 0.0
)
if len(non_zero_indices) == 0:
high_res_multibboxes.append([0, 0, 0, 0])
else:
y_min, x_min = non_zero_indices.min(axis=0)
y_max, x_max = non_zero_indices.max(axis=0)
high_res_multibboxes.append([x_min, y_min, x_max, y_max])
# compute the IoU between the predicted bbox and the high_res_multibboxes
kf_ious = np.array(
self.kf.compute_iou(self.kf_mean[:4], high_res_multibboxes)
)
# weighted iou
kf_score_weight = 0.25
weighted_ious = kf_score_weight * kf_ious + (1 - kf_score_weight) * ious
best_iou_inds = np.argmax(weighted_ious, axis=-1)
batch_inds = np.arange(B)
low_res_masks = np.expand_dims(
low_res_multimasks[batch_inds, best_iou_inds], axis=1
)
high_res_masks = np.expand_dims(
high_res_multimasks[batch_inds, best_iou_inds], axis=1
)
if obj_ptrs.shape[1] > 1:
obj_ptr = obj_ptrs[batch_inds, best_iou_inds]
stable_ious_threshold = 0.3
if ious[0][best_iou_inds] < stable_ious_threshold:
self.stable_frames = 0
else:
self.kf_mean, self.kf_covariance = self.kf.update(
self.kf_mean,
self.kf_covariance,
self.kf.xyxy_to_xyah(
high_res_multibboxes[best_iou_inds.item()]
),
)
else:
best_iou_inds = 0
low_res_masks, high_res_masks = low_res_multimasks, high_res_multimasks
return (
low_res_multimasks,
high_res_multimasks,
ious,
low_res_masks,
high_res_masks,
obj_ptr,
object_score_logits,
ious[0][best_iou_inds],
kf_ious[best_iou_inds] if kf_ious is not None else None,
)
def _prepare_backbone_features(self, backbone_out):
"""Prepare and flatten visual features."""
backbone_out = backbone_out.copy()
feature_maps = backbone_out["backbone_fpn"][-self.num_feature_levels :]
vision_pos_embeds = backbone_out["vision_pos_enc"][-self.num_feature_levels :]
feat_sizes = [(x.shape[-2], x.shape[-1]) for x in vision_pos_embeds]
# flatten NxCxHxW to HWxNxC
vision_feats = [
x.reshape(x.shape[0], x.shape[1], -1).transpose(2, 0, 1)
for x in feature_maps
]
vision_pos_embeds = [
x.reshape(x.shape[0], x.shape[1], -1).transpose(2, 0, 1)
for x in vision_pos_embeds
]
return backbone_out, vision_feats, vision_pos_embeds, feat_sizes
def _prepare_memory_conditioned_features(
self,
frame_idx,
is_init_cond_frame,
current_vision_feats,
current_vision_pos_embeds,
feat_sizes,
output_dict,
num_frames,
track_in_reverse=False, # tracking in reverse time order (for demo usage)
):
"""Fuse the current frame's visual feature map with previous memory."""
B = current_vision_feats[-1].shape[1] # batch size on this frame
C = self.hidden_dim
H, W = feat_sizes[-1] # top-level (lowest-resolution) feature size
num_obj_ptr_tokens = 0
tpos_sign_mul = -1 if track_in_reverse else 1
# Step 1: condition the visual features of the current frame on previous memories
if not is_init_cond_frame:
# Retrieve the memories encoded with the maskmem backbone
to_cat_memory, to_cat_memory_pos_embed = [], []
# Select a maximum number of temporally closest cond frames for cross attention
cond_outputs = output_dict["cond_frame_outputs"]
max_cond_frames_in_attn = -1
selected_cond_outputs, unselected_cond_outputs = select_closest_cond_frames(
frame_idx, cond_outputs, max_cond_frames_in_attn
)
t_pos_and_prevs = [(0, out) for out in selected_cond_outputs.values()]
valid_indices = []
if frame_idx > 1: # Ensure we have previous frames to evaluate
for i in range(
frame_idx - 1, 1, -1
): # Iterate backwards through previous frames
iou_score = output_dict["non_cond_frame_outputs"][i][
"best_iou_score"
] # Get mask affinity score
obj_score = output_dict["non_cond_frame_outputs"][i][
"object_score_logits"
] # Get object score
kf_score = (
output_dict["non_cond_frame_outputs"][i]["kf_score"]
if "kf_score" in output_dict["non_cond_frame_outputs"][i]
else None
) # Get motion score if available
# Check if the scores meet the criteria for being a valid index
memory_bank_iou_threshold = 0.5
memory_bank_obj_score_threshold = 0.0
memory_bank_kf_score_threshold = 0.0
if (
iou_score > memory_bank_iou_threshold
and obj_score > memory_bank_obj_score_threshold
and (
kf_score is None
or kf_score > memory_bank_kf_score_threshold
)
):
valid_indices.insert(0, i)
# Check the number of valid indices
max_obj_ptrs_in_encoder = 16
if len(valid_indices) >= max_obj_ptrs_in_encoder - 1:
break
if frame_idx - 1 not in valid_indices:
valid_indices.append(frame_idx - 1)
num_maskmem = 7
for t_pos in range(
1, num_maskmem
): # Iterate over the number of mask memories
idx = t_pos - num_maskmem # Calculate the index for valid indices
if idx < -len(valid_indices): # Skip if index is out of bounds
continue
out = output_dict["non_cond_frame_outputs"].get(
valid_indices[idx], None
) # Get output for the valid index
if out is None: # If not found, check unselected outputs
out = unselected_cond_outputs.get(valid_indices[idx], None)
t_pos_and_prevs.append(
(t_pos, out)
) # Append the temporal position and output to the list
for t_pos, prev in t_pos_and_prevs:
if prev is None:
continue # skip padding frames
feats = prev["maskmem_features"]
to_cat_memory.append(
feats.reshape(feats.shape[0], feats.shape[1], -1).transpose(2, 0, 1)
)
# Spatial positional encoding (it might have been offloaded to CPU in eval)
maskmem_enc = prev["maskmem_pos_enc"][-1]
maskmem_enc = maskmem_enc.reshape(
feats.shape[0], feats.shape[1], -1
).transpose(2, 0, 1)
# Temporal positional encoding
maskmem_enc = (
maskmem_enc + self.maskmem_tpos_enc[num_maskmem - t_pos - 1]
)
to_cat_memory_pos_embed.append(maskmem_enc)
# Construct the list of past object pointers
max_obj_ptrs_in_encoder = min(num_frames, 16)
# First add those object pointers from selected conditioning frames
ptr_cond_outputs = {
t: out
for t, out in selected_cond_outputs.items()
if (t >= frame_idx if track_in_reverse else t <= frame_idx)
}
pos_and_ptrs = [
# Temporal pos encoding contains how far away each pointer is from current frame
((frame_idx - t) * tpos_sign_mul, out["obj_ptr"])
for t, out in ptr_cond_outputs.items()
]
# Add up to (max_obj_ptrs_in_encoder - 1) non-conditioning frames before current frame
for t_diff in range(1, max_obj_ptrs_in_encoder):
t = frame_idx + t_diff if track_in_reverse else frame_idx - t_diff
if t < 0 or (num_frames is not None and t >= num_frames):
break
out = output_dict["non_cond_frame_outputs"].get(
t, unselected_cond_outputs.get(t, None)
)
if out is not None:
pos_and_ptrs.append((t_diff, out["obj_ptr"]))
# If we have at least one object pointer, add them to the across attention
mem_dim = 64
if 0 < len(pos_and_ptrs):
pos_list, ptrs_list = zip(*pos_and_ptrs)
# stack object pointers along dim=0 into [ptr_seq_len, B, C] shape
obj_ptrs = np.stack(ptrs_list, axis=0)
# a temporal positional embedding based on how far each object pointer is from
# the current frame (sine embedding normalized by the max pointer num).
t_diff_max = max_obj_ptrs_in_encoder - 1
tpos_dim = C
obj_pos = np.array(pos_list, dtype=np.float32)
obj_pos = get_1d_sine_pe(obj_pos / t_diff_max, dim=tpos_dim)
obj_pos = (
obj_pos @ self.obj_ptr_tpos_proj_weight.T
+ self.obj_ptr_tpos_proj_bias
)
obj_pos = np.broadcast_to(
np.expand_dims(obj_pos, axis=1), (obj_pos.shape[0], B, mem_dim)
)
if mem_dim < C:
# split a pointer into (C // mem_dim) tokens for mem_dim < C
obj_ptrs = obj_ptrs.reshape(-1, B, C // mem_dim, mem_dim)
obj_ptrs = obj_ptrs.transpose(0, 2, 1, 3).reshape(
-1, obj_ptrs.shape[1], obj_ptrs.shape[3]
)
obj_pos = np.repeat(obj_pos, repeats=(C // mem_dim), axis=0)
to_cat_memory.append(obj_ptrs)
to_cat_memory_pos_embed.append(obj_pos)
num_obj_ptr_tokens = obj_ptrs.shape[0]
else:
num_obj_ptr_tokens = 0
else:
# directly add no-mem embedding (instead of using the transformer encoder)
pix_feat_with_mem = current_vision_feats[-1] + self.no_mem_embed
pix_feat_with_mem = pix_feat_with_mem.transpose(1, 2, 0).reshape(B, C, H, W)
return pix_feat_with_mem
# Step 2: Concatenate the memories and forward through the transformer encoder
memory = np.concatenate(to_cat_memory, axis=0)
memory_pos_embed = np.concatenate(to_cat_memory_pos_embed, axis=0)
pix_feat_with_mem = self.run_memory_attention(
curr=current_vision_feats,
curr_pos=current_vision_pos_embeds,
memory=memory,
memory_pos=memory_pos_embed,
num_obj_ptr_tokens=num_obj_ptr_tokens,
)
# reshape the output (HW)BC => BCHW
pix_feat_with_mem = pix_feat_with_mem.transpose(1, 2, 0).reshape(B, C, H, W)
return pix_feat_with_mem
def _encode_new_memory(
self,
current_vision_feats,
feat_sizes,
pred_masks_high_res,
object_score_logits,
is_mask_from_pts,
):
current_vision_feats = current_vision_feats[-1]
feat_sizes = np.array(feat_sizes[-1])
is_mask_from_pts = np.array(is_mask_from_pts, dtype=np.int64)
if not self.flg_onnx:
output = self.memory_encoder.predict(
[
current_vision_feats,
feat_sizes,
pred_masks_high_res,
object_score_logits,
is_mask_from_pts,
]
)
else:
output = self.memory_encoder.run(
None,
{
"current_vision_feats": current_vision_feats,
"feat_sizes": feat_sizes,
"high_res_masks": pred_masks_high_res,
"object_score_logits": object_score_logits,
"is_mask_from_pts": is_mask_from_pts,
},
)
maskmem_features, maskmem_pos_enc = output
return maskmem_features, maskmem_pos_enc
def _track_step(
self,
frame_idx,
is_init_cond_frame,
current_vision_feats,
current_vision_pos_embeds,
feat_sizes,
point_inputs,
output_dict,
num_frames,
track_in_reverse,
prev_sam_mask_logits,
):
current_out = {"point_inputs": point_inputs}
# High-resolution feature maps for the SAM head, reshape (HW)BC => BCHW
high_res_features = [
x.transpose(1, 2, 0).reshape(x.shape[1], x.shape[2], *s)
for x, s in zip(current_vision_feats[:-1], feat_sizes[:-1])
]
# fused the visual feature with previous memory features in the memory bank
pix_feat = self._prepare_memory_conditioned_features(
frame_idx=frame_idx,
is_init_cond_frame=is_init_cond_frame,
current_vision_feats=current_vision_feats[-1:],
current_vision_pos_embeds=current_vision_pos_embeds[-1:],
feat_sizes=feat_sizes[-1:],
output_dict=output_dict,
num_frames=num_frames,
track_in_reverse=track_in_reverse,
)
# apply SAM-style segmentation head
# here we might feed previously predicted low-res SAM mask logits into the SAM mask decoder
mask_inputs = None
if prev_sam_mask_logits is not None:
mask_inputs = prev_sam_mask_logits
multimask_output = self._use_multimask(point_inputs)
sam_outputs = self._forward_sam_heads(
backbone_features=pix_feat,
point_inputs=point_inputs,
mask_inputs=mask_inputs,
high_res_features=high_res_features,
multimask_output=multimask_output,
)
return current_out, sam_outputs, high_res_features, pix_feat
def _encode_memory_in_output(
self,
current_vision_feats,
feat_sizes,
point_inputs,
run_mem_encoder,
high_res_masks,
object_score_logits,
current_out,
):
if run_mem_encoder:
high_res_masks_for_mem_enc = high_res_masks
maskmem_features, maskmem_pos_enc = self._encode_new_memory(
current_vision_feats=current_vision_feats,
feat_sizes=feat_sizes,
pred_masks_high_res=high_res_masks_for_mem_enc,
object_score_logits=object_score_logits,
is_mask_from_pts=(point_inputs is not None),
)
current_out["maskmem_features"] = maskmem_features
current_out["maskmem_pos_enc"] = [maskmem_pos_enc]
else:
current_out["maskmem_features"] = None
current_out["maskmem_pos_enc"] = None
return current_out
def track_step(
self,
frame_idx,
is_init_cond_frame,
current_vision_feats,
current_vision_pos_embeds,
feat_sizes,
point_inputs,
output_dict,
num_frames,
track_in_reverse=False, # tracking in reverse time order (for demo usage)
run_mem_encoder=True,
prev_sam_mask_logits=None,
):
current_out, sam_outputs, _, _ = self._track_step(
frame_idx,
is_init_cond_frame,
current_vision_feats,
current_vision_pos_embeds,
feat_sizes,
point_inputs,
output_dict,
num_frames,
track_in_reverse,
prev_sam_mask_logits,
)
(
_,
_,
_,
low_res_masks,
high_res_masks,
obj_ptr,
object_score_logits,
best_iou_score,
kf_ious,
) = sam_outputs
current_out["pred_masks"] = low_res_masks
current_out["pred_masks_high_res"] = high_res_masks
current_out["obj_ptr"] = obj_ptr
current_out["best_iou_score"] = best_iou_score
current_out["kf_ious"] = kf_ious
current_out["object_score_logits"] = object_score_logits
# Finally run the memory encoder on the predicted mask to encode
# it into a new memory feature (that can be used in future frames)
current_out = self._encode_memory_in_output(
current_vision_feats,
feat_sizes,
point_inputs,
run_mem_encoder,
high_res_masks,
object_score_logits,
current_out,
)
return current_out
def _use_multimask(self, point_inputs):
"""Whether to use multimask output in the SAM head."""
num_pts = 0 if point_inputs is None else point_inputs["point_labels"].shape[1]
multimask_output = 0 <= num_pts <= 1
return multimask_output
### VideoPredictor
def init_state(self, images, video_height, video_width):
self.images = images
self.num_frames = len(images)
self.video_height = video_height
self.video_width = video_width
# inputs on each frame
self.point_inputs_per_obj = {}
self.mask_inputs_per_obj = {}
# visual features on a small number of recently visited frames for quick interactions
self.cached_features = {}
# values that don't change across frames (so we only need to hold one copy of them)
self.constants = {}
# mapping between client-side object id and model-side object index
self.obj_id_to_idx = OrderedDict()
self.obj_idx_to_id = OrderedDict()
self.obj_ids = []
# A storage to hold the model's tracking results and states on each frame
self.output_dict = {
"cond_frame_outputs": {}, # dict containing {frame_idx: <out>}
"non_cond_frame_outputs": {}, # dict containing {frame_idx: <out>}
}
# Slice (view) of each object tracking results, sharing the same memory with "output_dict"
self.output_dict_per_obj = {}
# A temporary storage to hold new outputs when user interact with a frame
# to add clicks or mask (it's merged into "output_dict" before propagation starts)
self.temp_output_dict_per_obj = {}
# Frames that already holds consolidated outputs from click or mask inputs
# (we directly use their consolidated outputs during tracking)
self.consolidated_frame_inds = {
"cond_frame_outputs": set(), # set containing frame indices
"non_cond_frame_outputs": set(), # set containing frame indices
}
# metadata for each tracking frame (e.g. which direction it's tracked)
self.tracking_has_started = False
self.frames_already_tracked = {}
def _obj_id_to_idx(self, obj_id):
"""Map client-side object id to model-side object index."""
obj_idx = self.obj_id_to_idx.get(obj_id, None)
if obj_idx is not None:
return obj_idx
# This is a new object id not sent to the server before. We only allow adding
# new objects *before* the tracking starts.
if not self.tracking_has_started:
# get the next object slot
obj_idx = len(self.obj_id_to_idx)
self.obj_id_to_idx[obj_id] = obj_idx
self.obj_idx_to_id[obj_idx] = obj_id
self.obj_ids = list(self.obj_id_to_idx)
# set up input and output structures for this object
self.point_inputs_per_obj[obj_idx] = {}
self.mask_inputs_per_obj[obj_idx] = {}
self.output_dict_per_obj[obj_idx] = {
"cond_frame_outputs": {}, # dict containing {frame_idx: <out>}
"non_cond_frame_outputs": {}, # dict containing {frame_idx: <out>}
}
self.temp_output_dict_per_obj[obj_idx] = {
"cond_frame_outputs": {}, # dict containing {frame_idx: <out>}
"non_cond_frame_outputs": {}, # dict containing {frame_idx: <out>}
}
return obj_idx
else:
raise RuntimeError(
f"Cannot add new object id {obj_id} after tracking starts. "
f"All existing object ids: {self.obj_ids}. "
f"Please call 'reset_state' to restart from scratch."
)
def get_obj_num(self):
return len(self.obj_idx_to_id)
def add_new_points_or_box(
self,
frame_idx,
obj_id,
points=None,
labels=None,
clear_old_points=True,
box=None,
):
"""Add new points to a frame."""
obj_idx = self._obj_id_to_idx(obj_id)
point_inputs_per_frame = self.point_inputs_per_obj[obj_idx]
mask_inputs_per_frame = self.mask_inputs_per_obj[obj_idx]
if points is None:
points = np.zeros((0, 2), dtype=np.float32)
else:
points = np.array(points, dtype=np.float32)
if labels is None:
labels = np.zeros(0, dtype=np.int64)
else:
labels = np.array(labels, dtype=np.int32)
if points.ndim == 2:
points = np.expand_dims(points, axis=0) # add batch dimension
if labels.ndim == 1:
labels = np.expand_dims(labels, axis=0) # add batch dimension
# If `box` is provided, we add it as the first two points with labels 2 and 3
# along with the user-provided points (consistent with how SAM 2 is trained).
if box is not None:
if not clear_old_points:
raise ValueError(
"cannot add box without clearing old points, since "
"box prompt must be provided before any point prompt "
"(please use clear_old_points=True instead)"
)
if not isinstance(box, np.ndarray):
box = np.array(box, dtype=np.float32)
box_coords = box.reshape(1, 2, 2)
box_labels = np.array([2, 3], dtype=np.int64)
box_labels = box_labels.reshape(1, 2)
points = np.concatenate([box_coords, points], axis=1)
labels = np.concatenate([box_labels, labels], axis=1)
points = points / np.array(
[self.video_width, self.video_height], dtype=np.float32
)
# scale the (normalized) coordinates by the model's internal image size
points = points * IMAGE_SIZE
if not clear_old_points:
point_inputs = point_inputs_per_frame.get(frame_idx, None)
else:
point_inputs = None
if point_inputs is not None:
points = np.concatenate([point_inputs["point_coords"], points], axis=1)
labels = np.concatenate([point_inputs["point_labels"], labels], axis=1)
point_inputs = {"point_coords": points, "point_labels": labels}
point_inputs_per_frame[frame_idx] = point_inputs
mask_inputs_per_frame.pop(frame_idx, None)
is_init_cond_frame = frame_idx not in self.frames_already_tracked
# whether to track in reverse time order
if is_init_cond_frame:
reverse = False
else:
reverse = self.frames_already_tracked[frame_idx]["reverse"]
obj_output_dict = self.output_dict_per_obj[obj_idx]
obj_temp_output_dict = self.temp_output_dict_per_obj[obj_idx]
# Add a frame to conditioning output if it's an initial conditioning frame or
# if the model sees all frames receiving clicks/mask as conditioning frames.
is_cond = is_init_cond_frame
storage_key = "cond_frame_outputs" if is_cond else "non_cond_frame_outputs"
# Get any previously predicted mask logits on this object and feed it along with
# the new clicks into the SAM mask decoder.
prev_sam_mask_logits = None
# lookup temporary output dict first, which contains the most recent output
# (if not found, then lookup conditioning and non-conditioning frame output)
prev_out = obj_temp_output_dict[storage_key].get(frame_idx)
if prev_out is None:
prev_out = obj_output_dict["cond_frame_outputs"].get(frame_idx)
if prev_out is None:
prev_out = obj_output_dict["non_cond_frame_outputs"].get(frame_idx)
if prev_out is not None and prev_out["pred_masks"] is not None:
prev_sam_mask_logits = prev_out["pred_masks"]
# Clamp the scale of prev_sam_mask_logits to avoid rare numerical issues.
prev_sam_mask_logits = np.clip(prev_sam_mask_logits, -32.0, 32.0)
current_out, _ = self.single_frame_inference(
output_dict=obj_output_dict, # run on the slice of a single object
frame_idx=frame_idx,
batch_size=1, # run on the slice of a single object
is_init_cond_frame=is_init_cond_frame,
point_inputs=point_inputs,
reverse=reverse,
# Skip the memory encoder when adding clicks or mask.
run_mem_encoder=False,
prev_sam_mask_logits=prev_sam_mask_logits,
)
# Add the output to the output dict (to be used as future memory)
obj_temp_output_dict[storage_key][frame_idx] = current_out
# Resize the output mask to the original video resolution
obj_ids = self.obj_ids
consolidated_out = self.consolidate_temp_output_across_obj(
frame_idx,
is_cond=is_cond,
run_mem_encoder=False,
consolidate_at_video_res=True,
)
_, video_res_masks = self.get_orig_video_res_output(
consolidated_out["pred_masks_video_res"]
)
return frame_idx, obj_ids, video_res_masks
def get_orig_video_res_output(self, any_res_masks):
"""
Resize the object scores to the original video resolution (video_res_masks)
and apply non-overlapping constraints for final output.
"""
video_H = self.video_height
video_W = self.video_width
if any_res_masks.shape[-2:] == (video_H, video_W):
video_res_masks = any_res_masks
else:
video_res_masks = cv2.resize(
any_res_masks.squeeze(0).squeeze(0),
dsize=(video_W, video_H),
interpolation=cv2.INTER_LINEAR,
)
video_res_masks = video_res_masks[np.newaxis, np.newaxis, :, :]
return any_res_masks, video_res_masks
def consolidate_temp_output_across_obj(
self,
frame_idx,
is_cond,
run_mem_encoder,
consolidate_at_video_res=False,
):
batch_size = self.get_obj_num()
storage_key = "cond_frame_outputs" if is_cond else "non_cond_frame_outputs"
# Optionally, we allow consolidating the temporary outputs at the original
# video resolution (to provide a better editing experience for mask prompts).
if consolidate_at_video_res:
consolidated_H = self.video_height