liveBook · Manning

SamAutomaticMaskGenerator

As explained in Deep Learning with PyTorch, Second Edition

A convenience abstraction in the Segment Anything library that generates segmentation masks from a model and image.

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

How Deep Learning with PyTorch, Second Edition covers it

How deep it goes
Explains wrapper behavior and demonstrates outputs, but defers prompted prediction and direct model components.
Examples
Demonstrates automatic segmentation on an astronaut image and visualizes individual and overlaid masks.
What it assumes
Assumes basic Python, NumPy, PyTorch, image loading, and pretrained-checkpoint usage while showing the SAM setup sequence.
What you can do after
Operate/configure it to generate and inspect binary segmentation masks from a loaded SAM model and image.
How it connects
Connects model checkpoints, image preprocessing, binary masks, confidence scores, and prompted segmentation.

Related concepts

This explainer was generated by Manning's knowledge graph from the text of Deep Learning with PyTorch, Second Edition. The section links point to the parts of the book it draws on.