computational-cell-analytics / micro-sam

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

micro_sam.automatic_segmentation error #773

Open aloboa opened 3 days ago

aloboa commented 3 days ago

If I do: (sam) alobo2@alobo-ws:~$ micro_sam.automatic_segmentation -i "/media/alobo2/SP PHD U3/Islandia/Alteration/Cristina_nDann/DI17_2/DI17_2.tif" -o "/media/alobo2/SP PHD U3/Islandia/Alteration/microsam/DI17_2labelsout.tif" -e /home/alobo2/embeddings -m vit_b --tile_shape 1024 1024 --halo 256 256 -d 'cpu' --pred_iou_thresh 0.7 --stability_score_thresh 0.5 --box_nms_thresh 0.5 --min_mask_region_area 10

I get:

progress: : 0it [00:00, ?it/s]
Traceback (most recent call last):
  File "/home/alobo2/miniforge3/envs/sam/bin/micro_sam.automatic_segmentation", line 8, in <module>
    sys.exit(main())
             ^^^^^^
  File "/home/alobo2/micro-sam/micro_sam/automatic_segmentation.py", line 238, in main
    automatic_instance_segmentation(
  File "/home/alobo2/micro-sam/micro_sam/automatic_segmentation.py", line 124, in automatic_instance_segmentation
    masks = segmenter.generate(**generate_kwargs)
            ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/home/alobo2/miniforge3/envs/sam/lib/python3.11/site-packages/torch/utils/_contextlib.py", line 116, in decorate_context
    return func(*args, **kwargs)
           ^^^^^^^^^^^^^^^^^^^^^
  File "/home/alobo2/micro-sam/micro_sam/instance_segmentation.py", line 561, in generate
    masks = self._postprocess_masks(data, min_mask_region_area, box_nms_thresh, crop_nms_thresh, output_mode)
            ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/home/alobo2/micro-sam/micro_sam/instance_segmentation.py", line 243, in _postprocess_masks
    mask_data = self._postprocess_small_regions(
                ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/home/alobo2/micro-sam/micro_sam/instance_segmentation.py", line 209, in _postprocess_small_regions
    mask, changed = amg_utils.remove_small_regions(mask, min_area, mode="holes")
                    ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/home/alobo2/miniforge3/envs/sam/lib/python3.11/site-packages/segment_anything/utils/amg.py", line 274, in remove_small_regions
    import cv2  # type: ignore
    ^^^^^^^^^^
ModuleNotFoundError: No module named 'cv2'
anwai98 commented 3 days ago

Hi @aloboa,

Can you try installing opencv using mamba install -c conda-forge opencv and rerun the script?

aloboa commented 1 day ago

After installing opncv, I still get:

(sam) alobo2@alobo-ws:/media/alobo2/SP PHD U3/Islandia/Alteration/microsam$ micro_sam.automatic_segmentation -i "/media/alobo2/SP PHD U3/Islandia/Alteration/Cristina_nDann/DI17_2/DI17_2.tif" -o "/media/alobo2/SP PHD U3/Islandia/Alteration/microsam/DI17_2labelsout.tif" -e /home/alobo2/embeddings -m vit_b --tile_shape 1024 1024 --halo 256 256 -d 'cpu' --pred_iou_thresh 0.7 --stability_score_thresh 0.5 --box_nms_thresh 0.5 --min_mask_region_area 10
progress: : 0it [00:00, ?it/s]
Traceback (most recent call last):
  File "/home/alobo2/miniforge3/envs/sam/bin/micro_sam.automatic_segmentation", line 8, in <module>
    sys.exit(main())
             ^^^^^^
  File "/home/alobo2/micro-sam/micro_sam/automatic_segmentation.py", line 238, in main
    automatic_instance_segmentation(
  File "/home/alobo2/micro-sam/micro_sam/automatic_segmentation.py", line 124, in automatic_instance_segmentation
    masks = segmenter.generate(**generate_kwargs)
            ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/home/alobo2/miniforge3/envs/sam/lib/python3.11/site-packages/torch/utils/_contextlib.py", line 116, in decorate_context
    return func(*args, **kwargs)
           ^^^^^^^^^^^^^^^^^^^^^
  File "/home/alobo2/micro-sam/micro_sam/instance_segmentation.py", line 561, in generate
    masks = self._postprocess_masks(data, min_mask_region_area, box_nms_thresh, crop_nms_thresh, output_mode)
            ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/home/alobo2/micro-sam/micro_sam/instance_segmentation.py", line 243, in _postprocess_masks
    mask_data = self._postprocess_small_regions(
                ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/home/alobo2/micro-sam/micro_sam/instance_segmentation.py", line 221, in _postprocess_small_regions
    boxes = batched_mask_to_box(masks)
            ^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/home/alobo2/micro-sam/micro_sam/_vendored.py", line 41, in batched_mask_to_box
    assert masks.dtype == torch.bool
           ^^^^^^^^^^^^^^^^^^^^^^^^^
AssertionError

The same command works without the--min_mask_region_area 10 argument.

anwai98 commented 1 day ago

Hi @aloboa,

Can you share with me the CLI script you use? (that will help me reproduce the issue from my side)

aloboa commented 1 day ago

It is just a command typed on the linux terminal: $ micro_sam.automatic_segmentation -i "/media/alobo2/SP PHD U3/Islandia/Alteration/Cristina_nDann/DI17_2/DI17_2.tif" -o "/media/alobo2/SP PHD U3/Islandia/Alteration/microsam/DI17_2labelsout.tif" -e /home/alobo2/embeddings -m vit_b --tile_shape 1024 1024 --halo 256 256 -d 'cuda' --pred_iou_thresh 0.7 --stability_score_thresh 0.5 --box_nms_thresh 0.5 --min_mask_region_area 10

It nicely works in 1'30'' without the --min_mask_region_area 10 argument and without installing opencv. If I install opencv and run with the --min_mask_region_area 10 argument, it takes much longer and finally ends with the error. If I install opencv and run without the --min_mask_region_area 10 argument, no error but the process takes a bit longer (1m45s) than not having installed opencv

anwai98 commented 1 day ago

Thanks for sharing the details @aloboa.

We figured out the issue and should be fixed by https://github.com/computational-cell-analytics/micro-sam/pull/780.

Can you pull the latest commits in our master branch and try again? (if I remember correctly, you have the installation from source, just doing git pull origin master in the micro-sam repo should do the trick)

Let us know if the issue is fixed.