Images, slicing, utilities¶
Images¶
load_image ¶
Load a file path, PIL image, HWC NumPy array, or tensor as an RGB [3, H, W] tensor.
register_frame_buffer ¶
Page-lock a contiguous writeable NumPy array or CPU tensor for faster CUDA uploads.
FrameBufferRegistration ¶
Handle for a host buffer page-locked by :func:register_frame_buffer.
unregister ¶
Unlock the buffer (idempotent). Uploads from it take the plain copy afterwards.
Tile grid and slicing¶
calculate_slice_grid ¶
calculate_slice_grid(
image_width: int,
image_height: int,
slice_width: int = 512,
slice_height: int = 512,
overlap_width_ratio: float = 0.2,
overlap_height_ratio: float = 0.2,
) -> list[SliceWindow]
Build row-major overlapping slice windows covering the canvas.
SliceWindow
dataclass
¶
A tile on the canvas, in pixel coordinates with exclusive x2/y2.
windows_to_tensor ¶
Stack windows into an [N, 4] int64 tensor of (x1, y1, x2, y2).
yield_slice_batches ¶
yield_slice_batches(
image: Tensor,
windows: Sequence[SliceWindow] | Tensor,
slice_size: tuple[int, int],
batch_size: int = 8,
dtype: dtype | None = None,
channels_last: bool = False,
) -> Iterator[SliceBatch]
Crop image into mini-batches of patches without leaving its device.
Only batch_size patches exist at a time, so peak memory stays bounded on 8K canvases.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
image
|
Tensor
|
|
required |
windows
|
Sequence[SliceWindow] | Tensor
|
Windows from :func: |
required |
slice_size
|
tuple[int, int]
|
|
required |
batch_size
|
int
|
Patches per yielded batch. |
8
|
dtype
|
dtype | None
|
Cast patches to this dtype; |
None
|
channels_last
|
bool
|
Return batches in |
False
|
SliceBatch ¶
Bases: NamedTuple
CanvasDetections ¶
Bases: NamedTuple
truncation
instance-attribute
¶
[M, 4] bool: box touches that (left, top, right, bottom) edge of its tile and the
edge is interior to the canvas, i.e. the object may continue in a neighbouring tile.
to_canvas ¶
to_canvas(
predictions: Sequence[SlicePrediction],
windows: Tensor,
canvas_size: tuple[int, int],
first_tile_id: int = 0,
edge_tolerance: float = 2.0,
) -> CanvasDetections
Shift per-slice predictions to canvas coordinates and flag truncated boxes.
Boxes are clipped to their window, then shifted by the window origin. Boxes predicted
entirely inside the zero padding become zero-area; :func:~easi.postprocess.fuse
drops them, so this function adds no host sync.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
predictions
|
Sequence[SlicePrediction]
|
One |
required |
windows
|
Tensor
|
|
required |
canvas_size
|
tuple[int, int]
|
|
required |
first_tile_id
|
int
|
Tile id of |
0
|
edge_tolerance
|
float
|
Pixels within which a box counts as touching a tile edge. |
2.0
|
concat_detections ¶
Concatenate detections from several batches.
Timing¶
StageTimer ¶
Accumulates time per named stage across one or more predict calls.
On CUDA, stages are bracketed with CUDA events. That adds no host syncs, so the
measured run behaves like an untimed one; :meth:totals_ms syncs once at the end.
Elsewhere, each stage synchronizes the device before and after and uses wall time,
which serializes the pipeline slightly.
Example::
timer = StageTimer(image.device)
engine.predict(image, detector, timer=timer)
timer.totals_ms() # {"slice": 1.2, "detect": 30.5, "shift": 0.4, "fuse": 2.1}
SAHI compatibility¶
get_sliced_prediction ¶
get_sliced_prediction(
image: Any,
detection_model: Any,
slice_height: int | None = None,
slice_width: int | None = None,
overlap_height_ratio: float = 0.2,
overlap_width_ratio: float = 0.2,
perform_standard_pred: bool = True,
postprocess_type: str = "GREEDYNMM",
postprocess_match_metric: str = "IOS",
postprocess_match_threshold: float = 0.5,
postprocess_class_agnostic: bool = False,
verbose: int = 1,
exclude_classes_by_name: list[str] | None = None,
exclude_classes_by_id: list[int] | None = None,
batch_size: int = 8,
confidence_threshold: float | None = None,
device: device | str | None = None,
edge_cleanup: EdgeCleanup | bool = "balanced",
**ignored: Any,
) -> Any
Sliced prediction with the sahi.predict.get_sliced_prediction signature.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
image
|
Any
|
File path, |
required |
detection_model
|
Any
|
A :class: |
required |
device
|
device | str | None
|
Where to run; defaults to the sahi model's device, else CUDA → MPS → CPU. |
None
|
edge_cleanup
|
EdgeCleanup | bool
|
Mode for removing cut fragment boxes at tile boundaries
( |
'balanced'
|
Returns:
| Type | Description |
|---|---|
Any
|
|
Any
|
class: |
Other arguments match sahi; see the module docstring for differences.