2021年4月23日金曜日

ultra96v2+orb_slam2のaccelerationでcv::resizeを置き換えてみた話

ultra96v2+orb_slam2のaccelerationでcv::resizeをハードウェアカーネルに置き換えてみた話。 単純に置き換えるだけじゃやっぱりだめだよねっていう結果が得られた。

環境

  • ubuntu:20.04
  • vitis:2020.2
  • yocto:zeus(https://github.com/akira-nishiyama/petalinux-ros2-manifests feature-v2020.2ブランチ)
  • board:ultra96v2(https://github.com/akira-nishiyama/ultra96v2_4z.git feature-v2020.2ブランチ)

準備

前回と同じ

変更内容

とりあえずの対象はACRiの記事で示されているExtractORBとする。
記事に記載の内容を実装してみることとするが、ultra96v2向けにはリソース使用量から考えて記事に記載の回路そのままは入らないと考えられるため並列処理数は減らしていく方向で検討する。
とりあえずはcv::resizeを回路に置き換えてみて様子を見る。
このためにORBExtractor.ccとORBExtractor.hを変更した。
主にOpen CLのランタイム環境設定と、cv::resizeに対してカーネル呼び出しコードの追加を行った。
(参考:https://japan.xilinx.com/html_docs/xilinx2020_2/vitis_doc/devhostapp.html#ariaid-title2)

sw_emu

build

sw_emuでは簡単に動作を見ていく。 そのためにENABLE_KRNL_CHECKオプションを有効にする。 ENABLE_KRNL_CHECKオプションはKRNL_CHECKをdefineして必要な処理をORBExtractorに追加している。

hashは9fd1ad11b8982927366b687b527595651a82d8a9。将来変更となると思うので。

git clone https://github.com/akira-nishiyama/orb_slam_2_ros.git -b feature-fpga
cd orb_slam_2_ros
mkdir build
cd build
source <vitis-installation-path>/settings64.sh
source <sdk-installation-path>/environment-setup-aarch64-xilinx-linux
export AMENT_PREFIX_PATH=$OECORE_NATIVE_SYSROOT/usr:$OECORE_TARGET_SYSROOT/usr
export PYTHONPATH=$OECORE_NATIVE_SYSROOT/usr/lib/python3.7/site-packages:$OECORE_TARGET_SYSROOT/usr/lib/python3.7/site-packages
cmake .. -DCMAKE_INSTALL_SO_NO_EXE=0 -DBUILD_TESTING=OFF -DCMAKE_NO_SYSTEM_FROM_IMPORTED=1 -GNinja -DCMAKE_INSTALL_PREFIX=<path-to-sdcard>/usr -DPLATFORM_COMPONENTS_PATH=<path-to-platform-components> -DBUILD_TEST_TARGETS=ON -DBUILD_TARGET=sw_emu -DCMAKE_BUILD_TYPE=Debug -DENABLE_KRNL_CHECK=ON
ninja -j4

run

エミュレータを起動する。次はビルドディレクトリからのコマンド例である。

cd hw/hw_link/package_sw_emu/
source <vitis-installation-path>/settings64.sh
./launch_sw_emu.sh -pid-file emulation.pid -no-reboot -forward-port 1440 1534 -forward-port 2222 22

エミュレータが起動したら必要なファイルを転送する。
リポジトリのトップからの転送コマンドを示す。

ssh -p 2222 root@localhost "mkdir -p /usr/bin/xclbin"
scp -P 2222 build/orb_slam2_ros_mono_tum_test root@localhost:/home/root/
scp -P 2222 orb_slam2/lib/liborb_slam2_ros_core.so root@localhost:/usr/lib/liborb_slam2_ros_core.accel.so
scp -P 2222 build/hw/hw_link/orbslam2_accel_sw_emu.xclbin root@localhost:/usr/bin/xclbin/resize_krnl.xclbin

エミュレータ上でプログラムを実行する。(参考:https://japan.xilinx.com/html_docs/xilinx2020_2/vitis_doc/runemulation1.html#ariaid-title6)
オリジナルのliborb_slam2_ros_core.soはsoft実行用に保存し、新たにacceleration向けのものをリンクしておきこれを実行する。

export LD_LIBRARY_PATH=/mnt/sd-mmcblk0p1:/tmp:$LD_LIBRARY_PATH
export XCL_EMULATION_MODE=sw_emu
export XILINX_XRT=/usr
export XILINX_VITIS=/mnt/sd-mmcblk0p1
mv /usr/lib/liborb_slam2_ros_core.so /usr/lib/liborb_slam2_ros_core.soft.so
ln -s -f /usr/lib/liborb_slam2_ros_core.accel.so /usr/lib/liborb_slam2_ros_core.so
cp /mnt/sd-mmcblk0p1/emconfig.json .
./orb_slam2_ros_mono_tum_test /usr/share/orb_slam2_ros/orb_slam2/Vocabulary/ORBvoc.txt ./tum-dataset

result

一生かかっても終わる気配が無かったので途中で中断している。 opencvと同じ結果が得られているようなのでとりあえずよしとする。 (意図的にfailさせてないのでテストプログラムが正しいかは少し心配)

root@ultra96v2-zynqmp:~# ./orb_slam2_ros_mono_tum_test /usr/share/orb_slam2_ros/orb_slam2/Vocabulary/ORBvoc.txt ./tum-dataset

ORB-SLAM2 Copyright (C) 2014-2016 Raul Mur-Artal, University of Zaragoza.
This program comes with ABSOLUTELY NO WARRANTY;
This is free software, and you are welcome to redistribute it
under certain conditions. See LICENSE.txt.

OpenCV version : 3.4.3
Major version : 3
Minor version : 4
Subminor version : 3
Input sensor was set to: Monocular

Loading ORB Vocabulary.
Vocabulary loaded!


Camera Parameters: 
- fx: 517.306
- fy: 516.469
- cx: 318.643
- cy: 255.314
- k1: 0.262383
- k2: -0.953104
- k3: 0.002628
- p1: 1.16331
- p2: -0.005358
- fps: 30
- bf: 0
- color order: RGB (ignored if grayscale)
Found Platform
Platform Name: Xilinx
INFO: Device found - avnet_com_av_ULTRA96V2_1_0
Loading: '/usr/bin/xclbin/resize_krnl.xclbin'
Found Platform
Platform Name: Xilinx
INFO: Device found - avnet_com_av_ULTRA96V2_1_0
Loading: '/usr/bin/xclbin/resize_krnl.xclbin'

ORB Extractor Parameters: 
- Number of Features: 1000
- Scale Levels: 8
- Scale Factor: 1.2
- Initial Fast Threshold: 20
- Minimum Fast Threshold: 7

-------
Start processing sequence ...
Images in the sequence: 798

here! level=1
in_height:480,in_width:640
out_height:400,out_width:533
    Minimum error in intensity = 0
    Maximum error in intensity = 0
    Percentage of pixels above error threshold = 0
Check Passed 
here! level=2
in_height:400,in_width:533
out_height:333,out_width:444
    Minimum error in intensity = 0
    Maximum error in intensity = 0
    Percentage of pixels above error threshold = 0
Check Passed 
here! level=3
in_height:333,in_width:444
out_height:278,out_width:370
    Minimum error in intensity = 0
    Maximum error in intensity = 0
    Percentage of pixels above error threshold = 0
Check Passed 
here! level=4
in_height:278,in_width:370
out_height:231,out_width:309
    Minimum error in intensity = 0
    Maximum error in intensity = 0
    Percentage of pixels above error threshold = 0
Check Passed 
here! level=5
in_height:231,in_width:309
out_height:193,out_width:257
    Minimum error in intensity = 0
    Maximum error in intensity = 0
    Percentage of pixels above error threshold = 0
Check Passed 
here! level=6
in_height:193,in_width:257
out_height:161,out_width:214
    Minimum error in intensity = 0
    Maximum error in intensity = 0
    Percentage of pixels above error threshold = 0
Check Passed 
here! level=7
in_height:161,in_width:214
out_height:134,out_width:179
    Minimum error in intensity = 0
    Maximum error in intensity = 0
    Percentage of pixels above error threshold = 0
Check Passed 
here! level=1
in_height:480,in_width:640
out_height:400,out_width:533
    Minimum error in intensity = 0
    Maximum error in intensity = 0
    Percentage of pixels above error threshold = 0
Check Passed 
here! level=2
in_height:400,in_width:533
out_height:333,out_width:444
    Minimum error in intensity = 0
    Maximum error in intensity = 0
    Percentage of pixels above error threshold = 0
Check Passed 
here! level=3
in_height:333,in_width:444
out_height:278,out_width:370
^C
root@ultra96v2-zynqmp:~# 

hw

build

buildディレクトリで以下のように実行。 環境変数設定済の場合はcmake以降を実行すれば良い。

source <vitis-installation-path>/settings64.sh
source <sdk-installation-path>/environment-setup-aarch64-xilinx-linux
export AMENT_PREFIX_PATH=$OECORE_NATIVE_SYSROOT/usr:$OECORE_TARGET_SYSROOT/usr
export PYTHONPATH=$OECORE_NATIVE_SYSROOT/usr/lib/python3.7/site-packages:$OECORE_TARGET_SYSROOT/usr/lib/python3.7/site-packages
cmake .. -DCMAKE_INSTALL_SO_NO_EXE=0 -DBUILD_TESTING=OFF -DCMAKE_NO_SYSTEM_FROM_IMPORTED=1 -GNinja -DCMAKE_INSTALL_PREFIX=<path-to-sdcard>/usr -DPLATFORM_COMPONENTS_PATH=<path-to-platform-components> -DBUILD_TEST_TARGETS=ON -DBUILD_TARGET=hw -DCMAKE_BUILD_TYPE=Debug -DENABLE_KRNL_CHECK=OFF
ninja -j4

run

ビルドディレクトリから以下の通り実行してsdカードを作成する。

sudo dd if=hw/hw_link/package_hw/sd_card.img of=<path-to-sd-device>

sdをultra96v2に挿して起動させれば良い。 wifiは有効なはずなので、これに接続すれば、次のコマンドでターミナルにアクセスできる。

ssh 192.168.2.1 -l root

sdに直接書いてもいいが、何度かビルドと変更を繰り返すことになると思うので ホストで次のコマンドを実行して必要なファイルを転送する。 実行はリポジトリのトップから行っている。

ssh root@192.168.2.1 "mkdir -p /usr/bin/xclbin"
scp build/orb_slam2_ros_mono_tum_test root@192.168.2.1:/home/root/
scp orb_slam2/lib/liborb_slam2_ros_core.so root@192.168.2.1:/usr/lib/liborb_slam2_ros_core.accel.so
scp build/hw/hw_link/orbslam2_accel_sw_emu.xclbin root@192.168.2.1:/usr/bin/xclbin/resize_krnl.xclbin

ultra96v2のターミナルで以下のコマンドを入力してオリジナルの状態を確認する。

root@ultra96v2-zynqmp:~# export LD_LIBRARY_PATH=/mnt/sd-mmcblk0p1:/tmp:$LD_LIBRARY_PATH
root@ultra96v2-zynqmp:~# export XILINX_XRT=/usr
root@ultra96v2-zynqmp:~# export XILINX_VITIS=/mnt/sd-mmcblk0p1
root@ultra96v2-zynqmp:~# mv /usr/lib/liborb_slam2_ros_core.so /usr/lib/liborb_slam2_ros_core.soft.so
root@ultra96v2-zynqmp:~# ln -s -f /usr/lib/liborb_slam2_ros_core.soft.so /usr/lib/liborb_slam2_ros_core.so
root@ultra96v2-zynqmp:~# time ./orb_slam2_ros_mono_tum_test /usr/share/orb_slam2_ros/orb_slam2/Vocabulary/ORBvoc.txt ./tum-dataset

result

liborb_slam2_ros_core.soft.soを実行した場合の結果は次の通りだった。

ORBvoc.txt ./tum-dataset

ORB-SLAM2 Copyright (C) 2014-2016 Raul Mur-Artal, University of Zaragoza.
This program comes with ABSOLUTELY NO WARRANTY;
This is free software, and you are welcome to redistribute it
under certain conditions. See LICENSE.txt.

OpenCV version : 3.4.3
Major version : 3
Minor version : 4
Subminor version : 3
Input sensor was set to: Monocular

Loading ORB Vocabulary.
Vocabulary loaded!


Camera Parameters: 
- fx: 517.306
- fy: 516.469
- cx: 318.643
- cy: 255.314
- k1: 0.262383
- k2: -0.953104
- k3: 0.002628
- p1: 1.16331
- p2: -0.005358
- fps: 30
- bf: 0
- color order: RGB (ignored if grayscale)

ORB Extractor Parameters: 
- Number of Features: 1000
- Scale Levels: 8
- Scale Factor: 1.2
- Initial Fast Threshold: 20
- Minimum Fast Threshold: 7

-------
Start processing sequence ...
Images in the sequence: 798

-------

median tracking time: 0.129078
mean tracking time: 0.129074

Saving keyframe trajectory to KeyFrameTrajectory.txt ...

trajectory saved!

real    2m9.262s
user    2m19.686s
sys 0m5.226s
root@ultra96v2-zynqmp:~# cat KeyFrameTrajectory.txt 
root@ultra96v2-zynqmp:~# 

accelerationを使用した場合の結果は次の通りであった。

root@ultra96v2-zynqmp:~# ln -s -f /usr/lib/liborb_slam2_ros_core.accel.so /usr/lib/liborb_slam2_ros_core.so
root@ultra96v2-zynqmp:~# time ./orb_slam2_ros_mono_tum_test /usr/share/orb_slam2_ros/orb_slam2/Vocabulary/ORBvoc.txt ./tum-dataset

ORB-SLAM2 Copyright (C) 2014-2016 Raul Mur-Artal, University of Zaragoza.
This program comes with ABSOLUTELY NO WARRANTY;
This is free software, and you are welcome to redistribute it
under certain conditions. See LICENSE.txt.

OpenCV version : 3.4.3
Major version : 3
Minor version : 4
Subminor version : 3
Input sensor was set to: Monocular

Loading ORB Vocabulary.
Vocabulary loaded!


Camera Parameters: 
- fx: 517.306
- fy: 516.469
- cx: 318.643
- cy: 255.314
- k1: 0.262383
- k2: -0.953104
- k3: 0.002628
- p1: 1.16331
- p2: -0.005358
- fps: 30
- bf: 0
- color order: RGB (ignored if grayscale)
Found Platform
Platform Name: Xilinx
INFO: Device found - edge
Loading: '/usr/bin/xclbin/resize_krnl.xclbin'
Found Platform
Platform Name: Xilinx
INFO: Device found - edge
Loading: '/usr/bin/xclbin/resize_krnl.xclbin'

ORB Extractor Parameters: 
- Number of Features: 1000
- Scale Levels: 8
- Scale Factor: 1.2
- Initial Fast Threshold: 20
- Minimum Fast Threshold: 7

-------
Start processing sequence ...
Images in the sequence: 798

-------

median tracking time: 0.148386
mean tracking time: 0.148938

Saving keyframe trajectory to KeyFrameTrajectory.txt ...

trajectory saved!

real    2m25.290s
user    2m20.827s
sys 0m5.976s
root@ultra96v2-zynqmp:~# cat KeyFrameTrajectory.txt 
root@ultra96v2-zynqmp:~# 

0 件のコメント:

コメントを投稿