Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

micro_sam.automatic_segmentation error #773

Closed
aloboa opened this issue Nov 12, 2024 · 25 comments
Closed

micro_sam.automatic_segmentation error #773

aloboa opened this issue Nov 12, 2024 · 25 comments

Comments

@aloboa
Copy link

aloboa commented Nov 12, 2024

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
Copy link
Contributor

anwai98 commented Nov 12, 2024

Hi @aloboa,

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

@aloboa
Copy link
Author

aloboa commented Nov 14, 2024

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
Copy link
Contributor

anwai98 commented Nov 14, 2024

Hi @aloboa,

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

@aloboa
Copy link
Author

aloboa commented Nov 14, 2024

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
Copy link
Contributor

anwai98 commented Nov 14, 2024

Thanks for sharing the details @aloboa.

We figured out the issue and should be fixed by #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.

@aloboa
Copy link
Author

aloboa commented Nov 18, 2024

I do:

(base) alobo2@alobo-ws:~$ cd micro-sam
(base) alobo2@alobo-ws:~/micro-sam$ git pull origin master
remote: Enumerating objects: 26, done.
remote: Counting objects: 100% (26/26), done.
remote: Compressing objects: 100% (19/19), done.
remote: Total 26 (delta 11), reused 18 (delta 7), pack-reused 0 (from 0)
Unpacking objects: 100% (26/26), 507.43 KiB | 2.22 MiB/s, done.
From https://github.com/computational-cell-analytics/micro-sam
 * branch            master     -> FETCH_HEAD
   b9f3f87..962e34d  master     -> origin/master
Updating b9f3f87..962e34d
Fast-forward
 doc/cli_tools.md                       |  20 ++++++++
 examples/annotator_2d.py               |  11 +++--
 examples/automatic_segmentation.py     | 150 +++++++++++++++++++++++++++++++++++++++++++++++++++++++
 micro_sam/__init__.py                  |   1 +
 micro_sam/_vendored.py                 |   4 +-
 micro_sam/automatic_segmentation.py    |   7 ++-
 micro_sam/instance_segmentation.py     |   7 +--
 notebooks/automatic_segmentation.ipynb | 242 +++++++++++++++++++++++++++++++++++++++++++++--------------------------------------------
 8 files changed, 306 insertions(+), 136 deletions(-)
 create mode 100644 doc/cli_tools.md
 create mode 100644 examples/automatic_segmentation.py
(base) alobo2@alobo-ws:~/micro-sam$ mamba activate micro-sam
(micro-sam) alobo2@alobo-ws:~/micro-sam$ pip install -e .

And then:

(micro-sam) alobo2@alobo-ws:~/micro-sam$ 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
2024-11-18 09:10:29.877378: I tensorflow/core/util/port.cc:153] oneDNN custom operations are on. You may see slightly different numerical results due to floating-point round-off errors from different computation orders. To turn them off, set the environment variable `TF_ENABLE_ONEDNN_OPTS=0`.
2024-11-18 09:10:29.881342: I external/local_xla/xla/tsl/cuda/cudart_stub.cc:32] Could not find cuda drivers on your machine, GPU will not be used.
2024-11-18 09:10:29.913511: I external/local_xla/xla/tsl/cuda/cudart_stub.cc:32] Could not find cuda drivers on your machine, GPU will not be used.
2024-11-18 09:10:29.940442: E external/local_xla/xla/stream_executor/cuda/cuda_fft.cc:485] Unable to register cuFFT factory: Attempting to register factory for plugin cuFFT when one has already been registered
2024-11-18 09:10:29.964688: E external/local_xla/xla/stream_executor/cuda/cuda_dnn.cc:8454] Unable to register cuDNN factory: Attempting to register factory for plugin cuDNN when one has already been registered
2024-11-18 09:10:29.972461: E external/local_xla/xla/stream_executor/cuda/cuda_blas.cc:1452] Unable to register cuBLAS factory: Attempting to register factory for plugin cuBLAS when one has already been registered
2024-11-18 09:10:30.014176: I tensorflow/core/platform/cpu_feature_guard.cc:210] This TensorFlow binary is optimized to use available CPU instructions in performance-critical operations.
To enable the following instructions: AVX2 AVX_VNNI FMA, in other operations, rebuild TensorFlow with the appropriate compiler flags.
2024-11-18 09:10:30.565607: W tensorflow/compiler/tf2tensorrt/utils/py_utils.cc:38] TF-TRT Warning: Could not find TensorRT
/home/alobo2/miniforge3/envs/micro-sam/lib/python3.11/site-packages/kornia/feature/lightglue.py:44: FutureWarning: `torch.cuda.amp.custom_fwd(args...)` is deprecated. Please use `torch.amp.custom_fwd(args..., device_type='cuda')` instead.
  @torch.cuda.amp.custom_fwd(cast_inputs=torch.float32)
