Commit a3d5160
Perf: faster get_largest_connected_component_mask (bincount + LUT gather) (#8978)
Fixes #8974 .
### Description
`get_largest_connected_component_mask` (`monai/transforms/utils.py`)
ranked component sizes by gathering every non-background voxel through
`lib.nonzero(features)` and then built the mask with `lib.isin` over the
whole label field. Both allocate large transient arrays. This counts
labels with a single full-field `lib.bincount` (zeroing index 0 to drop
background), and builds the mask with a boolean lookup-table gather
(`keep[features]`) instead of `isin`. Output is bit-identical, verified
across 2D and 3D at several foreground fractions and component counts,
and the numpy and cupy/cucim paths are covered unchanged.
The `isin` replacement is the dominant win on both time and peak memory;
the `bincount` change removes the redundant `nonzero` index arrays and
gathered copy on top.
| case | metric | before | after | improvement |
|---|---|---|---|---|
| 3D 192^3, fg 50% | time | 82.0 ms | 8.6 ms | 9.5x |
| 3D 192^3, fg 50% | peak transient mem | 108.0 MB | 7.2 MB | 15x |
| 2D 1024^2, fg 60% | time | 21.5 ms | 1.8 ms | 11.7x |
| 2D 1024^2, fg 60% | peak transient mem | 18.0 MB | 1.4 MB | 12.9x |
| 3D 128^3, fg 5% (48k comps) | time | 10.7 ms | 6.2 ms | 1.7x |
### Types of changes
- [x] Non-breaking change (fix or new feature that would not break
existing functionality).
Signed-off-by: Soumya Snigdha Kundu <soumya_snigdha.kundu@kcl.ac.uk>
Co-authored-by: Eric Kerfoot <17726042+ericspod@users.noreply.github.com>1 parent 8885fcb commit a3d5160
1 file changed
Lines changed: 9 additions & 9 deletions
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
1224 | 1224 | | |
1225 | 1225 | | |
1226 | 1226 | | |
1227 | | - | |
1228 | | - | |
1229 | | - | |
1230 | | - | |
1231 | | - | |
1232 | | - | |
1233 | | - | |
1234 | | - | |
1235 | | - | |
| 1227 | + | |
| 1228 | + | |
| 1229 | + | |
| 1230 | + | |
| 1231 | + | |
| 1232 | + | |
| 1233 | + | |
| 1234 | + | |
| 1235 | + | |
1236 | 1236 | | |
1237 | 1237 | | |
1238 | 1238 | | |
| |||
0 commit comments