| 20 | |
| 21 | |
| 22 | class SAM2Base(torch.nn.Module): |
| 23 | def __init__( |
| 24 | self, |
| 25 | image_encoder, |
| 26 | memory_attention, |
| 27 | memory_encoder, |
| 28 | num_maskmem=7, # default 1 input frame + 6 previous frames |
| 29 | image_size=512, |
| 30 | backbone_stride=16, # stride of the image backbone output |
| 31 | sigmoid_scale_for_mem_enc=1.0, # scale factor for mask sigmoid prob |
| 32 | sigmoid_bias_for_mem_enc=0.0, # bias factor for mask sigmoid prob |
| 33 | # During evaluation, whether to binarize the sigmoid mask logits on interacted frames with clicks |
| 34 | binarize_mask_from_pts_for_mem_enc=False, |
| 35 | use_mask_input_as_output_without_sam=False, # on frames with mask input, whether to directly output the input mask without using a SAM prompt encoder + mask decoder |
| 36 | # The maximum number of conditioning frames to participate in the memory attention (-1 means no limit; if there are more conditioning frames than this limit, |
| 37 | # we only cross-attend to the temporally closest `max_cond_frames_in_attn` conditioning frames in the encoder when tracking each frame). This gives the model |
| 38 | # a temporal locality when handling a large number of annotated frames (since closer frames should be more important) and also avoids GPU OOM. |
| 39 | max_cond_frames_in_attn=-1, |
| 40 | # on the first frame, whether to directly add the no-memory embedding to the image feature |
| 41 | # (instead of using the transformer encoder) |
| 42 | directly_add_no_mem_embed=False, |
| 43 | # whether to use high-resolution feature maps in the SAM mask decoder |
| 44 | use_high_res_features_in_sam=False, |
| 45 | # whether to output multiple (3) masks for the first click on initial conditioning frames |
| 46 | multimask_output_in_sam=False, |
| 47 | # the minimum and maximum number of clicks to use multimask_output_in_sam (only relevant when `multimask_output_in_sam=True`; |
| 48 | # default is 1 for both, meaning that only the first click gives multimask output; also note that a box counts as two points) |
| 49 | multimask_min_pt_num=1, |
| 50 | multimask_max_pt_num=1, |
| 51 | # whether to also use multimask output for tracking (not just for the first click on initial conditioning frames; only relevant when `multimask_output_in_sam=True`) |
| 52 | multimask_output_for_tracking=False, |
| 53 | # Whether to use multimask tokens for obj ptr; Only relevant when both |
| 54 | # use_obj_ptrs_in_encoder=True and multimask_output_for_tracking=True |
| 55 | use_multimask_token_for_obj_ptr: bool = False, |
| 56 | # whether to use sigmoid to restrict ious prediction to [0-1] |
| 57 | iou_prediction_use_sigmoid=False, |
| 58 | # The memory bank's temporal stride during evaluation (i.e. the `r` parameter in XMem and Cutie; XMem and Cutie use r=5). |
| 59 | # For r>1, the (self.num_maskmem - 1) non-conditioning memory frames consist of |
| 60 | # (self.num_maskmem - 2) nearest frames from every r-th frames, plus the last frame. |
| 61 | memory_temporal_stride_for_eval=1, |
| 62 | # if `add_all_frames_to_correct_as_cond` is True, we also append to the conditioning frame list any frame that receives a later correction click |
| 63 | # if `add_all_frames_to_correct_as_cond` is False, we conditioning frame list to only use those initial conditioning frames |
| 64 | add_all_frames_to_correct_as_cond=False, |
| 65 | # whether to apply non-overlapping constraints on the object masks in the memory encoder during evaluation (to avoid/alleviate superposing masks) |
| 66 | non_overlap_masks_for_mem_enc=False, |
| 67 | # whether to cross-attend to object pointers from other frames (based on SAM output tokens) in the encoder |
| 68 | use_obj_ptrs_in_encoder=False, |
| 69 | # the maximum number of object pointers from other frames in encoder cross attention (only relevant when `use_obj_ptrs_in_encoder=True`) |
| 70 | max_obj_ptrs_in_encoder=16, |
| 71 | # whether to add temporal positional encoding to the object pointers in the encoder (only relevant when `use_obj_ptrs_in_encoder=True`) |
| 72 | add_tpos_enc_to_obj_ptrs=True, |
| 73 | # whether to add an extra linear projection layer for the temporal positional encoding in the object pointers to avoid potential interference |
| 74 | # with spatial positional encoding (only relevant when both `use_obj_ptrs_in_encoder=True` and `add_tpos_enc_to_obj_ptrs=True`) |
| 75 | proj_tpos_enc_in_obj_ptrs=False, |
| 76 | # whether to only attend to object pointers in the past (before the current frame) in the encoder during evaluation |
| 77 | # (only relevant when `use_obj_ptrs_in_encoder=True`; this might avoid pointer information too far in the future to distract the initial tracking) |
| 78 | only_obj_ptrs_in_the_past_for_eval=False, |
| 79 | # Whether to predict if there is an object in the frame |
nothing calls this directly
no outgoing calls
no test coverage detected