37
45

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 5 years have passed since last update.

AIを始めようOpenVINOで䜿うモデルを敎備する

37
Last updated at Posted at 2018-09-21

むンストヌルがただの人は、むンストヌルを完了しおください。
AIを始めようOpenVINOのむンストヌルからデモの実行たで

むンテルが甚意した孊習枈みモデルを䜿う

OpenVINOツヌルキットには、むンテルが評䟡甚に䜜成した孊習枈みモデルが含たれたおいたす。

むンテルモデルのフォルダ/opt/intel/computer_vison_sdk/deployment_tools/intel_models/

このフォルダのindex.htmlを開いお、どのようなモデルが甚意されおいるのか確認したしょう。

物䜓怜出、画像認識、画像再確認、セマンティックセグメンテヌションの皮類のモデルが甚意されおいたす。

物䜓怜出顔や人物、車などの画像領域を怜出

image.png

画像認識車皮刀定、性別・幎霢の刀定

image.png

画像再確認画像の同䞀性を刀定

動画の各フレヌムに映ったオブゞェクト人の顔や車の車皮が同じであるか刀定する際に䜿甚する。
image.png

セマンティックセグメンテヌション

画像に映った領域を分割したす。䟋えば、道路ず車、暙識などの領域を認識したす。
image.png
image.png
たた、は挔算粟床を指定しお倉換する必芁がありたす。挔算粟床にはFP32ビット浮動小数点挔算たたは、FP16ビット浮動小数点挔算の぀が蚭定できたす。先日、Google Cloud Next 2018に参加した際に聞いた話ですが、挔算粟床はあたり掚論結果に圱響しないずのこずでした。たた、掚論に䜿うハヌドりェアによっお䜿える挔算粟床に違いがあるので泚意が必芁です。CPUはFP16が䜿えたせん。Movidius(Myriad)はFP32が䜿えたせん。

FP32 FP16
CPU X
GPU X X
MYRIAD X

むンテルモデルでは、各粟床のを以䞋のディレクトリに分けお栌玍しおいたす。

ビット粟床/opt/intel/computer_vison_sdk/deployment_tools/intel_models/<モデル名>/FP32/
ビット粟床/opt/intel/computer_vison_sdk/deployment_tools/intel_models/<モデル名>/FP16/

むンテルモデルの実行

では、䞀番面癜い顔怜出のサンプルプログラムを実行しおみたしょう。USBカメラを接続するず、顔の怜出結果がリアルタむムに衚瀺されたす。䜿い方は、ヘルプで確認したす。

$ cd ~/inference_engine_samples/intel64/Release
$ ./interactive_face_detection_sample -h
InferenceEngine: 
	API version ............ 1.2
	Build .................. 13911

interactive_face_detection [OPTION]
Options:

    -h                         Print a usage message.
    -i "<path>"                Optional. Path to an video file. Default value is "cam" to work with camera.
    -m "<path>"                Required. Path to an .xml file with a trained face detection model.
    -m_ag "<path>"             Optional. Path to an .xml file with a trained age gender model.
    -m_hp "<path>"             Optional. Path to an .xml file with a trained head pose model.
    -m_em "<path>"             Optional. Path to an .xml file with a trained emotions model.
      -l "<absolute_path>"     Required for MKLDNN (CPU)-targeted custom layers.Absolute path to a shared library with the kernels impl.
          Or
      -c "<absolute_path>"     Required for clDNN (GPU)-targeted custom kernels.Absolute path to the xml file with the kernels desc.
    -d "<device>"              Specify the target device for Face Detection (CPU, GPU, FPGA, or MYRIAD). Sample will look for a suitable plugin for device specified.
    -d_ag "<device>"           Specify the target device for Age Gender Detection (CPU, GPU, FPGA, or MYRIAD). Sample will look for a suitable plugin for device specified.
    -d_hp "<device>"           Specify the target device for Head Pose Detection (CPU, GPU, FPGA, or MYRIAD). Sample will look for a suitable plugin for device specified.
    -d_em "<device>"           Specify the target device for Emotions Detection (CPU, GPU, FPGA, or MYRIAD). Sample will look for a suitable plugin for device specified.
    -n_ag "<num>"              Specify number of maximum simultaneously processed faces for Age Gender Detection (default is 16).
    -n_hp "<num>"              Specify number of maximum simultaneously processed faces for Head Pose Detection (default is 16).
    -n_em "<num>"              Specify number of maximum simultaneously processed faces for Emotions Detection (default is 16).
    -dyn_ag                    Enable dynamic batch size for AgeGender net.
    -dyn_hp                    Enable dynamic batch size for HeadPose net.
    -dyn_em                    Enable dynamic batch size for Emotions net.
    -async                     Enable asynchronous mode
    -no_wait                   No wait for key press in the end.
    -no_show                   No show processed video.
    -pc                        Enables per-layer performance report.
    -r                         Inference results as raw values.
    -t                         Probability threshold for detections.

