AI/모델 구현

[코드 구현-1] InternImage: Exploring Large-Scale Vision Foundation Models with Deformable Convolutions(2022)

jeonga 2023. 3. 28. 16:09

2023.03.28 일자로 1위를 하고 있는 InternImage 모델을 학습해보려 한다.

 

비교적 최근에 나온 모델이라 그런지 코드 구현 포스트가 부족해서 직접 작성한다.


internimage 코드 구현 목차 참고

2023.03.28 - [AI/논문 리뷰] - [코드 구현-1] InternImage: Exploring Large-Scale Vision Foundation Models with Deformable Convolutions(2022)

 

[코드 구현-1] InternImage: Exploring Large-Scale Vision Foundation Models with Deformable Convolutions(2022)

2023.03.28 일자로 1위를 하고 있는 InternImage 모델을 학습해보려 한다. 비교적 최근에 나온 모델이라 그런지 코드 구현 포스트가 부족해서 직접 작성한다. https://github.com/OpenGVLab/InternImage/tree/master/det

jeonga0649.tistory.com

2023.03.29 - [AI/논문 리뷰] - [코드 구현-2] InternImage: Exploring Large-Scale Vision Foundation Models with Deformable Convolutions(2022)

 

[코드 구현-2] InternImage: Exploring Large-Scale Vision Foundation Models with Deformable Convolutions(2022)

모든 실행 환경은 저번 글에서 소개한 컨테이너 내부에서 이루어집니다. 2023.03.28 - [AI/논문 리뷰] - [코드 구현-1] InternImage: Exploring Large-Scale Vision Foundation Models with Deformable Convolutions(2022) [코드 구

jeonga0649.tistory.com

2023.03.29 - [AI/논문 리뷰] - [코드 구현-3, Custom Datasets] InternImage: Exploring Large-Scale Vision Foundation Models with Deformable Convolutions(2022)

 

[코드 구현-3, Custom Datasets] InternImage: Exploring Large-Scale Vision Foundation Models with Deformable Convolutions(2022

2023.03.28 - [AI/논문 리뷰] - [코드 구현-1] InternImage: Exploring Large-Scale Vision Foundation Models with Deformable Convolutions(2022) [코드 구현-1] InternImage: Exploring Large-Scale Vision Foundation Models with Deformable Convolutions(20

jeonga0649.tistory.com


https://github.com/OpenGVLab/InternImage/tree/master/detection

 

GitHub - OpenGVLab/InternImage: [CVPR 2023 Highlight] InternImage: Exploring Large-Scale Vision Foundation Models with Deformabl

[CVPR 2023 Highlight] InternImage: Exploring Large-Scale Vision Foundation Models with Deformable Convolutions - GitHub - OpenGVLab/InternImage: [CVPR 2023 Highlight] InternImage: Exploring Large-S...

github.com


Docker 개발 환경 구축하기

 

1. 레포지토리 다운로드

git clone https://github.com/OpenGVLab/InternImage.git
cd InternImage

 

2. 본인의 GPU 환경 알아보기

nvidia-smi

 

3. Dockerfile 작성하기 

FROM pytorch/pytorch:1.12.1-cuda11.3-cudnn8-devel
USER root
RUN apt-get update && apt-get install -y && apt-get install python3.7 -y
RUN apt-get -y install libgl1-mesa-glx
RUN apt-get -y install libglib2.0-0

ADD . .

WORKDIR /home/user/internimage

#runtime 은 nvcc 안되고, devel로 깔아야 가능하다..

❗주의할 점

보통 pytorch image를 받아올 때 runtime 버전이 용량이 가벼워서 자주 사용하는데, 무조건 devel로 깔아야 합니다.

runtime으로 깔게 되면 container 내부에서 nvcc 버전 확인이 안되고, 

소스 환경 설정할 때 CUDA_HOME이 인식이 안돼요 (미리 겪어본 시행착오 😓)

 

그리고 dockerfile을 작성했으면 build 해주시면 됩니다.

docker build -t 'internimage' .

 

4. docker-compose.yml 작성하기

version: '3.7'
services:
  nap1p2_classification:
    container_name: internimage
    image: internimage:latest
    volumes:
      - .:/home/user/internimage
    deploy:
      resources:
        reservations:
          devices:
          - driver: nvidia
            device_ids: ['3']
            capabilities: [gpu]
    environment:
        - GRANT_SUDO=yes
    ipc: host
    tty: true

device_ids에서 gpu 번호를 지정할 수 있습니다. 

 

도커 이미지를 빌드했고, docker-compose 파일을 작성했다면 백그라운드로 실행시켜주기

docker-compose up -d

컨테이너 내부에 접속할 때는 attach를 사용합니다.

vs code extensions에서 docker 플러그인을 설치하면 손쉽게 GUI로 접속 가능합니다.


코드 실행 환경 구축하기

 

1. 컨테이너에 접속합니다.

그리고 nvidia-smi와 nvcc --version이 잘 출력되는 지 확인!

만약 되지 않는다면, 다시 처음부터 시도해보세요.

 

또한 깃허브에 나와있는 conda 환경은 사용하지 않아도 됩니다.

torch와 torchvision도 image를 통해 세팅되었으므로 pass 

 

2.  필요한 패키지 설치하기

pip install -U openmim
mim install mmcv-full==1.7.0
pip install timm==0.6.11 mmdet==2.28.1
pip install opencv-python termcolor yacs pyyaml scipy

깃허브에는 mmcv-full 버전을 1.5.0으로 설치하라고 나와있는데,

우리가 base로 쓴 image는 torch가 1.12.0 버전이라서 1.7.0을 쓰지 않으면 train 할 때 오류가 생긴다

이거 때문에 고생..ㅠ

https://mmcv.readthedocs.io/en/latest/get_started/installation.html

 

Installation — mmcv 1.7.1 documentation

Docs > Installation 以中文阅读 Shortcuts

mmcv.readthedocs.io

 

pip 패키지 설치가 완료되었다면, 

cd detection
cd ops_dcnv3
sh ./make.sh
# unit test (should see all checking is True)
python test.py

여기까지 실행이 된다면 개발환경 구축이 완료되었습니다.

 

다음글은 custom dataset으로 train하는 방법에 대해 설명하겠습니다.