Hailo Model Zoo : hailomz API를 이용한 커스텀 U-Net 모델을 Hailo에서 동작가능한 바이너리파일(.hef)로 컴파일 및 테스트

실행환경

Build PC

  • CPU: Intel i7-1355U
  • RAM: 16G
  • OS: Ubuntu-22.04 LTS
  • Target Hailo System: Hailo8

Hailo Version

  • Hailo DFC: 3.27.0
  • Hailo Model Zoo: 2.11.0

사전설치

Hailo Dataflow Compiler

  • 헤일로에서 실행가능한 바이너리파일(.hef) 컴파일용 SDK
  • Parse - Optimize - Compile 단계로 구성
  • Log In • Hailo 다음 경로에서 개발자 로그인 후 Dataflow Compiler 설치
  • Dataflow Compiler v3.27.0 Install guid 를 따라 가상환경 생성 후 라이브러리 설치

Hailo Model Zoo

Hailo Ai Suite 를 설치하여 위 두 라이브러리 설치 가능


구조 및 컴파일

+full

준비물

커스텀 빌드를 위한 ONNX 파일 준비

커스텀 빌드를 위한 스크립트 파일 준비

커스텀 모델 학습에 사용된 데이터셋 준비

  • hailo model zoo 에서는 .tfrecord 파일을 지원하므로, 해당 포맷의 데이터셋 필요
  • carvana_dataset.tfrecord : Access via Synology

custom_unet.yaml

#### base : 지정된 base yaml 파일에 의존하며 하위계층 순으로 변수값을 오버라이딩함
base:
- base/custom_unet_carvana.yaml
evaluation:
  labels_offset: 1
network:
  network_name: custom_unet_256
 
paths:
  # 최적화 과정에서 실행하는 함수를 작성한 alls 스크립트
  alls_script: custom_unet_mobilenet_v2.alls
 
  # 변환할 모델 path, 자동으로 다운로드 받기위함. custom model 컴파일에는 불필요
  #network_path:--
  #- models_files/Segmentation/Oxford_Pet/unet_mobilenet_v2/pretrained/2022-02-03/unet/saved_model.pb
  #- models_files/Segmentation/Oxford_Pet/unet_mobilenet_v2/pretrained/2022-02-03/unet/variables/variables.index
  #- models_files/Segmentation/Oxford_Pet/unet_mobilenet_v2/pretrained/2022-02-03/unet/variables/variables.data-00000-of-00001
  # url: https://hailo-model-zoo.s3.eu-west-2.amazonaws.com/Segmentation/Oxford_Pet/unet_mobilenet_v2/pretrained/2022-02-03/unet_mobilenet_v2.zip
 
# 입력 데이터의 normalization 정보
parser:
  normalization_params:
    normalize_in_net: true
    std_list:
    - 255.0
    - 255.0
    - 255.0
    mean_list:
    - 0.0
    - 0.0
    - 0.0
 
# 전처리 및 후처리 관련
postprocessing:
  device_pre_post_layers:
    max_finder: false
    bilinear: true
    argmax: false
    softmax: false
preprocessing:
  network_type: segmentation
  meta_arch: fcn_resnet
 
# 모델의 메타데이터
info: ~~

custom_unet_carvana.yaml

base:
- base/base.yaml
# hailomz eval - 평가에서 사용되는 데이터셋
evaluation:
  labels_offset: 0
  classes: 1
  dataset_name: oxford_pet
  data_set: models_files/my_dataset/carvana_calib.tfrecord
  validation_data_set: models_files/my_dataset/carvana_calib.tfrecord
  
# hailomz optimize - 캘리브레이션(양자화) 과정에서 사용되는 데이터셋
quantization:
  calib_set:
  - models_files/my_dataset/carvana_calib.tfrecord

custom_unet_mobilenet_v2.alls

# 노멀라이즈 옵션 - 네트워크의 첫 레이어에 노멀라이즈 과정을 추가함
normalization1 = normalization([0.0, 0.0, 0.0], [255.0, 255.0, 255.0])
# optimization 설정
model_optimization_config(calibration, batch_size=8, calibset_size=2048)
post_quantization_optimization(finetune, policy=enabled, dataset_size=4096, epochs=4, learning_rate=0.0001)

Parse

  • 헤일로에서 이해하는 구문파일(.har)로 변환하는 과정
  • CKPTONNX 포맷을 지원하며 hailomz parse 명령어를 통해 실행함

