본문 바로가기
환경구축

SAM 환경 설정(에러 경우의 수 정리 포함)

by Yuchulnote 2025. 1. 14.
728x90

SAM(Segment Anything) 을 window 환경에서 python으로 환경설정 및 빌드하는 방법에 대한 포스트입니다!

에러 발생 case는 글의 맨 뒤에 붙여놓았으니, 참고바랍니다.

 


  • SAM 공식 권장 사양 요약
    • Python ≥ 3.10, PyTorch ≥ 2.5.1, CUDA ≥ 12.4

환경설정 순서

conda 가상환경 만들기

  • Anaconda Navigator로 만들기
  • 터미널
  • conda create -n seg python=3.11 -y (SAM 권장, Nuitrack 사용 불가) conda create -n seg python=3.10 -y (pynuitrack <= 3.10)

 

Torch install(가상환경 activate & python interpreter 설정 후)

conda install pytorch torchvision torchaudio pytorch-cuda=12.1 -c pytorch -c nvidia

 

CUDA Toolkit 12.4 이상 버전으로 setting

그 아래 version이라면 프로그램 추가/제거 들어가서 nvidia 현재 ver 관련된 파일 전부 삭제 후

https://developer.nvidia.com/cuda-toolkit-archive

12.4 이상 ver으로 재설치

torch gpu 테스트 코드로 gpu 설정이 잘 되었는지 확인

본인 컴퓨터에 맞는 toolkit 설치 후, torch gpu 설정이 안되는 수준이라면 SAM 사용 불가.

 

GPU 작동 테스트

# pytorch gpu test
import torch

print(torch.__version__)
print(torch.version.cuda)

print("CUDA is available:", torch.cuda.is_available())
print("Current device:", torch.cuda.get_device_name(0))

# 랜덤 텐서를 GPU에 할당
device = torch.device("cuda" if torch.cuda.is_available() else "cpu")
x = torch.rand(10000, 10000).to(device)

# GPU 연산 테스트
print("GPU 연산 테스트 완료:", torch.matmul(x, x).is_cuda)

 

git clone

SAM2 공식 깃허브) https://github.com/facebookresearch/sam2

참고) https://github.com/khw11044/SAM2_streaming

깃 클론 후 sam 공식 레파지토리로 이동 후 환경설정 진행

cd sam2

pip install -e .
pip install -e ".[notebooks]"

python setup.py build_ext --inplace

마지막줄까지 성공했다면 아래와 같은 메세지가 반겨줄 것 입니다

LINK : /LTCG를 지정했지만 코드를 생성할 필요가 없습니다. 명령줄에서 /LTCG를 제거하면 링커 성능이 향상됩니다.
copying build\\lib.win-amd64-cpython-310\\sam2\\_C.pyd -> sam2

레파지토리 경로에 아래와 같이 빌드 완료된 폴더와 파일들이 생성되면 성공입니다!

 

SAM 모델 다운로드

깃허브를 통해 clone한 파일은 쉘파일만 있을겁니다, sh파일이라 window에서 sam pt파일들을 다운로드하려면 python 스크립트로 변환해야하는 과정이 필요한데요, 제가 아래에 다 제공해드리고 있으니 그대로 복붙하셔서 실행하시면 됩니다~

download_ckpts.sh → python 스크립트 변환 후 실행

SAM2 모델들 다운로드하는 python 스크립트

import os
import requests
from tqdm import tqdm

def download_file(url, filename):
    """파일을 다운로드하고 진행률을 표시하는 함수"""
    response = requests.get(url, stream=True)
    response.raise_for_status()  # 오류 발생시 예외 발생
    
    total_size = int(response.headers.get('content-length', 0))
    
    with open(filename, 'wb') as f, tqdm(
        desc=filename,
        total=total_size,
        unit='iB',
        unit_scale=True,
        unit_divisor=1024,
    ) as pbar:
        for data in response.iter_content(chunk_size=1024):
            size = f.write(data)
            pbar.update(size)

def main():
    # 체크포인트 URL 정의
    BASE_URL = "<https://dl.fbaipublicfiles.com/segment_anything_2/072824/>"
    checkpoints = {
        "sam2_hiera_tiny.pt": f"{BASE_URL}sam2_hiera_tiny.pt",
        "sam2_hiera_small.pt": f"{BASE_URL}sam2_hiera_small.pt",
        "sam2_hiera_base_plus.pt": f"{BASE_URL}sam2_hiera_base_plus.pt",
        "sam2_hiera_large.pt": f"{BASE_URL}sam2_hiera_large.pt"
    }

    # 각 체크포인트 다운로드
    for filename, url in checkpoints.items():
        print(f"Downloading {filename}...")
        try:
            download_file(url, filename)
        except Exception as e:
            print(f"Failed to download checkpoint from {url}: {str(e)}")
            continue
    
    print("All checkpoints are downloaded successfully.")

