SAM2跑通(Ubuntu20.04)内含安装多个cuda

参考链接:

github链接
安装cuda,之前借鉴的方法
安装多个cuda

补充cuda安装:

A symlink already exists at /usr/local/cuda. Update to this installation?
选择no,否则会创建一个软连接覆盖之前那个/usr/local/cuda

sudo gedit ~/.bashrc
如果按第二个链接安装的cuda,手动改一下版本即可

SAM环境安装步骤

除了本地cuda版本要对应pytorch的cuda版本之外,没有什么难点。
但YOLO为啥不需要本地cuda,导致我一直以为不需要装cuda就能跑项目,pytorch自带?

git clone https://github.com/facebookresearch/segment-anything-2.git

cd segment-anything-2

conda create -n SAM python=3.10
conda activate SAM

# setup.py要求了环境,python>=3.10
pip install -e .
问题:RuntimeError:
The detected CUDA version (11.7) mismatches the version that was used to compile
PyTorch (12.1). Please make sure to use the same CUDA versions.
解决办法:先去pytorch官网查看torch>=2.3.1要求的cuda为11.8至少,只能再装个cuda12.1

pip install -e ".[demo]"

#下载权重
cd checkpoints
./download_ckpts.sh

SAM推理步骤

有点晕,感觉是标注软件的感觉,需要提供点或者框,然后进行预测。
这里展示使用点预测分割的效果,只需要修改image和input_point,其它详细信息可以查看github或者其他介绍。

import torch
from sam2.build_sam import build_sam2
from sam2.sam2_image_predictor import SAM2ImagePredictor
from PIL import Image
import numpy as np
import matplotlib.pyplot as plt

def show_mask(mask, ax, random_color=False, borders = True):
    if random_color:
        color = np.concatenate([np.random.random(3), np.array([0.6])], axis=0)
    else:
        color = np.array([30/255, 144/255, 255/255, 0.6])
    h, w = mask.shape[-2:]
    mask = mask.astype(np.uint8)
    mask_image =  mask.reshape(h, w, 1) * color.reshape(1, 1, -1)
    if borders:
        import cv2
        contours, _ = cv2.findContours(mask,cv2.RETR_EXTERNAL, cv2.CHAIN_APPROX_NONE) 
        # Try to smooth contours
        contours = [cv2.approxPolyDP(contour, epsilon=0.01, closed=True) for contour in contours]
        mask_image = cv2.drawContours(mask_image, contours, -1, (1, 1, 1, 0.5), thickness=2) 
    ax.imshow(mask_image)

def show_points(coords, labels, ax, marker_size=375):
    pos_points = coords[labels==1]
    neg_points = coords[labels==0]
    ax.scatter(pos_points[:, 0], pos_points[:, 1], color='green', marker='*', s=marker_size, edgecolor='white', linewidth=1.25)
    ax.scatter(neg_points[:, 0], neg_points[:, 1], color='red', marker='*', s=marker_size, edgecolor='white', linewidth=1.25)   

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))    

def show_masks(image, masks, scores, point_coords=None, box_coords=None, input_labels=None, borders=True):
    for i, (mask, score) in enumerate(zip(masks, scores)):
        plt.figure(figsize=(10, 10))
        plt.imshow(image)
        show_mask(mask, plt.gca(), borders=borders)
        if point_coords is not None:
            assert input_labels is not None
            show_points(point_coords, input_labels, plt.gca())
        if box_coords is not None:
            # boxes
            show_box(box_coords, plt.gca())
        if len(scores) > 1:
            plt.title(f"Mask {i+1}, Score: {score:.3f}", fontsize=18)
        plt.axis('off')
        plt.show()

checkpoint = "./checkpoints/sam2_hiera_large.pt"
model_cfg = "sam2_hiera_l.yaml"
predictor = SAM2ImagePredictor(build_sam2(model_cfg, checkpoint))

with torch.inference_mode(), torch.autocast("cuda", dtype=torch.bfloat16):
    # 提供图片
    image = Image.open('images/a8.jpg')
    image = np.array(image.convert("RGB"))
    plt.figure(figsize=(10, 10))
    plt.imshow(image)
    plt.axis('on')
    plt.show()
    predictor.set_image(image=image) # 查看输入图片并送入预测

    # 提供标签(prompt:point)
    input_point = np.array([[2700, 435]])
    input_label = np.array([1])
    plt.figure(figsize=(10, 10))
    plt.imshow(image)
    show_points(input_point, input_label, plt.gca())
    plt.axis('on')
    plt.show()  
    
    # 开始预测
    masks, scores, logits = predictor.predict(
    point_coords=input_point,
    point_labels=input_label,
    multimask_output=True,
    )  
    sorted_ind = np.argsort(scores)[::-1]
    masks = masks[sorted_ind]
    scores = scores[sorted_ind]
    logits = logits[sorted_ind]

    show_masks(image, masks, scores, point_coords=input_point, input_labels=input_label, borders=True)

你可能感兴趣的:(pytorch,linux)