Member-only story
Segment Anything Model (SAM) on Apple Silicon M1 and M2
2 min readMay 12, 2023
SAM on Mac M1 and M2
Check if your Mac with M1/M2 chip is compatible with Metal Performance Shaders (MPS). Read link below:
Insert these two lines into code to run on Metal Performance Shaders (MPS) backend.
device = 'mps' if torch.backends.mps.is_available() else 'cpu'
sam.to(device=device)Press enter or click to view image in full size![]()
Set-up
Necessary imports and helper functions for displaying points, boxes, and masks.
Import Libraries
import numpy as np
import torch
import torchvision
import matplotlib.pyplot as plt
import cv2
import pydicomFunctions
def show_mask(mask, ax, random_color=False):
if random_color:
color = np.concatenate([np.random.random(3), np.array([0.6])], axis=0)
else:
color = np.array([255/255, 200/255, 0/255, 0.6])
h, w = mask.shape[-2:]
mask_image = mask.reshape(h, w, 1) *…