SamAutomaticMaskGenerator is a convenience wrapper in Segment Anything that turns a loaded model and an image into segmentation masks. It handles the preprocessing and postprocessing needed for automatic, whole-image segmentation. section 15.4.1
Generate masks for an image
Load a registered SAM checkpoint, move it to an available device, wrap it with SamAutomaticMaskGenerator, and pass a NumPy image array to generate. For the astronaut image, the pipeline produces 79 masks with the original 1024×1024 dimensions. Each segmentation is a binary array: True marks pixels belonging to the object and False marks background pixels. The returned records also include confidence information such as predicted_iou. section 15.4.1
from segment_anything import SamAutomaticMaskGenerator, sam_model_registry
device = torch.device("cuda" if torch.cuda.is_available() else "cpu")
model_config, model_weights_path = get_sam_model()
sam = sam_model_registry[model_config](checkpoint=model_weights_path)
sam.to(device)
mask_generator = SamAutomaticMaskGenerator(sam)
Shows the model-loading sequence and the construction of a SamAutomaticMaskGenerator instance. section 15.4.1
# In[] image_array = np.array(image) masks = mask_generator.generate(image_array) print(len(masks)) print(masks[0]["segmentation"].shape) print(masks[0]["predicted_iou"]) # Out[] 79 (1024, 1024) 1.033138632774353 #1
Shows the generate call and the shape, count, and confidence value of the resulting masks. section 15.4.1
Automatic generation is useful for seeing every candidate region in an image. When segmentation must target a particular structure, use SAM directly with a point, box, or mask prompt instead of relying on this generator. section 15.4.1 section 15.5
