본문 바로가기
공부공부/2023 쌓여가는 나의 지식~

Detectron2(Centernet) 이미지 인식기 만들기(Linux)(1)

by Lee_story_.. 2023. 9. 21.
728x90

 

 

이미지 인식에서 항상 yolo모델을 사용하다가 다른 모델은 어떨까 해서 찾아본 Centernet모델!

 

 

yolo모델은 이미지를 고정된 크기의 그리드로 나누어 준 후, 앵커박스를 통해 찾고자하는 객체와의 일치성을 각 그리드에 부여해 객체를 찾는 느낌이라면

 

Centernet모델은 임의의 한 점에서부터 히트맵처럼 퍼져나가며 객체에 대해 판단하는 모델입니다.

 

 

 

 

아래 블로그에 엄청 자세히 설명 되어 있습니다. 

 

센터넷, CenterNet (Objects as Points)

gaussian37's blog

gaussian37.github.io

 

 

그럼 Centernet 모델을 어떻게 사용하느냐

 

 

Centernet의 소스코드의 경우는 아래 깃허브에 공개되어있습니다.

 

GitHub - xingyizhou/CenterNet: Object detection, 3D detection, and pose estimation using center point detection:

Object detection, 3D detection, and pose estimation using center point detection: - GitHub - xingyizhou/CenterNet: Object detection, 3D detection, and pose estimation using center point detection:

github.com

 

 

 

하지만 오늘 사용해볼것은 최신버전인 Centernet 2

 

(Centernet 2는 Centernet에서 한 단계의 인식구조를 더 추가하여 조금 더 빠르고 정확한 모델)

 

GitHub - xingyizhou/CenterNet2: Two-stage CenterNet

Two-stage CenterNet. Contribute to xingyizhou/CenterNet2 development by creating an account on GitHub.

github.com

위의 링크를 들어가보면 Centernet2의 사용방법이 적혀있는데

 

Our project is developed on detectron2. Please follow the official detectron2 installation.

Centernet2의 경우는 detectron2를 이용하여 사용가능하다고 합니다. 

 

 

 

이제 detectron2를 설치 해봅시다.

 

GitHub - facebookresearch/detectron2: Detectron2 is a platform for object detection, segmentation and other visual recognition t

Detectron2 is a platform for object detection, segmentation and other visual recognition tasks. - GitHub - facebookresearch/detectron2: Detectron2 is a platform for object detection, segmentation a...

github.com

 

 

설치

 

Installation — detectron2 0.6 documentation

© Copyright 2019-2020, detectron2 contributors Revision 8c4a333c.

detectron2.readthedocs.io

 

설치방법도 사이트에 자세히 설명되어있습니다. 

cuda 11.3과 11.8에서 각각 진행해 보았습니다.

 

11.8에서 실행은 되지만 11.3까지만 미리 빌드된 파일을 제공하기에 11.3을 사용하는 것이 좋아 보입니다.

 

가장 먼저 python 버전을 3.9로 맞춰준 후 

 

기본적으로 필요한 라이브러리들을 다운 받아 줍시다. 

apt install gcc
apt-get install g++

pip install opencv-python

 

 

그 다음 pytorch, torchvision, cudatoolkit 등을 다운 받아야하는데

 

cuda 11.3은

conda install pytorch==1.10.1 torchvision==0.11.2 torchaudio==0.10.1 cudatoolkit=11.3 -c pytorch -c conda-forge

 

 

cuda 11.8은

pip3 install torch torchvision torchaudio --index-url https://download.pytorch.org/whl/cu118

 

으로 설치하여 줍시다. (cuda 버전에 맞는 pytorch를 설치하면 됩니다!)

 

 

다음으로 

detectron2의 깃허브 파일을 내려받고

git clone https://github.com/facebookresearch/detectron2.git

 

 

cuda 11.3은 미리 빌드된 detectron2를 설치하여줍시다.

 

 

python -m pip install detectron2 -f \https://dl.fbaipublicfiles.com/detectron2/wheels/cu113/torch1.10/index.html

 

 

그리고 둘다 detectron2를 설치

python -m pip install -e detectron2

 

여기 까지가 끝!

 

 

 

이제 demo를 실행해봅시다.

/Detectron2/demo/demo.py 에서 실행 해 볼수 있고

 

 

 

https://github.com/facebookresearch/detectron2/blob/main/MODEL_ZOO.md

 

해당 링크를 통해 원하는 인식 모델을 다운 받은 후

 

ex) 
wget https://dl.fbaipublicfiles.com/detectron2/COCO-Detection/faster_rcnn_R_101_FPN_3x/137851257/model_final_f6e8b1.pkl

 

 

기본적으로 아래 처럼 커맨드를 입력하면 실행 가능합니다. 

python demo.py --config-file ../configs/COCO-Detection/모델 정보(.yaml) --input ./image/2.jpg(인식할 이미지) --opts MODEL.WEIGHTS 위치/모델이름(.pkl)

 

이때 모델 정보 파일과 모델의 형식이 동일해야 제대로 인식이 됩니다!

ex )  faster_rcnn_R_101_FPN_3x.yaml을 사용한다면

 

아래의 모델 사용

 

 

 

 

잘 나오는것 같습니다!

 

 

여기서 Person keypoint를 이용하면

 

아래처럼 Person 스켈레톤도 추적할 수 있습니다! 

 

python demo.py --config-file ../configs/COCO-Keypoints/keypoint_rcnn_X_101_32x8d_FPN_3x.yaml --input ./images/5.jpg --opts MODEL.WEIGHTS /모델위치

 

 

 

 

이번엔 여기까지 

다음글에서는 실제 데이터를 학습하여 인식하는 부분을 다뤄보도록 하겠습니다.

 

 

 

 

 

 

<참고 블로그>

 

 

Detectron

오랜만에 포스트를 작성한다. 핑계를 말하자면.. 요즘 논문 작성 때문에 논문 읽기와 아이디어 생산에 집중하였다.. 결과는 좋지 않아 답답한 마음에 오랜만에 포스팅을 한다. 후에 논문을 쓰면

jjeamin.github.io

 

 

 

 

 

 

 

틀린점이 있다면 댓 달아주세요!

 

 

 

 

 

댓글