computational-cell-analytics / micro-sam

Segment Anything for Microscopy
https://computational-cell-analytics.github.io/micro-sam/
MIT License
324 stars 40 forks source link

multi_dimensional_segmentation.merge_instance_segmentation_3D broken #662

Open jamesdimartino opened 1 month ago

jamesdimartino commented 1 month ago

Have been trying to run this function using micro_sam as a python library. Not only when calling it on its own to segment my own segmentations but also when running automatic 3D segmentation through the micro sam pipeline, it breaks, giving segmentation fault core dumped error. I've tried doing some troubleshooting by replicating the source code and have pinpointed the line where it breaks:

INFO:root:[After computing edge costs] Memory usage: 2.15 GB INFO:root:Costs shape: (3719,) INFO:root:Costs sample: [-1.3059922 -2.966726 -1.4197599 3.2087207 -3.6252928] INFO:root:Background costs set successfully INFO:root:[After setting background costs] Memory usage: 2.15 GB ERROR:root:Error during multicut decomposition: Nifty Error: wrong shape Nifty check : array.shape()[0]==graph.edgeIdUpperBound()+1 failed: array.shape()[0] = 3719 graph.edgeIdUpperBound()+1 = 60 in file /home/conda/feedstock_root/build_artifacts/nifty_1685943383516/work/src/python/lib/graph/opt/multicut/multicut_objective.cxx, line 89

ERROR:root:Error during merging for tile 1: Nifty Error: wrong shape Nifty check : array.shape()[0]==graph.edgeIdUpperBound()+1 failed: array.shape()[0] = 3719 graph.edgeIdUpperBound()+1 = 60 in file /home/conda/feedstock_root/build_artifacts/nifty_1685943383516/work/src/python/lib/graph/opt/multicut/multicut_objective.cxx, line 89

I think it's something to do with the edge detector finding too many edges but not sure.

jamesdimartino commented 1 month ago

It also breaks when merging 3d automatic segmentations in napari using it as a napari plugin

jamesdimartino commented 1 month ago

I believe I've fixed the issue - I think the error is in multi_dimensional_segmentation at line 325:

n_nodes = int(slice_segmentation[-1].max() + 1)

should instead be

n_nodes = int(slice_segmentation.max() + 1)

Not sure what the reasoning behind calling only [-1] from slice_segmentation was so not sure if this causes issues elsewhere but the code runs properly now.

constantinpape commented 1 month ago

Thanks for reporting this. I will double check it.