私的AI研究会 > NCAppVol1

Neural Compute Application を作る

 多少ソースコードをアレンジできるようになってきたので、これまでの成果を参考に Neural Compute Application を製作してみる。

※ 最終更新:2021/10/23 

製作方針

  1. Intel® Neural Compute Stick 2 (NCS2) と Raspberri Pi4 を使用する。
  2. OpenVINO™ ツールキットのバージョンは「2021.2」
  3. IR学習済みモデルはこれまでの手順で取得した「FP16」モデルを用いる。
  4. コマンドオプションを使用して起動時に入力画像などを指定できるようにする。
  5. 日本語/英語 表示を切り替えることができる。
  6. カメラのリアルタイム画像/動画ファイル/静止画像ファイルを扱うことができる。

感情分析アプリケーション

 画像から顔を部分を特定しディープラーニングで感情推論をする。

  

動作環境

「emotion.py」

▼ 入力簡易版
▼「emotion.py」

「emotion2.py」

▲ モデル対応版
  • 実行時に利用できるコマンドオプション
    コマンドオプションデフォールト設定意味
    -h, --help-ヘルプ表示
    -i, --imagecamカメラ(cam)または動画・静止画像ファイル
    -m_dt, --m_detector必須指定IR フォーマットの顔検出モデル
    -m_re, --m_recognition必須指定IR フォーマット顔識別モデル
    -d, --device必須指定デバイス指定 (CPU/MYRIAD)
    -l, --languagejp言語 (en/jp)
    -t, --titleyタイトル表示 (y/n)
    -s, --speedyスピード計測表示 (y/n)
    -o, --outnon処理結果を出力する場合のファイルパス
    $ python3 emotion2.py -h
    
    --- Emotion Recognition 2 ---
    4.5.1-openvino
    OpenVINO inference_engine: 2.1.2021.2.0-1877-176bdf51370-releases/2021/2
    
    usage: emotion2.py [-h] [-i IMAGE_FILE] [-m_dt M_DETECTOR]
                       [-m_re M_RECOGNITION] [-d DEVICE] [-l LANGUAGE] [-t TITLE]
                       [-s SPEED] [-o IMAGE_OUT]
    
    optional arguments:
      -h, --help            show this help message and exit
      -i IMAGE_FILE, --image IMAGE_FILE
                            Absolute path to image file or cam for camera stream.
      -m_dt M_DETECTOR, --m_detector M_DETECTOR
                            Detector Path to an .xml file with a trained
                            model.Default value is
                            /home/mizutu/model/intel/FP32/face-detection-
                            adas-0001.xml
      -m_re M_RECOGNITION, --m_recognition M_RECOGNITION
                            Emotion Path to an .xml file with a trained
                            model.Default value is
                            /home/mizutu/model/intel/FP32/emotions-recognition-
                            retail-0003.xml
      -d DEVICE, --device DEVICE
                            Optional. Specify a target device to infer on. CPU,
                            GPU, FPGA, HDDL or MYRIAD is acceptable. The demo will
                            look for a suitable plugin for the device specified.
                            Default value is CPU
      -l LANGUAGE, --language LANGUAGE
                            Language.(jp/en) Default value is 'jp'
      -t TITLE, --title TITLE
                            Program title flag.(y/n) Default value is 'y'
      -s SPEED, --speed SPEED
                            Speed display flag.(y/n) Default calue is 'y'
      -o IMAGE_OUT, --out IMAGE_OUT
                            Processed image file path. Default value is 'non'
  • 実行結果
    $ python3 emotion2.py -i ~/Images/emo2.jpg
    
    --- Emotion Recognition 2 ---
    4.5.2-openvino
    OpenVINO inference_engine: 2.1.2021.3.0-2787-60059f2c755-releases/2021/3
    
    Emotion Recognition 2: Starting application...
       - Image File   :  /home/mizutu/Images/emo2.jpg
       - m_detect     :  /home/mizutu/model/intel/FP32/face-detection-adas-0001.xml
       - m_recognition:  /home/mizutu/model/intel/FP32/emotions-recognition-retail-0003.xml
       - Device       :  CPU
       - Language     :  jp
       - Input Shape1 :  data
       - Output Shape1:  detection_out
       - Input Shape2 :  data
       - Output Shape2:  prob_emotion
       - Program Title:  y
       - Speed flag   :  y
       - Processed out:  non
    
    FPS average:      19.00
    
     Finished.
  • その他の実行例
    $ python3 emotion2.py -i ~/Images/emo1.jpg
    
    --- Emotion Recognition 2 ---
    4.5.2-openvino
    OpenVINO inference_engine: 2.1.2021.3.0-2787-60059f2c755-releases/2021/3
    
    Emotion Recognition 2: Starting application...
       - Image File   :  /home/mizutu/Images/emo1.jpg
       - m_detect     :  /home/mizutu/model/intel/FP32/face-detection-adas-0001.xml
       - m_recognition:  /home/mizutu/model/intel/FP32/emotions-recognition-retail-0003.xml
       - Device       :  CPU
       - Language     :  jp
       - Input Shape1 :  data
       - Output Shape1:  detection_out
       - Input Shape2 :  data
       - Output Shape2:  prob_emotion
       - Program Title:  y
       - Speed flag   :  y
       - Processed out:  non
    
    FPS average:      22.40
    
     Finished.
  • その他のコマンド入力例
    $ cd ~/workspace/apps
    ◦ CPU
    $ python3 emotion2.py -i ~/Images/emo2.jpg
    $ python3 emotion2.py -i ~/Images/emo1.jpg
    $ python3 emotion2.py -i ~/Images/emo3.jpg
    $ python3 emotion2.py -i ~/Videos/video-test.mp4
    $ python3 emotion2.py -i cam
    ◦ NCS2(MYRIAD)
    $ python3 emotion2.py -m_dt ~/model/intel/FP16/face-detection-adas-0001.xml -m_re ~/model/intel/FP16/emotions-recognition-retail-0003.xml -i ~/Images/emo2.jpg -d MYRIAD
    $ python3 emotion2.py -m_dt ~/model/intel/FP16/face-detection-adas-0001.xml -m_re ~/model/intel/FP16/emotions-recognition-retail-0003.xml -i ~/Images/emo1.jpg -d MYRIAD
    $ python3 emotion2.py -m_dt ~/model/intel/FP16/face-detection-adas-0001.xml -m_re ~/model/intel/FP16/emotions-recognition-retail-0003.xml -i ~/Images/emo3.jpg -d MYRIAD
    $ python3 emotion2.py -m_dt ~/model/intel/FP16/face-detection-adas-0001.xml -m_re ~/model/intel/FP16/emotions-recognition-retail-0003.xml -i ~/Videos/video-test.mp4 -d MYRIAD
    $ python3 emotion2.py -m_dt ~/model/intel/FP16/face-detection-adas-0001.xml -m_re ~/model/intel/FP16/emotions-recognition-retail-0003.xml -i cam -d MYRIAD
