Skip to content

Caffe Example : 6.Classifying ImageNet: using the C API (Kor)

HanJiHoon edited this page Dec 30, 2016 · 3 revisions

์ด๋ฏธ์ง€๋ง ๋ถ„๋ฅ˜ํ•˜๊ธฐ : C++ API ์‚ฌ์šฉ (Classifying ImageNet: using the C++ API)

Caffe์˜ ํ•ต์‹ฌ์€ C++๋กœ ์“ฐ์—ฌ์กŒ๋‹ค๋Š” ๊ฒƒ์ด๋‹ค. ๋…ธํŠธ๋ถ์— ์˜ˆ์‹œ์ค‘ ํ•˜๋‚˜๋กœ ์ œ๊ณต๋œ ํŒŒ์ด์ฌ ์ฝ”๋“œ์™€ ๋น„์Šทํ•œ ์ด๋ฏธ์ง€ ๋ถ„๋ฅ˜ํ™” ์‘์šฉํ”„๋กœ๊ทธ๋žจ์„ ์ˆ˜ํ–‰ํ•˜๋Š”๋ฐ์— Caffe ์˜ C++ API๋ฅผ ์‚ฌ์šฉํ•  ์ˆ˜ ์žˆ๋‹ค. Caffe C++ API์˜ ๋‹ค๋ชฉ์  ์˜ˆ์‹œ๋ฅผ ์ข€ ๋” ๋ณด๊ธฐ์œ„ํ•ด์„œ๋Š”, ๋‹น์‹ ์€ tools/caffe.cpp์— ์žˆ๋Š” ์ปค๋งจ๋“œ ๋ผ์ธ์˜ ์†Œ์Šค ์ฝ”๋“œ๋ฅผ ๊ณต๋ถ€ํ•ด์•ผ๋งŒํ•œ๋‹ค.

์ œ๊ณต(Presentation)

๊ฐ„๋‹จํ•œ C++ ์ฝ”๋“œ๋Š” examples/cpp_classification/classification.cpp์—์„œ ์ œ๊ณต๋˜์–ด์ง„๋‹ค. ๊ฐ„๋‹จํ•˜๊ธฐ๋•Œ๋ฌธ์—, ์ด ์˜ˆ์‹œ๋Š” ๋‹ค์ค‘ ๋…๋ฆฝ ์˜ˆ์‹œ์˜ ๋‹จ์ผ ์ƒ˜ํ”Œ์ด๋‚˜ ์ผํšŒ์ฒ˜๋ฆฌ๋Ÿ‰์˜ ์˜ค๋ฒ„์ƒ˜ํ”Œ๋ง์ด ๋‘˜ ์ชฝ ๋ชจ๋‘ ๋ฐœ์ƒํ•˜์ง€ ์•Š๋Š”๋‹ค. ์ด ์˜ˆ์‹œ๋Š” ์‹œ์Šคํ…œ์—๋Œ€ํ•œ ์ตœ๋Œ€ ๊ฐ€๋Šฅํ•œ ๋ถ„๋ฅ˜ํ™” ์ฒ˜๋ฆฌ๋Ÿ‰์— ๋„๋‹ฌํ•˜๋ ค๋Š”๊ณ  ์‹œ๋„ํ•˜๋Š” ๊ฒƒ์€ ์•„๋‹ˆ์ง€๋งŒ, ์ฝ”๋“œ๋ฅผ ์ฝ๋„๋ก ์œ ์ง€ํ•˜๋Š” ๋ฐ˜๋ฉด์— ๋ถˆํ•„์š”ํ•œ ํŒจ์‹œ๋ฏธ์ œ์ด์…˜(pessimization)์„ ํšŒํ”ผํ•˜ํ”ผ์œ„ํ•ด ํŠน๋ณ„ํ•œ ๊ด€๋ฆฌ๊ฐ€ ์ฃผ์–ด์ง„๋‹ค.

๊ฒ€์‚ฌ(Compiling)

C++์˜ˆ์‹œ๋Š” Caffe๋ฅผ ์ปดํŒŒ์ผํ•  ๋•Œ ์ž๋™์ ์œผ๋กœ ๊ตฌ์ถ•๋œ๋‹ค. Caffe๋ฅผ ์ปดํŒŒ์ผํ•˜๊ธฐ ์œ„ํ•ด์„œ๋Š”, ๋ฌธ์„œํ™”๋œ ์„ค๋ช…๋ฌธ์„ ๋”ฐ๋ฅผํ•„์š”๊ฐ€ ์žˆ๋‹ค. ๋ถ„๋ฅ˜ํ™” ์˜ˆ์‹œ๋Š” ๋‹น์‹ ์˜ build ๋””๋ ‰ํ† ๋ฆฌ์— examples/classification.bin๋กœ์จ ๊ตฌ์ถ•๋  ๊ฒƒ์ด๋‹ค.

์‚ฌ์šฉ๋ฒ•(Usage)