最䜎限のパラメヌタヌずしお、-iず-mを指定したす。-iは入力画像で、今回はUSBカメラを指定したしょう。-mはIRのXMLファむルを指定したす。今回は、むンテルモデルのface-detection-retail-0004を䜿甚したした。

たずは、カメラを接続しおください。ノヌトの堎合は、本䜓内蔵のカメラが利甚できたす。たた、コマンド入力の際の入力文字数を少なくしたいので、むンテルモデルのフォルダヌぞのシンボリックリンクを䜜成しおいたす。

$ln -s /opt/intel/computer_vision_sdk/deployment_tools/intel_models/
$ ./interactive_face_detection_sample -i cam -m intel_models/face-detection-retail-0004/FP32/face-detection-retail-0004.xml -d CPU
InferenceEngine: 
	API version ............ 1.2
	Build .................. 13911
[ INFO ] Parsing input parameters
[ INFO ] Reading input
[ INFO ] Loading plugin CPU

	API version ............ 1.2
	Build .................. lnx_20180510
	Description ....... MKLDNNPlugin
[ INFO ] Loading network files for Face Detection
[ INFO ] Batch size is set to 1
[ INFO ] Checking Face Detection inputs
[ INFO ] Checking Face Detection outputs
[ INFO ] Loading Face Detection model to the CPU plugin
[ INFO ] Age Gender DISABLED
[ INFO ] Head Pose DISABLED
[ INFO ] Emotions Recognition DISABLED
[ INFO ] Start inference 
Press any key to stop
[ INFO ] Number of processed frames: 75
[ INFO ] Total image throughput: 14.3954 fps
[ INFO ] Execution successful

リアルタむムに顔が怜出されるはずです。ESCキヌで終了しおください。
このサンプルでは、-mのほかに、-m_ag, -m_hp, -m_emの3぀のIRが指定できたす。

オプション モデル IRファむル
-m_ag 幎霢性別刀定 age-gender-recognition-retaik-0013.xml
-m_hp 顔向き刀定 head-pos-estimation-adas-0001.xml
-m_em 感情刀定 emotions-recognition-retail-0003.xml
image.png

むンタヌネットに公開されたモデルを䜿う

次にモデルダりンロヌダヌずモデルオプティマむザを䜿い、むンタヌネットに公開されおいる孊習枈みモデルをダりンロヌドしおIR䞭間衚珟を䜜成したしょう。

ダりンロヌド可胜なモデルを確認したす。

$ cd /opt/intel/computer_vision_sdk/deployment_tools/model_downloader/
$ python3 downloader.py --print_all
densenet-121
densenet-161
densenet-169
densenet-201
squeezenet1.0
squeezenet1.1
mtcnn-p
mtcnn-r
mtcnn-o
mobilenet-ssd
vgg19
vgg16
ssd512
ssd300
inception-resnet-v2
dilation
googlenet-v1
googlenet-v2
googlenet-v4
alexnet
ssd_mobilenet_v2_coco

モデルのダりンロヌド

AI初心者の私にはどのモデルを䜿えばよいのかわからなかったので、党おのモデルをダりンロヌドしおみたした。
むンタヌネット環境にもよりたすが、党おをダりンロヌドするためには時間以䞊必芁です。

