Skip to content

perf(tilers): keep instance-segmentation tile-merge masks as crops - #673

Merged
kprokofi merged 6 commits into
masterfrom
kprokofi/is-tiler-memory
Nov 27, 2026
Merged

kprokofi merged 6 commits into
masterfrom
kprokofi/is-tiler-memory

Conversation

@kprokofi

Copy link
Copy Markdown
Contributor

Problem

InstanceSegmentationTiler._merge_results built one full-image canvas per kept object and stacked them into a dense (N, H, W) array. On a 5000x4000 image with ~1000 objects that is ~21 GB peak and ~2 minutes in the merge alone.

Change

  • Merged results keep per-object (crop, x0, y0) in full-image coordinates: InstanceSegmentationResult.mask_crops plus image_shape.
  • result.masks still works: it builds the dense array on first access, so existing callers see the same output.
  • New helpers _segm_postprocess_crop and _postprocess_tile_mask_crop (base and DETR models). _segm_postprocess now wraps the crop helper, so its output is unchanged.

Measurements

  • Synthetic merge, 5000x4000, ~1000 objects: master 122 s / +21,042 MB, this PR 0.17 s / +11 MB.
  • Real MaskRCNN IR on a 5000x4000 mosaic (CPU, tile 400/0.2, 100 objects): +3415 MB on master, +1249 MB here, same time (~138 s, inference bound).
  • Real RF-DETR IR (getitune export, 5000x4000 image, 329 objects): boxes, scores, labels and dense masks are bit-identical to master.
  • Unit tests: 1281 passed. Added a test that crops densify to the old dense output.

Caveats

  • result.masks, __str__, the visualizer and models/utils.py still densify. Callers that want the memory saving must read mask_crops (getitune will, in a follow-up PR).
  • Each kept object still costs ~3.5 ms in mask postprocessing (mostly cv2.resize) at 1500 detections; not addressed here.
  • Default max_pred_number (100) and confidence_threshold (0.5) cap detections well below what dense images hold; not changed here.

🤖 Generated with Claude Code

kprokofi and others added 3 commits November 26, 2026 01:33
…dense stack

Merged IS results held N full-image uint8 canvases (GBs on 20 MP inputs).
Store per-object crops with offsets; result.masks densifies lazily so
existing callers are unchanged.

Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com>
Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com>
Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com>
@leoll2

leoll2 commented Nov 26, 2026

Copy link
Copy Markdown
Contributor

When this PR is merged, please backport it to https://github.com/open-edge-platform/geti/tree/develop/libraries/model_api ; Evgeny is currently migrating this codebase to the geti repo.

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🟡 Changes recommended

Full-image DETR masks can still retain about 2 GB for 100 detections, and unresolved subclass-decoding compatibility and non-independent regression tests leave dense-output behavior insufficiently protected.

3 open issues
What changed in this PR

This PR changes instance-segmentation tile merging to retain positioned mask crops instead of eagerly stacking full-image masks, reducing memory use on large images while preserving access through result.masks.

Changes:

  • Adds crop storage, image dimensions, and lazy dense-mask construction to segmentation results.
  • Introduces crop-returning postprocessing helpers and uses them during tile merging.
  • Updates tiler test mocks and adds a crop-to-dense comparison test.
File Description
model_api/​tests/​unit/​tilers/​test_instance_segmentation_tiler.py Updates mocks and tests lazy dense-mask reconstruction.
model_api/​src/​model_api/​tilers/​instance_segmentation.py Collects positioned mask crops when merging detections.
model_api/​src/​model_api/​models/​result/​segmentation.py Stores crops and constructs dense masks on demand.
model_api/​src/​model_api/​models/​instance_segmentation.py Adds crop postprocessing hooks and retains the dense-mask wrapper.

Review effort: Balanced

leoll2
leoll2 previously approved these changes Nov 26, 2026

@leoll2 leoll2 left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

If Geti needs result.masks and result.masks still needs to build the dense array, where does the performance gain come from? Is it because _merge_results now doesn't have to resize every single mask to the full resolution?

…nt legacy test

- DETR crop hook trims to the nonzero extent (full-image tile no longer keeps an (H, W) mask per detection)
- base crop hook goes through _postprocess_tile_mask; the direct crop path lives on MaskRCNNModel
- dense-parity tests compare against a verbatim copy of the old implementation, incl. boundary-clipped boxes

Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com>
@kprokofi

Copy link
Copy Markdown
Contributor Author

If Geti needs result.masks and result.masks still needs to build the dense array, where does the performance gain come from? Is it because _merge_results now doesn't have to resize every single mask to the full resolution?

From Geti's side the gain comes from never building the dense array, not from making it cheaper. Geti doesn't read result.masks. getitune's OpenVINO forward_tiles reads mask_crops and converts each crop straight to RLE, which is what the metric needs. No (N, H, W) array is ever created, so memory no longer grows with image size or detection count.

Before this PR, _merge_results built one full-image canvas per kept object and stacked them. On a 5000x4000 image that is about 20 MB per object, so 1000 objects is about 20 GB, and forward_tiles then made a second bool copy. That was the memory blow-up and the slow merge.

It isn't the resize. cv2.resize always went to each object's box size, never full resolution, and that cost is unchanged. What disappears is allocating, zeroing, pasting and stacking the full-size canvases.

Measured through Geti's OVEngine.test on 30 images of 5000x4000 (tile 400/0.2, CPU): peak 8.0 GB across the process tree, 587 s, mAP50 0.935 (torch: 0.936).

Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com>
leoll2
leoll2 previously approved these changes Nov 27, 2026

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

…tationResult inputs, decouple legacy test helper

The default _postprocess_tile_mask_crop builds a full-image canvas per detection, so its docstring now says subclasses on large tiled images should override it. InstanceSegmentationResult accepts masks=None only together with mask_crops and image_shape, and raises ValueError otherwise. The legacy-parity test now uses its own copy of _expand_box instead of the production one.

Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com>
@kprokofi
kprokofi merged commit 647b3d5 into master Nov 27, 2026
25 checks passed
@kprokofi
kprokofi deleted the kprokofi/is-tiler-memory branch November 27, 2026 14:18
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

python python related changes tests Related to tests

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants