brunch

You can make anything
by writing

C.S.Lewis

by 오준석 Oct 28. 2016

[TensorFlow] 이미지 학습 및 검증

작년에 이어 올 해에도 운이 좋게 구글 핵페어 2016에 참가하였었는데, 이 때 개발한 앱에 TensorFlow 를 접목하였기 때문에 기록하려고 합니다.



동작 영상


앱을 실행하고 전면 카메라에 사람이 보이면 인사를 하고, 얼굴 위치를 눈이 트래킹 하며 방향에 따라 움직입니다. 

사람이 웃으면 앱도 웃고, 사람이 안 웃으면 앱도 무표정이 됩니다.


여기까지 작년 핵페어 참가 내용이었고, 올 해 버전은 여기에 업그레이드되서 텐서플로우를 이용하여 남여 성별 분석을 추가하였습니다. 


텐서플로우로 학습시킨 데이터를 검증하는 것 까지의 경험을 공유합니다.


https://www.youtube.com/watch?v=HNaKBJfR1rI

반갑다 친구야. 동작 영상



텐서플로우


머신러닝을 할 수 있는 구글에서 공개한 오픈소스 라이브러리 입니다. https://www.tensorflow.org/

알파고처럼 학습을 시킬 수 있습니다. 



TensorFlow 로 학습 시키기


일단 TensorFlow 저장소에서 소스를 가져옵니다.

git clone --recurse-submodules https://github.com/tensorflow/tensorflow/
cd tensorflow


python 환경이 이미 되어 있다는 전제로 환경 설정을 합니다.

numpy 등이 요구 됩니다. 요구되는 것들은 모두 설치 해 주세요.

빌드를 위해 bazel 도 설치 해야 됩니다.

./configure
Please specify the location of python.
Do you wish to build TensorFlow with Google Cloud Platform support? [y/N] N
No Google Cloud Platform support will be enabled for TensorFlow
Do you wish to build TensorFlow with GPU support? [y/N] N

bazel build -c opt --copt=-mavx tensorflow/examples/image_retraining:retrain


학습을 시키기 위해 오리지널 이미지는 다음과 같은 구조로 정리합니다.


-original_images  

    -label0    

        0da3da.jpg   

         ...  

    -label1    

        0fdf8f.jpg    

        ...  

    -label2    

        02dfgd.jpg   

         ...


original_images 디렉토리를 tensorflow/examples 에 두고 빌드 스타트

bazel-bin/tensorflow/examples/image_retraining/retrain --image_dir tensorflow/examples/original_images


상당 시간 후에 빌드가 완료 됩니다. 제 맥북 13인치 (i5) 에서는 약 15분 정도의 시간이 걸렸었던 것 같습니다. i7에서는 딱 3배 빨랐습니다.


끝나면, /tmp/output_graph.pb 모델이 생성되고, 대응하는 라벨 리스트가 /tmp/output_labels.txt 로 생성됩니다.



검증


다음을 실행하여 모델이 잘 학습되었는지 검증 할 수 있습니다.

bazel build tensorflow/examples/label_image:label_imagebazel-bin/tensorflow/examples/label_image/label_image --graph=/tmp/output_graph.pb --labels=/tmp/output_labels.txt --output_layer=final_result --image=/path/to/0000.jpg


아래와 같이 첫째 줄의 에러는 무시하고, 결과가 표시됩니다.

tensorflow/examples/label_image/main.cc:207] label9 (8): 0.871714I tensorflow/examples/label_image/main.cc:207] label3 (2): 0.0572503I tensorflow/examples/label_image/main.cc:207] label5 (4): 0.0252654I tensorflow/examples/label_image/main.cc:207] label2 (3): 0.0217753I tensorflow/examples/label_image/main.cc:207] label6 (7): 0.014649






매거진의 이전글 [Android] 오래되면 까먹는 라이브러리들

작품 선택

키워드 선택 0 / 3 0

댓글여부

afliean
브런치는 최신 브라우저에 최적화 되어있습니다. IE chrome safari