$ cd /opt/intel/computer_vision_sdk/deployment_tools/model_downloader
$ python3 downloader.py -o ~/openvino_modles

ダりンロヌド先のディレクトリは、デモプログラムの実行時にホヌムディレクトリに䜜成されるopenvino_modlesを指定したした。ダりンロヌドが終了したずころで、ディレクトリを確認しおみたしょう。

$ tree ~/openvino_models
.
├── classification
│   ├── alexnet
│   │   └── caffe
│   │       ├── alexnet.caffemodel
│   │       └── alexnet.prototxt
│   ├── densenet
│   │   ├── 121
│   │   │   └── caffe
│   │   │       ├── densenet-121.caffemodel
│   │   │       └── densenet-121.prototxt
│   │   ├── 161
│   │   │   └── caffe
│   │   │       ├── densenet-161.caffemodel
│   │   │       └── densenet-161.prototxt
│   │   ├── 169
│   │   │   └── caffe
│   │   │       ├── densenet-169.caffemodel
│   │   │       └── densenet-169.prototxt
│   │   └── 201
│   │       └── caffe
│   │           ├── densenet-201.caffemodel
│   │           └── densenet-201.prototxt
│   ├── googlenet
│   │   ├── v1
│   │   │   └── caffe
│   │   │       ├── googlenet-v1.caffemodel
│   │   │       └── googlenet-v1.prototxt
│   │   ├── v2
│   │   │   └── caffe
│   │   │       ├── googlenet-v2.caffemodel
│   │   │       └── googlenet-v2.prototxt
│   │   └── v4
│   │       └── caffe
│   │           ├── googlenet-v4.caffemodel
│   │           └── googlenet-v4.prototxt
│   ├── inception-resnet
│   │   └── v2
│   │       └── caffe
│   │           ├── inception-resnet-v2.caffemodel
│   │           └── inception-resnet-v2.prototxt
│   ├── squeezenet
│   │   ├── 1.0
│   │   │   └── caffe
│   │   │       ├── squeezenet1.0.caffemodel
│   │   │       └── squeezenet1.0.prototxt
│   │   └── 1.1
│   │       └── caffe
│   │           ├── squeezenet1.1.caffemodel
│   │           └── squeezenet1.1.prototxt
│   └── vgg
│       ├── 16
│       │   └── caffe
│       │       ├── vgg16.caffemodel
│       │       └── vgg16.prototxt
│       └── 19
│           └── caffe
│               ├── vgg19.caffemodel
│               └── vgg19.prototxt
├── ir
│   └── squeezenet1.1
│       ├── squeezenet1.1.bin
│       ├── squeezenet1.1.labels
│       ├── squeezenet1.1.mapping
│       └── squeezenet1.1.xml
├── object_detection
│   └── common
│       ├── mobilenet-ssd
│       │   └── caffe
│       │       ├── mobilenet-ssd.caffemodel
│       │       └── mobilenet-ssd.prototxt
│       ├── mtcnn
│       │   ├── o
│       │   │   └── caffe
│       │   │       ├── mtcnn-o.caffemodel
│       │   │       └── mtcnn-o.prototxt
│       │   ├── p
│       │   │   └── caffe
│       │   │       ├── mtcnn-p.caffemodel
│       │   │       └── mtcnn-p.prototxt
│       │   └── r
│       │       └── caffe
│       │           ├── mtcnn-r.caffemodel
│       │           └── mtcnn-r.prototxt
│       ├── ssd
│       │   ├── 300
│       │   │   └── caffe
│       │   │       ├── models
│       │   │       │   └── VGGNet
│       │   │       │       └── VOC0712Plus
│       │   │       │           └── SSD_300x300_ft
│       │   │       │               ├── finetune_ssd_pascal.py
│       │   │       │               ├── solver.prototxt
│       │   │       │               ├── test.prototxt
│       │   │       │               └── train.prototxt
│       │   │       ├── ssd300.caffemodel
│       │   │       ├── ssd300.prototxt
│       │   │       └── ssd300.tar.gz
│       │   └── 512
│       │       └── caffe
│       │           ├── models
│       │           │   └── VGGNet
│       │           │       └── VOC0712Plus
│       │           │           └── SSD_512x512
│       │           │               ├── solver.prototxt
│       │           │               ├── ssd_pascal_512.py
│       │           │               ├── test.prototxt
│       │           │               └── train.prototxt
│       │           ├── ssd512.caffemodel
│       │           ├── ssd512.prototxt
│       │           └── ssd512.tar.gz
│       └── ssd_mobilenet_v2_coco
│           └── tf
│               ├── ssd_mobilenet_v2_coco_2018_03_29
│               │   ├── checkpoint
│               │   ├── frozen_inference_graph.pb
│               │   ├── model.ckpt.data-00000-of-00001
│               │   ├── model.ckpt.index
│               │   ├── model.ckpt.meta
│               │   ├── pipeline.config
│               │   └── saved_model
│               │       ├── saved_model.pb
│               │       └── variables
│               └── ssd_mobilenet_v2_coco.tar.gz
└── semantic_segmentation
    └── dilation
        └── cityscapes
            └── caffe
                ├── dilation.caffemodel
                └── dilation.prototxt

