Yolov6 Using Vitis AI Library
Yolov6 Using Vitis AI Library
Last time we looked into different API levels provided by Vitis AI. VITIS AI API_1 are the
interfaces provided in the Vitis AI Library to use the Model Zoo models directly to
speed up the process of building ML applications.
In this post we will look into the YOLOv6 model that has been added in the Vitis AI 3.0
and basic code to test it using Vitis AI Library. Our targetwill be KV260 but same will
also work for ZCU102 and ZCU104.
If we have a custom developed hardware then we will also have to follow the Step 2
and Step 3 from the above link.
Inside the model.yaml file we will find links to model for the respective boards.
Our target is KV260 so we will choose the download link below board: zcu102 &
zcu104 & kv260. I have also referenced the download link here below:
https://www.xilinx.com/bin/public/openDownload?filename=yolov6m_pt-
zcu102_zcu104_kv260-r3.0.0.tar.gz
Now that we have downloaded the model and extracted it we will see two folders
1. yolov6m_pt_acc
2. yolov6m_pt
We will use model yolov6m_pt which contains md5sum, meta.json, prototxt and
xmodel files
Running Demo
Next, lets go to the board terminal. In the terminal using the ls command we can see
two folders Vitis-AI and dpu_sw_optimize.
Vitis-AI folder contains all the Vitis AI library examples. Change the directory using the
command:
cd examples/vai_library/samples/yolov6
Before running the Yolov6 example we will have to compile the examples. The example
comes with a build script build.sh, which is we will execute with as
sh build.sh
Before we run the demo we have to copy the .xmodel and .prototxt file into the same
folder where we have our demo code.
cp ../../../../../yolov6m_pt.xmodel ./
cp ../../../../../yolov6m_pt.prototxt ./
Note: We assume that you have used scp to transfer model files to /home/root
directory and are execute cp command from /home/root/Vitis-
AI/examples/vai_library/samples/yolov6
If you have transfered the model files to a different location, please modify the
commands accordingly.
Then we will run the provided test_jpeg_yolov6 to test the model on an image. Run the
command:
We will get the following log showing class label, coordinates and confidence level.
If we open the result file we can see a bounding box iin the detected image. In our
case it is a bear.
Code Snippets
Next lets look into the sample code(test_jpeg_yolov6) provided:
#include <glog/logging.h>
#include <iostream>
#include <memory>
#include <opencv2/core.hpp>
#include <opencv2/highgui.hpp>
#include <opencv2/imgproc.hpp>
#include <vitis/ai/yolov6.hpp>
#include <vitis/ai/demo.hpp>
#include "./process_result.hpp"
return vitis::ai::main_for_jpeg_demo(
argc, argv,
[model] {
return vitis::ai::YOLOv6::create(model);
},
process_result, 2);
Our main function for running the image test, main_for_jpeg_demo , is located inside
the demo.hpp file. However, this file is only accessible on the target board after the
installation of Vitis AI. You can find the demo.hpp file in the following directory:
/usr/include/vitis/ai/demo.hpp
if (argc <= 1) {
usage_jpeg(argv[0]);
exit(1);
if (ENV_PARAM(SAMPLES_ENABLE_BATCH)) {
std::vector<std::string> image_files;
image_files.push_back(std::string(argv[i]));
if (image_files.empty()) {
exit(1);
if (ENV_PARAM(SAMPLES_BATCH_NUM)) {
batch = batch_set;
std::vector<std::string> batch_files(batch);
std::vector<cv::Mat> images(batch);
batch_files[index] = file;
images[index] = cv::imread(file);
assert(results.size() == batch);
batch_files[i].substr(0, batch_files[i].size() - 4)
+
"_result.jpg";
cv::imwrite(out_file, image);
LOG_IF(INFO, ENV_PARAM(DEBUG_DEMO))
} else {
if (image.empty()) {
abort();
auto out_file =
image_file_name.substr(0, image_file_name.size() - 4) +
"_result.jpg";
cv::imwrite(out_file, image);
LOG_IF(INFO, ENV_PARAM(DEBUG_DEMO))
return 0;
process_result.hpp processes the result and creates the bounding box over the image.
#pragma once
#include <iomanip>
#include <iostream>
#include <opencv2/core.hpp>
#include <opencv2/highgui.hpp>
#include <opencv2/imgproc.hpp>
bool is_jpeg) {
LOG_IF(INFO, is_jpeg) << "RESULT: " << label << "\t" << std::fixed
<< "\t" << box[2] << "\t" << box[3] << "\t"
getColor(label), 1, 1, 0);
return image;
Using Vitis AI library we can rapidly prototype ML applications for our use case. If the
our required models are already in the Model Zoo or is supported by Vitis AI Library, it
can really speedup our application development using Vitis AI Library APIs.
Compiled by Abhidan([email protected])