# Step 3 - Installing the Vitis-AI runtime packages

หลังจากที่เราทำการติดตั้ง Package ที่จะใช้งานในการ Compile โปรแกรม รวมถึงการใช้งานผ่าน Hardware module ที่เขียนไว้ โดยตัวโมดูลที่จะใช้งานกันบน FPGA วันนี้คือ [DPU (Deep processing unit)](https://docs.xilinx.com/r/en-US/ug1414-vitis-ai/Deep-Learning-Processor-Unit)

<div><figure><img src="/files/VVehQuBZlJYZ4HkydQOv" alt=""><figcaption><p>โครงสร้างการทำงานของ DPU</p></figcaption></figure> <figure><img src="/files/tCHnnJChDXUNgoFOj86U" alt=""><figcaption><p>การต่อ DPU ให้สามารถทำงานได้บนชิพ</p></figcaption></figure></div>

เมื่อผู้ทดลองสามารถเข้าถึงบอร์ด Ultra96v2 ได้แล้ว เราจะเริ่มทำการติดตั้ง Package และเตรียม Hardware module เพื่อใช้งานร่วมกับโปรแกรมโดยเริ่มจาก

## Step 3-1 : Setting Package for compile program

1. ไปที่ WSL ที่ ssh ไปที่ Ultra96v2 แล้วพิมคำสั่งด้านล่าง เพื่อ copy ไฟล์ที่เรียกการใช้งาน Hardware module DPU ไปเตรียมทำงาน

{% code title="WSL Terminal SSH to U96v2" %}

```bash
$ cp /media/sd-mmcblk0p1/dpu.xclbin /usr/lib/.
```

{% endcode %}

2. ทำการติดตั้งแพคเกจ Vitis-AI 1.4 runtime เพื่อใช้ในการ compile AI application ในการทำงานร่วมกับโมดูล DPU

{% code title="WSL Terminal SSH to U96v2" %}

```bash
$ cd ~/install/vitis-ai-runtime-1.4.0
$ source ./setup.sh
```

{% endcode %}

<figure><img src="/files/5Bp4RE6WwdJX4Ddam2mz" alt=""><figcaption><p>ติดตั้ง library และ runtime ที่ต้องใช้</p></figcaption></figure>

3. ทำการตรวจสอบโมดูล DPU บนชิพ FPGA ว่าพร้อมทำงานไหม

{% code title="WSL Terminal SSH to U96v2" %}

```bash
$ xdputil query
```

{% endcode %}

<figure><img src="/files/ozHRm31JzSDJlkHXCgcj" alt=""><figcaption><p>สภานะการทำงานของ DPU</p></figcaption></figure>

## Step 3-2 : Inference Vitsi-AI on U96v2

1. ทำการ optimize โมดูล DPU ให้เกิดข้อผิดพลาดในการแสดงผลลดลง&#x20;

{% code title="WSL Terminal SSH to U96v2" %}

```bash
$ cd ~/dpu_sw_optimize/zynqmp
$ source ./zynqmp_dpu_optimize.sh
```

{% endcode %}

โดยทำการ รัน script นี้จะแก้ไขปัญหาสองอย่างคือ

* แก้ไขพื้นที่ partition rootfs ให้เหมาะสมกับการใช้งาน
* แก้ปัญหา Qos ในการใช้งานหน่วยความจำ DDR เพื่อให้สามารถทำงานในการแสดงภาพได้&#x20;

<figure><img src="/files/bEZ07RDTqJPJjSZExuOI" alt=""><figcaption></figcaption></figure>

2. สำหรับกลุ่มทดลองที่มีสาย Display port ให้ทำการตั้งค่าตัวแปรในการแสดงภาพ

{% code title="WSL Terminal SSH to U96v2" %}

```bash
$ export DISPLAY=:0.0
```

{% endcode %}

โดยตัว command นี้จะเป็นการตั้งค่าตัวแปรที่ใช้ใน LinuxOS เพื่อใช้ในการแสดงผลออกที่ตัว Display Port

3. ตั้งค่า resolution ของจอแสดงผล

{% code title="WSL Terminal SSH to U96v2" %}

```bash
$ xrandr --output DP-1 --mode 800x600
```

{% endcode %}

4. ทำการปิด Debug message

{% code title="WSL Terminal SSH to U96v2" %}

```bash
$ dmesg -D
```

{% endcode %}

5. ทำการทดสอบ Vitis-AI-Library ในการทำงานของ AI application ร่วมกับ DPU&#x20;

{% code title="WSL Terminal SSH to U96v2" %}

```bash
$ cd ~/Vitis-AI/demo/Vitis-AI-Library/samples/facedetect
$ ./test_jpeg_facedetect densebox_640_360 sample_facedetect.jpg 
# ถ้ามีกล้องสามารถทดสอบกล้องได้ด้วยคำสั่ง
# $ ./test_video_facedetect densebox_320_320 0
```

{% endcode %}

## Step 3-3 : Inference Point cloud 3D detection on U96v2

แต่ในการทดลองนี้เราจะโฟกัสไปที่การใช้งาน Poinpillars เพื่อทำการทดสอบการวิเคราะห์ข้อมูลบนท้องถนนด้วย sensor Lidar ขั้นตอนต่อไปจะทดสอบเฉพาะผู้ที่กำลังต่อสายสาย DP และจออยู่เท่านั้น

1. Command WSL2 ที่จะใช้ในการโยนไฟล์ pp\_3d\_detect.tar.gz ที่ดาวน์โหลดมาในตอนแรก ไปที่บอร์ด

{% hint style="info" %}
ดูที่เก็บไฟล์ให้ดีเพื่อทำการ copy path ที่เก็บไฟล์ไว้ โดยในหน้า File exporter ให้กด Ctrl + L&#x20;
{% endhint %}

จากตัวอย่างไฟล์เก็บไว้ใน Download สิ่งที่ต้องแก้ไขคือ&#x20;

\[Name of your user] --> ชื่อของ User ที่เราใช้งานอยู่บน Windows แตกต่างกันไปตามแต่ละเครื่อง

\[IP board] --> IP ของบอร์ด Ultra96v2 ที่เราจดไว้จาก step ที่ 2

{% code title="WSL terminal" %}

```
# Secure copy file Point Pillar detection
$ scp -r /mnt/c/User/[Name of your user]/Downloads/pp_3d_detect.tar.gz root@[IP board]:~/.
# Secure shell to the Ultra96v2 board
$ ssh root@[IP board]
```

{% endcode %}

2. กลับไปที่ command ssh ของบอร์ด Ultra96v2 บนเครื่องคอมพิวเตอร์ของนิสิตแล้วทำการแตกไฟล์

{% code title="WSL Terminal SSH to U96v2" %}

```bash
# cd to the home and extract file
$ cd ~
# Exatrac tar file
$ tar -zxvf pp_3d_detect.tar.gz
# Go to Extracted directory
$ cd ~/ppdemo1216
```

{% endcode %}

3. แก้ไขไฟล์ demo.cpp เพื่อแก้ไขโปรแกรม demo.cpp เพื่อใช้งาน *Vitis-AI 1.4.* โดยการเปลี่ยน header ของโปรแกรม

{% code title="WSL Terminal SSH to U96v2" %}

```bash
# Open text editer 
$ sudo vim demo.cpp
```

{% endcode %}

4. แก้ไข Header ทำการ comment และเติม header ดัง code ด้านล่าง

{% code title="demo.cpp" %}

```cpp
//#include <iostream>
#include <fstream>
```

{% endcode %}

5. ออกจาก Vim โดยการกด Esc แล้วทำการพิมคำสั่ง

<pre class="language-bash" data-title="VIM on WSL Terminal SSH to U96v2"><code class="lang-bash"><strong># กดปุ่ม Esc แล้วพิมคำสั่งข้างล่าง
</strong><strong>:wq! 
</strong></code></pre>

6. แก้ไขไฟล์ build.sh เพื่อแก้ไขการ compile c++ และ OPENCV\_FLAGS เป็น version ใหม่

{% code title="WSL Terminal SSH to U96v2" %}

```bash
$ vim build.sh
```

{% endcode %}

7. แก้ไขการ compile ดัง code ด้านล่าง

{% code title="build.sh" %}

```bash

#g++ -std=c++11 -I. -o demo demo.cpp -lopencv_core -lopencv_video -lopencv_videoio -lopencv_imgproc -lopencv_imgcodecs -lopencv_highgui -lvitis_ai_library-pointpillars  -lvart-util -pthread -lglog

result=0 && pkg-config --list-all | grep opencv4 && result=1
if [ $result -eq 1 ]; then
OPENCV_FLAGS=$(pkg-config --cflags --libs-only-L opencv4)
else
OPENCV_FLAGS=$(pkg-config --cflags --libs-only-L opencv)
fi

g++ -std=c++17 -I. ${OPENCV_FLAGS} -o demo demo.cpp -lopencv_core -lopencv_video -lopencv_videoio -lopencv_imgproc -lopencv_imgcodecs -lopencv_highgui -lvitis_ai_library-pointpillars  -lvart-util -pthread -lglog
```

{% endcode %}

<figure><img src="/files/9fOuiDEbIazrtmnChZJ3" alt=""><figcaption></figcaption></figure>

8. ออกจาก Vim โดยการกด Esc แล้วทำการพิมคำสั่ง

<pre class="language-bash" data-title="VIM on WSL Terminal SSH to U96v2"><code class="lang-bash"><strong># กดปุ่ม Esc แล้วพิมคำสั่งข้างล่าง
</strong><strong>:wq! 
</strong></code></pre>

9. ทำการ compile code เพื่อเตรียมสำหรับการรัน

{% code title="WSL Terminal SSH to U96v2" %}

```bash
# Build the application
$ source ./build.sh
```

{% endcode %}

10. ทำการรัน Application Point pillar detection

* แสดงภาพ Object detection จาก application

{% code title="WSL Terminal SSH to U96v2" %}

```bash
./demo ./ppd/vlist.txt ./ppd/ 1
```

{% endcode %}

* แสดงภาพ Point pillar detection จาก application

{% code title="WSL Terminal SSH to U96v2" %}

```bash
./demo ./ppd/vlist.txt ./ppd/ 2
```

{% endcode %}

* แสดงทั้งสองผลลัพธ์ในหน้าจอ

{% code title="WSL Terminal SSH to U96v2" %}

```bash
./demo ./ppd/vlist.txt ./ppd/ 3
```

{% endcode %}

จบกันไปแล้วสำหรับแลปในวันนี้นะครับ คิดว่าได้ความรู้อะไรไปบ้าง ลองไปเขียนอธิบายในบันทึกผลการทดลองดูนะครับ ซึ่งในคาบหน้าเราจะมาแกะเจ้าตัวโปรแกรมและ AI ที่รันกันไปในคาบนี้เพื่อให้เพิ่มความเข้าใจและทักษะโปรแกรม Embedded programing กันมากขึ้น


---

# Agent Instructions: Querying This Documentation

If you need additional information that is not directly available in this page, you can query the documentation dynamically by asking a question.

Perform an HTTP GET request on the current page URL with the `ask` query parameter:

```
GET https://docs.aic-eec.com/fpga-design/c-c++-programming-on-ultra96v2-fpga-board/application-c-c++-on-ultra96v2-part-1/step-3-installing-the-vitis-ai-runtime-packages.md?ask=<question>
```

The question should be specific, self-contained, and written in natural language.
The response will contain a direct answer to the question and relevant excerpts and sources from the documentation.

Use this mechanism when the answer is not explicitly present in the current page, you need clarification or additional context, or you want to retrieve related documentation sections.