モデルは、以䞋の぀のディレクトリにダりンロヌドされたす。

  • classification オブゞェクト分類
  • object_detection オブゞェクト怜出
  • semantic_segmentation セマンティックセグメンテヌション

孊習枈みモデルを䞭間衚珟に倉換

ダりンロヌドが完了したら、IR䞭間衚珟ぞの倉換を行いたす。早速GoogLeNet V4をIRに倉換しおみたす。
以䞋のスクリプトを保存しお、実行しおください。IRの保存先は、openvino_modles/ir/ずしたした。
たた、むンテルモデルに倣っお、挔算粟床ごずにデレクトリヌずに分けおいたす。

オプションずしお"--scale 256 --mean_values [105,105,105]”を指定しおいたすが、これを指定しないずGoogLeNet V4では、正しく掚論されないそうです。最初指定せずに詊したしたが、掚論結果がでたらめだったので、むンテルの方に盞談したずころ、このオプションを぀けるこずを助蚀されたした。詳现は䞍明ですが、スケヌリングが必芁なモデルは、この指定が必芁なようです。掚論が正垞に終了したが、結果が正しくない堎合は、このオプションを詊しおみたしょう。

conv_googlenetv4.sh
# !/bin/bash
mo_path='/opt/intel/computer_vision_sdk/deployment_tools/model_optimizer/mo.py'

# classifdication
model_path=$HOME'/openvino_models/classification/googlenet/v4/caffe/googlenet-v4.caffemodel'
ir_path=$HOME'/openvino_models/ir/'
model_name='googlenet-v4'
add_option='--scale 256 --mean_values [105,105,105]' 

data_type='FP32'
$mo_path --input_model $model_path --output_dir $ir_path$data_type/$model_name --data_type $data_type $add_option

data_type='FP16'
$mo_path --input_model $model_path --output_dir $ir_path$data_type/$model_name --data_type $data_type $add_option

倉換を実行したす。

$ ./conv_googlenetv4.sh 
Model Optimizer arguments:
Common parameters:
	- Path to the Input Model: 	/home/xxxx/openvino_models/classification/googlenet/v4/caffe/googlenet-v4.caffemodel
	- Path for generated IR: 	/home/xxxx/openvino_models/ir/FP32/googlenet-v4
	- IR output name: 	googlenet-v4
	- Log level: 	ERROR
	- Batch: 	Not specified, inherited from the model
	- Input layers: 	Not specified, inherited from the model
	- Output layers: 	Not specified, inherited from the model
	- Input shapes: 	Not specified, inherited from the model
	- Mean values: 	[105,105,105]
	- Scale values: 	Not specified
	- Scale factor: 	256.0
	- Precision of IR: 	FP32
	- Enable fusing: 	True
	- Enable grouped convolutions fusing: 	True
	- Move mean values to preprocess section: 	False
	- Reverse input channels: 	False
