{{indexmenu_n>1}} ====== サンプルを動作させる ====== このページでは,opencv のdnn samplesを動作させるために必要な前提知識を説明します.すでに example_dnn_object_detection 等の実行ファイルがビルド済みという前提でお話を進めますので,まだOpenCVをビルドしていない場合は [[opencv_dnn:環境構築:install|OpenCVでDNN]] ここからスタートしてください.dnnを利用したexampleはまだそんな多くはないですが,以下に各サンプルに関する動作確認ページを用意していますので,このページを読み終わったら各自進めてみてください. {{indexmenu>:opencv_dnn:samples#1|js}} ===== models.yml ===== opencv-version/samples/dnn/models.yml に置かれているファイルになります.中身を見てもらえば想像つくと思いますが,各種dnnサンプルを実行するために必要なパラメータ設定や重みファイル名,ネットワーク構成ファイル名等が記載されています.これを利用すると,コマンドラインからexampleを実行する際,いちいち細かなパラメータを引数で設定する必要がないので,とても便利です.覚えて置きましょう.ファイルの中身は以下のようになっています.ちなみにyml(yaml)というファイルは,xmlのように構造データを記載するためによく使われるテキスト形式のデータファイルです.xmlよりも可読性が高いので結構色んな所で使われています.簡潔にいうとweb系じゃない人がこれまでよく使ってきたjsonだと思ってください(適当すぎる). %YAML:1.0 ################################################################################ # Object detection models. from opencv.4.1.1 ################################################################################ # OpenCV's face detection network opencv_fd: model: "opencv_face_detector.caffemodel" config: "opencv_face_detector.prototxt" mean: [104, 177, 123] scale: 1.0 width: 300 height: 300 rgb: false sample: "object_detection" # YOLO object detection family from Darknet (https://pjreddie.com/darknet/yolo/) # Might be used for all YOLOv2, TinyYolov2 and YOLOv3 yolo: model: "yolov3.weights" config: "yolov3.cfg" mean: [0, 0, 0] scale: 0.00392 width: 416 height: 416 rgb: true classes: "object_detection_classes_yolov3.txt" sample: "object_detection" tiny-yolo-voc: model: "tiny-yolo-voc.weights" config: "tiny-yolo-voc.cfg" mean: [0, 0, 0] scale: 0.00392 width: 416 height: 416 rgb: true classes: "object_detection_classes_pascal_voc.txt" sample: "object_detection" # Caffe implementation of SSD model from https://github.com/chuanqi305/MobileNet-SSD ssd_caffe: model: "MobileNetSSD_deploy.caffemodel" config: "MobileNetSSD_deploy.prototxt" mean: [127.5, 127.5, 127.5] scale: 0.007843 width: 300 height: 300 rgb: false classes: "object_detection_classes_pascal_voc.txt" sample: "object_detection" # TensorFlow implementation of SSD model from https://github.com/tensorflow/models/tree/master/research/object_detection ssd_tf: model: "ssd_mobilenet_v1_coco_2017_11_17.pb" config: "ssd_mobilenet_v1_coco_2017_11_17.pbtxt" mean: [0, 0, 0] scale: 1.0 width: 300 height: 300 rgb: true classes: "object_detection_classes_coco.txt" sample: "object_detection" # TensorFlow implementation of Faster-RCNN model from https://github.com/tensorflow/models/tree/master/research/object_detection faster_rcnn_tf: model: "faster_rcnn_inception_v2_coco_2018_01_28.pb" config: "faster_rcnn_inception_v2_coco_2018_01_28.pbtxt" mean: [0, 0, 0] scale: 1.0 width: 800 height: 600 rgb: true sample: "object_detection" ################################################################################ # Image classification models. ################################################################################ # SqueezeNet v1.1 from https://github.com/DeepScale/SqueezeNet squeezenet: model: "squeezenet_v1.1.caffemodel" config: "squeezenet_v1.1.prototxt" mean: [0, 0, 0] scale: 1.0 width: 227 height: 227 rgb: false classes: "classification_classes_ILSVRC2012.txt" sample: "classification" # Googlenet from https://github.com/BVLC/caffe/tree/master/models/bvlc_googlenet googlenet: model: "bvlc_googlenet.caffemodel" config: "bvlc_googlenet.prototxt" mean: [104, 117, 123] scale: 1.0 width: 224 height: 224 rgb: false classes: "classification_classes_ILSVRC2012.txt" sample: "classification" ################################################################################ # Semantic segmentation models. ################################################################################ # ENet road scene segmentation network from https://github.com/e-lab/ENet-training # Works fine for different input sizes. enet: model: "Enet-model-best.net" mean: [0, 0, 0] scale: 0.00392 width: 512 height: 256 rgb: true classes: "enet-classes.txt" sample: "segmentation" fcn8s: model: "fcn8s-heavy-pascal.caffemodel" config: "fcn8s-heavy-pascal.prototxt" mean: [0, 0, 0] scale: 1.0 width: 500 height: 500 rgb: false sample: "segmentation" ===== dnnSamplePlayground ===== というわけで,このymlファイルを利用すると簡単に動作させるために必要なパラメータを設定できるので,手軽にテストできるような環境を作成します. まずはデスクトップに dnnSamplePlaygroundというフォルダを作成します.このフォルダにmodels.ymlファイル,opencv-version/samples/data/dnn にあるすべてのtxtファイルを dnnSamplePlayground にコピーします.さらに opencv_version/build/bin のフォルダ内にある example_dnn_* をdnnSamplePlaygroundにコピーしておきます. > cp opencv-version/samples/data/dnn/*.txt dnnSamplePlayground/ 最後にサンプル画像({{:opencv_dnn:samples:space_shuttle.jpg?linkonly|}}をダウンロードして,dnnSamplePlaygroundにコピーしましょう.出来上がったフォルダは次のようになっています. {{:opencv_dnn:samples:pasted:20190730-112038.png}} では,example_dnn_classification を実際にこのymlを利用して動作させてみます.今回はネットワークが軽量な SqueezeNetを利用します.dnnSamplePlaygroudフォルダ内からterminalで,下記を実行してモデルファイルをダウンロードしましょう. wget https://github.com/DeepScale/SqueezeNet/raw/master/SqueezeNet_v1.1/squeezenet_v1.1.caffemodel wget https://raw.githubusercontent.com/DeepScale/SqueezeNet/master/SqueezeNet_v1.1/deploy.prototxt ダウンロード完了後,次のコマンドを実行してください.本来であればymlファイルにクラスラベルリストも記述されているので,--classesの引数はないくても良いはずなんですが,この部分だけymlからのclasses読み込みが未実装のため,直接引数でラベルリストを追記しておく必要があります. $ ./example_dnn_classification squeezenet --zoo=models.yml --classes=classification_classes_ILSVRC2012.txt --input=space_shuttle.jpg {{:opencv_dnn:samples:pasted:20190730-112704.png}} なお,--models と --zoo は同じオプションになります.--zoo オプションで指定している例もよく見られますが,modelsのほうが意味がわかりやすいのでこちらを指定しています.--model はネットワーク重みファイルを指すのでそちらと混同しやすいかもしれませんが. また,classes はmodels.ymlにて指定されているはずなので,わざわざオプションで渡す必要はないはずなのですが,なぜかオプション指定しないと読み込んでくれませんでした.--classesオプションなしにするとクラス番号が表示されるのみで動きはしますがちょっと分かりづらいです. 以上,その他サンプルファイルについては, {{indexmenu>:opencv_dnn:samples#1|js}} を参照してください.これらページではコマンドラインから直接オプションを指定していますが,ここで習ったやり方でmodels.ymlを読み込むとぐっと動作確認が楽になると思います.