Traceback (most recent call last):
  File "/home/alobo2/miniforge3/envs/micro-sam/bin/micro_sam.automatic_segmentation", line 8, in <module>
    sys.exit(main())
             ^^^^^^
  File "/home/alobo2/micro-sam/micro_sam/automatic_segmentation.py", line 229, in main
    predictor, segmenter = get_predictor_and_segmenter(
                           ^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/home/alobo2/micro-sam/micro_sam/automatic_segmentation.py", line 40, in get_predictor_and_segmenter
    device = util.get_device(device=device)
             ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/home/alobo2/micro-sam/micro_sam/util.py", line 199, in get_device
    raise RuntimeError("PyTorch CUDA backend is not available.")
RuntimeError: PyTorch CUDA backend is not available.
(micro-sam) alobo2@alobo-ws:~/micro-sam$ 

@aloboa
Copy link
Author

aloboa commented Nov 18, 2024

Big problem: micro_sam.automatic_segmentation was working with my gpu using the former version if the --min_mask_region_area argument was omitted (which was not a big issue), but what used to work does not work any more with the newer version (big problem...):



(micro-sam) alobo2@alobo-ws:~/micro-sam$ 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
2024-11-18 09:20:08.272852: I tensorflow/core/util/port.cc:153] oneDNN custom operations are on. You may see slightly different numerical results due to floating-point round-off errors from different computation orders. To turn them off, set the environment variable `TF_ENABLE_ONEDNN_OPTS=0`.
2024-11-18 09:20:08.273393: I external/local_xla/xla/tsl/cuda/cudart_stub.cc:32] Could not find cuda drivers on your machine, GPU will not be used.
2024-11-18 09:20:08.275084: I external/local_xla/xla/tsl/cuda/cudart_stub.cc:32] Could not find cuda drivers on your machine, GPU will not be used.
2024-11-18 09:20:08.280061: E external/local_xla/xla/stream_executor/cuda/cuda_fft.cc:485] Unable to register cuFFT factory: Attempting to register factory for plugin cuFFT when one has already been registered
2024-11-18 09:20:08.288010: E external/local_xla/xla/stream_executor/cuda/cuda_dnn.cc:8454] Unable to register cuDNN factory: Attempting to register factory for plugin cuDNN when one has already been registered
2024-11-18 09:20:08.290346: E external/local_xla/xla/stream_executor/cuda/cuda_blas.cc:1452] Unable to register cuBLAS factory: Attempting to register factory for plugin cuBLAS when one has already been registered
2024-11-18 09:20:08.296212: I tensorflow/core/platform/cpu_feature_guard.cc:210] This TensorFlow binary is optimized to use available CPU instructions in performance-critical operations.
To enable the following instructions: AVX2 AVX_VNNI FMA, in other operations, rebuild TensorFlow with the appropriate compiler flags.
2024-11-18 09:20:08.682775: W tensorflow/compiler/tf2tensorrt/utils/py_utils.cc:38] TF-TRT Warning: Could not find TensorRT
/home/alobo2/miniforge3/envs/micro-sam/lib/python3.11/site-packages/kornia/feature/lightglue.py:44: FutureWarning: `torch.cuda.amp.custom_fwd(args...)` is deprecated. Please use `torch.amp.custom_fwd(args..., device_type='cuda')` instead.
  @torch.cuda.amp.custom_fwd(cast_inputs=torch.float32)
