Segment Anything Model (SAM) on Apple Silicon M1 and M2

GeoSense ✅
2 min readMay 12

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)
Input image. Source SAM-Medical-Imaging

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 pydicom

Functions

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) * color.reshape(1, 1, -1)
ax.imshow(mask_image)

def show_box(box, ax):
x0, y0 = box[0], box[1]
w, h = box[2] - box[0], box[3] - box[1]
ax.add_patch(plt.Rectangle((x0, y0), w, h, edgecolor='green', facecolor=(0,0,0,0), lw=2))

Import a medical imaging image (dcm image)

def prepare_dicoms(dcm_file, show=False):
dicom_file_data = pydicom.dcmread(dcm_file).pixel_array

HOUNSFIELD_MAX = np.max(dicom_file_data)
HOUNSFIELD_MIN = np.min(dicom_file_data)

HOUNSFIELD_RANGE = HOUNSFIELD_MAX - HOUNSFIELD_MIN

dicom_file_data[dicom_file_data < HOUNSFIELD_MIN] = HOUNSFIELD_MIN
dicom_file_data[dicom_file_data > HOUNSFIELD_MAX] = HOUNSFIELD_MAX
normalized_image = (dicom_file_data - HOUNSFIELD_MIN) / HOUNSFIELD_RANGE
uint8_image = np.uint8(normalized_image*255)

opencv_image = cv2.cvtColor(uint8_image, cv2.COLOR_GRAY2BGR)

if show:
cv2.imshow("a",opencv_image)
cv2.waitKey()

return…
GeoSense ✅

🌏 Remote sensing | 🛰️ Geographic Information Systems (GIS) | ℹ️ https://www.tnmthai.com/medium