본문 바로가기
딥러닝관련/Detection

Detectron2 (1) 환경 세팅 및 데모

by 머리올리자 2021. 9. 29.

1. 아래에서 깃으로 파일들 다운

 

https://github.com/facebookresearch/detectron2

 

GitHub - facebookresearch/detectron2: Detectron2 is FAIR's next-generation platform for object detection, segmentation and other

Detectron2 is FAIR's next-generation platform for object detection, segmentation and other visual recognition tasks. - GitHub - facebookresearch/detectron2: Detectron2 is FAIR's next-genera...

github.com

 

2. Docker를 이용하여 환경 세팅

https://github.com/facebookresearch/detectron2/tree/main/docker

 

GitHub - facebookresearch/detectron2: Detectron2 is FAIR's next-generation platform for object detection, segmentation and other

Detectron2 is FAIR's next-generation platform for object detection, segmentation and other visual recognition tasks. - GitHub - facebookresearch/detectron2: Detectron2 is FAIR's next-genera...

github.com

 

Docker 내 README (2021-09-28 기준)

Use the container (with docker ≥ 19.03)
cd docker/
# Build:
docker build --build-arg USER_ID=$UID -t detectron2:v0 .
# Launch (require GPUs):
docker run --gpus all -it \
  --shm-size=8gb --env="DISPLAY" --volume="/tmp/.X11-unix:/tmp/.X11-unix:rw" \
  --name=detectron2 detectron2:v0

# Grant docker access to host X server to show images
xhost +local:`docker inspect --format='{{ .Config.Hostname }}' detectron2`

- docker 폴더로 이동

도커 파일을 아래에 맞게 수정

FROM nvidia/cuda:11.1.1-cudnn8-devel-ubuntu20.04
# use an older system (18.04) to avoid opencv incompatibility (issue#3524)

ENV DEBIAN_FRONTEND noninteractive
RUN apt-get update && apt-get install -y \
	python3-opencv ca-certificates python3-dev git wget sudo ninja-build
RUN ln -sv /usr/bin/python3 /usr/bin/python

# create a non-root user
ARG USER_ID=1000
RUN useradd -m --no-log-init --system  --uid ${USER_ID} appuser -g sudo
RUN echo '%sudo ALL=(ALL) NOPASSWD:ALL' >> /etc/sudoers
USER appuser
WORKDIR /home/appuser

ENV PATH="/home/appuser/.local/bin:${PATH}"
RUN wget https://bootstrap.pypa.io/get-pip.py && \
	python3 get-pip.py --user && \
	rm get-pip.py

# install dependencies
# See https://pytorch.org/ for other options if you use a different version of CUDA
RUN pip install --user tensorboard cmake   # cmake from apt-get is too old
RUN pip install --user torch==1.10 torchvision==0.11.1 -f https://download.pytorch.org/whl/cu111/torch_stable.html

RUN pip install --user 'git+https://github.com/facebookresearch/fvcore'
# install detectron2
# set FORCE_CUDA because during `docker build` cuda is not accessible
ENV FORCE_CUDA="1"
# This will by default build detectron2 for all common cuda architectures and take a lot more time,
# because inside `docker build`, there is no way to tell which architecture will be used.
ARG TORCH_CUDA_ARCH_LIST="Kepler;Kepler+Tesla;Maxwell;Maxwell+Tegra;Pascal;Volta;Turing"
ENV TORCH_CUDA_ARCH_LIST="${TORCH_CUDA_ARCH_LIST}"

# Set a fixed model cache directory.
ENV FVCORE_CACHE="/tmp"
WORKDIR /workspace

# run detectron2 under user "appuser":
# wget http://images.cocodataset.org/val2017/000000439715.jpg -O input.jpg
# python3 demo/demo.py  \
	#--config-file configs/COCO-InstanceSegmentation/mask_rcnn_R_50_FPN_3x.yaml \
	#--input input.jpg --output outputs/ \
	#--opts MODEL.WEIGHTS detectron2://COCO-InstanceSegmentation/mask_rcnn_R_50_FPN_3x/137849600/model_final_f10217.pkl

 

도커 빌드

docker build --build-arg USER_ID=$UID -t detectron2:v0 .

아래와 같이 docker container 실행