Traceback (most recent call last):
  File "/home/alobo2/miniforge3/envs/micro-sam/bin/micro_sam.automatic_segmentation", line 8, in <module>
    sys.exit(main())
             ^^^^^^
  File "/home/alobo2/micro-sam/micro_sam/automatic_segmentation.py", line 229, in main
    predictor, segmenter = get_predictor_and_segmenter(
                           ^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/home/alobo2/micro-sam/micro_sam/automatic_segmentation.py", line 40, in get_predictor_and_segmenter
    device = util.get_device(device=device)
             ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/home/alobo2/micro-sam/micro_sam/util.py", line 199, in get_device
    raise RuntimeError("PyTorch CUDA backend is not available.")
RuntimeError: PyTorch CUDA backend is not available.

Should I go back to
mamba env create -f environment_gpu.yaml
again?

@anwai98
Copy link
Contributor

anwai98 commented Nov 18, 2024

Hi @aloboa,

Can you try running in the code via terminal in the activated micro-sam environment to verify the PyTorch installation?

python -c "import torch; print(torch.cuda.is_available())"

@aloboa
Copy link
Author

aloboa commented Nov 19, 2024

It is FALSE, but I remember it was TRUE before the upgrade.
Also, the option GPU (or CUDA, do not remember) in the GUI for "Annotator 2d" is not available now as it used to be.

@constantinpape
Copy link
Contributor

Hi @aloboa ,
this means you have now installed a pytorch version without GPU support. Please refer to https://pytorch.org/ for how to install a pytorch with CUDA support for your system. micro_sam is compatible with any recent pytorch version.

@aloboa
Copy link
Author

aloboa commented Nov 19, 2024

I do not understand. Why was the GPU working with the previous version of micro-sam? This problem raises after I do the
git pull origin master

@constantinpape
Copy link
Contributor

constantinpape commented Nov 19, 2024

I do not understand. Why was the GPU working with the previous version of micro-sam? This problem raises after I do the git pull origin master

That is indeed quite weird. micro_sam on its own can't affect the GPU installation.

In this case, I would suggest to install a new environment via mamba env create -f environment_gpu.yaml.
Then install microsam via pip install -e . in that environment and check if everything works as expected.

@aloboa
Copy link
Author

aloboa commented Nov 19, 2024

Also, I get error even if selecting 'cpu':

(micro-sam) alobo2@alobo-ws:~$ micro_sam.automatic_segmentation -i "/media/alobo2/SP PHD U3/Islandia/Alteration/Cristina_nDann/DI17_3/DI17_3.tif" -o "/media/alobo2/SP PHD U3/Islandia/Alteration/microsam/DI17_3_autoseg/DI17_3_autoseg_03.tif" -e /home/alobo2/embeddings -c /home/alobo2/micro-sam/checkpoints/sam_model/best.pt -m vit_b --tile_shape 1024 1024 --halo 256 256 -d 'cpu' --pred_iou_thresh 0.1 --stability_score_thresh 0.95 --box_nms_thresh 0.7
2024-11-19 09:18:37.872676: I tensorflow/core/util/port.cc:153] oneDNN custom operations are on. You may see slightly different numerical results due to floating-point round-off errors from different computation orders. To turn them off, set the environment variable `TF_ENABLE_ONEDNN_OPTS=0`.
2024-11-19 09:18:37.873238: I external/local_xla/xla/tsl/cuda/cudart_stub.cc:32] Could not find cuda drivers on your machine, GPU will not be used.
2024-11-19 09:18:37.874964: I external/local_xla/xla/tsl/cuda/cudart_stub.cc:32] Could not find cuda drivers on your machine, GPU will not be used.
2024-11-19 09:18:37.880015: E external/local_xla/xla/stream_executor/cuda/cuda_fft.cc:485] Unable to register cuFFT factory: Attempting to register factory for plugin cuFFT when one has already been registered
2024-11-19 09:18:37.888047: E external/local_xla/xla/stream_executor/cuda/cuda_dnn.cc:8454] Unable to register cuDNN factory: Attempting to register factory for plugin cuDNN when one has already been registered
2024-11-19 09:18:37.890387: E external/local_xla/xla/stream_executor/cuda/cuda_blas.cc:1452] Unable to register cuBLAS factory: Attempting to register factory for plugin cuBLAS when one has already been registered
2024-11-19 09:18:37.896396: I tensorflow/core/platform/cpu_feature_guard.cc:210] This TensorFlow binary is optimized to use available CPU instructions in performance-critical operations.
To enable the following instructions: AVX2 AVX_VNNI FMA, in other operations, rebuild TensorFlow with the appropriate compiler flags.
2024-11-19 09:18:38.282024: W tensorflow/compiler/tf2tensorrt/utils/py_utils.cc:38] TF-TRT Warning: Could not find TensorRT
/home/alobo2/miniforge3/envs/micro-sam/lib/python3.11/site-packages/kornia/feature/lightglue.py:44: FutureWarning: `torch.cuda.amp.custom_fwd(args...)` is deprecated. Please use `torch.amp.custom_fwd(args..., device_type='cuda')` instead.
  @torch.cuda.amp.custom_fwd(cast_inputs=torch.float32)