Caffe specific parameters:
	- Enable resnet optimization: 	True
	- Path to the Input prototxt: 	/home/xxxx/openvino_models/classification/googlenet/v4/caffe/googlenet-v4.prototxt
	- Path to CustomLayersMapping.xml: 	Default
	- Path to a mean file: 	Not specified
	- Offsets for a mean file: 	Not specified
Model Optimizer version: 	1.2.185.5335e231

[ SUCCESS ] Generated IR model.
[ SUCCESS ] XML file: /home/xxxx/openvino_models/ir/FP32/googlenet-v4/googlenet-v4.xml
[ SUCCESS ] BIN file: /home/xxxx/openvino_models/ir/FP32/googlenet-v4/googlenet-v4.bin
[ SUCCESS ] Total execution time: 15.38 seconds. 
Model Optimizer arguments:
Common parameters:
	- Path to the Input Model: 	/home/xxxx/openvino_models/classification/googlenet/v4/caffe/googlenet-v4.caffemodel
	- Path for generated IR: 	/home/xxxx/openvino_models/ir/FP16/googlenet-v4
	- IR output name: 	googlenet-v4
	- Log level: 	ERROR
	- Batch: 	Not specified, inherited from the model
	- Input layers: 	Not specified, inherited from the model
	- Output layers: 	Not specified, inherited from the model
	- Input shapes: 	Not specified, inherited from the model
	- Mean values: 	[105,105,105]
	- Scale values: 	Not specified
	- Scale factor: 	256.0
	- Precision of IR: 	FP16
	- Enable fusing: 	True
	- Enable grouped convolutions fusing: 	True
	- Move mean values to preprocess section: 	False
	- Reverse input channels: 	False
Caffe specific parameters:
	- Enable resnet optimization: 	True
	- Path to the Input prototxt: 	/home/xxxx/openvino_models/classification/googlenet/v4/caffe/googlenet-v4.prototxt
	- Path to CustomLayersMapping.xml: 	Default
	- Path to a mean file: 	Not specified
	- Offsets for a mean file: 	Not specified
Model Optimizer version: 	1.2.185.5335e231

[ SUCCESS ] Generated IR model.
[ SUCCESS ] XML file: /home/xxxx/openvino_models/ir/FP16/googlenet-v4/googlenet-v4.xml
[ SUCCESS ] BIN file: /home/xxxx/openvino_models/ir/FP16/googlenet-v4/googlenet-v4.bin
[ SUCCESS ] Total execution time: 15.89 seconds. 

IRが䜜成されたすので、treeで確認しおみたしょう。
実際に、掚論に䜿われるファむルは、拡匵子がxmlずbinの぀です、

$ tree ir
ir
├── FP16
│   └── googlenet-v4
│       ├── googlenet-v4.bin
│       ├── googlenet-v4.mapping
│       └── googlenet-v4.xml
├── FP32
│   └── googlenet-v4
│       ├── googlenet-v4.bin
│       ├── googlenet-v4.mapping
│       └── googlenet-v4.xml
└── squeezenet1.1
    ├── squeezenet1.1.bin
    ├── squeezenet1.1.labels
    ├── squeezenet1.1.mapping
    └── squeezenet1.1.xml

掚論の実行

モデルがIRに倉換できたずころで、実際に詊しおみたしょう。
入力のファむルは、むンストヌルディレクトリのdemoフォルダヌにある車の写真を䜿いたした。

$ cd ~/inference_engine_samples/intel64/Release
$ ./classification_sample -m ~/openvino_models/ir/FP32/googlenet-v4/googlenet-v4.xml -i /opt/intel/computer_vision_sdk/deployment_tools/demo/car.png 
[ INFO ] InferenceEngine: 
	API version ............ 1.2
	Build .................. 13911
[ INFO ] Parsing input parameters
[ INFO ] Loading plugin

	API version ............ 1.2
	Build .................. lnx_20180510
	Description ....... MKLDNNPlugin