▼「emotion2.py」
 

年齢/性別分析アプリケーション

 画像から顔を部分を特定しディープラーニングで年齢/性別を推論する。

  

動作環境

「age_gender.py」

▼ 入力簡易版
▼「age_gender.py」

「age_gender2.py」

▲ モデル対応版
  • 実行時に利用できるコマンドオプション
    コマンドオプションデフォールト設定意味
    -h, --help-ヘルプ表示
    -i, --imagecamカメラ(cam)または動画・静止画像ファイル
    -m_dt, --m_detector必須指定IR フォーマットの顔検出モデル
    -m_re, --m_recognition必須指定IR フォーマット年齢/性別分析モデル
    -d, --device必須指定デバイス指定 (CPU/MYRIAD)
    -l, --languagejp言語 (en/jp)
    -t, --titleyタイトル表示 (y/n)
    -s, --speedyスピード計測表示 (y/n)
    -o, --outnon処理結果を出力する場合のファイルパス
    $ python3 age_gender2.py -h
    
    --- Age/Gender Recognition 2 ---
    4.5.2-openvino
    OpenVINO inference_engine: 2.1.2021.3.0-2787-60059f2c755-releases/2021/3
    
    usage: age_gender2.py [-h] [-i IMAGE_FILE] [-m_dt M_DETECTOR]
                          [-m_re M_RECOGNITION] [-d DEVICE] [-l LANGUAGE]
                          [-t TITLE] [-s SPEED] [-o IMAGE_OUT]
    
    optional arguments:
      -h, --help            show this help message and exit
      -i IMAGE_FILE, --image IMAGE_FILE
                            Absolute path to image file or cam for camera stream.
      -m_dt M_DETECTOR, --m_detector M_DETECTOR
                            Detector Path to an .xml file with a trained
                            model.Default value is
                            /home/mizutu/model/intel/FP32/face-detection-
                            adas-0001.xml
      -m_re M_RECOGNITION, --m_recognition M_RECOGNITION
                            Recognition Path to an .xml file with a trained
                            model.Default value is
                            /home/mizutu/model/intel/FP32/age-gender-recognition-
                            retail-0013.xml
      -d DEVICE, --device DEVICE
                            Optional. Specify a target device to infer on. CPU,
                            GPU, FPGA, HDDL or MYRIAD is acceptable. The demo will
                            look for a suitable plugin for the device specified.
                            Default value is CPU
      -l LANGUAGE, --language LANGUAGE
                            Language.(jp/en) Default value is 'jp'
      -t TITLE, --title TITLE
                            Program title flag.(y/n) Default value is 'y'
      -s SPEED, --speed SPEED
                            Speed display flag.(y/n) Default calue is 'y'
      -o IMAGE_OUT, --out IMAGE_OUT
                            Processed image file path. Default value is 'non'
  • 実行結果
    $ python3 age_gender2.py -i ~/Images/emo2.jpg
     
    --- Age/Gender Recognition 2 ---
    4.5.2-openvino
    OpenVINO inference_engine: 2.1.2021.3.0-2787-60059f2c755-releases/2021/3
    
    Age/Gender Recognition 2: Starting application...
       - Image File   :  /home/mizutu/Images/emo2.jpg
       - m_detect     :  /home/mizutu/model/intel/FP32/face-detection-adas-0001.xml
       - m_recognition:  /home/mizutu/model/intel/FP32/age-gender-recognition-retail-0013.xml
       - Device       :  CPU
       - Language     :  jp
       - Input Shape1 :  data
       - Output Shape1:  detection_out
       - Input Shape2 :  data
       - Output Shape2:  age_conv3
       - Program Title:  y
       - Speed flag   :  y
       - Processed out:  non
    
    FPS average:       3.30
    
     Finished.
  • その他の実行例
    $ python3 age_gender2.py -i ~/Images/photo3.jpg
    
    --- Age/Gender Recognition 2 ---
    4.5.2-openvino
    OpenVINO inference_engine: 2.1.2021.3.0-2787-60059f2c755-releases/2021/3
    
    Age/Gender Recognition 2: Starting application...
       - Image File   :  /home/mizutu/Images/photo3.jpg
       - m_detect     :  /home/mizutu/model/intel/FP32/face-detection-adas-0001.xml
       - m_recognition:  /home/mizutu/model/intel/FP32/age-gender-recognition-retail-0013.xml
       - Device       :  CPU
       - Language     :  jp
       - Input Shape1 :  data
       - Output Shape1:  detection_out
       - Input Shape2 :  data
       - Output Shape2:  age_conv3
       - Program Title:  y
       - Speed flag   :  y
       - Processed out:  non
    
    FPS average:      14.80
    
     Finished.
  • その他のコマンド入力例
    $ cd ~/workspace/apps
    ◦ CPU
    $ python3 age_gender2.py -i ~/Images/emo2.jpg
    $ python3 age_gender2.py -i ~/Images/main001.jpg
    $ python3 age_gender2.py -i ~/Images/photo3.jpg
    $ python3 age_gender2.py -i ~/Videos/video-test.mp4
    $ python3 age_gender2.py -i cam
    ◦ NCS2(MYRIAD)
    $ python3 age_gender2.py -m_dt ~/model/intel/FP16/face-detection-adas-0001.xml -m_re ~/model/intel/FP16/age-gender-recognition-retail-0013.xml -i ~/Images/emo2.jpg -d MYRIAD
    $ python3 age_gender2.py -m_dt ~/model/intel/FP16/face-detection-adas-0001.xml -m_re ~/model/intel/FP16/age-gender-recognition-retail-0013.xml -i ~/Images/main001.jpg -d MYRIAD
    $ python3 age_gender2.py -m_dt ~/model/intel/FP16/face-detection-adas-0001.xml -m_re ~/model/intel/FP16/age-gender-recognition-retail-0013.xml -i ~/Images/photo3.jpg -d MYRIAD
    $ python3 age_gender2.py -m_dt ~/model/intel/FP16/face-detection-adas-0001.xml -m_re ~/model/intel/FP16/age-gender-recognition-retail-0013.xml -i ~/Videos/video-test.mp4 -d MYRIAD
    $ python3 age_gender2.py -m_dt ~/model/intel/FP16/face-detection-adas-0001.xml -m_re ~/model/intel/FP16/age-gender-recognition-retail-0013.xml -i cam -d MYRIAD
