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

Add support for MPS device #176

Merged
merged 11 commits into from
Sep 7, 2023
Merged
Show file tree
Hide file tree
Changes from 5 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
3 changes: 3 additions & 0 deletions micro_sam/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,5 +5,8 @@
.. include:: ../doc/python_library.md
.. include:: ../doc/finetuned_models.md
"""
import os

from .__version__ import __version__

os.environ["PYTORCH_ENABLE_MPS_FALLBACK"] = "1"
4 changes: 2 additions & 2 deletions micro_sam/evaluation/inference.py
Original file line number Diff line number Diff line change
Expand Up @@ -136,7 +136,7 @@ def _run_inference_with_prompts_for_image(
prompts = deepcopy((input_point, input_label, input_box))

# Transform the prompts into batches
device = torch.device("cuda" if torch.cuda.is_available() else "cpu")
device = util._get_device()
input_point = torch.tensor(np.array(input_point)).to(device) if len(input_point) > 0 else None
input_label = torch.tensor(np.array(input_label)).to(device) if len(input_label) > 0 else None
input_box = torch.tensor(np.array(input_box)).to(device) if len(input_box) > 0 else None
Expand Down Expand Up @@ -524,7 +524,7 @@ def run_inference_with_iterative_prompting(
"""@private"""
warnings.warn("The iterative prompting functionality is not working correctly yet.")

device = torch.device("cuda")
device = util._get_device()
model = get_trainable_sam_model(model_type, checkpoint_path)

# create all prediction folders
Expand Down
9 changes: 5 additions & 4 deletions micro_sam/instance_segmentation.py
Original file line number Diff line number Diff line change
Expand Up @@ -189,7 +189,7 @@ def _postprocess_small_regions(self, mask_data, min_area, nms_thresh):
mask, changed = amg_utils.remove_small_regions(mask, min_area, mode="islands")
unchanged = unchanged and not changed

new_masks.append(torch.as_tensor(mask).unsqueeze(0))
new_masks.append(torch.as_tensor(mask, dtype=torch.int).unsqueeze(0))
# give score=0 to changed masks and score=1 to unchanged masks
# so NMS will prefer ones that didn't need postprocessing
scores.append(float(unchanged))
Expand All @@ -199,7 +199,7 @@ def _postprocess_small_regions(self, mask_data, min_area, nms_thresh):
boxes = amg_utils.batched_mask_to_box(masks)
keep_by_nms = batched_nms(
boxes.float(),
torch.as_tensor(scores),
torch.as_tensor(scores, dtype=torch.float),
torch.zeros_like(boxes[:, 0]), # categories
iou_threshold=nms_thresh,
)
Expand Down Expand Up @@ -258,7 +258,7 @@ def _to_mask_data(self, masks, iou_preds, crop_box, original_size, points=None):
# serialize predictions and store in MaskData
data = amg_utils.MaskData(masks=masks.flatten(0, 1), iou_preds=iou_preds.flatten(0, 1))
if points is not None:
data["points"] = torch.as_tensor(points.repeat(masks.shape[1], axis=0))
data["points"] = torch.as_tensor(points.repeat(masks.shape[1], axis=0), dtype=torch.float)

del masks

Expand All @@ -269,6 +269,7 @@ def _to_mask_data(self, masks, iou_preds, crop_box, original_size, points=None):

# threshold masks and calculate boxes
data["masks"] = data["masks"] > self._predictor.model.mask_threshold
data["masks"] = data["masks"].type(torch.int)
Copy link
Contributor Author

@constantinpape constantinpape Sep 6, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I have traced down the failing test to this line. It seems like the bounding boxes computed by batched_mask_to_box are different when a bool tensor is passed than when an int tensor is passed. This is unfortunate, but would be a bit of effort to fix since this is part of upstream code.
@GenevieveBuckley I assume that you have made this change because something fails with mps if a bool tensor is used. Can you please send me the exact error message you get without this line? Then I will think about the best strategy here.

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

  1. Wow, I did not know boolean masks returned correct results, but integer masks return something totally different. Yikes! I have changed the type of this line to torch.bool, thank you for pointing it out.

  2. I have also gone ahead with my first suggestion here about how to handle the problem with batched_mask_to_box(). I have made a new file _vendored.py containing a copy of batched_mask_to_box() from segment_anything/util/amg.py, which I have edited to (a) make sure the input mask is boolean, and (b) make compatible with the MPS Pytorch backend for apple silicon.

I think this should fix the problems.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

  1. Wow, I did not know boolean masks returned correct results, but integer masks return something totally different. Yikes! I have changed the type of this line to torch.bool, thank you for pointing it out.

Yes, it is really unfortunate... I think this is a pretty big bug on the torch / SegmentAnything side. It could be worth it to figure out what exactly causes it and report that at some point.

2. I have also gone ahead with my first suggestion here about how to handle the problem with batched_mask_to_box(). I have made a new file _vendored.py containing a copy of batched_mask_to_box() from segment_anything/util/amg.py, which I have edited to (a) make sure the input mask is boolean, and (b) make compatible with the MPS Pytorch backend for apple silicon.

I think this should fix the problems.

👍

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ah, I see you made an issue already :D
facebookresearch/segment-anything#552

data["boxes"] = amg_utils.batched_mask_to_box(data["masks"])

# compress to RLE
Expand Down Expand Up @@ -364,7 +365,7 @@ def __init__(
def _process_batch(self, points, im_size, crop_box, original_size):
# run model on this batch
transformed_points = self._predictor.transform.apply_coords(points, im_size)
in_points = torch.as_tensor(transformed_points, device=self._predictor.device)
in_points = torch.as_tensor(transformed_points, device=self._predictor.device, dtype=torch.float)
in_labels = torch.ones(in_points.shape[0], dtype=torch.int, device=in_points.device)
masks, iou_preds, _ = self._predictor.predict_torch(
in_points[:, None, :],
Expand Down
5 changes: 2 additions & 3 deletions micro_sam/training/util.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
import numpy as np

from ..prompt_generators import PointAndBoxPromptGenerator
from ..util import get_centers_and_bounding_boxes, get_sam_model
from ..util import get_centers_and_bounding_boxes, get_sam_model, _get_device
from .trainable_sam import TrainableSAM


Expand All @@ -28,8 +28,7 @@ def get_trainable_sam_model(
The trainable segment anything model.
"""
# set the device here so that the correct one is passed to TrainableSAM below
if device is None:
device = "cuda" if torch.cuda.is_available() else "cpu"
device = _get_device(device)
_, sam = get_sam_model(device, model_type, checkpoint_path, return_sam=True)

