Skip to article frontmatterSkip to article content
Site not loading correctly?

This may be due to an incorrect BASE_URL configuration. See the MyST Documentation for reference.

Scaling segmentation across large rasters with batching and tiling

Every workflow so far has processed one image at a time. This page covers two different ways to scale up: batch segmentation across many separate images, and tiled segmentation for a single raster too large to process in one pass.


1. Motivation

Real projects rarely involve just one image. Whether you have many separate scenes or one large raster that exceeds GPU memory, the right scaling approach depends on which situation you are actually in, and this page covers both.


2. Core idea

Batch segmentation processes multiple separate images with the same prompt in one workflow, useful when you have many images to cover. Tiled inference divides one large image into overlapping windows, processes each independently, and merges the results, useful when a single image exceeds available GPU memory. These solve different problems and are not interchangeable.


3. Workflow

A. Batch segmentation

set_image_batch() and generate_masks_batch() are the batch inference counterparts of set_image() and generate_masks(), running the same prompt across every image in the list. min_size=100 filters out detections smaller than 100 pixels, removing small, likely spurious detections before you even inspect the results.

batch_results holds each image’s detections, letting you check counts per image before committing to a full export. show_anns_batch() displays all results in a grid, and save_masks_batch() exports every image’s masks as separate georeferenced GeoTIFFs in one call.

B. Tiled segmentation for one image

generate_masks_tiled() divides the input into overlapping tile_size-pixel windows, segments each independently, and merges the results into one seamless georeferenced output, the same overlap-and-blend pattern used for tiled inference in earlier lessons, now applied to a prompt-based, zero-shot workflow.

C. Tuning tile parameters

D. Vectorize and smooth the tiled result

smooth_vector() softens pixelation artifacts from the raster-to-vector conversion, a lighter-weight alternative to full regularization, well suited to naturally curved features like water bodies where right-angle regularization would not make sense.


4. Python reactivation

enumerate(sam3.batch_results) pairs each result with its position in the list, the same pattern used for indexed iteration throughout this course. The f-string loop building image_paths (for i in range(1, 5): ... image_paths.append(...)) is a standard accumulation pattern, equivalent to a list comprehension but written as an explicit loop for readability.


5. Common pitfalls


6. Mini task

You have a single 20,000-by-20,000-pixel satellite scene, far too large to load into GPU memory at once, and you expect a few thousand distinct objects. Would you reach for batch segmentation or tiled segmentation, and what dtype would you choose?

Sample solution

Tiled segmentation fits here, since this is one large image exceeding memory limits, not multiple separate images; batch segmentation would not apply to a single raster at all. For dtype, with a few thousand expected objects, int32 or uint16 (up to 65,535) would both work; uint16 is the more memory-efficient choice if you are confident the object count will stay comfortably under its limit, while int32 provides a safer margin if your object-count estimate is uncertain.


7. Key takeaways

Further reading