지구탐사

faster R-CNN tutorial (os : window 10) 본문

ML study/Object Detection

faster R-CNN tutorial (os : window 10)

mwna40000 2019. 8. 30. 17:32

이 글은 https://www.youtube.com/watch?v=Rgpfk6eYxJA을 바탕으로 작성하였습니다.

Tensorflow Object Detection API 중 faster R-CNN tutorial에 대한 것입니다.

  • tensorflow-gpu 를 설치하는 경우 CUDA, cuDNN은 이미 설치
  • 파일 경로는 E:\ (자신의 파일 경로를 알고 있음)
  • anaconda prompt는 C:\Users\user
  • 이미지 파일의 xml 파일을 가지고 있음

를 가정합니다.


1. 파일 세팅

  1. https://github.com/tensorflow/models 파일 다운로드 (models-master.zip) 후 압축 해제 (경로 : E:\)
  2. https://github.com/tensorflow/models/blob/master/research/object_detection/g3doc/detection_model_zoo.md 에서 faster_rcnn_inception_v2_coco 다운로드 (faster_rcnn_inception_v2_coco_2018_01_28.tar.gz)
  3. 2에서 다운로드한 파일을 압축 해제 후 E:\models-master\research\object_detection 폴더로 옮김
  4. https://github.com/EdjeElectronics/TensorFlow-Object-Detection-API-Tutorial-Train-Multiple-Objects-Windows-10#1-install-anaconda-cuda-and-cudnn 다운로드 (TensorFlow-Object-Detection-API-Tutorial-Train-Multiple-Objects-Windows-10-master.zip)
  5. 4에서 다운로드한 파일을 압축 해제 후 폴더 내 모든 파일을 E:\models-master\research\object_detection 로 옮김

여기까지 되었다면 몇 가지 삭제해야 할 파일 존재 (향후 다시 생성 됩니다.)

  1. E:\models-master\research\object_detection\training 파일 삭제
  2. E:\models-master\research\object_detection\inference_graph 파일 삭제
  3. E:\models-master\research\object_detection\images 에서 test_labels.csv 와 train_labels.csv 파일 삭제

2. 가상환경 만들기

  • 필요한 가상환경과 라이브러리 설치를 위해 anaconda prompt를 열고 아래 코드 입력


>conda create -n faster_rcnn python=3.6 
>conda activate faster_rcnn
>python -m pip install --upgrade pip
>pip install --ignore-installed --upgrade tensorflow-gpu
>conda install -c anaconda protobuf
>pip install pillow lxml Cython contextlib2 jupyter matplotlib pandas opencv-python

3. 경로지정


>e:
>set PYTHONPATH=E:\models-master;E:\models-master\research;E:\models-master\research\slim
>set PATH=%PATH%;PYTHONPATH
  • note : models-master 폴더가 c: 드라이브에 있다면 코드의 첫번째 줄인 e: 는 필요하지 않습니다.

4. .proto 파일의 .py 파일 생성


>cd E:\models-master\research
>protoc --python_out=. .\object_detection\protos\anchor_generator.proto .\object_detection\protos\argmax_matcher.proto .\object_detection\protos\bipartite_matcher.proto .\object_detection\protos\box_coder.proto .\object_detection\protos\box_predictor.proto .\object_detection\protos\eval.proto .\object_detection\protos\faster_rcnn.proto .\object_detection\protos\faster_rcnn_box_coder.proto .\object_detection\protos\grid_anchor_generator.proto .\object_detection\protos\hyperparams.proto .\object_detection\protos\image_resizer.proto .\object_detection\protos\input_reader.proto .\object_detection\protos\losses.proto .\object_detection\protos\matcher.proto .\object_detection\protos\mean_stddev_box_coder.proto .\object_detection\protos\model.proto .\object_detection\protos\optimizer.proto .\object_detection\protos\pipeline.proto .\object_detection\protos\post_processing.proto .\object_detection\protos\preprocessor.proto .\object_detection\protos\region_similarity_calculator.proto .\object_detection\protos\square_box_coder.proto .\object_detection\protos\ssd.proto .\object_detection\protos\ssd_anchor_generator.proto .\object_detection\protos\string_int_label_map.proto .\object_detection\protos\train.proto .\object_detection\protos\keypoint_box_coder.proto .\object_detection\protos\multiscale_anchor_generator.proto .\object_detection\protos\graph_rewriter.proto .\object_detection\protos\calibration.proto .\object_detection\protos\flexible_grid_anchor_generator.proto
  • note : E:\models-master\research\object_detection\protos 에 .py 파일이 생성되는 것을 볼 수 있음

5. setup


>python setup.py build
>python setup.py install

6. 여기까지 테스트