# freeze components of the model if freeze was passed
Expand Down
24 changes: 20 additions & 4 deletions micro_sam/util.py
Original file line number Diff line number Diff line change
Expand Up @@ -116,6 +116,24 @@ def _get_checkpoint(model_type, checkpoint_path=None):
return checkpoint_path


def _get_device(device):
if device is not None:
return device

# Use cuda enabled gpu if it's available.
if torch.cuda.is_available():
device = "cuda"
# As second priority use mps.
# See https://pytorch.org/docs/stable/notes/mps.html for details
elif torch.backends.mps.is_available() and torch.backends.mps.is_built():
print("Using apple MPS device.")
device = "mps"
# Use the CPU as fallback.
else:
device = "cpu"
return device


def get_sam_model(
device: Optional[str] = None,
model_type: str = _DEFAULT_MODEL,
Expand All @@ -138,8 +156,7 @@ def get_sam_model(
The segment anything predictor.
"""
checkpoint = _get_checkpoint(model_type, checkpoint_path)
if device is None:
device = "cuda" if torch.cuda.is_available() else "cpu"
device = _get_device(device)

# Our custom model types have a suffix "_...". This suffix needs to be stripped
# before calling sam_model_registry.
Expand Down Expand Up @@ -196,8 +213,7 @@ def get_custom_sam_model(
custom_pickle = pickle
custom_pickle.Unpickler = _CustomUnpickler

if device is None:
device = "cuda" if torch.cuda.is_available() else "cpu"
device = _get_device()
sam = sam_model_registry[model_type]()

# load the model state, ignoring any attributes that can't be found by pickle
Expand Down
Loading