Repository navigation
perf(tilers): keep instance-segmentation tile-merge masks as crops - #673
Conversation
…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>
|
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. |
There was a problem hiding this comment.
🟡 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
left a comment
There was a problem hiding this comment.
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>
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>
There was a problem hiding this comment.
Warning
Copilot couldn't run its full agentic review because it didn't start before the timeout. Make sure your repository has a runner available, or add a copilot-code-review.yml file specifying one with the runs-on attribute. See the docs for more details.
Copilot review overview
4 open issues
The base implementation still builds a full(im_h, im_w)dense mask per detection (then trims),… · New Whenmasksis accessed, this densifies and retains bothself._masksandself.mask_crops,… · NewInstanceSegmentationResultis now constructed withmasks=Nonewhenmask_cropsexist, but the… · New The docstring claims this is an “independent reference”, but it calls_expand_boxfrom production… · New
3 resolved since last review
Review effort: Lite
…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>



Problem
InstanceSegmentationTiler._merge_resultsbuilt 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
(crop, x0, y0)in full-image coordinates:InstanceSegmentationResult.mask_cropsplusimage_shape.result.masksstill works: it builds the dense array on first access, so existing callers see the same output._segm_postprocess_cropand_postprocess_tile_mask_crop(base and DETR models)._segm_postprocessnow wraps the crop helper, so its output is unchanged.Measurements
Caveats
result.masks,__str__, the visualizer andmodels/utils.pystill densify. Callers that want the memory saving must readmask_crops(getitune will, in a follow-up PR).cv2.resize) at 1500 detections; not addressed here.max_pred_number(100) andconfidence_threshold(0.5) cap detections well below what dense images hold; not changed here.🤖 Generated with Claude Code