-
-
Notifications
You must be signed in to change notification settings - Fork 108
/
samv2.py
1343 lines (1333 loc) · 84.4 KB
/
samv2.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
'''
Function:
Implementation of SAMV2
Author:
Zhenchao Jin
'''
import os
import torch
import numpy as np
import torch.nn as nn
import torch.nn.functional as F
import torch.utils.model_zoo as model_zoo
from tqdm import tqdm
from PIL.Image import Image
from ..base import BaseSegmentor
from ...backbones.hiera import MLP
from collections import OrderedDict
from .maskdecoder import MaskDecoder
from ...backbones import BuildBackbone
from .transforms import SAMV2Transforms
from .memoryencoder import MemoryEncoder
from .promptencoder import PromptEncoder
from .transformer import TwoWayTransformer
from .memoryattention import MemoryAttention
from torchvision.ops.boxes import batched_nms, box_area
from .misc import selectclosestcondframes, get1dsinepe, loadvideoframes, fillholesinmaskscores, concatpoints
from .amg import (
buildalllayerpointgrids, areafromrle, batchiterator, batchedmasktobox, boxxyxytoxywh, calculatestabilityscore, cocoencoderle, generatecropboxes, isboxnearcropedge,
masktorlepytorch, MaskData, removesmallregions, rletomask, uncropboxesxyxy, uncropmasks, uncroppoints
)
'''a large negative value as a placeholder score for missing objects'''
NO_OBJ_SCORE = -1024.0
'''SAMV2'''
class SAMV2(BaseSegmentor):
def __init__(self, cfg, mode):
backbone = cfg.pop('backbone')
super(SAMV2, self).__init__(cfg=cfg, mode=mode)
cfg['backbone'] = backbone
assert mode in ['TEST'], f'only support TEST mode for {self.__class__.__name__}'
# Part 1: the image backbone
self.image_encoder = BuildBackbone(cfg['backbone'])
# use level 0, 1, 2 for high-res setting, or just level 2 for the default setting
self.use_high_res_features_in_sam = cfg['head']['use_high_res_features_in_sam']
self.num_feature_levels = 3 if cfg['head']['use_high_res_features_in_sam'] else 1
self.use_obj_ptrs_in_encoder = cfg['head']['use_obj_ptrs_in_encoder']
self.max_obj_ptrs_in_encoder = cfg['head']['max_obj_ptrs_in_encoder']
if cfg['head']['use_obj_ptrs_in_encoder']:
self.mask_downsample = nn.Conv2d(1, 1, kernel_size=4, stride=4)
self.add_tpos_enc_to_obj_ptrs = cfg['head']['add_tpos_enc_to_obj_ptrs']
if cfg['head']['proj_tpos_enc_in_obj_ptrs']:
assert cfg['head']['add_tpos_enc_to_obj_ptrs']
self.proj_tpos_enc_in_obj_ptrs = cfg['head']['proj_tpos_enc_in_obj_ptrs']
self.only_obj_ptrs_in_the_past_for_eval = cfg['head']['only_obj_ptrs_in_the_past_for_eval']
# Part 2: memory attention to condition current frame's visual features with memories (and obj ptrs) from past frames
self.memory_attention = MemoryAttention(**cfg['head']['memory_attention_cfg'])
self.hidden_dim = self.memory_attention.d_model
# Part 3: memory encoder for the previous frame's outputs
self.memory_encoder = MemoryEncoder(**cfg['head']['memory_encoder_cfg'])
self.mem_dim = self.hidden_dim
# if there is compression of memories along channel dim
if hasattr(self.memory_encoder, "out_proj") and hasattr(self.memory_encoder.out_proj, "weight"):
self.mem_dim = self.memory_encoder.out_proj.weight.shape[0]
# number of memories accessible
self.num_maskmem = cfg['head']['num_maskmem']
# temporal encoding of the memories
self.maskmem_tpos_enc = nn.Parameter(torch.zeros(cfg['head']['num_maskmem'], 1, 1, self.mem_dim))
nn.init.trunc_normal_(self.maskmem_tpos_enc, std=0.02)
# a single token to indicate no memory embedding from previous frames
self.no_mem_embed = nn.Parameter(torch.zeros(1, 1, self.hidden_dim))
self.no_mem_pos_enc = nn.Parameter(torch.zeros(1, 1, self.hidden_dim))
nn.init.trunc_normal_(self.no_mem_embed, std=0.02)
nn.init.trunc_normal_(self.no_mem_pos_enc, std=0.02)
self.directly_add_no_mem_embed = cfg['head']['directly_add_no_mem_embed']
# apply sigmoid to the output raw mask logits (to turn them from range (-inf, +inf) to range (0, 1)) before feeding them into the memory encoder
self.sigmoid_scale_for_mem_enc = cfg['head']['sigmoid_scale_for_mem_enc']
self.sigmoid_bias_for_mem_enc = cfg['head']['sigmoid_bias_for_mem_enc']
self.binarize_mask_from_pts_for_mem_enc = cfg['head']['binarize_mask_from_pts_for_mem_enc']
self.non_overlap_masks_for_mem_enc = cfg['head']['non_overlap_masks_for_mem_enc']
self.memory_temporal_stride_for_eval = cfg['head']['memory_temporal_stride_for_eval']
# on frames with mask input, whether to directly output the input mask without using a SAM prompt encoder + mask decoder
self.use_mask_input_as_output_without_sam = cfg['head']['use_mask_input_as_output_without_sam']
self.multimask_output_in_sam = cfg['head']['multimask_output_in_sam']
self.multimask_min_pt_num = cfg['head']['multimask_min_pt_num']
self.multimask_max_pt_num = cfg['head']['multimask_max_pt_num']
self.multimask_output_for_tracking = cfg['head']['multimask_output_for_tracking']
self.use_multimask_token_for_obj_ptr = cfg['head']['use_multimask_token_for_obj_ptr']
self.iou_prediction_use_sigmoid = cfg['head']['iou_prediction_use_sigmoid']
# Part 4: SAM-style prompt encoder (for both mask and point inputs) and SAM-style mask decoder for the final mask output
self.image_size = cfg['head']['image_size']
self.backbone_stride = cfg['head']['backbone_stride']
self.sam_mask_decoder_extra_args = cfg['head']['sam_mask_decoder_extra_args']
self.pred_obj_scores = cfg['head']['pred_obj_scores']
self.pred_obj_scores_mlp = cfg['head']['pred_obj_scores_mlp']
self.fixed_no_obj_ptr = cfg['head']['fixed_no_obj_ptr']
self.soft_no_obj_ptr = cfg['head']['soft_no_obj_ptr']
if self.fixed_no_obj_ptr:
assert self.pred_obj_scores
assert self.use_obj_ptrs_in_encoder
if self.pred_obj_scores and self.use_obj_ptrs_in_encoder:
self.no_obj_ptr = nn.Parameter(torch.zeros(1, self.hidden_dim))
nn.init.trunc_normal_(self.no_obj_ptr, std=0.02)
self.use_mlp_for_obj_ptr_proj = cfg['head']['use_mlp_for_obj_ptr_proj']
self.buildsamheads()
self.add_all_frames_to_correct_as_cond = cfg['head']['add_all_frames_to_correct_as_cond']
self.max_cond_frames_in_attn = cfg['head']['max_cond_frames_in_attn']
# Model compilation
if cfg['head']['compile_image_encoder']:
print("Image encoder compilation is enabled. First forward pass will be slow.")
self.image_encoder.forward = torch.compile(self.image_encoder.forward, mode="max-autotune", fullgraph=True, dynamic=False)
'''device'''
@property
def device(self):
return next(self.parameters()).device
'''forward'''
def forward(self, data_meta):
raise NotImplementedError(f'train {self.__class__.__name__} not to be implemented')
'''buildsamheads'''
def buildsamheads(self):
self.sam_prompt_embed_dim = self.hidden_dim
self.sam_image_embedding_size = self.image_size // self.backbone_stride
# prompt encoder
self.sam_prompt_encoder = PromptEncoder(
embed_dim=self.sam_prompt_embed_dim, image_embedding_size=(self.sam_image_embedding_size, self.sam_image_embedding_size),
input_image_size=(self.image_size, self.image_size), mask_in_chans=16,
)
# mask decoder
self.sam_mask_decoder = MaskDecoder(
num_multimask_outputs=3, transformer=TwoWayTransformer(depth=2, embedding_dim=self.sam_prompt_embed_dim, mlp_dim=2048, num_heads=8),
transformer_dim=self.sam_prompt_embed_dim, iou_head_depth=3, iou_head_hidden_dim=256, use_high_res_features=self.use_high_res_features_in_sam,
iou_prediction_use_sigmoid=self.iou_prediction_use_sigmoid, pred_obj_scores=self.pred_obj_scores, pred_obj_scores_mlp=self.pred_obj_scores_mlp,
use_multimask_token_for_obj_ptr=self.use_multimask_token_for_obj_ptr, **(self.sam_mask_decoder_extra_args or {}),
)
# use_obj_ptrs_in_encoder
if self.use_obj_ptrs_in_encoder:
self.obj_ptr_proj = nn.Linear(self.hidden_dim, self.hidden_dim)
if self.use_mlp_for_obj_ptr_proj:
self.obj_ptr_proj = MLP(self.hidden_dim, self.hidden_dim, self.hidden_dim, 3)
else:
self.obj_ptr_proj = nn.Identity()
# proj_tpos_enc_in_obj_ptrs
if self.proj_tpos_enc_in_obj_ptrs:
self.obj_ptr_tpos_proj = nn.Linear(self.hidden_dim, self.mem_dim)
else:
self.obj_ptr_tpos_proj = nn.Identity()
'''forwardsamheads'''
def forwardsamheads(self, backbone_features, point_inputs=None, mask_inputs=None, high_res_features=None, multimask_output=False):
# prepare and assert
B = backbone_features.size(0)
device = backbone_features.device
assert backbone_features.size(1) == self.sam_prompt_embed_dim
assert backbone_features.size(2) == self.sam_image_embedding_size
assert backbone_features.size(3) == self.sam_image_embedding_size
# a) Handle point prompts
if point_inputs is not None:
sam_point_coords = point_inputs["point_coords"]
sam_point_labels = point_inputs["point_labels"]
assert sam_point_coords.size(0) == B and sam_point_labels.size(0) == B
else:
sam_point_coords = torch.zeros(B, 1, 2, device=device)
sam_point_labels = -torch.ones(B, 1, dtype=torch.int32, device=device)
# b) Handle mask prompts
if mask_inputs is not None:
assert len(mask_inputs.shape) == 4 and mask_inputs.shape[:2] == (B, 1)
if mask_inputs.shape[-2:] != self.sam_prompt_encoder.mask_input_size:
sam_mask_prompt = F.interpolate(mask_inputs.float(), size=self.sam_prompt_encoder.mask_input_size, align_corners=False, mode="bilinear", antialias=True)
else:
sam_mask_prompt = mask_inputs
else:
sam_mask_prompt = None
sparse_embeddings, dense_embeddings = self.sam_prompt_encoder(points=(sam_point_coords, sam_point_labels), boxes=None, masks=sam_mask_prompt)
low_res_multimasks, ious, sam_output_tokens, object_score_logits = self.sam_mask_decoder(
image_embeddings=backbone_features, image_pe=self.sam_prompt_encoder.getdensepe(), sparse_prompt_embeddings=sparse_embeddings,
dense_prompt_embeddings=dense_embeddings, multimask_output=multimask_output, repeat_image=False, high_res_features=high_res_features,
)
if self.pred_obj_scores:
is_obj_appearing = object_score_logits > 0
low_res_multimasks = torch.where(is_obj_appearing[:, None, None], low_res_multimasks, NO_OBJ_SCORE)
# convert masks from possibly bfloat16 (or float16) to float32 (older PyTorch versions before 2.1 don't support `interpolate` on bf16)
low_res_multimasks = low_res_multimasks.float()
high_res_multimasks = F.interpolate(low_res_multimasks, size=(self.image_size, self.image_size), mode="bilinear", align_corners=False)
sam_output_token = sam_output_tokens[:, 0]
# multimask_output
if multimask_output:
best_iou_inds = torch.argmax(ious, dim=-1)
batch_inds = torch.arange(B, device=device)
low_res_masks = low_res_multimasks[batch_inds, best_iou_inds].unsqueeze(1)
high_res_masks = high_res_multimasks[batch_inds, best_iou_inds].unsqueeze(1)
if sam_output_tokens.size(1) > 1:
sam_output_token = sam_output_tokens[batch_inds, best_iou_inds]
else:
low_res_masks, high_res_masks = low_res_multimasks, high_res_multimasks
# extract object pointer from the SAM output token (with occlusion handling)
obj_ptr = self.obj_ptr_proj(sam_output_token)
if self.pred_obj_scores:
if self.soft_no_obj_ptr:
lambda_is_obj_appearing = object_score_logits.sigmoid()
else:
lambda_is_obj_appearing = is_obj_appearing.float()
if self.fixed_no_obj_ptr:
obj_ptr = lambda_is_obj_appearing * obj_ptr
obj_ptr = obj_ptr + (1 - lambda_is_obj_appearing) * self.no_obj_ptr
# return
return low_res_multimasks, high_res_multimasks, ious, low_res_masks, high_res_masks, obj_ptr, object_score_logits
'''usemaskasoutput'''
def usemaskasoutput(self, backbone_features, high_res_features, mask_inputs):
out_scale, out_bias = 20.0, -10.0
mask_inputs_float = mask_inputs.float()
high_res_masks = mask_inputs_float * out_scale + out_bias
low_res_masks = F.interpolate(high_res_masks, size=(high_res_masks.size(-2) // 4, high_res_masks.size(-1) // 4), align_corners=False, mode="bilinear", antialias=True)
ious = mask_inputs.new_ones(mask_inputs.size(0), 1).float()
if not self.use_obj_ptrs_in_encoder:
obj_ptr = torch.zeros(mask_inputs.size(0), self.hidden_dim, device=mask_inputs.device)
else:
_, _, _, _, _, obj_ptr, _ = self.forwardsamheads(backbone_features=backbone_features, mask_inputs=self.mask_downsample(mask_inputs_float), high_res_features=high_res_features)
is_obj_appearing = torch.any(mask_inputs.flatten(1).float() > 0.0, dim=1)
is_obj_appearing = is_obj_appearing[..., None]
lambda_is_obj_appearing = is_obj_appearing.float()
object_score_logits = out_scale * lambda_is_obj_appearing + out_bias
if self.pred_obj_scores:
if self.fixed_no_obj_ptr:
obj_ptr = lambda_is_obj_appearing * obj_ptr
obj_ptr = obj_ptr + (1 - lambda_is_obj_appearing) * self.no_obj_ptr
return low_res_masks, high_res_masks, ious, low_res_masks, high_res_masks, obj_ptr, object_score_logits
'''forwardimage'''
def forwardimage(self, img_batch):
backbone_out = self.image_encoder(img_batch)
if self.use_high_res_features_in_sam:
backbone_out["backbone_fpn"][0] = self.sam_mask_decoder.conv_s0(backbone_out["backbone_fpn"][0])
backbone_out["backbone_fpn"][1] = self.sam_mask_decoder.conv_s1(backbone_out["backbone_fpn"][1])
return backbone_out
'''preparebackbonefeatures'''
def preparebackbonefeatures(self, backbone_out):
backbone_out = backbone_out.copy()
assert len(backbone_out["backbone_fpn"]) == len(backbone_out["vision_pos_enc"])
assert len(backbone_out["backbone_fpn"]) >= self.num_feature_levels
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]
vision_feats = [x.flatten(2).permute(2, 0, 1) for x in feature_maps]
vision_pos_embeds = [x.flatten(2).permute(2, 0, 1) for x in vision_pos_embeds]
return backbone_out, vision_feats, vision_pos_embeds, feat_sizes
'''preparememoryconditionedfeatures'''
def preparememoryconditionedfeatures(self, frame_idx, is_init_cond_frame, current_vision_feats, current_vision_pos_embeds, feat_sizes, output_dict, num_frames, track_in_reverse=False):
# basic information
B = current_vision_feats[-1].size(1)
C = self.hidden_dim
H, W = feat_sizes[-1]
device = current_vision_feats[-1].device
# the case of `self.num_maskmem == 0` below is primarily used for reproducing SAM on images. in this case, we skip the fusion with any memory.
if self.num_maskmem == 0:
pix_feat = current_vision_feats[-1].permute(1, 2, 0).view(B, C, H, W)
return pix_feat
num_obj_ptr_tokens = 0
# Step 1: condition the visual features of the current frame on previous memories
if not is_init_cond_frame:
to_cat_memory, to_cat_memory_pos_embed = [], []
assert len(output_dict["cond_frame_outputs"]) > 0
cond_outputs = output_dict["cond_frame_outputs"]
selected_cond_outputs, unselected_cond_outputs = selectclosestcondframes(frame_idx, cond_outputs, self.max_cond_frames_in_attn)
t_pos_and_prevs = [(0, out) for out in selected_cond_outputs.values()]
r = self.memory_temporal_stride_for_eval
for t_pos in range(1, self.num_maskmem):
t_rel = self.num_maskmem - t_pos
if t_rel == 1:
if not track_in_reverse:
prev_frame_idx = frame_idx - t_rel
else:
prev_frame_idx = frame_idx + t_rel
else:
if not track_in_reverse:
prev_frame_idx = ((frame_idx - 2) // r) * r
prev_frame_idx = prev_frame_idx - (t_rel - 2) * r
else:
prev_frame_idx = -(-(frame_idx + 2) // r) * r
prev_frame_idx = prev_frame_idx + (t_rel - 2) * r
out = output_dict["non_cond_frame_outputs"].get(prev_frame_idx, None)
if out is None:
out = unselected_cond_outputs.get(prev_frame_idx, None)
t_pos_and_prevs.append((t_pos, out))
for t_pos, prev in t_pos_and_prevs:
if prev is None:
continue
feats = prev["maskmem_features"].cuda(non_blocking=True)
to_cat_memory.append(feats.flatten(2).permute(2, 0, 1))
maskmem_enc = prev["maskmem_pos_enc"][-1].cuda()
maskmem_enc = maskmem_enc.flatten(2).permute(2, 0, 1)
maskmem_enc = maskmem_enc + self.maskmem_tpos_enc[self.num_maskmem - t_pos - 1]
to_cat_memory_pos_embed.append(maskmem_enc)
if self.use_obj_ptrs_in_encoder:
max_obj_ptrs_in_encoder = min(num_frames, self.max_obj_ptrs_in_encoder)
if not self.training and self.only_obj_ptrs_in_the_past_for_eval:
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)}
else:
ptr_cond_outputs = selected_cond_outputs
pos_and_ptrs = [(abs(frame_idx - t), out["obj_ptr"]) for t, out in ptr_cond_outputs.items()]
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 len(pos_and_ptrs) > 0:
pos_list, ptrs_list = zip(*pos_and_ptrs)
obj_ptrs = torch.stack(ptrs_list, dim=0)
if self.add_tpos_enc_to_obj_ptrs:
t_diff_max = max_obj_ptrs_in_encoder - 1
tpos_dim = C if self.proj_tpos_enc_in_obj_ptrs else self.mem_dim
obj_pos = torch.tensor(pos_list, device=device)
obj_pos = get1dsinepe(obj_pos / t_diff_max, dim=tpos_dim)
obj_pos = self.obj_ptr_tpos_proj(obj_pos)
obj_pos = obj_pos.unsqueeze(1).expand(-1, B, self.mem_dim)
else:
obj_pos = obj_ptrs.new_zeros(len(pos_list), B, self.mem_dim)
if self.mem_dim < C:
obj_ptrs = obj_ptrs.reshape(-1, B, C // self.mem_dim, self.mem_dim)
obj_ptrs = obj_ptrs.permute(0, 2, 1, 3).flatten(0, 1)
obj_pos = obj_pos.repeat_interleave(C // self.mem_dim, dim=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:
if self.directly_add_no_mem_embed:
pix_feat_with_mem = current_vision_feats[-1] + self.no_mem_embed
pix_feat_with_mem = pix_feat_with_mem.permute(1, 2, 0).view(B, C, H, W)
return pix_feat_with_mem
to_cat_memory = [self.no_mem_embed.expand(1, B, self.mem_dim)]
to_cat_memory_pos_embed = [self.no_mem_pos_enc.expand(1, B, self.mem_dim)]
# Step 2: Concatenate the memories and forward through the transformer encoder
memory = torch.cat(to_cat_memory, dim=0)
memory_pos_embed = torch.cat(to_cat_memory_pos_embed, dim=0)
pix_feat_with_mem = self.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.permute(1, 2, 0).view(B, C, H, W)
# return
return pix_feat_with_mem
'''encodenewmemory'''
def encodenewmemory(self, current_vision_feats, feat_sizes, pred_masks_high_res, is_mask_from_pts):
B = current_vision_feats[-1].size(1)
C = self.hidden_dim
H, W = feat_sizes[-1]
pix_feat = current_vision_feats[-1].permute(1, 2, 0).view(B, C, H, W)
if self.non_overlap_masks_for_mem_enc and not self.training:
pred_masks_high_res = self.applynonoverlappingconstraints(pred_masks_high_res)
binarize = self.binarize_mask_from_pts_for_mem_enc and is_mask_from_pts
if binarize and not self.training:
mask_for_mem = (pred_masks_high_res > 0).float()
else:
mask_for_mem = torch.sigmoid(pred_masks_high_res)
if self.sigmoid_scale_for_mem_enc != 1.0:
mask_for_mem = mask_for_mem * self.sigmoid_scale_for_mem_enc
if self.sigmoid_bias_for_mem_enc != 0.0:
mask_for_mem = mask_for_mem + self.sigmoid_bias_for_mem_enc
maskmem_out = self.memory_encoder(pix_feat, mask_for_mem, skip_mask_sigmoid=True)
maskmem_features = maskmem_out["vision_features"]
maskmem_pos_enc = maskmem_out["vision_pos_enc"]
return maskmem_features, maskmem_pos_enc
'''trackstep'''
def trackstep(self, frame_idx, is_init_cond_frame, current_vision_feats, current_vision_pos_embeds, feat_sizes, point_inputs, mask_inputs,
output_dict, num_frames, track_in_reverse=False, run_mem_encoder=True, prev_sam_mask_logits=None):
current_out = {"point_inputs": point_inputs, "mask_inputs": mask_inputs}
if len(current_vision_feats) > 1:
high_res_features = [x.permute(1, 2, 0).view(x.size(1), x.size(2), *s) for x, s in zip(current_vision_feats[:-1], feat_sizes[:-1])]
else:
high_res_features = None
if mask_inputs is not None and self.use_mask_input_as_output_without_sam:
pix_feat = current_vision_feats[-1].permute(1, 2, 0)
pix_feat = pix_feat.view(-1, self.hidden_dim, *feat_sizes[-1])
sam_outputs = self.usemaskasoutput(pix_feat, high_res_features, mask_inputs)
else:
pix_feat_with_mem = self.preparememoryconditionedfeatures(
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,
)
if prev_sam_mask_logits is not None:
assert point_inputs is not None and mask_inputs is None
mask_inputs = prev_sam_mask_logits
multimask_output = self.usemultimask(is_init_cond_frame, point_inputs)
sam_outputs = self.forwardsamheads(backbone_features=pix_feat_with_mem, point_inputs=point_inputs, mask_inputs=mask_inputs, high_res_features=high_res_features, multimask_output=multimask_output)
_, _, _, low_res_masks, high_res_masks, obj_ptr, _ = sam_outputs
current_out["pred_masks"] = low_res_masks
current_out["pred_masks_high_res"] = high_res_masks
current_out["obj_ptr"] = obj_ptr
if run_mem_encoder and self.num_maskmem > 0:
high_res_masks_for_mem_enc = high_res_masks
maskmem_features, maskmem_pos_enc = self.encodenewmemory(
current_vision_feats=current_vision_feats, feat_sizes=feat_sizes, pred_masks_high_res=high_res_masks_for_mem_enc, 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
'''usemultimask'''
def usemultimask(self, is_init_cond_frame, point_inputs):
num_pts = 0 if point_inputs is None else point_inputs["point_labels"].size(1)
multimask_output = self.multimask_output_in_sam and (is_init_cond_frame or self.multimask_output_for_tracking) and (self.multimask_min_pt_num <= num_pts <= self.multimask_max_pt_num)
return multimask_output
'''applynonoverlappingconstraints'''
def applynonoverlappingconstraints(self, pred_masks):
batch_size = pred_masks.size(0)
if batch_size == 1:
return pred_masks
device = pred_masks.device
max_obj_inds = torch.argmax(pred_masks, dim=0, keepdim=True)
batch_obj_inds = torch.arange(batch_size, device=device)[:, None, None, None]
keep = max_obj_inds == batch_obj_inds
pred_masks = torch.where(keep, pred_masks, torch.clamp(pred_masks, max=-10.0))
return pred_masks
'''SAMV2ImagePredictor'''
class SAMV2ImagePredictor(nn.Module):
def __init__(self, samv2_cfg=None, use_default_samv2_t=False, use_default_samv2_s=False, use_default_samv2_bplus=False, use_default_samv2_l=False,
device='cuda', load_ckpt_strict=True, mask_threshold=0.0, max_hole_area=0.0, max_sprinkle_area=0.0, apply_postprocessing=True):
super(SAMV2ImagePredictor, self).__init__()
# build sam model
if samv2_cfg is None:
samv2_cfg = {
'backbone': {
'type': 'HieraWithFPN', 'scalp': 1.0,
'hiera_cfg': {
'embed_dim': 144, 'num_heads': 2, 'stages': [2, 6, 36, 4], 'global_att_blocks': [23, 33, 43], 'window_pos_embed_bkg_spatial_size': [7, 7], 'window_spec': [8, 4, 16, 8],
},
'fpn_cfg': {
'd_model': 256, 'backbone_channel_list': [1152, 576, 288, 144], 'fpn_top_down_levels': [2, 3], 'fpn_interp_model': 'nearest',
'position_encoding_cfg': dict(num_pos_feats=256, normalize=True, scale=None, temperature=10000, type='PositionEmbeddingSine'),
},
},
'head': {
'memory_attention_cfg': {
'd_model': 256, 'pos_enc_at_input': True, 'num_layers': 4,
'layer_cfg': {
'act_cfg': {'type': 'ReLU'}, 'dim_feedforward': 2048, 'dropout': 0.1, 'pos_enc_at_attn': False, 'd_model': 256, 'pos_enc_at_cross_attn_keys': True, 'pos_enc_at_cross_attn_queries': False,
'self_attention_cfg': dict(type='RoPEAttention', rope_theta=10000.0, feat_sizes=[32, 32], embedding_dim=256, num_heads=1, downsample_rate=1, dropout=0.1),
'cross_attention_cfg': dict(type='RoPEAttention', rope_theta=10000.0, feat_sizes=[32, 32], rope_k_repeat=True, embedding_dim=256, num_heads=1, downsample_rate=1, dropout=0.1, kv_in_dim=64),
}
},
'memory_encoder_cfg': {
'out_dim': 64, 'position_encoding_cfg': dict(num_pos_feats=64, normalize=True, scale=None, temperature=10000, type='PositionEmbeddingSine'),
'mask_downsampler_cfg': dict(kernel_size=3, stride=2, padding=1), 'fuser_cfg': dict(num_layers=2, layer_cfg=dict(dim=256, kernel_size=7, padding=3, layer_scale_init_value=1e-6, use_dwconv=True)),
},
'num_maskmem': 7, 'image_size': 1024, 'backbone_stride': 16, 'sigmoid_scale_for_mem_enc': 20.0, 'sigmoid_bias_for_mem_enc': -10.0, 'binarize_mask_from_pts_for_mem_enc': False,
'use_mask_input_as_output_without_sam': True, 'max_cond_frames_in_attn': -1, 'directly_add_no_mem_embed': True, 'use_high_res_features_in_sam': True, 'multimask_output_in_sam': True,
'multimask_min_pt_num': 0, 'multimask_max_pt_num': 1, 'multimask_output_for_tracking': True, 'use_multimask_token_for_obj_ptr': True, 'iou_prediction_use_sigmoid': True,
'memory_temporal_stride_for_eval': 1, 'add_all_frames_to_correct_as_cond': False, 'non_overlap_masks_for_mem_enc': False, 'use_obj_ptrs_in_encoder': True, 'max_obj_ptrs_in_encoder': 16,
'add_tpos_enc_to_obj_ptrs': False, 'proj_tpos_enc_in_obj_ptrs': False, 'only_obj_ptrs_in_the_past_for_eval': True, 'pred_obj_scores': True, 'pred_obj_scores_mlp': True, 'fixed_no_obj_ptr': True,
'soft_no_obj_ptr': False, 'use_mlp_for_obj_ptr_proj': True, 'sam_mask_decoder_extra_args': None, 'compile_image_encoder': False,
},
}
if use_default_samv2_l:
assert (not use_default_samv2_t) and (not use_default_samv2_s) and (not use_default_samv2_bplus)
samv2_cfg['ckptpath'] = 'https://dl.fbaipublicfiles.com/segment_anything_2/072824/sam2_hiera_large.pt'
elif use_default_samv2_bplus:
assert (not use_default_samv2_t) and (not use_default_samv2_s) and (not use_default_samv2_l)
samv2_cfg['backbone']['hiera_cfg'] = dict(embed_dim=112, num_heads=2)
samv2_cfg['backbone']['fpn_cfg'] = dict(
position_encoding_cfg=dict(num_pos_feats=256, normalize=True, scale=None, temperature=10000, type='PositionEmbeddingSine'),
d_model=256, backbone_channel_list=[896, 448, 224, 112], fpn_top_down_levels=[2, 3], fpn_interp_model='nearest',
)
samv2_cfg['ckptpath'] = 'https://dl.fbaipublicfiles.com/segment_anything_2/072824/sam2_hiera_base_plus.pt'
elif use_default_samv2_s:
assert (not use_default_samv2_t) and (not use_default_samv2_bplus) and (not use_default_samv2_l)
samv2_cfg['backbone']['hiera_cfg'] = dict(embed_dim=96, num_heads=1, stages=[1, 2, 11, 2], global_att_blocks=[7, 10, 13], window_pos_embed_bkg_spatial_size=[7, 7])
samv2_cfg['backbone']['fpn_cfg'] = dict(
position_encoding_cfg=dict(num_pos_feats=256, normalize=True, scale=None, temperature=10000, type='PositionEmbeddingSine'),
d_model=256, backbone_channel_list=[768, 384, 192, 96], fpn_top_down_levels=[2, 3], fpn_interp_model='nearest',
)
samv2_cfg['ckptpath'] = 'https://dl.fbaipublicfiles.com/segment_anything_2/072824/sam2_hiera_small.pt'
elif use_default_samv2_t:
assert (not use_default_samv2_s) and (not use_default_samv2_bplus) and (not use_default_samv2_l)
samv2_cfg['backbone']['hiera_cfg'] = dict(embed_dim=96, num_heads=1, stages=[1, 2, 7, 2], global_att_blocks=[5, 7, 9], window_pos_embed_bkg_spatial_size=[7, 7])
samv2_cfg['backbone']['fpn_cfg'] = dict(
position_encoding_cfg=dict(num_pos_feats=256, normalize=True, scale=None, temperature=10000, type='PositionEmbeddingSine'),
d_model=256, backbone_channel_list=[768, 384, 192, 96], fpn_top_down_levels=[2, 3], fpn_interp_model='nearest',
)
samv2_cfg['ckptpath'] = 'https://dl.fbaipublicfiles.com/segment_anything_2/072824/sam2_hiera_tiny.pt'
else:
assert (not use_default_samv2_t) and (not use_default_samv2_s) and (not use_default_samv2_bplus) and (not use_default_samv2_l)
self.model = self.buildsamv2(samv2_cfg=samv2_cfg, device=device, apply_postprocessing=apply_postprocessing)
if 'ckptpath' in samv2_cfg and (os.path.exists(samv2_cfg['ckptpath']) or samv2_cfg['ckptpath'].startswith('https')):
if os.path.exists(samv2_cfg['ckptpath']):
with open(samv2_cfg['ckptpath'], 'rb') as fp:
state_dict = torch.load(fp, map_location='cpu')
elif samv2_cfg['ckptpath'].startswith('https'):
state_dict = model_zoo.load_url(samv2_cfg['ckptpath'], map_location='cpu')
else:
raise ValueError('ckptpath %s could not be loaded.' % samv2_cfg['ckptpath'])
self.model.load_state_dict(state_dict['model'], strict=load_ckpt_strict)
# build transforms
self._transforms = SAMV2Transforms(resolution=self.model.image_size, mask_threshold=mask_threshold, max_hole_area=max_hole_area, max_sprinkle_area=max_sprinkle_area)
# predictor state
self._is_image_set = False
self._features = None
self._orig_hw = None
# whether the predictor is set for single image or a batch of images
self._is_batch = False
# predictor config
self.mask_threshold = mask_threshold
# spatial dim for backbone feature maps
self._bb_feat_sizes = [(256, 256), (128, 128), (64, 64)]
'''buildsamv2'''
def buildsamv2(self, samv2_cfg, device, apply_postprocessing=True):
if apply_postprocessing:
samv2_cfg['head']['sam_mask_decoder_extra_args'] = {
'dynamic_multimask_via_stability': True,
'dynamic_multimask_stability_delta': 0.05,
'dynamic_multimask_stability_thresh': 0.98,
}
samv2_model = SAMV2(cfg=samv2_cfg, mode='TEST')
samv2_model.to(device=device)
samv2_model.eval()
return samv2_model
'''setimage'''
@torch.no_grad()
def setimage(self, image):
self.resetpredictor()
# transform the image to the form expected by the model
if isinstance(image, np.ndarray):
assert image.shape[-1] <= 3, 'For numpy array image, we assume (HxWxC) format'
self._orig_hw = [image.shape[:2]]
elif isinstance(image, Image):
w, h = image.size
self._orig_hw = [(h, w)]
else:
raise NotImplementedError("Image format not supported.")
input_image = self._transforms(image)
input_image = input_image[None, ...].to(self.device)
assert len(input_image.shape) == 4 and input_image.shape[1] == 3 and input_image.shape[0] == 1, f"input_image must be of size 1x3xHxW, got {input_image.shape}"
# computing image embeddings for the provided image
backbone_out = self.model.forwardimage(input_image)
_, vision_feats, _, _ = self.model.preparebackbonefeatures(backbone_out)
# add no_mem_embed, which is added to the lowest rest feat. map during training on videos
if self.model.directly_add_no_mem_embed:
vision_feats[-1] = vision_feats[-1] + self.model.no_mem_embed
# construct results
feats = [feat.permute(1, 2, 0).view(1, -1, *feat_size) for feat, feat_size in zip(vision_feats[::-1], self._bb_feat_sizes[::-1])][::-1]
self._features = {"image_embed": feats[-1], "high_res_feats": feats[:-1]}
self._is_image_set = True
'''setimagebatch'''
@torch.no_grad()
def setimagebatch(self, image_list):
# initialize
self.resetpredictor()
assert isinstance(image_list, list)
self._orig_hw = []
for image in image_list:
assert isinstance(image, np.ndarray) and (image.shape[-1] <= 3), "images are expected to be an np.ndarray in RGB format, and of shape HxWxC"
self._orig_hw.append(image.shape[:2])
# transform the image to the form expected by the model
img_batch = self._transforms.forwardbatch(image_list)
img_batch = img_batch.to(self.device)
batch_size = img_batch.shape[0]
assert len(img_batch.shape) == 4 and img_batch.shape[1] == 3, f"img_batch must be of size Bx3xHxW, got {img_batch.shape}"
# computing image embeddings for the provided images
backbone_out = self.model.forwardimage(img_batch)
_, vision_feats, _, _ = self.model.preparebackbonefeatures(backbone_out)
# add no_mem_embed, which is added to the lowest rest feat. map during training on videos
if self.model.directly_add_no_mem_embed:
vision_feats[-1] = vision_feats[-1] + self.model.no_mem_embed
# construct results
feats = [feat.permute(1, 2, 0).view(batch_size, -1, *feat_size) for feat, feat_size in zip(vision_feats[::-1], self._bb_feat_sizes[::-1])][::-1]
self._features = {"image_embed": feats[-1], "high_res_feats": feats[:-1]}
self._is_image_set = True
self._is_batch = True
'''predictbatch'''
def predictbatch(self, point_coords_batch=None, point_labels_batch=None, box_batch=None, mask_input_batch=None, multimask_output=True, return_logits=False, normalize_coords=True):
# assert
assert self._is_batch, "this function should only be used when in batched mode"
assert self._is_image_set, "an image must be set with .setimagebatch(...) before mask prediction."
# iter to predict
num_images = len(self._features["image_embed"])
all_masks, all_ious, all_low_res_masks = [], [], []
for img_idx in range(num_images):
point_coords = point_coords_batch[img_idx] if point_coords_batch is not None else None
point_labels = point_labels_batch[img_idx] if point_labels_batch is not None else None
box = box_batch[img_idx] if box_batch is not None else None
mask_input = mask_input_batch[img_idx] if mask_input_batch is not None else None
mask_input, unnorm_coords, labels, unnorm_box = self.prepprompts(point_coords, point_labels, box, mask_input, normalize_coords, img_idx=img_idx)
masks, iou_predictions, low_res_masks = self.purepredict(unnorm_coords, labels, unnorm_box, mask_input, multimask_output, return_logits=return_logits, img_idx=img_idx)
masks_np = masks.squeeze(0).float().detach().cpu().numpy()
iou_predictions_np = iou_predictions.squeeze(0).float().detach().cpu().numpy()
low_res_masks_np = low_res_masks.squeeze(0).float().detach().cpu().numpy()
all_masks.append(masks_np)
all_ious.append(iou_predictions_np)
all_low_res_masks.append(low_res_masks_np)
# return
return all_masks, all_ious, all_low_res_masks
'''predict'''
def predict(self, point_coords=None, point_labels=None, box=None, mask_input=None, multimask_output=True, return_logits=False, normalize_coords=True):
assert self._is_image_set, "an image must be set with .setimage(...) before mask prediction."
# transform input prompts
mask_input, unnorm_coords, labels, unnorm_box = self.prepprompts(point_coords, point_labels, box, mask_input, normalize_coords)
# predict
masks, iou_predictions, low_res_masks = self.purepredict(unnorm_coords, labels, unnorm_box, mask_input, multimask_output, return_logits=return_logits)
# convert
masks_np = masks.squeeze(0).float().detach().cpu().numpy()
iou_predictions_np = iou_predictions.squeeze(0).float().detach().cpu().numpy()
low_res_masks_np = low_res_masks.squeeze(0).float().detach().cpu().numpy()
# return
return masks_np, iou_predictions_np, low_res_masks_np
'''prepprompts'''
def prepprompts(self, point_coords, point_labels, box, mask_logits, normalize_coords, img_idx=-1):
unnorm_coords, labels, unnorm_box, mask_input = None, None, None, None
# point_coords
if point_coords is not None:
assert point_labels is not None, "point_labels must be supplied if point_coords is supplied."
point_coords = torch.as_tensor(point_coords, dtype=torch.float, device=self.device)
unnorm_coords = self._transforms.transformcoords(point_coords, normalize=normalize_coords, orig_hw=self._orig_hw[img_idx])
labels = torch.as_tensor(point_labels, dtype=torch.int, device=self.device)
if len(unnorm_coords.shape) == 2:
unnorm_coords, labels = unnorm_coords[None, ...], labels[None, ...]
# box
if box is not None:
box = torch.as_tensor(box, dtype=torch.float, device=self.device)
unnorm_box = self._transforms.transformboxes(box, normalize=normalize_coords, orig_hw=self._orig_hw[img_idx])
# mask_logits
if mask_logits is not None:
mask_input = torch.as_tensor(mask_logits, dtype=torch.float, device=self.device)
if len(mask_input.shape) == 3:
mask_input = mask_input[None, :, :, :]
# return
return mask_input, unnorm_coords, labels, unnorm_box
'''purepredict'''
@torch.no_grad()
def purepredict(self, point_coords, point_labels, boxes=None, mask_input=None, multimask_output=True, return_logits=False, img_idx=-1):
assert self._is_image_set, "an image must be set with .setimage(...) before mask prediction."
# concat_points
if point_coords is not None:
concat_points = (point_coords, point_labels)
else:
concat_points = None
# embed prompts
if boxes is not None:
box_coords = boxes.reshape(-1, 2, 2)
box_labels = torch.tensor([[2, 3]], dtype=torch.int, device=boxes.device)
box_labels = box_labels.repeat(boxes.size(0), 1)
if concat_points is not None:
concat_coords = torch.cat([box_coords, concat_points[0]], dim=1)
concat_labels = torch.cat([box_labels, concat_points[1]], dim=1)
concat_points = (concat_coords, concat_labels)
else:
concat_points = (box_coords, box_labels)
sparse_embeddings, dense_embeddings = self.model.sam_prompt_encoder(points=concat_points, boxes=None, masks=mask_input)
# predict masks
batched_mode = concat_points is not None and concat_points[0].shape[0] > 1
high_res_features = [feat_level[img_idx].unsqueeze(0) for feat_level in self._features["high_res_feats"]]
low_res_masks, iou_predictions, _, _ = self.model.sam_mask_decoder(
image_embeddings=self._features["image_embed"][img_idx].unsqueeze(0), image_pe=self.model.sam_prompt_encoder.getdensepe(), sparse_prompt_embeddings=sparse_embeddings,
dense_prompt_embeddings=dense_embeddings, multimask_output=multimask_output, repeat_image=batched_mode, high_res_features=high_res_features,
)
# upscale the masks to the original image resolution
masks = self._transforms.postprocessmasks(low_res_masks, self._orig_hw[img_idx])
low_res_masks = torch.clamp(low_res_masks, -32.0, 32.0)
# return
if not return_logits:
masks = masks > self.mask_threshold
return masks, iou_predictions, low_res_masks
'''getimageembedding'''
def getimageembedding(self):
assert self._is_image_set, "an image must be set with .setimage(...) to generate an embedding."
assert self._features is not None, "features must exist if an image has been set."
return self._features["image_embed"]
'''device'''
@property
def device(self):
return self.model.device
'''resetpredictor'''
def resetpredictor(self):
self._is_image_set = False
self._features = None
self._orig_hw = None
self._is_batch = False
'''SAMV2AutomaticMaskGenerator'''
class SAMV2AutomaticMaskGenerator(nn.Module):
def __init__(self, points_per_side=32, points_per_batch=64, pred_iou_thresh=0.8, stability_score_thresh=0.95, stability_score_offset=1.0, mask_threshold=0.0, box_nms_thresh=0.7, crop_n_layers=0, crop_nms_thresh=0.7,
crop_overlap_ratio=512/1500, crop_n_points_downscale_factor=1, point_grids=None, min_mask_region_area=0, output_mode="binary_mask", use_m2m=False, multimask_output=True, samv2_cfg=None, use_default_samv2_t=False,
use_default_samv2_s=False, use_default_samv2_bplus=False, use_default_samv2_l=True, device='cuda', load_ckpt_strict=True, apply_postprocessing=False):
super(SAMV2AutomaticMaskGenerator, self).__init__()
# deal with points_per_side and point_grids
assert (points_per_side is None) != (point_grids is None), "exactly one of points_per_side or point_grid must be provided."
if points_per_side is not None:
self.point_grids = buildalllayerpointgrids(points_per_side, crop_n_layers, crop_n_points_downscale_factor)
elif point_grids is not None:
self.point_grids = point_grids
else:
raise ValueError("Can't have both points_per_side and point_grid be None.")
# output_mode
assert output_mode in ["binary_mask", "uncompressed_rle", "coco_rle"], f"unknown output_mode {output_mode}."
if output_mode == "coco_rle":
try:
from pycocotools import mask as mask_utils
except ImportError as e:
print("please install pycocotools")
raise e
# predictor
self.predictor = SAMV2ImagePredictor(
samv2_cfg=samv2_cfg, use_default_samv2_l=use_default_samv2_l, use_default_samv2_bplus=use_default_samv2_bplus, use_default_samv2_s=use_default_samv2_s, use_default_samv2_t=use_default_samv2_t,
device=device, load_ckpt_strict=load_ckpt_strict, max_hole_area=min_mask_region_area, max_sprinkle_area=min_mask_region_area, apply_postprocessing=apply_postprocessing,
)
# set attributes
self.points_per_batch = points_per_batch
self.pred_iou_thresh = pred_iou_thresh
self.stability_score_thresh = stability_score_thresh
self.stability_score_offset = stability_score_offset
self.mask_threshold = mask_threshold
self.box_nms_thresh = box_nms_thresh
self.crop_n_layers = crop_n_layers
self.crop_nms_thresh = crop_nms_thresh
self.crop_overlap_ratio = crop_overlap_ratio
self.crop_n_points_downscale_factor = crop_n_points_downscale_factor
self.min_mask_region_area = min_mask_region_area
self.output_mode = output_mode
self.use_m2m = use_m2m
self.multimask_output = multimask_output
'''generate'''
@torch.no_grad()
def generate(self, image):
# generate masks
mask_data = self.generatemasks(image)
# encode masks
if self.output_mode == "coco_rle":
mask_data["segmentations"] = [cocoencoderle(rle) for rle in mask_data["rles"]]
elif self.output_mode == "binary_mask":
mask_data["segmentations"] = [rletomask(rle) for rle in mask_data["rles"]]
else:
mask_data["segmentations"] = mask_data["rles"]
# write mask records
curr_anns = []
for idx in range(len(mask_data["segmentations"])):
ann = {
"segmentation": mask_data["segmentations"][idx], "area": areafromrle(mask_data["rles"][idx]), "bbox": boxxyxytoxywh(mask_data["boxes"][idx]).tolist(), "predicted_iou": mask_data["iou_preds"][idx].item(),
"point_coords": [mask_data["points"][idx].tolist()], "stability_score": mask_data["stability_score"][idx].item(), "crop_box": boxxyxytoxywh(mask_data["crop_boxes"][idx]).tolist(),
}
curr_anns.append(ann)
# return
return curr_anns
'''generatemasks'''
def generatemasks(self, image):
orig_size = image.shape[:2]
crop_boxes, layer_idxs = generatecropboxes(orig_size, self.crop_n_layers, self.crop_overlap_ratio)
# iterate over image crops
data = MaskData()
for crop_box, layer_idx in zip(crop_boxes, layer_idxs):
crop_data = self.processcrop(image, crop_box, layer_idx, orig_size)
data.cat(crop_data)
# remove duplicate masks between crops
if len(crop_boxes) > 1:
scores = 1 / box_area(data["crop_boxes"])
scores = scores.to(data["boxes"].device)
keep_by_nms = batched_nms(data["boxes"].float(), scores, torch.zeros_like(data["boxes"][:, 0]), iou_threshold=self.crop_nms_thresh)
data.filter(keep_by_nms)
data.tonumpy()
# return
return data
'''processcrop'''
def processcrop(self, image, crop_box, crop_layer_idx, orig_size):
# crop the image and calculate embeddings
x0, y0, x1, y1 = crop_box
cropped_im = image[y0:y1, x0:x1, :]
cropped_im_size = cropped_im.shape[:2]
self.predictor.setimage(cropped_im)
# get points for this crop
points_scale = np.array(cropped_im_size)[None, ::-1]
points_for_image = self.point_grids[crop_layer_idx] * points_scale
# generate masks for this crop in batches
data = MaskData()
for (points,) in batchiterator(self.points_per_batch, points_for_image):
batch_data = self.processbatch(points, cropped_im_size, crop_box, orig_size, normalize=True)
data.cat(batch_data)
del batch_data
self.predictor.resetpredictor()
# remove duplicates within this crop.
keep_by_nms = batched_nms(data["boxes"].float(), data["iou_preds"], torch.zeros_like(data["boxes"][:, 0]), iou_threshold=self.box_nms_thresh)
data.filter(keep_by_nms)
# return to the original image frame
data["boxes"] = uncropboxesxyxy(data["boxes"], crop_box)
data["points"] = uncroppoints(data["points"], crop_box)
data["crop_boxes"] = torch.tensor([crop_box for _ in range(len(data["rles"]))])
# return
return data
'''processbatch'''
def processbatch(self, points, im_size, crop_box, orig_size, normalize=False):
orig_h, orig_w = orig_size
# run model on this batch
points = torch.as_tensor(points, device=self.predictor.device)
in_points = self.predictor._transforms.transformcoords(points, normalize=normalize, orig_hw=im_size)
in_labels = torch.ones(in_points.shape[0], dtype=torch.int, device=in_points.device)
masks, iou_preds, low_res_masks = self.predictor.purepredict(in_points[:, None, :], in_labels[:, None], multimask_output=self.multimask_output, return_logits=True)
# serialize predictions and store in MaskData
data = MaskData(masks=masks.flatten(0, 1), iou_preds=iou_preds.flatten(0, 1), points=points.repeat_interleave(masks.shape[1], dim=0), low_res_masks=low_res_masks.flatten(0, 1))
del masks
# start
if not self.use_m2m:
if self.pred_iou_thresh > 0.0:
keep_mask = data["iou_preds"] > self.pred_iou_thresh
data.filter(keep_mask)
data["stability_score"] = calculatestabilityscore(data["masks"], self.mask_threshold, self.stability_score_offset)
if self.stability_score_thresh > 0.0:
keep_mask = data["stability_score"] >= self.stability_score_thresh
data.filter(keep_mask)
else:
in_points = self.predictor._transforms.transformcoords(data["points"], normalize=normalize, orig_hw=im_size)
labels = torch.ones(in_points.shape[0], dtype=torch.int, device=in_points.device)
masks, ious = self.refinewithm2m(in_points, labels, data["low_res_masks"], self.points_per_batch)
data["masks"] = masks.squeeze(1)
data["iou_preds"] = ious.squeeze(1)
if self.pred_iou_thresh > 0.0:
keep_mask = data["iou_preds"] > self.pred_iou_thresh
data.filter(keep_mask)
data["stability_score"] = calculatestabilityscore(data["masks"], self.mask_threshold, self.stability_score_offset)
if self.stability_score_thresh > 0.0:
keep_mask = data["stability_score"] >= self.stability_score_thresh
data.filter(keep_mask)
# threshold masks and calculate boxes
data["masks"] = data["masks"] > self.mask_threshold
data["boxes"] = batchedmasktobox(data["masks"])
# filter boxes that touch crop boundaries
keep_mask = ~isboxnearcropedge(data["boxes"], crop_box, [0, 0, orig_w, orig_h])
if not torch.all(keep_mask):
data.filter(keep_mask)
# compress to RLE
data["masks"] = uncropmasks(data["masks"], crop_box, orig_h, orig_w)
data["rles"] = masktorlepytorch(data["masks"])
del data["masks"]
# return
return data
'''postprocesssmallregions'''
@staticmethod
def postprocesssmallregions(mask_data, min_area, nms_thresh):
if len(mask_data["rles"]) == 0:
return mask_data
# filter small disconnected regions and holes
new_masks, scores = [], []
for rle in mask_data["rles"]:
mask = rletomask(rle)
mask, changed = removesmallregions(mask, min_area, mode="holes")
unchanged = not changed
mask, changed = removesmallregions(mask, min_area, mode="islands")
unchanged = unchanged and not changed
new_masks.append(torch.as_tensor(mask).unsqueeze(0))
scores.append(float(unchanged))
# recalculate boxes and remove any new duplicates
masks = torch.cat(new_masks, dim=0)
boxes = batchedmasktobox(masks)
keep_by_nms = batched_nms(boxes.float(), torch.as_tensor(scores), torch.zeros_like(boxes[:, 0]), iou_threshold=nms_thresh)
# only recalculate RLEs for masks that have changed
for i_mask in keep_by_nms:
if scores[i_mask] == 0.0:
mask_torch = masks[i_mask].unsqueeze(0)
mask_data["rles"][i_mask] = masktorlepytorch(mask_torch)[0]
mask_data["boxes"][i_mask] = boxes[i_mask]
mask_data.filter(keep_by_nms)
# return
return mask_data
'''refinewithm2m'''
def refinewithm2m(self, points, point_labels, low_res_masks, points_per_batch):
new_masks = []
new_iou_preds = []
for cur_points, cur_point_labels, low_res_mask in batchiterator(points_per_batch, points, point_labels, low_res_masks):
best_masks, best_iou_preds, _ = self.predictor.purepredict(
cur_points[:, None, :], cur_point_labels[:, None], mask_input=low_res_mask[:, None, :], multimask_output=False, return_logits=True,
)
new_masks.append(best_masks)
new_iou_preds.append(best_iou_preds)
masks = torch.cat(new_masks, dim=0)
return masks, torch.cat(new_iou_preds, dim=0)
'''SAMV2VideoPredictor'''
class SAMV2VideoPredictor(SAMV2ImagePredictor):
def __init__(self, fill_hole_area=0, non_overlap_masks=False, clear_non_cond_mem_around_input=False, clear_non_cond_mem_for_multi_obj=False, **kwargs):
self.fill_hole_area = fill_hole_area
self.non_overlap_masks = non_overlap_masks
self.clear_non_cond_mem_around_input = clear_non_cond_mem_around_input
self.clear_non_cond_mem_for_multi_obj = clear_non_cond_mem_for_multi_obj
super(SAMV2VideoPredictor, self).__init__(**kwargs)
'''initstate'''
@torch.inference_mode()
def initstate(self, video_path, offload_video_to_cpu=False, offload_state_to_cpu=False, async_loading_frames=False):
images, video_height, video_width = loadvideoframes(video_path=video_path, image_size=self.model.image_size, offload_video_to_cpu=offload_video_to_cpu, async_loading_frames=async_loading_frames)
inference_state = {}
inference_state["images"] = images
inference_state["num_frames"] = len(images)
# whether to offload the video frames to CPU memory turning on this option saves the GPU memory with only a very small overhead
inference_state["offload_video_to_cpu"] = offload_video_to_cpu
# whether to offload the inference state to CPU memory turning on this option saves the GPU memory at the cost of a lower tracking fps (e.g. in a test case of 768x768 model, fps dropped from 27 to 24 when tracking one object and from 24 to 21 when tracking two objects)
inference_state["offload_state_to_cpu"] = offload_state_to_cpu
# the original video height and width, used for resizing final output scores
inference_state["video_height"] = video_height
inference_state["video_width"] = video_width
inference_state["device"] = self.device
if offload_state_to_cpu:
inference_state["storage_device"] = torch.device("cpu")
else:
inference_state["storage_device"] = torch.device("cuda")
# inputs on each frame
inference_state["point_inputs_per_obj"] = {}
inference_state["mask_inputs_per_obj"] = {}
# visual features on a small number of recently visited frames for quick interactions
inference_state["cached_features"] = {}
# values that don't change across frames (so we only need to hold one copy of them)
inference_state["constants"] = {}
# mapping between client-side object id and model-side object index
inference_state["obj_id_to_idx"] = OrderedDict()
inference_state["obj_idx_to_id"] = OrderedDict()
inference_state["obj_ids"] = []
# a storage to hold the model's tracking results and states on each frame
inference_state["output_dict"] = {"cond_frame_outputs": {}, "non_cond_frame_outputs": {}}
# slice (view) of each object tracking results, sharing the same memory with "output_dict"
inference_state["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)
inference_state["temp_output_dict_per_obj"] = {}
# frames that already holds consolidated outputs from click or mask inputs (we directly use their consolidated outputs during tracking)
inference_state["consolidated_frame_inds"] = {"cond_frame_outputs": set(), "non_cond_frame_outputs": set()}
# metadata for each tracking frame (e.g. which direction it's tracked)
inference_state["tracking_has_started"] = False
inference_state["frames_already_tracked"] = {}
# warm up the visual backbone and cache the image feature on frame 0
self.getimagefeature(inference_state, frame_idx=0, batch_size=1)
# return
return inference_state
'''buildsamv2'''
def buildsamv2(self, samv2_cfg, device, apply_postprocessing=True):
if apply_postprocessing:
samv2_cfg['head']['sam_mask_decoder_extra_args'] = {
'dynamic_multimask_via_stability': True,
'dynamic_multimask_stability_delta': 0.05,
'dynamic_multimask_stability_thresh': 0.98,
}
samv2_cfg['head']['binarize_mask_from_pts_for_mem_enc'] = True
self.fill_hole_area = 8
samv2_model = SAMV2(cfg=samv2_cfg, mode='TEST')
samv2_model.to(device=device)
samv2_model.eval()
return samv2_model
'''objidtoidx'''
def objidtoidx(self, inference_state, obj_id):
obj_idx = inference_state["obj_id_to_idx"].get(obj_id, None)
if obj_idx is not None:
return obj_idx
allow_new_object = not inference_state["tracking_has_started"]
if allow_new_object:
obj_idx = len(inference_state["obj_id_to_idx"])
inference_state["obj_id_to_idx"][obj_id] = obj_idx
inference_state["obj_idx_to_id"][obj_idx] = obj_id
inference_state["obj_ids"] = list(inference_state["obj_id_to_idx"])
inference_state["point_inputs_per_obj"][obj_idx] = {}
inference_state["mask_inputs_per_obj"][obj_idx] = {}
inference_state["output_dict_per_obj"][obj_idx] = {"cond_frame_outputs": {}, "non_cond_frame_outputs": {}}
inference_state["temp_output_dict_per_obj"][obj_idx] = {"cond_frame_outputs": {}, "non_cond_frame_outputs": {}}
return obj_idx
else:
raise RuntimeError(f"Cannot add new object id {obj_id} after tracking starts. All existing object ids: {inference_state['obj_ids']}. Please call 'resetstate' to restart from scratch.")
'''objidxtoid'''
def objidxtoid(self, inference_state, obj_idx):
return inference_state["obj_idx_to_id"][obj_idx]
'''getobjnum'''
def getobjnum(self, inference_state):
return len(inference_state["obj_idx_to_id"])
'''addnewpoints'''
@torch.inference_mode()
def addnewpoints(self, inference_state, frame_idx, obj_id, points, labels, clear_old_points=True, normalize_coords=True):
obj_idx = self.objidtoidx(inference_state, obj_id)
point_inputs_per_frame = inference_state["point_inputs_per_obj"][obj_idx]
mask_inputs_per_frame = inference_state["mask_inputs_per_obj"][obj_idx]
if not isinstance(points, torch.Tensor):
points = torch.tensor(points, dtype=torch.float32)
if not isinstance(labels, torch.Tensor):
labels = torch.tensor(labels, dtype=torch.int32)
if points.dim() == 2:
points = points.unsqueeze(0)
if labels.dim() == 1:
labels = labels.unsqueeze(0)
if normalize_coords:
video_H = inference_state["video_height"]
video_W = inference_state["video_width"]
points = points / torch.tensor([video_W, video_H]).to(points.device)
points = points * self.model.image_size
points = points.to(inference_state["device"])
labels = labels.to(inference_state["device"])
if not clear_old_points:
point_inputs = point_inputs_per_frame.get(frame_idx, None)
else:
point_inputs = None
point_inputs = concatpoints(point_inputs, points, 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 inference_state["frames_already_tracked"]
if is_init_cond_frame:
reverse = False