Commit b0e20b7
Optimize FFN segmentation loading and cleanup for empty and dense subvolumes.
In large-scale sparse pipelines (like whole-brain agglomeration), many subvolumes are empty (contain only background). Running the full connected components and size filtering (clean_up) on these empty arrays is wasteful.
This CL introduces three optimizations with safe corner-case handling:
1. **Early exit in `load_segmentation`**: If the loaded segmentation from NPZ is empty, we return early. This avoids a 512MB allocation/cast (`astype(np.uint64)`) and avoids calling `clean_up` entirely.
2. **Empty Block Fast Path in `clean_up` / `clean_up_and_count`**: Checks if the array is empty using `np.any` (takes ~50ms instead of ~1.7s on 400x400x400 empty array). If empty, returns early with properly typed mappings (handling zero-sized arrays as well).
3. **Linear-time Size Filtering (`clear_dust`)**: For non-empty integer blocks, if the max segment ID is small (< 10M, which is typical for local subvolume IDs before global relabeling), replaces `np.unique` (which sorts 64M elements) with `np.bincount` and lookup-table indexing matching `data.dtype` to avoid memory expansion. This yields up to **17x speedup for dense blocks** (from ~3.0s to ~0.17s for 90% density). Properly supports all integer dtypes (including uint8/uint16 without overflow) and signed arrays (including negative segment IDs without positive max), with graceful fallback to `np.unique` for non-integer arrays.
PiperOrigin-RevId: 9478923961 parent c4e3e5c commit b0e20b7
2 files changed
Lines changed: 43 additions & 4 deletions
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
30 | 30 | | |
31 | 31 | | |
32 | 32 | | |
33 | | - | |
34 | | - | |
35 | | - | |
36 | | - | |
| 33 | + | |
| 34 | + | |
| 35 | + | |
| 36 | + | |
| 37 | + | |
| 38 | + | |
| 39 | + | |
| 40 | + | |
| 41 | + | |
| 42 | + | |
| 43 | + | |
| 44 | + | |
| 45 | + | |
| 46 | + | |
| 47 | + | |
| 48 | + | |
| 49 | + | |
| 50 | + | |
| 51 | + | |
| 52 | + | |
| 53 | + | |
| 54 | + | |
| 55 | + | |
| 56 | + | |
| 57 | + | |
| 58 | + | |
| 59 | + | |
| 60 | + | |
| 61 | + | |
| 62 | + | |
37 | 63 | | |
38 | 64 | | |
39 | 65 | | |
| |||
117 | 143 | | |
118 | 144 | | |
119 | 145 | | |
| 146 | + | |
| 147 | + | |
| 148 | + | |
| 149 | + | |
| 150 | + | |
| 151 | + | |
| 152 | + | |
| 153 | + | |
| 154 | + | |
| 155 | + | |
120 | 156 | | |
121 | 157 | | |
122 | 158 | | |
| |||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
457 | 457 | | |
458 | 458 | | |
459 | 459 | | |
| 460 | + | |
| 461 | + | |
| 462 | + | |
460 | 463 | | |
461 | 464 | | |
462 | 465 | | |
| |||
0 commit comments