๋ถ„๋ฅ˜ํ™” ์˜ˆ์‹œ๋กœ ์‚ฌ์ „ ํ•™์Šต๋œ CaffeNet ๋ชจ๋ธ์„ ์‚ฌ์šฉํ•˜๊ธฐ ์œ„ํ•ด์„œ๋Š”. ๋‹น์‹ ์€ ๋‹ค์Œ๊ณผ ๊ฐ™์€ ์Šคํฌ๋ฆฝํŠธ๋ฅผ ์‚ฌ์šฉํ•˜๋Š” "Zoo ๋ชจ๋ธ"๋กœ๋ถ€ํ„ฐ ๋‹ค์šด๋กœ๋“œํ•  ํ•„์š”๊ฐ€ ์žˆ๋‹ค : ./scripts/download_model_binary.py models/bvlc_reference_caffenet. ์ด๋ฏธ์ง€๋ง ๋ผ๋ฒจ ํŒŒ์ผ(๋˜ํ•œ ์‹ ์…‹(synset) ํŒŒ์ผ์ด๋ผ๊ณ ๋„ ๋ถˆ๋ฆฌ๋Š”)์€ ๋˜ํ•œ ํด๋ž˜์Šค์˜ ์ด๋ฆ„์„ ์˜ˆ์ธก์„ ๋งตํ•‘ํ•˜๊ธฐ์œ„ํ•ด ์š”๊ตฌ๋˜์–ด์ง„๋‹ค : ./data/ilsvrc12/get_ilsvrc_aux.sh. ๋‹ค์šด๋กœ๋“œํ•œ ํŒŒ์ผ๋“ค์„ ์‚ฌ์šฉํ•ด์„œ, ์šฐ๋ฆฌ๋Š” ๋‹ค์Œ๊ณผ ๊ฐ™์€ ๋ช…๋ น์–ด๋ฅผ ์‚ฌ์šฉํ•ด์„œ (examples/images/cat.jpg) ์ฃผ์–ด์ง„ ๊ณ ์–‘์ด ์‚ฌ์ง„์„ ๋ถ„๋ฅ˜ํ•  ์ˆ˜ ์žˆ๋‹ค.

./build/examples/cpp_classification/classification.bin \ models/bvlc_reference_caffenet/deploy.prototxt \ models/bvlc_reference_caffenet/bvlc_reference_caffenet.caffemodel \ data/ilsvrc12/imagenet_mean.binaryproto \ data/ilsvrc12/synset_words.txt \ examples/images/cat.jpg

๊ฒฐ๊ณผ๋Š” ๋‹ค์Œ๊ณผ ๊ฐ™์„ ๊ฒƒ์ด๋‹ค.

---------- Prediction for examples/images/cat.jpg ----------
0.3134 - "n02123045 tabby, tabby cat" 0.2380 - "n02123159 tiger cat" 0.1235 - "n02124075 Egyptian cat" 0.1003 - "n02119022 red fox, Vulpes vulpes" 0.0715 - "n02127052 lynx, catamount"

์ˆ˜ํ–‰ํ–ฅ์ƒ(Improving Performance)

์ˆ˜ํ–‰์„ ์ข€ ๋” ํ–ฅ์ƒ์‹œํ‚ค๊ธฐ ์œ„ํ•ด, ์šฐ๋ฆฌ๋Š” GPU๋ชจ๋“œ ํ™œ์šฉ์„ ํ•„์š”ํ•  ๊ฒƒ์ธ๋ฐ, ์—ฌ๊ธฐ ๋ช‡๊ฐ€์ง€ ๊ฐ€์ด๋“œ๋ผ์ธ์ด ์žˆ๋‹ค:

  • ์ดˆ๊ธฐ์— GPU์ƒ์— ๋ฐ์ดํ„ฐ๋ฅผ ์˜ฎ๊ธฐ๊ณ  ๋ชจ๋“  ์‚ฌ์ „์ฒ˜๋ฆฌ์ž‘์—…์„ ๊ฑฐ๊ธฐ์„œ ์ˆ˜ํ–‰ํ•˜๋ผ.
  • ๋งŒ์•ฝ ๋™์‹œ์— ๋ถ„๋ฅ˜ํ•  ์ด๋ฏธ์ง€๊ฐ€ ๋งŽ๋‹ค๋ฉด, ๋ฒ ์นญ(batching)์„ ์‚ฌ์šฉํ•ด์•ผ๋งŒ ํ•œ๋‹ค(๋…๋ฆฝ์  ์ด๋ฏธ์ง€๋“ค์€ ๋‹จ์ผ ์ •๋ฐฉํ–ฅ ๊ณผ์ •์—์„œ ๋ถ„๋ฅ˜ํ™”๋œ๋‹ค.).
  • GPU๊ฐ€ ํ•ญ์ƒ ์™„๋ฒฝํžˆ ์‚ฌ์šฉ๋˜์–ด์ง€๊ณ  CPU ์“ฐ๋ ˆ๋“œ๊ฐ€ ์ฐจ๋‹จ๋œ I/O๋ฅผ ๊ธฐ๋‹ค๋ฆด ํ•„์š”๊ฐ€ ์—†๋„๋ก ํ™•์‹คํžˆํ•˜๋Š” ๋‹ค์ค‘ ๋ถ„๋ฅ˜ํ™” ์“ฐ๋ ˆ๋“œ๋ฅผ ์‚ฌ์šฉํ•˜๋ผ.
Clone this wiki locally