>jupyter notebook
  • object_detection\object_detection_tutorial.ipynb 실행
  • 오류 없이 돌아가지만 마지막 셀에 그림이 나오지 않을 경우 %matplotlib inline 셀을 한 번 더 컴파일하고, 마지막 셀을 실행
  • jupyter notebook 종료
  • note : 현재 prompt 경로 E:\models-master\research

7. xml -> csv -> tfrecord

  • 동영상에서는 이미지 파일에서 labelimg를 이용하여 xml파일을 만드는 방법을 설명
  • 여기서는 다운로드한 파일에 xml 파일이 포함되어 있으므로 생략(궁금하신 분은 동영상 참고)


>cd object_detection
>python xml_to_csv.py
>python generate_tfrecord.py --csv_input=images\train_labels.csv --image_dir=images\train --output_path=train.record
>python generate_tfrecord.py --csv_input=images\test_labels.csv --image_dir=images\test --output_path=test.record
  • note : E:\models-master\research\object_detection\images에 test_labels.csv와 train_labels.csv 생성
  • E:\models-master\research\object_detection에 train.record와test.record 생성
  • 주의! 0KB 아닌지 확인!! 만약, 0KB 라면 뭔가 잘못된 것!!!
  • 향후 모델을 사용할 때 label개수에 따라 generate_tfrecord.py의 label 수정가능

8. label map 

  1. E:\models-master\research\object_detection\data 에서 .pbtxt 파일 하나를 열고 새파일 창을 하나 만듦
  2. 새파일에 다음 내용을 붙여 넣기


item {
  id: 1
  name: 'nine'
}

item {
  id: 2
  name: 'ten'
}

item {
  id: 3
  name: 'jack'
}

item {
  id: 4
  name: 'queen'
}

item {
  id: 5
  name: 'king'
}

item {
  id: 6
  name: 'ace'
}
  1. E:\models-master\research\object_detection\training 에 labelmap.pbtxt 이름으로 저장

9. confiure training

  1. E:\models-master\research\object_detection\samples\configs 에서 faster_rcnn_inception_v2_pets.config 파일 복사
  2. E:\models-master\research\object_detection\training 에 붙여넣기
  3. faster_rcnn_inception_v2_pets.config 파일 열어서 아래와 같이 내용 변경 후 저장 (줄번호가 다를 수 있음)
위치 변 경 전 변 경 후
9번째  num_classes: 37 num_clsses: 6
106번째 PATH_TO_BE_CONFIGURED/model.ckpt E:\\models-master\\research\\object_detection\\faster_rcnn_inception_v2_coco_2018_01_28\\model.ckpt
113번째 num_steps: 200000 num_steps: 2000
123번째 PATH_TO_BE_CONFIGURED/pet_faces_train.record-?????-of-00010 E:\\models-master\\research\\object_detection\\train.record
125번째 PATH_TO_BE_CONFIGURED/pet_label_map.pbtxt E:\\models-master\\research\\object_detection\\training\\labelmap.pbtxt
129번째 metrics_set: "coco_detection_metrics" num_examples:67
130번째 num_examples: 1101 max_evals:10
135번째 PATH_TO_BE_CONFIGURED/pet_faces_val.record-?????-of-00010 E:\\models-master\\research\\object_detection\\test.record
137번째 PATH_TO_BE_CONFIGURED/pet_label_map.pbtxt E:\\models-master\\research\\object_detection\\training\\labelmap.pbtxt
  • num_classes: 6 은 향후 모델을 사용할 때 label개수에 따라 변경
  • 변경후에서 윈도우의 경우 경로는 \\ 으로 (오타아님)
  • num_steps 는 epoch
  • num_examples:67 은 text 이미지의 개수

10. training

  1. E:\models-master\research\object_detection\legacy 에서 train.py 파일을 복사
  2. E:\models-master\research\object_detection 에 붙여넣기
  3. E:\models-master\research\slim 에서 nets 와 deployment 폴더를 복사
  4. E:\models-master\research\object_detection 에 붙여넣기
  5. 아래 코드를 실행하면 학습 시작 (현재 경로 : E:\models-master\research\object_detection)


>python train.py --logtostderr --train_dir=training/ --pipeline_config_path=training/faster_rcnn_inception_v2_pets.config
  • note : 중간에 학습을 그만두고 싶다면 ctrl+c

11. tensorbord


>tensorbord --logdir=training
  • 필자의 경우 오류로 인해 패스

12. inference_graph

  • 아래 코드 마지막에 model.ckpt-XXXX 변경
  • E:\models-master\research\object_detection\training 폴더에서 가장 마지막에 있는 model.ckpt-XXXX.meta 숫자로


>python export_inference_graph.py --input_type image_tensor --pipeline_config_path training/faster_rcnn_inception_v2_pets.config --trained_checkpoint_prefix training/model.ckpt-XXXX --output_directory inference_graph

13. test

>python Object_detection_video.py
Comments