segment-anything: run with "mps" is error:Cannot convert a MPS Tensor to float64 dtype as the MPS framework doesn't support float64. Please use float32 instead.

when I follow the automatic_mask_generator_example to generating masks, It works in my rtx3080 and m1pro’s cpu, but when I change the device to ‘mps’,the error is show:"Cannot convert a MPS Tensor to float64 dtype as the MPS framework doesn’t support float64. Please use float32 instead."

this is my code:

    # 打开图片并转换为numpy数组
    image = cv2.imread(image_path)
    image = cv2.cvtColor(image, cv2.COLOR_BGR2RGB)

    # get image_path name
    file_name = os.path.splitext(os.path.basename(image_path))[0]
    # 创建 output 文件夹(如果不存在)
    image_folder = os.path.join(OutPutFolder, file_name)
    check_floder(image_folder)

    # 获取当前时间戳
    start_time = time.time()

    # 根据输入图像生成多个区域掩码
    masks = mask_generator.generate(image)  ###### **error in here**

    # 获取当前时间戳并计算消耗时间
    end_time = time.time()
    elapsed_time = end_time - start_time

    print(f"Time elapsed for mask_generator.generate{file_name}: {elapsed_time:.2f} seconds")

    # 创建一个空白图像,用于存储融合的结果
    result_image = np.zeros_like(image, dtype=np.float32)

About this issue

  • Original URL
  • State: open
  • Created a year ago
  • Reactions: 18
  • Comments: 25

Most upvoted comments

it works with device=‘cpu’

I had the same issue running Auto1111 on M1 Max and struggled a while. Loading a different checkpoint solved it ¯_(ツ)_/¯

replace this line: https://github.com/facebookresearch/segment-anything/blob/6fdee8f2727f4506cfbbe553e23b895e27956588/segment_anything/automatic_mask_generator.py#L277 with
in_points= torch.from_numpy(transformed_points.astype(np.float32)).to(self.predictor.device) will make it work in mps

same here 😦 Has anyone figured out a solution? best regards

There is already a PR, but unfortunately the speed up is quite marginal compared to CUDA.

#122

this code worked for me: 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 < HOUNDSFILD_MIN] = HOUNSFIELD_MIN
dicom_file_data[dicom_file_data > HOUNSFIELD_MAX] = HOUNSFIELD_MAX
normalized_image = (dicom_file_data - HOUNS_MIN) / HOUNSFIELD_RANGE
uint8_image = np.uint8(normalized*255)

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

if show:
    cv2_imshow(opencv_image) # for Google Colab

return opencv_image

https://github.com/amine0110/SAM-Medical-Imaging

I had the same issue running Auto1111 on M1 Max and struggled a while. Loading a different checkpoint solved it ¯_(ツ)_/¯

Yes! This worked! Load a different checkpoint, then reload the one you want to use and it works. Thanks!

same here 😦

Has anyone figured out a solution? best regards

There is already a PR, but unfortunately the speed up is quite marginal compared to CUDA.

https://github.com/facebookresearch/segment-anything/pull/122