docker run --rm --gpus all -it --privileged -v /dev/video0:/dev/video0 --shm-size=12gb --net=host --env=unix$DISPLAY -e="QT_X11_NO_MITSHM=1" -v="/tmp/.X11-unix:/tmp/.X11-unix" -v $(pwd):/workspace --name=detectron2 detectron2:v0

컨테이너 테스트

 

코드 수정에 따라 원본 Dockerfile을 참고해서 상위 디렉토리에서 아래와 같은 명령어 실행

pip install --user -e .

아래 명령어로 컨테이너 내부를 업데이트 해주자

sudo apt update && sudo apt upgrade

컨테이너 밖 host에서 이미지를 보기 위해 아래 명령어 실행

xhost +local:`docker inspect --format='{{ .Config.Hostname }}' detectron2`

 

세팅이 어느 정도 된 것 같으니 demo 실행

 

Demo(Inference with pre-trained model)

https://detectron2.readthedocs.io/en/latest/tutorials/getting_started.html#inference-demo-with-pre-trained-models

 

Getting Started with Detectron2 — detectron2 0.5 documentation

This document provides a brief intro of the usage of builtin command-line tools in detectron2. For a tutorial that involves actual coding with the API, see our Colab Notebook which covers how to run inference with an existing model, and how to train a buil

detectron2.readthedocs.io

위를 참고하여 pre-trained model로 inference를 해본다

 

사자 이미지를 다운 받았다.

 

출처 : https://cdn.britannica.com/29/150929-050-547070A1/lion-Kenya-Masai-Mara-National-Reserve.jpg

위 링크에 있던 inference를 참고하여 다운받은 lion.jpg 파일에 대하여 아래의 코드를 실행해본다

python demo.py --config-file ../configs/COCO-InstanceSegmentation/mask_rcnn_R_50_FPN_3x.yaml --input lion.jpg --opts MODEL.WEIGHTS detectron2://COCO-InstanceSegmentation/mask_rcnn_R_50_FPN_3x/137849600/model_final_f10217.pkl

이미지가 나오지 않으면 컨테이너 아래서 명령어 실행

export DISPLAY=:1

Demo 결과

아래와 같은 결과가 나오는 것을 확인할 수 있다.

 

Demo webcam

Webcam으로도 사용할 수 있는 기능이 있어 실행시켜보았다.

python demo.py --config-file ../configs/COCO-InstanceSegmentation/mask_rcnn_R_50_FPN_3x.yaml --webcam --opts MODEL.WEIGHTS detectron2://COCO-InstanceSegmentation/mask_rcnn_R_50_FPN_3x/137849600/model_final_f10217.pkl

 

그러나 위와 같이 can't open camera by index라고 뜬다.

 

아무래도 컨테이너 안에서 camera device를 인식하지 못해 생기는 에러로 보인다.

보면 리눅스 local 에는 video0, video1 디바이스가 잡히는 반면

 

container 안에는 video 디바이스가 잡히지 않는다.

 

이에 container 종료 후 아래의 명령어로 video device를 마운트 시킨다.

 

docker run --rm --gpus all -it --shm-size=8gb --env="DISPLAY" --volume="/tmp/.X11-unix:/tmp/.X11-unix:rw" -v /dev/video0:/dev/video0 -v $(pwd):/workspace -w="/workspace" --name=detectron2 detectron2:fixed[바뀐 tag명]

 

다시 데모 실행

또 다시 에러가 발생한다.

 

구글 검색 후 아래와 같이 docker container를 실행하니 제대로 작동한다.

 

docker run --rm --gpus all -it --privileged -v /dev/video0:/dev/video0  --shm-size=8gb --env=unix$DISPLAY -e="QT_X11_NO_MITSHM=1" -v="/tmp/.X11-unix:/tmp/.X11-unix" -v $(pwd):/workspace -w="/workspace" --name=detectron2 detectron2:fixed

 

'딥러닝관련 > Detection' 카테고리의 다른 글

MMCV 의 Config  (0) 2021.10.26
MMdetection(1) 설치 및 Demo 실행 - (1)  (2) 2021.10.19
Fast R-CNN 정리  (0) 2021.06.28
Bounding box regression  (0) 2021.06.28
Non-maximum Suppression  (0) 2021.06.24