Predict masks given image and prompt embeddings. Arguments: image_embeddings (torch.Tensor): the embeddings from the image encoder image_pe (torch.Tensor): positional encoding with the shape of image_embeddings sparse_prompt_embeddings (torch.Tensor):
(
self,
image_embeddings: torch.Tensor,
image_pe: torch.Tensor,
sparse_prompt_embeddings: torch.Tensor,
dense_prompt_embeddings: torch.Tensor,
multimask_output: bool,
repeat_image: bool,
high_res_features: Optional[List[torch.Tensor]] = None,
)
| 97 | self.dynamic_multimask_stability_thresh = dynamic_multimask_stability_thresh |
| 98 | |
| 99 | def forward( |
| 100 | self, |
| 101 | image_embeddings: torch.Tensor, |
| 102 | image_pe: torch.Tensor, |
| 103 | sparse_prompt_embeddings: torch.Tensor, |
| 104 | dense_prompt_embeddings: torch.Tensor, |
| 105 | multimask_output: bool, |
| 106 | repeat_image: bool, |
| 107 | high_res_features: Optional[List[torch.Tensor]] = None, |
| 108 | ) -> Tuple[torch.Tensor, torch.Tensor]: |
| 109 | """ |
| 110 | Predict masks given image and prompt embeddings. |
| 111 | |
| 112 | Arguments: |
| 113 | image_embeddings (torch.Tensor): the embeddings from the image encoder |
| 114 | image_pe (torch.Tensor): positional encoding with the shape of image_embeddings |
| 115 | sparse_prompt_embeddings (torch.Tensor): the embeddings of the points and boxes |
| 116 | dense_prompt_embeddings (torch.Tensor): the embeddings of the mask inputs |
| 117 | multimask_output (bool): Whether to return multiple masks or a single |
| 118 | mask. |
| 119 | |
| 120 | Returns: |
| 121 | torch.Tensor: batched predicted masks |
| 122 | torch.Tensor: batched predictions of mask quality |
| 123 | torch.Tensor: batched SAM token for mask output |
| 124 | """ |
| 125 | masks, iou_pred, mask_tokens_out, object_score_logits = self.predict_masks( |
| 126 | image_embeddings=image_embeddings, |
| 127 | image_pe=image_pe, |
| 128 | sparse_prompt_embeddings=sparse_prompt_embeddings, |
| 129 | dense_prompt_embeddings=dense_prompt_embeddings, |
| 130 | repeat_image=repeat_image, |
| 131 | high_res_features=high_res_features, |
| 132 | ) |
| 133 | |
| 134 | # Select the correct mask or masks for output |
| 135 | if multimask_output: |
| 136 | masks = masks[:, 1:, :, :] |
| 137 | iou_pred = iou_pred[:, 1:] |
| 138 | elif self.dynamic_multimask_via_stability and not self.training: |
| 139 | masks, iou_pred = self._dynamic_multimask_via_stability(masks, iou_pred) |
| 140 | else: |
| 141 | masks = masks[:, 0:1, :, :] |
| 142 | iou_pred = iou_pred[:, 0:1] |
| 143 | |
| 144 | if multimask_output and self.use_multimask_token_for_obj_ptr: |
| 145 | sam_tokens_out = mask_tokens_out[:, 1:] # [b, 3, c] shape |
| 146 | else: |
| 147 | # Take the mask output token. Here we *always* use the token for single mask output. |
| 148 | # At test time, even if we track after 1-click (and using multimask_output=True), |
| 149 | # we still take the single mask token here. The rationale is that we always track |
| 150 | # after multiple clicks during training, so the past tokens seen during training |
| 151 | # are always the single mask token (and we'll let it be the object-memory token). |
| 152 | sam_tokens_out = mask_tokens_out[:, 0:1] # [b, 1, c] shape |
| 153 | |
| 154 | # Prepare output |
| 155 | return masks, iou_pred, sam_tokens_out, object_score_logits |
| 156 |
nothing calls this directly
no test coverage detected