Traceback (most recent call last):
  File "/home/alobo2/miniforge3/envs/micro-sam/bin/micro_sam.automatic_segmentation", line 8, in <module>
    sys.exit(main())
             ^^^^^^
  File "/home/alobo2/micro-sam/micro_sam/automatic_segmentation.py", line 237, in main
    automatic_instance_segmentation(
  File "/home/alobo2/micro-sam/micro_sam/automatic_segmentation.py", line 111, in automatic_instance_segmentation
    image_embeddings = util.precompute_image_embeddings(
                       ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/home/alobo2/micro-sam/micro_sam/util.py", line 846, in precompute_image_embeddings
    _check_saved_embeddings(input_, predictor, f, save_path, tile_shape, halo)
  File "/home/alobo2/micro-sam/micro_sam/util.py", line 758, in _check_saved_embeddings
    raise RuntimeError(
RuntimeError: Embeddings file /home/alobo2/embeddings is invalid due to mismatch in data_signature: 856c3630427d76d381b319f635e0a043df589b8d != dde76ebe804bc0a72d966783f553da2a06b4343a. Please recompute embeddings in a new file.

@constantinpape
Copy link
Contributor

This is because you are trying to cache embeddings, but the embedding file exists already and the embeddings were computed for a different image. Removing the file /home/alobo2/embeddings or not specifying an embedding path will fix this.

@aloboa
Copy link
Author

aloboa commented Nov 19, 2024

Same error with a simpler command:

micro_sam.automatic_segmentation -i "/media/alobo2/SP PHD U3/Islandia/Alteration/Cristina_nDann/DI17_3/DI17_3.tif" -o "/media/alobo2/SP PHD U3/Islandia/Alteration/microsam/DI17_3_autoseg/DI17_3_autoseg_03.tif" -m vit_b --tile_shape 1024 1024 --halo 256 256 -d 'cpu' --pred_iou_thresh 0.1 --stability_score_thresh 0.95 --box_nms_thresh 0.7
2024-11-19 09:49:40.903321: I tensorflow/core/util/port.cc:153] oneDNN custom operations are on. You may see slightly different numerical results due to floating-point round-off errors from different computation orders. To turn them off, set the environment variable `TF_ENABLE_ONEDNN_OPTS=0`.
2024-11-19 09:49:40.903888: I external/local_xla/xla/tsl/cuda/cudart_stub.cc:32] Could not find cuda drivers on your machine, GPU will not be used.
2024-11-19 09:49:40.905622: I external/local_xla/xla/tsl/cuda/cudart_stub.cc:32] Could not find cuda drivers on your machine, GPU will not be used.
2024-11-19 09:49:40.910601: E external/local_xla/xla/stream_executor/cuda/cuda_fft.cc:485] Unable to register cuFFT factory: Attempting to register factory for plugin cuFFT when one has already been registered
2024-11-19 09:49:40.918613: E external/local_xla/xla/stream_executor/cuda/cuda_dnn.cc:8454] Unable to register cuDNN factory: Attempting to register factory for plugin cuDNN when one has already been registered
2024-11-19 09:49:40.920984: E external/local_xla/xla/stream_executor/cuda/cuda_blas.cc:1452] Unable to register cuBLAS factory: Attempting to register factory for plugin cuBLAS when one has already been registered
2024-11-19 09:49:40.926898: I tensorflow/core/platform/cpu_feature_guard.cc:210] This TensorFlow binary is optimized to use available CPU instructions in performance-critical operations.
To enable the following instructions: AVX2 AVX_VNNI FMA, in other operations, rebuild TensorFlow with the appropriate compiler flags.
2024-11-19 09:49:41.314533: W tensorflow/compiler/tf2tensorrt/utils/py_utils.cc:38] TF-TRT Warning: Could not find TensorRT
/home/alobo2/miniforge3/envs/micro-sam/lib/python3.11/site-packages/kornia/feature/lightglue.py:44: FutureWarning: `torch.cuda.amp.custom_fwd(args...)` is deprecated. Please use `torch.amp.custom_fwd(args..., device_type='cuda')` instead.
  @torch.cuda.amp.custom_fwd(cast_inputs=torch.float32)
