Skip to content
Edit this page

Adapters

An adapter wraps a model so the engine can call it on a batch of tiles. Any callable that maps a [B, 3, h, w] batch to B tuples of (boxes, scores, labels) works as well.

TorchDetector

Bases: Protocol

Callable that runs detection on a batch of tiles.

UltralyticsAdapter

UltralyticsAdapter(
    model: Any,
    conf: float = 0.25,
    iou: float = 0.7,
    max_det: int = 300,
    classes: list[int] | None = None,
    multi_label: bool = False,
    half: bool | Literal["auto"] = "auto",
    imgsz: int | None = 640,
    direct: bool = True,
    channels_last: bool | Literal["auto"] = "auto",
    compile: bool | Literal["auto"] = "auto",
)

Feed tile batches to an Ultralytics model as tensors, skipping its NumPy path.

Tiles are scaled on-device so the longer side equals imgsz, zero-padded bottom/right to a stride multiple, and boxes are scaled back to tile pixels.

Parameters:

Name Type Description Default
model Any

Weights path/name ("yolo11n.pt"), exported model (.engine, .onnx), or an ultralytics.YOLO object (modified in place on device/precision moves).

required
conf float

Confidence threshold.

0.25
iou float

Per-tile NMS IoU threshold.

0.7
max_det int

Maximum detections per tile.

300
classes list[int] | None

Keep only these class ids.

None
multi_label bool

Keep all classes above conf per box (YOLO NMS heads with direct=True only).

False
half bool | Literal['auto']

"auto" uses FP16 on CUDA for .pt models and FP32 elsewhere; True forces FP16 on CUDA; False forces FP32.

'auto'
imgsz int | None

Longer-side network input size, or None to run tiles at their own size.

640
direct bool

Run the network and decoding directly instead of model.predict().

True
channels_last bool | Literal['auto']

Use torch.channels_last memory format ("auto" enables on CUDA FP16 for NVIDIA GPUs).

'auto'
compile bool | Literal['auto']

"auto" compiles the direct network with torch.compile on CUDA and falls back to eager on failure or output mismatch.

'auto'

names property

names: dict[int, str]

Class id → name, from the model.

at_input_size

at_input_size(size: int) -> UltralyticsAdapter

This adapter at network input size (longer side), for EASI's full_frame_size pass. It shares the model and every setting except imgsz and runs eagerly: one image per call, whose shape changes with the frame's, so a compiled graph would recompile per shape (and pad the image to the tiles' batch); eager costs the same there (RTX 3090). Exported models run only at the size they were built for and raise.

detect_flat

detect_flat(batch: Tensor) -> FlatPrediction

The boxes of __call__ in one piece, (boxes, scores, labels, tile), with tile each box's index in batch (same boxes, same order). EASI prefers this: YOLO's batched NMS already has every box's tile, so the per-tile split, its host sync and the join in :func:~easi.slicing.to_canvas are skipped (0.5 / 2.4 / 9.7 ms less per 1080p / 4K / 8K frame, yolo11n on an RTX 3070). Other heads (RT-DETR, NMS-free, direct=False) give their per-tile lists joined.

TorchvisionAdapter

TorchvisionAdapter(
    model: Module,
    score_threshold: float = 0.3,
    device: device | str | None = None,
    names: dict[int, str] | None = None,
    half: bool = False,
)

Wrap a torchvision detection model.

These models normalize and resize internally and return boxes in input pixels, so tiles are passed through unchanged.

Parameters:

Name Type Description Default
model Module

e.g. torchvision.models.detection.fasterrcnn_resnet50_fpn_v2(weights=...).

required
score_threshold float

Drop detections below this score.

0.3
device device | str | None

Move the model here; defaults to wherever it already is.

None
names dict[int, str] | None

Optional class id → name map, e.g. from weights.meta["categories"].

None
half bool

Run the model under FP16 autocast (CUDA; MPS with PyTorch 2.5+): PyTorch's mixed precision, which keeps precision-sensitive ops (box decoding, softmax) in float32. Boxes and scores are returned as float32. On the CPU it needs PyTorch 2.3+: with an older PyTorch, half=True on the CPU raises a ValueError here.

False

Raises:

Type Description
ValueError

half=True for a model on the CPU with PyTorch older than 2.3.

HuggingFaceAdapter

HuggingFaceAdapter(
    model: Any,
    processor: Any = None,
    conf: float = 0.3,
    decode: Literal["processor", "max"] = "processor",
    revision: str | None = None,
    half: bool = False,
)

Run a transformers object-detection model on tile batches, on the device.

The image processor's resize and normalization are reproduced with tensor ops, so tiles never go through PIL or NumPy. Boxes come back in tile pixels.

Parameters:

Name Type Description Default
model Any

A model id ("PekingU/rtdetr_r50vd") or an AutoModelForObjectDetection.

required
processor Any

The matching image processor; loaded from model when it is an id.

None
conf float

Score threshold.

0.3
decode Literal['processor', 'max']

"processor" uses the processor's post_process_object_detection (the model authors' decoding). "max" keeps each query's best class: a sigmoid for RT-DETR and RF-DETR-style models, a softmax without the no-object class for DETR-style models. That is how obss/sahi's HuggingfaceDetectionModel decodes, which keeps head-to-head comparisons about slicing alone.

'processor'
revision str | None

Model revision when loading by id (e.g. "no_timm" for facebook/detr-resnet-50 without the timm dependency).

None
half bool

Run the model under FP16 autocast (CUDA; MPS with PyTorch 2.5+); decoding stays float32.

False

names property

names: dict[int, str]

Class id → name, from the model config.

preprocess

preprocess(batch: Tensor) -> torch.Tensor

[B, 3, h, w] in [0, 1] → the processor's pixel_values (no padding needed: every tile in a batch has the same size).

export_tensorrt

export_tensorrt(
    model: str = "yolo11n.pt",
    batch: int = 16,
    half: bool = True,
    imgsz: int = 640,
    device: int = 0,
    dynamic: bool = False,
) -> str

Export an Ultralytics YOLO model to a TensorRT engine and return its file path.