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

Make model loadable on non-cuda systems #15

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1 +1,2 @@
__pycache__/
*.egg-info
6 changes: 3 additions & 3 deletions cellSAM/model.py
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ def download_file_with_progress(url, destination):
if total_size_in_bytes != 0 and progress_bar.n != total_size_in_bytes:
print("ERROR: Something went wrong")

def get_model(model: nn.Module = None) -> nn.Module:
def get_model(model: nn.Module = None, map_location="cpu") -> nn.Module:
"""
Returns a loaded CellSAM model. If model is None, downloads weights and loads the model with a progress bar.
"""
Expand All @@ -63,7 +63,7 @@ def get_model(model: nn.Module = None) -> nn.Module:
model_path,
)
model = CellSAM(config)
model.load_state_dict(torch.load(model_path))
model.load_state_dict(torch.load(model_path, map_location=map_location))
return model

def segment_cellular_image(
Expand All @@ -82,7 +82,7 @@ def segment_cellular_image(
if 'cuda' in device:
assert torch.cuda.is_available(), "cuda is not available. Please use 'cpu' as device."

model = get_model(model).eval()
model = get_model(model, map_location=device).eval()
model.bbox_threshold = bbox_threshold

img = format_image_shape(img)
Expand Down