Traceback (most recent call last):
  File "/home/alobo2/miniforge3/envs/micro-sam/bin/micro_sam.automatic_segmentation", line 8, in <module>
    sys.exit(main())
             ^^^^^^
  File "/home/alobo2/micro-sam/micro_sam/automatic_segmentation.py", line 229, in main
    predictor, segmenter = get_predictor_and_segmenter(
                           ^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/home/alobo2/micro-sam/micro_sam/automatic_segmentation.py", line 53, in get_predictor_and_segmenter
    raise RuntimeError("You have passed amg=False, but your model does not contain a segmentation decoder.")
RuntimeError: You have passed amg=False, but your model does not contain a segmentation decoder.
(micro-sam) alobo2@alobo-ws:~$ 

@constantinpape
Copy link
Contributor

constantinpape commented Nov 19, 2024

This is a different error, but it points to an issue in our recent change. We will get back to you about this ASAP.
In the meantime you can already try to set up the GPU env again.

cc @anwai98 : it looks like the current logic for choosing AMG vs. segmentation decoder does not work correctly. We should fix it and add a test for this so that we can ensure its functionality in the future.

@anwai98
Copy link
Contributor

anwai98 commented Nov 19, 2024

Hi @aloboa,

Thanks for spotting the issue. I've added a PR where this should be fixed. We will ping you here once it's merged.

@constantinpape I overlooked a minor detail in our heuristic which led to the issue. I've added some more CLI-level tests now.

@aloboa
Copy link
Author

aloboa commented Nov 19, 2024

Thanks, but I still wonder why my GPU was working with the previous version (I actually checked with the same python command) and not now.
The issue I had was only with the--min_mask_region_areaargument. The cli command worked fine with the cuda option if
--min_mask_region_area was not used.

@anwai98
Copy link
Contributor

anwai98 commented Nov 19, 2024

I would recommend following the instructions suggested by @constantinpape (also highlighted in the quoted text below) to make a fresh installation from source (it's often hard to back-trace the source issue over multiple hot fixes).

That is indeed quite weird. micro_sam on its own can't affect the GPU installation.

In this case, I would suggest to install a new environment via mamba env create -f environment_gpu.yaml. Then install microsam via pip install -e . in that environment and check if everything works as expected.

@aloboa
Copy link
Author

aloboa commented Nov 19, 2024

Many thanks for your help.
After

mamba env create -f environment_gpu.yaml.
pip install -e .

cuda is back to work but

(sam) alobo2@alobo-ws:~/micro-sam$ python -c "import torch; print(torch.cuda.is_available())"
True
(sam) alobo2@alobo-ws:~/micro-sam$ micro_sam.automatic_segmentation -i "/media/alobo2/SP PHD U3/Islandia/Alteration/Cristina_nDann/DI17_3/DI17_3.tif" -o "/media/alobo2/SP PHD U3/Islandia/Alteration/microsam/DI17_3_autoseg/DI17_3_autoseg_03.tif" -m vit_b --tile_shape 1024 1024 --halo 256 256 -d 'conda' --pred_iou_thresh 0.1 --stability_score_thresh 0.95 --box_nms_thresh 0.7
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 229, in main
    predictor, segmenter = get_predictor_and_segmenter(
                           ^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/home/alobo2/micro-sam/micro_sam/automatic_segmentation.py", line 40, in get_predictor_and_segmenter
    device = util.get_device(device=device)
             ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/home/alobo2/micro-sam/micro_sam/util.py", line 206, in get_device
    raise RuntimeError(f"Unsupported device: {device}\n"
RuntimeError: Unsupported device: conda
Please choose from 'cpu', 'cuda', or 'mps'.

@anwai98
Copy link
Contributor

anwai98 commented Nov 19, 2024

Hi @aloboa,

You need to provide the device as cuda (or in principle, you can choose to not pass it as well, i.e. if it finds GPU support, it would by default run it over GPU)

@aloboa
Copy link
Author

aloboa commented Nov 19, 2024

I apologize for the typo...
Just to let you know that the error appears both for cuda and cpu:

(sam) alobo2@alobo-ws:~/micro-sam$ micro_sam.automatic_segmentation -i "/media/alobo2/SP PHD U3/Islandia/Alteration/Cristina_nDann/DI17_3/DI17_3.tif" -o "/media/alobo2/SP PHD U3/Islandia/Alteration/microsam/DI17_3_autoseg/DI17_3_autoseg_03.tif" -m vit_b --tile_shape 1024 1024 --halo 256 256 -d 'cuda' --pred_iou_thresh 0.1 --stability_score_thresh 0.95 --box_nms_thresh 0.7
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 229, in main
    predictor, segmenter = get_predictor_and_segmenter(
                           ^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/home/alobo2/micro-sam/micro_sam/automatic_segmentation.py", line 53, in get_predictor_and_segmenter
    raise RuntimeError("You have passed amg=False, but your model does not contain a segmentation decoder.")
RuntimeError: You have passed amg=False, but your model does not contain a segmentation decoder.
(sam) alobo2@alobo-ws:~/micro-sam$ micro_sam.automatic_segmentation -i "/media/alobo2/SP PHD U3/Islandia/Alteration/Cristina_nDann/DI17_3/DI17_3.tif" -o "/media/alobo2/SP PHD U3/Islandia/Alteration/microsam/DI17_3_autoseg/DI17_3_autoseg_03.tif" -m vit_b --tile_shape 1024 1024 --halo 256 256 -d 'cpu' --pred_iou_thresh 0.1 --stability_score_thresh 0.95 --box_nms_thresh 0.7
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 229, in main
    predictor, segmenter = get_predictor_and_segmenter(
                           ^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/home/alobo2/micro-sam/micro_sam/automatic_segmentation.py", line 53, in get_predictor_and_segmenter
    raise RuntimeError("You have passed amg=False, but your model does not contain a segmentation decoder.")
RuntimeError: You have passed amg=False, but your model does not contain a segmentation decoder.

@anwai98
Copy link
Contributor

anwai98 commented Nov 19, 2024

Ahha yes. This PR has not been merged yet (there's some super minor detail I still need to take care of).

We'll ping you here once the PR is merged.

@anwai98
Copy link
Contributor

anwai98 commented Nov 20, 2024

Hi @aloboa,

The issue you faced should be fixed now (you need to pull the latest state, and things should work as expected).

Let us know if you come across any other issues. I'll go ahead and close this as the aforementioned issue is fixed.

@anwai98 anwai98 closed this as completed Nov 20, 2024
@aloboa
Copy link
Author

aloboa commented Nov 21, 2024

I confirm micro_sam.automatic_segmentation works now with and without --min_mask_region_area
Thanks!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants