> ## Documentation Index
> Fetch the complete documentation index at: https://docs.bioptimus.com/llms.txt
> Use this file to discover all available pages before exploring further.

# bioptimus.preprocess.wsi.provider.tiled

Tiled tissue mask generation using a batched segmentation model.

Implements `TiledTissueMask`, which walks a WSI in fixed-size patches, processes them through a `TissueMaskModel`, and stitches the per-tile predictions into a full-slide binary mask.

## TiledTissueMask

```python theme={null}
class TiledTissueMask(TissueMaskProvider)
```

Tile-based, batch-aware tissue mask generation.

Combines a `TissueMaskModel` with a tiling strategy to produce a full-slide binary mask.  The workflow is:

1. Compute the tissue bounding box at *resolution*.
2. Walk the bounding box in non-overlapping *tile\_size* patches.
3. Collect patches into batches of *batch\_size*.
4. Pass each batch to `model.process()` — this is where GPU parallelism happens for DL models.
5. Stitch the per-tile masks back into the output array.

Boundary tiles that are smaller than *tile\_size* are zero-padded before model processing and the padding is stripped from the output.

<ParamField body="model" type="TissueMaskModel">
  The processing model.
</ParamField>

<ParamField body="tile_size" type="tuple[int, int] | None">
  `(width, height)` of each patch.  When `None`, derived from `model.tile_spec`.
</ParamField>

<ParamField body="resolution" type="Resolution | None">
  Resolution at which to read tiles.  When `None`, derived from `model.tile_spec`.
</ParamField>

<ParamField body="batch_size" type="int">
  Number of tiles per model call.  Set higher for GPU models (e.g. 32–64).  Defaults to `1`.
</ParamField>

**Example:**

```python theme={null}
provider = TiledTissueMask(
    model=MyGPUModel(net),
    tile_size=(512, 512),
    resolution=Level(0),
    batch_size=32,
)
mask = provider.generate(reader)

# Or let the model's tile_spec drive the defaults:
provider = TiledTissueMaskProvider(model=my_model)
```

#### generate

```python theme={null}
def generate(reader: WSIReader,
             *,
             show_progress: bool = True,
             max_concurrency: int = _DEFAULT_MAX_CONCURRENCY) -> np.ndarray
```

Generates a tissue mask by tiling over the WSI.

Tile images are read sequentially from the WSI (readers are not thread-safe), then dispatched concurrently to the remote model endpoint for GPU-efficient batched inference. Results are stitched back into a single binary mask.

<ParamField body="reader" type="WSIReader" required>
  An **open** WSI reader.
</ParamField>

<ParamField body="show_progress" type="bool">
  Whether to display a per-tile progress bar. Defaults to `True`.
</ParamField>

<ParamField body="max_concurrency" type="int">
  Maximum concurrent HTTP requests. Defaults to 64.
</ParamField>

<ResponseField name="returns" type="np.ndarray">
  A `uint8` mask of shape `(H, W)` covering the tissue bounding box at `resolution`, with values in `{0, 1}` where `1` = tissue.
</ResponseField>