hailomz parse

$ hailomz parse --help
 
positional arguments:
  model_name            Which network to run
  // Hailo Model Zoo에 사전 정의된 모델을 입력(자동 컴파일)
 
options:
  -h, --help            show this help message and exit
  --yaml YAML_PATH      Path to YAML for network configuration.By default
                        using the default configuration
  --ckpt CKPT_PATH      Path to onnx or ckpt to use for parsing. By
                        default using the model cache location
  --hw-arch             Which hw arch to run: hailo8 / hailo8l/ hailo15h/
                        hailo15m. By default using hailo8.
  --start-node-names START_NODE_NAMES [START_NODE_NAMES ...]
                        List of names of the first nodes to parse.
                        Example: --start-node-names <start_name1>
                        <start_name2> ...
  --end-node-names END_NODE_NAMES [END_NODE_NAMES ...]
                        List of nodes that indicate the parsing end. The
                        order determines the order of the outputs.
                        Example: --end-node-names <end_name1> <end_name2> ...

custom .onnx parsing

$ hailomz parse --ckpt ./unet_13_256.onnx --yaml custom_unet.yaml
  • hailomz parse 는 커스텀 모델의 아키텍처에서 start/end node를 자동으로 찾음
  • 찾지 못하는 경우에는 custom_unet.yaml 스크립트에 직접 작성해야함

Results

  • custom_unet_256.har 파일 생성됨


Optimize

  • Parse 과정을 통해 변환된 .har 파일을 파라미터 양자화 및 입출력 데이터처리 등의 최적화를 통해 헤일로 시스템에서 높은 성능을 보장하도록하는 과정
  • 최적화를 위해 기존 모델의 훈련 시 사용한 데이터셋이 필요
  • 양자화 학습 및 입출력 데이터처리를 위한 함수는 .alls 파일에 작성됨
  • GPU 환경을 권장함

hailomz optimize

$ hailomz optimize --help
 
positional arguments:
  model_name            Which network to run
  // Hailo Model Zoo에 사전 정의된 모델을 입력(자동 컴파일)
 
options:
  -h, --help            show this help message and exit
  --yaml YAML_PATH      Path to YAML for network configuration.
  --ckpt CKPT_PATH      Path to onnx or ckpt to use for parsing.
  --hw-arch             hailo8 / hailo8l/ hailo15h/ hailo15m. By default using hailo8.
  --start-node-names START_NODE_NAMES [START_NODE_NAMES ...]
                        List of names of the first nodes to parse. Example: 
                        --start-node-names <start_name1> <start_name2>...
  --end-node-names END_NODE_NAMES [END_NODE_NAMES ...]
                        List of nodes that indicate the parsing end. The order determines the order of the outputs.
                        Example: --end-node-names <end_name1> <end_name2> ...
  --har HAR_PATH        Use external har file
  --calib-path CALIB_PATH
                        Path to external tfrecord for calibration or a directory containing images in jpg or png format
  --model-script MODEL_SCRIPT_PATH
                        Path to model script to use. By default using the model script specified
                        in the network YAML configuration
  --performance         Enable flag for benchmark performance
  --resize RESIZE [RESIZE ...]
                        Add input resize from given [h,w]
  --input-conversion {nv12_to_rgb,yuy2_to_rgb,rgbx_to_rgb}
                        Add input conversion from given type
  --classes             Number of classes for NMS configuration

custom .har optimize

hailomz optimize --har custom_unet_256.har --yaml custom_unet.yaml

Results

  • 최적화 과정을 마친 .har 파일이 생성됨

+full


++Optional

모델 구조 확인하기

  • hailo profiler 를 이용해 optimize 과정을 마친 .har 파일의 모델 구조를 확인할 수 있음
hailo profiler custom_unet_256.har
  • 생성되는 html 파일을 열어 다음과 같이 확인 가능

+full


Compile

  • Optimize 완료된 .har 파일을 헤일로에서 실행가능한 바이너리파일인 .hef 파일로 컴파일하는 과정
  • ONNX 파일을 입력으로, 전 과정을 한꺼번에 실행도 가능

har file compile

hailomz compile --har custom_unet.har --yaml custom_unet.yaml

Results

+full


Execute


참고


EmbeddedHailoDeepLearning