[MAC] Darkflow로 신호등 검출 yolo모델 학습시키기

Nanyoung Kim
8 min readNov 10, 2020

직접 수집한 보행자 신호등 데이터로 커스텀 YOLO 모델을 만들어보자.

Step 1. 동영상 촬영

  • 보행자 시선으로 데이터를 수집하기 위해 도보로 이동하며 신호등을 촬영했다.

Step 2. 확장자 변환

  • 아이폰 11 Pro 로 촬영한 동영상은 MOV 형식이므로, 이미지 추출을 위해 MP4 형식으로 변환해준다.
  • 터미널에 ffmpeg 를 설치한 뒤 입력동영상이 있는 경로에서 아래 코드를 실행시킨다.
ffmpeg -i TLvideo_16.MOV -c copy TLvideo_16.mp4
  • 형식 : ffmpeg -i 입력동영상이름 -c copy 출력동영상이름

수정) 확장자 변환 없이 MOV 에서 바로 jpg 파일 추출이 가능하다.

Step 3. 이미지 추출하기

  • 모델을 훈련시키기 위한 이미지가 필요하다.
  • 동영에서 이미지를 추출해보자.
  • 형식은 아래와 같다.
ffmpeg -i [입력동영상] -an -r [초당 출력할 프레임 수] -y -s [출력 해상도] [출력이미지파일 이름]%d.jpg
  • 임의로 1초당 5프레임을 추출해보았다.
ffmpeg -i TLvideo_0.mp4 -an -r 5 -y -s 1080x1920 trainimg_%d.jpg
  • %d에 순서대로 0, 1, ,,, 순으로 지정되어 저장된다.
  • 출력 이미지 크기는 프레임이 깨지지 않게 하기 위해 동영상과 크기가 같게 지정했다.
  • mp4동영상과 같은 디렉토리에 jpg파일이 생성된다.

Step 4. 이미지에 라벨링하기

  • YOLO 모델은 Supervised Learning 이므로 클래스 라벨을 지정해서 학습시켜야한다.
  • Object Detection 의 정답 라벨은 각 객체의 class label & boundary-box의 쌍으로 구성되며 이를 Annotation이라 부른다.
  • LabelImg 를 이용해 트레이닝 이미지의 라벨을 지정함으로써 Annotation을 생성해보자.
  • https://github.com/tzutalin/labelImg 로 이동해서 레포지토리를 클론한다.
git clone https://github.com/tzutalin/labelImg.git
  • 아래와 같이 설치한다.
맥용 설치 방법

그런데 두번째 라인인 ‘pip3 install pipenv’ 에서 아래와 같은 에러가 발생했다.

ERROR: After October 2020 you may experience errors when installing or updating packages. This is because pip will change the way that it resolves dependency conflicts.We recommend you use --use-feature=2020-resolver to test your packages with the new resolver before it becomes the default.pytest-astropy 0.8.0 requires pytest-cov>=2.0, which is not installed.pytest-astropy 0.8.0 requires pytest-filter-subpackage>=0.1, which is not installed.pytest-remotedata 0.3.2 requires pytest>=3.1, but you'll have pytest 0.0.0 which is incompatible.pytest-openfiles 0.5.0 requires pytest>=4.6, but you'll have pytest 0.0.0 which is incompatible.pytest-doctestplus 0.8.0 requires pytest>=4.0, but you'll have pytest 0.0.0 which is incompatible.pytest-astropy 0.8.0 requires pytest>=4.6, but you'll have pytest 0.0.0 which is incompatible.pytest-astropy-header 0.1.2 requires pytest>=2.8, but you'll have pytest 0.0.0 which is incompatible.
  • 문제 해결을 위해 아래와 같이 타이핑 하면 성공적으로 설치된다.
python -m pip install --upgrade pippip install example --use-feature=2020-resolverpip3 install pipenv
  • ModuleNotFoundError: No module named ‘libs.resources’ 에러 해결
pyrcc5 -o libs/resources.py resources.qrc
  • 에러 해결 후 다시 labelImg.py 를 실행시키자.
python labelImg.py
  • 성공적으로 labelImg 가 열렸다.
  • 클론한 lagelImg 폴더 안의 data/predefined_classes.txt 를 수정한다.
  • 본인은 신호등 검출을 목적으로 하기 때문에 아래와 같이 클래스 세개를 지정했다.
  • 초록불과 빨간불이 출력되지 않은 상태(깜빡거리다 꺼진 상태)를 ‘trafficlight’로 지정했다.
  • 그 후 LabelImg 프로그램에서 ‘Open Dir’를 클릭해서 라벨링할 이미지가 있는 폴더를 선택하면 이미지들이 불러와진다.
  • 그 후 라벨을 직접 지정한다.
단축키w : create rectangle box
d : next image
a : previous image

Step 5. Train 시작

  • 훈련을 시작하기에 앞서 darkflow 폴더 안의 labels.txt를 위에서 했던 것처럼 3개의 클래스를 지정해준다.
  • 그 다음에는 darkflow/cfg 폴더 안의 cfg 중에서 자신이 사용할 모델을 수정해줘야한다. 필자는 안드로이드 앱에 모델을 올려야해서 tiny-yolo.cfg 를 사용했다.
  • 원본을 훼손하지 않기 위해 원본 파일을 복사해서 my-tiny-yolo.cfg로 파일명을 바꾼뒤 수정했다.
  • 바꿀 부분은 filters(line 114)와 classes(line 120)이다.
  • classes 는 자신이 위에정한 클래스의 수(필자는 3) 지정한다.
  • filters = (classes수 + 5) * 5 로 지정해준다. (필자는 40)
  • 이제 학습을 시작한다.
python flow — model ./cfg/my-tiny-yolo.cfg — labels ./labels.txt — trainer adam — dataset ../yolovideo2/ — annotation ../yolovideo2 — train — summary ./logs — batch 3 — epoch 100 — save 50 — keep 5 — lr 1e-04 — gpu 0.5 — load -1
  • 결과
error: cannot find symbol variable Fill where T is a type-variable: T extends Object declared in class Zeros
  • 위와 같은 에러는 아래의 링크를 참고해서 해결했다.

https://github.com/tensorflow/tensorflow/issues/21431

다음 포스팅은 안드로이드 스튜디오 위에 훈련한 모델을 올려서 구동시켜보는 내용을 다룰 예정이다.

--

--