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

Errors and Solutions Encountered When Using SamDetector. #91

Open
mizu1 opened this issue Jan 16, 2024 · 1 comment
Open

Errors and Solutions Encountered When Using SamDetector. #91

mizu1 opened this issue Jan 16, 2024 · 1 comment

Comments

@mizu1
Copy link

mizu1 commented Jan 16, 2024

I encountered an error while using SamDetector, and the error message is as follows:

Traceback (most recent call last):
  File "c:\PythonProgram\diffweb\test_seg.py", line 12, in <module>
    processed = sam(image)
  File "C:\PythonProgram\diffweb\lib\site-packages\controlnet_aux\segment_anything\__init__.py", line 76, in __call__
    masks = self.mask_generator.generate(input_image)
  // ... Omitted intermediate error information ...
  File "C:\PythonProgram\diffweb\lib\site-packages\controlnet_aux\segment_anything\modeling\tiny_vit_sam.py", line 274, in forward
    (q @ k.transpose(-2, -1)) * self.scale

RuntimeError: Expected all tensors to be on the same device, but found at least two devices, cuda:0 and cpu!
After some investigation, I found that this issue was caused by some parts of the model using GPU (cuda:0) for computation, while other parts were using the CPU. This led to a device mismatch problem when executing the attention mechanism.
To solve this issue, I added the following code before the line:

attn = (
            (q @ k.transpose(-2, -1)) * self.scale 
                +
                (self.attention_biases[:, self.attention_bias_idxs] if self.training else self.ab)
            )

self.ab = self.ab.to('cuda:0') By doing this, all tensors in the model are computed on the GPU, thereby avoiding the device mismatch problem.
I hope this solution will be helpful to others.

@aycaecemgul
Copy link

this error makes the samdetector unusable.

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

2 participants