MCPcopy Create free account
hub / github.com/ImprintLab/Medical-SAM2 / predict

Method predict

sam2_train/sam2_image_predictor.py:217–283  ·  view source on GitHub ↗

Predict masks for the given input prompts, using the currently set image. Arguments: point_coords (np.ndarray or None): A Nx2 array of point prompts to the model. Each point is in (X,Y) in pixels. point_labels (np.ndarray or None): A length N array o

(
        self,
        point_coords: Optional[np.ndarray] = None,
        point_labels: Optional[np.ndarray] = None,
        box: Optional[np.ndarray] = None,
        mask_input: Optional[np.ndarray] = None,
        multimask_output: bool = True,
        return_logits: bool = False,
        normalize_coords=True,
    )

Source from the content-addressed store, hash-verified

215 return all_masks, all_ious, all_low_res_masks
216
217 def predict(
218 self,
219 point_coords: Optional[np.ndarray] = None,
220 point_labels: Optional[np.ndarray] = None,
221 box: Optional[np.ndarray] = None,
222 mask_input: Optional[np.ndarray] = None,
223 multimask_output: bool = True,
224 return_logits: bool = False,
225 normalize_coords=True,
226 ) -> Tuple[np.ndarray, np.ndarray, np.ndarray]:
227 """
228 Predict masks for the given input prompts, using the currently set image.
229
230 Arguments:
231 point_coords (np.ndarray or None): A Nx2 array of point prompts to the
232 model. Each point is in (X,Y) in pixels.
233 point_labels (np.ndarray or None): A length N array of labels for the
234 point prompts. 1 indicates a foreground point and 0 indicates a
235 background point.
236 box (np.ndarray or None): A length 4 array given a box prompt to the
237 model, in XYXY format.
238 mask_input (np.ndarray): A low resolution mask input to the model, typically
239 coming from a previous prediction iteration. Has form 1xHxW, where
240 for SAM, H=W=256.
241 multimask_output (bool): If true, the model will return three masks.
242 For ambiguous input prompts (such as a single click), this will often
243 produce better masks than a single prediction. If only a single
244 mask is needed, the model's predicted quality score can be used
245 to select the best mask. For non-ambiguous prompts, such as multiple
246 input prompts, multimask_output=False can give better results.
247 return_logits (bool): If true, returns un-thresholded masks logits
248 instead of a binary mask.
249 normalize_coords (bool): If true, the point coordinates will be normalized to the range [0,1] and point_coords is expected to be wrt. image dimensions.
250
251 Returns:
252 (np.ndarray): The output masks in CxHxW format, where C is the
253 number of masks, and (H, W) is the original image size.
254 (np.ndarray): An array of length C containing the model's
255 predictions for the quality of each mask.
256 (np.ndarray): An array of shape CxHxW, where C is the number
257 of masks and H=W=256. These low resolution logits can be passed to
258 a subsequent iteration as mask input.
259 """
260 if not self._is_image_set:
261 raise RuntimeError(
262 "An image must be set with .set_image(...) before mask prediction."
263 )
264
265 # Transform input prompts
266
267 mask_input, unnorm_coords, labels, unnorm_box = self._prep_prompts(
268 point_coords, point_labels, box, mask_input, normalize_coords
269 )
270
271 masks, iou_predictions, low_res_masks = self._predict(
272 unnorm_coords,
273 labels,
274 unnorm_box,

Callers

nothing calls this directly

Calls 2

_prep_promptsMethod · 0.95
_predictMethod · 0.95

Tested by

no test coverage detected