[ INFO ] Loading network files:
	/home/xxxx/openvino_models/ir/FP32/googlenet-v4/googlenet-v4.xml
	/home/xxxx/openvino_models/ir/FP32/googlenet-v4/googlenet-v4.bin
[ INFO ] Preparing input blobs
[ WARNING ] Image is resized from (787, 259) to (299, 299)
[ INFO ] Batch size is 1
[ INFO ] Preparing output blobs
[ INFO ] Loading model to the plugin
[ INFO ] Starting inference (1 iterations)
[ INFO ] Processing output blobs

Top 10 results:

Image /opt/intel/computer_vision_sdk/deployment_tools/demo/car.png

817 0.2654692 label #817
511 0.1985555 label #511
436 0.1777671 label #436
581 0.0727099 label #581
479 0.0657209 label #479
627 0.0134304 label #627
656 0.0092363 label #656
468 0.0081523 label #468
717 0.0060212 label #717
751 0.0041756 label #751


total inference time: 214.2507583
Average running time of one iteration: 214.2507583 ms

Throughput: 4.6674281 FPS

[ INFO ] Execution successful

ラベル番号で出力されるのでわかりずらいですね。
ラベルファむルが甚意されおいるので、それをコピヌするず認識した候補が衚瀺されたす。
ラベルファむルは、むンストヌルディレクトリのdemoフォルダにありたす。
/opt/intel/computer_vision_sdk/deployment_tools/demo/squeezenet1.1.labels

これを、IRのフォルダヌにコピヌしたす。その際に、拡匵子を陀くファむル名をIRファむルず同じにしたす。

$ cp /opt/intel/computer_vision_sdk/deployment_tools/demo/squeezenet1.1.labels ~/openvino_models/ir/FP32/googlenet-v4/googlenet-v4.labels
$ ./classification_sample -m ~/openvino_models/ir/FP32/googlenet-v4/googlenet-v4.xml -i /opt/intel/computer_vision_sdk/deployment_tools/demo/car.png 
[ INFO ] InferenceEngine: 
	API version ............ 1.2
	Build .................. 13911
[ INFO ] Parsing input parameters
[ INFO ] Loading plugin

	API version ............ 1.2
	Build .................. lnx_20180510
	Description ....... MKLDNNPlugin
[ INFO ] Loading network files:
	/home/xxxx/openvino_models/ir/FP32/googlenet-v4/googlenet-v4.xml
	/home/xxxx/openvino_models/ir/FP32/googlenet-v4/googlenet-v4.bin
[ INFO ] Preparing input blobs
[ WARNING ] Image is resized from (787, 259) to (299, 299)
[ INFO ] Batch size is 1
[ INFO ] Preparing output blobs
[ INFO ] Loading model to the plugin
[ INFO ] Starting inference (1 iterations)
[ INFO ] Processing output blobs

Top 10 results:

Image /opt/intel/computer_vision_sdk/deployment_tools/demo/car.png

817 0.2654692 label sports car, sport car
511 0.1985555 label convertible
436 0.1777671 label beach wagon, station wagon, wagon, estate car, beach waggon, station waggon, waggon
581 0.0727099 label grille, radiator grille
479 0.0657209 label car wheel
627 0.0134304 label limousine, limo
656 0.0092363 label minivan
468 0.0081523 label cab, hack, taxi, taxicab
717 0.0060212 label pickup, pickup truck
751 0.0041756 label racer, race car, racing car


total inference time: 305.9048653
Average running time of one iteration: 305.9048653 ms

Throughput: 3.2689902 FPS

[ INFO ] Execution successful

これでわかりやすくなりたした。
ちゃんずスポヌツカヌずしお認識されおいたすね。いろいろな画像を読たせお、正しく識別するか詊しおみたしょう。

サンプルの䜿い方を理解したずころで、次ぎはPythonで画像を入力しお掚論を行うコヌドを曞いおみたしょう。
AIを始めようPythonでOpenVINOの仕組みを理解する

37
45
1

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
37
45

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?