dbface-on-openvino icon indicating copy to clipboard operation
dbface-on-openvino copied to clipboard

Describes how to run DBFace, a real-time, single-shot face detection model on Intel OpenVINO

Overview

The objective of this project is to run the DBFace, a real-time, single-stage face detector on Intel(r) Distribution of OpenVINO(tm) Toolkit.
Original DBFace GitHub site: https://github.com/dlunion/DBFace

Thie original developer used PyTorch to train the model. You need to convert the PyTorch model to OpenVINO IR model. You need to run 2 conversion steps to get the final IR model.

  PyTorch (.pth) -> ONNX (.onnx) -> IR (.xml, .bin)

このプロジェクトの目的はリアルタイムでシングルステージの顔検出モデル、DBFaceをIntel(r) Distribution of OpenVINO(tm) Toolkit上で実行できるようにすることです。
Original DBFace GitHub site: https://github.com/dlunion/DBFace

元の開発者はPyTorchを使ってモデルの学習を行っています。そのためPyTorchモデルからOpenVINO IRモデルへの変換を行わなくてはなりません。 IRモデルを得るためには2つの変換ステップを要します。

  PyTorch (.pth) -> ONNX (.onnx) -> IR (.xml, .bin)

Detection result with a 4VGA (1280x960) input picture output

1. Prerequisites

pip3 -r ${INTEL_OPENVINO_DIR}/deployment_tools/tools/model_downloader/requirements.in
pip3 -r ${INTEL_OPENVINO_DIR}/deployment_tools/tools/model_downloader/requirements-pytorch.in

If you fail to install PyTorch, go to PyTorch official web site and follow the QUICK START LOCALLY guide to install it. You need to have PyTorch >=1.4.

2. Download DBFace PyTorch model and weight

Download DBFace.py and dbface.pth from original developer's GitHub page.

3. Convert PyTorch model into ONNX model

Use pytorch_to_onnx.py in the model_downloader directory.

python3 ${INTEL_OPENVINO_DIR}/deployment_tools/tools/model_downloader/pytorch_to_onnx.py \
 --model-name DBFace \
 --weights dbface.pth \
 --import-module DBFace \
 --input-shape 1,3,320,320 \
 --output-file dbface.onnx \
 --input-names x \
 --output-names sigmoid_hm,tlrb,landmark

Hint: You can get the input and output node names from the model source code (in this case, DBFace.py in the model directory)

    def forward(self, x):
        out = self.hs1(self.bn1(self.conv1(x)))
          :
        return sigmoid_hm, tlrb, landmark

4. Convert ONNX model into OpenVINO IR model

Use Model Optimizer (MO) to convert the ONNX model into IR model.

python3 ${INTEL_OPENVINO_DIR}/deployment_tools/model_optimizer/mo.py \
 --input_model dbface.onnx \
 --mean_values [180,154,150] \
 --scale_values [73.7,70.0,70.9] \
 --input_shape [1,3,960,1280] \
 --output_dir dbface-4vga \
 --data_type FP16

Hint: You can change the input shape with --input_shape option.
Hint: You can find the appropriate parameters (--mean_values and --scale_values) for input data preprocessing from the original source code (in this case, line 36-40 in main.py from the origianl GitHub site)

    mean = [0.408, 0.447, 0.47]
    std = [0.289, 0.274, 0.278]

    image = common.pad(image)
    image = ((image / 255.0 - mean) / std).astype(np.float32)

5. Run sample program

python3 dbface-infer.py -m model.xml -i input_image

A webCam #0 will be used when you specify 'cam' as the input file name. output.jpg will be ganarated in the current directry. (non-webCam version only)

Command line example:

 $ python3 dbface-infer.py -m dbface-4vga/dbface.xml -i image.jpg
 $ python3 dbface-infer.py -m dbface-vga/dbface.xml -i cam

6. Test Environment

  • Ubuntu 18.04 / Windows 10 1909
  • OpenVINO 2020.3 LTS

See Also