Notice
Recent Posts
Recent Comments
Link
«   2024/10   »
1 2 3 4 5
6 7 8 9 10 11 12
13 14 15 16 17 18 19
20 21 22 23 24 25 26
27 28 29 30 31
Tags
more
Archives
Today
Total
관리 메뉴

Dong Gyu

[Detectron2] 기본 설치 본문

AI vision model

[Detectron2] 기본 설치

오동규 2022. 3. 29. 11:02
728x90

 

기본적으로 detectron2를 사용할려면 visual studio build tools이 필요함

 

 

그 후 https://github.com/facebookresearch/detectron2 들어가서 코드를 받아오면 된다

 

그리고 detectron2 doc installation(https://detectron2.readthedocs.io/en/latest/tutorials/install.html)에서 필요 요구 사항 체크( 리눅스 온리라 적혀있지만 윈도우도 가능)

<현재 내 버전>

 

그런 다음

pip install fvcore

pip install ninja

설치 그외에는 사용하면서 필요하다 뜨면 추가로 설치하면 됨

 

다운 받은 코드로 들어가

python setup.py build_ext --inplace 입력( pip install e.로 설치가 가능하지만 버전관리를 위해 내부에 빌드함)

그러면 아래와 같이 빌드 폴더가 생기면서 아래와 같이 파일이 생성 됨( cuda를 설치 했다면 cuda 버전이 생기지만 없다면 cpu만 생김 그리고 cpu만 있어도 작동에는 문제가 없음)

 

그리고 detectron2 폴더 안에도 _C.cp37-win_amd64.pyd 이 생성 됨

그럼 detectron2 기본 세팅은 마무리가 됨

 

설치 확인 해보기

https://github.com/facebookresearch/detectron2/blob/main/MODEL_ZOO.md 에서 R50-FPN 모델 다운 후 detectron2 코드 폴더에 weights 폴더 생성 후 안에 넣기

 

그 후 inf_test.py 파이썬 파일 만들고 아무 이미지 가져온 뒤(난 자동차 이미지 사용) 코드 입력

from detectron2.utils.logger import setup_logger
setup_logger()
from detectron2 import model_zoo
from detectron2.engine import DefaultPredictor
from detectron2.config import get_cfg
from detectron2.utils.visualizer import Visualizer
from detectron2.data import MetadataCatalog
import cv2

cfg = get_cfg()
cfg.merge_from_file(model_zoo.get_config_file("COCO-Detection/faster_rcnn_R_50_FPN_1x.yaml"))
cfg.MODEL.WEIGHTS = "weights/model_final_b275ba.pkl"
predictor = DefaultPredictor(cfg)

img = cv2.imread('input.jpg')

outputs = predictor(img)
v = Visualizer(img[:, :, ::-1], MetadataCatalog.get(cfg.DATASETS.TRAIN[0]), scale=1.2)
out = v.draw_instance_predictions(outputs["instances"].to("cpu"))
cv2.imshow('1', out.get_image()[:, :, ::-1])
cv2.waitKey()

 

< 생성 파일 >

 

실행 결과

 

300x250
Comments