if __name__ == "__main__":
    main()

SAM2.1 모델들 다운로드하는 python 스크립트

import os
import requests
from tqdm import tqdm

def download_file(url: str, save_path: str) -> None:
    """
    URL에서 파일을 다운로드하고 진행률 표시줄을 보여줍니다.
    
    Args:
        url: 다운로드할 파일의 URL
        save_path: 저장할 파일 경로
    """
    response = requests.get(url, stream=True)
    response.raise_for_status()
    
    total_size = int(response.headers.get('content-length', 0))
    
    with open(save_path, 'wb') as f, tqdm(
        desc=os.path.basename(save_path),
        total=total_size,
        unit='iB',
        unit_scale=True,
        unit_divisor=1024,
    ) as pbar:
        for data in response.iter_content(chunk_size=1024):
            size = f.write(data)
            pbar.update(size)

def main():
    # SAM 2.1 체크포인트 URL 정의
    base_url = "<https://dl.fbaipublicfiles.com/segment_anything_2/092824>"
    checkpoints = {
        "sam2.1_hiera_tiny.pt": f"{base_url}/sam2.1_hiera_tiny.pt",
        "sam2.1_hiera_small.pt": f"{base_url}/sam2.1_hiera_small.pt",
        "sam2.1_hiera_base_plus.pt": f"{base_url}/sam2.1_hiera_base_plus.pt",
        "sam2.1_hiera_large.pt": f"{base_url}/sam2.1_hiera_large.pt"
    }

    # 각 체크포인트 다운로드
    for filename, url in checkpoints.items():
        print(f"\\n다운로드 중: {filename}")
        try:
            download_file(url, filename)
        except Exception as e:
            print(f"다운로드 실패: {filename}")
            print(f"에러: {str(e)}")
            continue

    print("\\n모든 체크포인트가 성공적으로 다운로드되었습니다.")

if __name__ == "__main__":
    main()

 

이렇게 모델 파일들을 모두 받고 나면, 실행할 코드에서 적절히 해당 checkpoints 경로와 config 경로 등 설정해주시고 진행해주시면 됩니다~


