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

samgeo.hq_sam not working in docker #223

Open
glemoine62 opened this issue Dec 4, 2023 · 4 comments
Open

samgeo.hq_sam not working in docker #223

glemoine62 opened this issue Dec 4, 2023 · 4 comments
Labels
bug Something isn't working

Comments

@glemoine62
Copy link

Same issue as reported in: #173

However, since the solution provided (install with pip install git+https://github.com/SysCV/sam-hq.git) there does not work, I tried running the docker installation instead. But throws same error:

RuntimeError: Attempting to deserialize object on a CUDA device but torch.cuda.is_available() is False. If you are running on a CPU-only machine, please use torch.load with map_location=torch.device('cpu') to map your storages to the CPU.

suggesting the docker image was not built with the alternative pip install (or, indeed, the alternative install does not work).

Environment Information

  • samgeo version: docker: giswqs/segment-geospatial:latest
  • Python version: 3.11.6
  • Operating System: linux
@glemoine62 glemoine62 added the bug Something isn't working label Dec 4, 2023
@MarcioDDCF
Copy link

Is this related to [https://github.com//issues/201] ?

@glemoine62
Copy link
Author

No. Not related to #201. I want to run samgeo.hq_sam on the CPU in the same way you can run "normal" samgeo on the CPU.
The solution proposed in #173 does not work, whether you install locally or in a docker container.

The speed difference between running inference with samgeo on a GPU or a CPU is not too dramatic, esp. when testing (GPU order of 6 faster). The models require a pretty large GPU forcing you to move to cloud resources. I prefer running on CPU.

@flxcp
Copy link

flxcp commented Jan 4, 2024

Try adding device='cpu' when initializing SamGeo:

from samgeo.hq_sam import SamGeo

sam = SamGeo(model_type="vit_h", sam_kwargs=None, device='cpu')

See if it helps!

@glemoine62
Copy link
Author

glemoine62 commented Jan 4, 2024

Thanks for the tip, but I tried that, and it doesn't work. The problem is in the sam_hq implementation, which does not check for the device key. To get this solved, you need to go into /opt/conda/lib/python3.11/site-packages/segment_anything_hq/build_sam.py

and edit line 160:

state_dict = torch.load(f, map_location=torch.device('cpu'))

which is for the vit_h model, you need to edit the equivalent statements for the other models. This is the quick and dirty solution, it would be better to do this on the basis of the device keyword.

(I assume use of the giswqs/segment-geospatial docker image. I (locally) commit the revised container to giswqs/segment-geospatial-cpu)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working
Projects
None yet
Development

No branches or pull requests

3 participants