Skip to content

feat(detection): support windowed GeoTIFF reads in InferenceSlicer#2281

Open
madhavcodez wants to merge 1 commit into
roboflow:developfrom
madhavcodez:feat/inference-slicer-geotiff
Open

feat(detection): support windowed GeoTIFF reads in InferenceSlicer#2281
madhavcodez wants to merge 1 commit into
roboflow:developfrom
madhavcodez:feat/inference-slicer-geotiff

Conversation

@madhavcodez
Copy link
Copy Markdown
Contributor

Description

Closes #2027.

InferenceSlicer can now run tiled inference directly on an open rasterio-style dataset, reading each tile via a windowed read instead of loading the whole image into memory. This makes it possible to slice multi-GB aerial/drone GeoTIFFs that don't fit in RAM — the exact pattern InferenceSlicer already implements, just sourced from disk windows rather than an in-memory array.

The original reporter outlined this with a rasterio.windows-based reference implementation; this PR integrates the capability into InferenceSlicer itself (the issue has been open and unclaimed for ~5 months).

import rasterio
import supervision as sv

def callback(tile):  # tile is (H, W, C); select/convert bands as needed
    return model.predict(tile)

slicer = sv.InferenceSlicer(callback, slice_wh=640, overlap_wh=100)

with rasterio.open("large_orthomosaic.tif") as dataset:
    detections = slicer(dataset)

Design

  • rasterio stays fully optional — the core library imports zero rasterio symbols. Datasets are detected by duck typing (_is_windowed_raster: a callable .read plus .crs/.width/.height), and tiles are read with rasterio's tuple-window form read(window=((row_start, row_stop), (col_start, col_stop))). Installable via pip install "supervision[geotiff]" (new optional extra).
  • Back-compat: the existing in-memory NumPy/PIL path is unchanged — same crop_image, resolution_wh, compact_masks, out-of-bounds warning, and move_detections semantics. All existing InferenceSlicer tests pass unmodified.
  • CRS validation: a geographic (non-projected) CRS raises ValueError, since pixel-space tiling on lat/lon is meaningless; crs is None (a plain TIFF) is allowed.
  • Tiles preserve the source dtype (e.g. uint16) and all bands; selecting/converting is the callback's responsibility (documented in the __call__ docstring).

Tests

tests/detection/tools/test_inference_slicer_geotiff.py — runs without rasterio installed via a lightweight fake dataset, and includes a real rasterio.MemoryFile integration test guarded by importorskip:

  • windowed path produces the same merged detections as the in-memory array path (no overlap and with overlap_wh > 0);
  • each tile handed to the callback is byte-identical to the crop_image tile;
  • crs=None works, geographic CRS raises, projected CRS does not;
  • source dtype (uint16) is preserved through to the callback.

Notes for reviewers

  • Scope: this accepts an already-open dataset rather than a file path, deliberately, so that no rasterio import leaks into core. Happy to add a str | Path convenience overload (opening/closing the dataset internally) in a follow-up if you'd prefer that ergonomics.
  • The accepted input type is expressed via a small structural WindowedRasterDataset Protocol so static type checkers accept a rasterio dataset without supervision depending on rasterio's types.
  • Changelog updated under UnReleased.

@madhavcodez madhavcodez requested a review from SkalskiP as a code owner May 29, 2026 07:15
@codecov
Copy link
Copy Markdown

codecov Bot commented May 29, 2026

Codecov Report

✅ All modified and coverable lines are covered by tests.
✅ Project coverage is 79%. Comparing base (918b613) to head (7354a40).

Additional details and impacted files
@@           Coverage Diff           @@
##           develop   #2281   +/-   ##
=======================================
  Coverage       79%     79%           
=======================================
  Files           66      66           
  Lines         8567    8584   +17     
=======================================
+ Hits          6726    6744   +18     
+ Misses        1841    1840    -1     
🚀 New features to boost your workflow:
  • ❄️ Test Analytics: Detect flaky tests, report on failures, and find test suite problems.

InferenceSlicer can now accept an open rasterio-style dataset and read each tile via a windowed read instead of loading the whole image into memory, enabling tiled inference on multi-GB aerial/drone GeoTIFFs. Detection is duck-typed so rasterio stays an optional dependency (supervision[geotiff]) and the library imports no rasterio symbols. Adds CRS projected validation and tests. Closes roboflow#2027.
@madhavcodez madhavcodez force-pushed the feat/inference-slicer-geotiff branch from a3a44a2 to 7354a40 Compare May 29, 2026 07:22
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Support for TIF files in InferenceSlicer

1 participant