에러 발생 case 정리

  • 에러 발생 case들 및 해결방안
    • ImportError: cannot import name '_C' from 'sam2'
      • pip install -e ".[notebooks]” 실행 안했을 경우.
      • 몇몇 시스템에서는 python setup.py build_ext --inplace 도 진행하지 않으면 에러 발생
    • MissingConfigException: Cannot find primary config 'configs/sam2.1/sam2.1_hiera_l.yaml'
      • pip install -e . 를 실행 안했을 경우
    • RuntimeError: Error(s) in loading state_dict for SAM2Base
      • 이는 SAM 2.1 체크포인트를 지원하는 새 모듈이 아직 없는 이 레파지토리의 이전 버전을 설치했기 때문일 수 있습니다. 다음 단계를 시도하세요
      1. SAM2 저장소의 메인 브랜치에서 최신 코드를 가져옵니다
      2. 이전 설치를 제거하려면 pip uninstall -y SAM-2를 실행
      3. pip install -e ".[notebooks]", pip install -e ".[notebooks]" 를 사용해서 최신 레파지토리에서 다시 설치
      4. 위 단계를 수행해도 오류가 해결되지 않으면 Python 환경에서 다음을 실행해 보세요.
      from sam2.modeling import sam2_base
      
      print(sam2_base.__file__)
      sam2/modeling/sam2_base.py의 인쇄된 로컬 경로에 있는 콘텐츠가 https://github.com/facebookresearch/sam2/blob/main/sam2/modeling/sam2_base.py 의 최신 콘텐츠와 일치하는지 확인하세요(예: 로컬 파일에는 이전 설치를 아직 사용하고 있는지 식별하기 위한 no_obj_embed_spatial이 있습니다)

    • CUDA_HOME environment variable is not set
      • CUDA toolkit을 찾지 못했기 때문에 발생
      • CUDA toolkit을 설치해도 여전히 발생한다면 환경변수 설정이 안되어 있는 것
      아래 코드로 출력 결과 확인 가능
      import torch 
      from torch.utils.cpp_extension import CUDA_HOME
      
      print(torch.cuda.is_available(), CUDA_HOME)
      
      예시 출력 결과
      True C:\\Program Files\\NVIDIA GPU Computing Toolkit\\CUDA\\v12.4
      ​

      • CUDA toolkit도 잘 설치하고, 환경변수도 잘 설정 했는데 에러가 발생한다면
        pip install --no-build-isolation -e .
    • undefined symbol: _ZN3c1015SmallVectorBaseIjE8grow_podEPKvmm (or similar errors)
      • 일반적으로 환경에 여러 버전의 dependencies(Pytorch or CUDA)가 있기 때문
      • pip이나 conda를 통해 별도로 설치된 Pytorch 혹은 CUDA 다른 버전이 있을 수 있음
      • 특히, Pytorch 버전이 2.5.1 보다 낮은 경우에는 Pytorch먼저 업그레이드를 하는 것이 좋음
      왜냐하면 설치 스크립트가 pip을 사용하여 최신 Pytorch로 업그레이드하려고 시도하기 때문에 이전에 conda를 사용하여 다른 Pytorch 버전을 설치한 경우 Pytorch 설치가 중복될 수 있음.
      • 정말 드문 경우 torch==2.1.0 으로 다운그레이드한 경우 해결이 된 사례도 존재
        • pyproject.toml 및 setup.py에서 제한 사항을 수정 후 빌드 가능
    • CUDA error: no kernel image is available for execution on the device
      • CUDA 커널이 GPU의 CUDA 기능에 맞게 컴파일 되지 않았기 때문일 수 있음
      • 런타임과 다른 환경(e.g. slurm system)에서 설치가 수행되는 경우 발생할 수 있음
      • GPU와 일치하는 컴파일 대상에서 CUDA 기능을 수동으로 지정해서 해결해 볼 수 있음
        export TORCH_CUDA_ARCH_LIST=9.0 8.0 8.6 8.9 7.0 7.2 7.5 6.0`
    • RuntimeError: No available kernel. Aborting execution. (or similar errors)
      • Flash Attention을 위한 GPU 또는 호환 가능한 Pytorch 버전이 없기 때문일 것.
      • sam2/modeling/sam/transtormer.py 파일에서 아래와 같이 수정하면 해결 됨 최신버전의 transformer.py에 아래 코드가 없을 수 있음. → 글 시작 부분에 참고로 걸어둔 깃허브의 transformer 코드로 대체하면 해결 가능
      OLD_GPU, USE_FLASH_ATTN, MATH_KERNEL_ON = get_sdpa_settings()
      
      위 부분을
      
      OLD_GPU, USE_FLASH_ATTN, MATH_KERNEL_ON = True, True, True
      
      로 수정
      
    • Error compiling objects for extension
      • 에러로그가 아래와 같이 보인다면
      unsupported Microsoft Visual Studio version! Only the versions between 2017 and
      2022 (inclusive) are supported! The nvcc flag '-allow-unsupported-compiler' can be
      used to override this version check; however, using an unsupported host compiler
      may cause compilation failure or incorrect run time execution. Use at your own risk.
      CUDA와 Visual Studio 버전이 서로 맞지 않아서 발생하는 문제.
      • 호환되지 않는 버전이거나 해당 에러가 발생시, 프로그램 추가/제거 들어가셔서 visual studio 관련된 것 모두 지워주시고 재설치 권장합니다.

      • 잘 설치한 후에 setup.py 에서 nvcc 관련 설정 부분 코드에 -allow-unsupported-compiler 추가하시면 해결 됩니다.
        def get_extensions():
            srcs = ["sam2/csrc/connected_components.cu"]
            compile_args = {
                "cxx": [],
                "nvcc": [
                    "-DCUDA_HAS_FP16=1",
                    "-D__CUDA_NO_HALF_OPERATORS__",
                    "-D__CUDA_NO_HALF_CONVERSIONS__",
                    "-D__CUDA_NO_HALF2_OPERATORS__",
                    "-allow-unsupported-compiler"  # 이 부분 추가
                ],
            }
            ext_modules = [CUDAExtension("sam2._C", srcs, extra_compile_args=compile_args)]
            return ext_modules
        ​
    • 실제 코드 동작시 아래와 같은 에러 메세지가 발생한다면
    A module that was compiled using NumPy 1.x cannot be run in
    NumPy 2.0.1 as it may crash. To support both 1.x and 2.x
    versions of NumPy, modules must be compiled with NumPy 2.0.
    Some module may need to rebuild instead e.g. with 'pybind11>=2.12'.
    
    If you are a user of the module, the easiest solution will be to
    downgrade to 'numpy<2' or try to upgrade the affected module.
    We expect that some modules will need time to support NumPy 2.
    
    위 에러 발생시, numpy<2 버전으로 재설치하면 해결 가능
pip install numpy==1.26.4

도움이 되셨다면~ 댓글 좋아요 부탁드립니다
이런 공유글을 쓰는데 많은 힘이 됩니다 ㅎㅎ

728x90
반응형