▼「age_gender2.py」
 

物体検出アプリケーション

 画像からディープラーニングで 80種類のオブジェクトを検出する。

  

動作環境

「object_detect_yolo3.py」

▼ 入力簡易版
▼「object_detect_yolo3.py」

「object_detect_yolo3_2.py」

▲ モデル対応版
  • 実行時に利用できるコマンドオプション
    コマンドオプションデフォールト設定意味
    -h, --help-ヘルプ表示
    --ir必須項目IRフォーマットの学習済みファイル
    -l, --labelscoco.names_jpラベルファイル(coco.names_jp/coco.names)
    -i, --imagecamカメラ(cam)または動画・静止画像ファイル
    -d, --device必須指定デバイス指定 (CPU/MYRIAD)
    --thresholdCENTER;0.60オブジェクトの閾値
    --iou'0.25オブジェクトの重なりレベル
    -t, --titleyタイトル表示 (y/n)
    -s, --speedyスピード計測表示 (y/n)
    -o, --outnon処理結果を出力する場合のファイルパス
    $ python3 object_detect_yolo3_2.py -h
    
    --- TinyYOLO V3 Object detection ---
    4.5.2-openvino
    OpenVINO inference_engine: 2.1.2021.3.0-2787-60059f2c755-releases/2021/3
    
    usage: object_detect_yolo3_2.py [-h] [--ir IR_File] [-lb LABEL_FILE]
                                    [-i IMAGE_FILE or cam] [-d DEVICE]
                                    [--threshold FLOAT] [--iou FLOAT] [-t TITLE]
                                    [-s SPEED] [-o IMAGE_OUT]
    
    optional arguments:
      -h, --help            show this help message and exit
      --ir IR_File          Absolute path to the neural network IR xml
                            file.Default value is
                            /home/mizutu/model/public/FP32/yolo-v3-tiny-tf.xml
      -lb LABEL_FILE, --labels LABEL_FILE
                            Absolute path to labels file.Default value is
                            coco.names_jp
      -i IMAGE_FILE or cam, --input IMAGE_FILE or cam
                            Absolute path to image file or cam for camera stream.
      -d DEVICE, --device DEVICE
                            Optional. Specify a target device to infer on. CPU,
                            GPU, FPGA, HDDL or MYRIAD is acceptable. The demo will
                            look for a suitable plugin for the device specified.
                            Default value is CPU
      --threshold FLOAT     Threshold for detection.
      --iou FLOAT           Intersection Over Union.
      -t TITLE, --title TITLE
                            Program title flag.(y/n) Default value is 'y'
      -s SPEED, --speed SPEED
                            Speed display flag.(y/n) Default calue is 'y'
      -o IMAGE_OUT, --out IMAGE_OUT
                            Processed image file path. Default value is 'non'
  • 実行結果
    $ python3 object_detect_yolo3_2.py -i ~/Videos/car.mp4
    
    --- TinyYOLO V3 Object detection ---
    4.5.2-openvino
    OpenVINO inference_engine: 2.1.2021.3.0-2787-60059f2c755-releases/2021/3
    
    Running OpenVINO NCS Tensorflow TinyYolo v3 example...
    
     Displaying image with objects detected in GUI...
     Click in the GUI window and hit any key to exit.
    object_detect_yolo3_2.py:289: DeprecationWarning: 'inputs' property of IENetwork class is deprecated. To access DataPtrs user need to use 'input_data' property of InputInfoPtr objects which can be accessed by 'input_info' property.
      input_blob = next(iter(net.inputs))
    Tiny Yolo v3: Starting application...
       - IR File      :  /home/mizutu/model/public/FP32/yolo-v3-tiny-tf.xml
       - Input Shape  :  [1, 3, 416, 416]
       - Output Shapes: 
          - output #0 name: conv2d_12/Conv2D/YoloRegion
             - output shape: [1, 255, 26, 26]
          - output #1 name: conv2d_9/Conv2D/YoloRegion
             - output shape: [1, 255, 13, 13]
       - Labels File  :  coco.names_jp
       - Image File   :  /home/mizutu/Videos/car.mp4
       - Threshold    :  0.6
       - Intersection Over Union:    0.25
       - Device       :  CPU
       - Program Title:  y
       - Speed flag   :  y
       - Processed out:  non
    object_detect_yolo3_2.py:367: DeprecationWarning: 'outputs' property of InferRequest is deprecated. Please instead use 'output_blobs' property.
      all_output_results = req_handle.outputs
    
    FPS average:      12.10
    
     Finished.
  • その他の実行例
    $ python3 object_detect_yolo3_2.py -i ~/Videos/car_person.mp4
    
    --- TinyYOLO V3 Object detection ---
    4.5.2-openvino
    OpenVINO inference_engine: 2.1.2021.3.0-2787-60059f2c755-releases/2021/3
    
    Running OpenVINO NCS Tensorflow TinyYolo v3 example...
    
     Displaying image with objects detected in GUI...
     Click in the GUI window and hit any key to exit.
    object_detect_yolo3_2.py:289: DeprecationWarning: 'inputs' property of IENetwork class is deprecated. To access DataPtrs user need to use 'input_data' property of InputInfoPtr objects which can be accessed by 'input_info' property.
      input_blob = next(iter(net.inputs))
    Tiny Yolo v3: Starting application...
       - IR File      :  /home/mizutu/model/public/FP32/yolo-v3-tiny-tf.xml
       - Input Shape  :  [1, 3, 416, 416]
       - Output Shapes: 
          - output #0 name: conv2d_12/Conv2D/YoloRegion
             - output shape: [1, 255, 26, 26]
          - output #1 name: conv2d_9/Conv2D/YoloRegion
             - output shape: [1, 255, 13, 13]
       - Labels File  :  coco.names_jp
       - Image File   :  /home/mizutu/Videos/car_person.mp4
       - Threshold    :  0.6
       - Intersection Over Union:    0.25
       - Device       :  CPU
       - Program Title:  y
       - Speed flag   :  y
       - Processed out:  non
    object_detect_yolo3_2.py:367: DeprecationWarning: 'outputs' property of InferRequest is deprecated. Please instead use 'output_blobs' property.
      all_output_results = req_handle.outputs
    
    FPS average:      12.50
    
     Finished.
  • その他のコマンド入力例
     $ cd ~/workspace/apps
    ◦ CPU
    $ python3 object_detect_yolo3_2.py -i ~/Videos/car.mp4
    $ python3 object_detect_yolo3_2.py -i ~/Videos/car_person.mp4
    $ python3 object_detect_yolo3_2.py -i ~/Images/desk-image.jpg
    $ python3 object_detect_yolo3_2.py -i cam
    ◦ NCS2(MYRIAD)
    $ python3 object_detect_yolo3_2.py --ir ~/model/public/FP16/yolo-v3-tiny-tf.xml -i ~/Videos/car.mp4 -d MYRIAD
    $ python3 object_detect_yolo3_2.py --ir ~/model/public/FP16/yolo-v3-tiny-tf.xml -i ~/Videos/car_person.mp4 -d MYRIAD
    $ python3 object_detect_yolo3_2.py --ir ~/model/public/FP16/yolo-v3-tiny-tf.xml -i ~/Images/desk-image.jpg -d MYRIAD
    $ python3 object_detect_yolo3_2.py --ir ~/model/public/FP16/yolo-v3-tiny-tf.xml -i cam -d MYRIAD
▼「object_detect_yolo3_2.py」

色パターン統一版

▼「object_detect_yolo3_3.py」
 

更新履歴

 

参考資料

 

Last-modified: 2021-10-23 (土) 16:37:28