/cuKCF-tracking-sys

A high speed tracking system based on KCF, DSST, optimized with CUDA. BUPT 2024 spring CV.

Primary LanguagePython

目标跟踪系统

小组成员仅按姓氏首字母进行排序:

  • 董亚竺 2021213628
  • 黄敬元 2021213640
  • 周睿杰 2021213611

Introduction

目标跟踪系统是一个基于Kernelized Correlation Filters以及相关衍生算法的、实时跟踪目标的综合推理系统。我们将这一份课题解读为涵盖:

  • 提供追踪制定目标的功能
  • 提供推理系统UI界面
  • 内部的推理优化(Inference Speed optimizing, http requests processing)

Roadmap

  • 初步实现MKCFup算法

  • 了解推理原理,做优化:

    • 重构代码,这个代码根本无法应用在工业上,必须重构为一份可移植的code
    • 可能的算法优化 - 能否使用CUDA、SIMD指令集
    • 推理方面优化
  • 重构一版Python版本的算法

  • 前后端的软件工程问题:有一个图像标注的过程转化

Quick Start

强烈建议使用linux环境,如果使用windows环境,遇到CMake编译问题请自行调整^ ^

1. 安装Python相关依赖库

首先是安装CMake与Libtorch(有可能会用到)

sudo apt install cmake

至于libtorch的下载方法,您可以查阅:PyTorch官方文档

完成了相关库的下载之后,您可以使用如下指令来安装代码依赖:

pip install numba	# 主要使用numba来进行加速
pip install fastapi[all]	# 推理部署

2. (可选)编译运行我们制作的CUDA推理

git clone https://github.com/SamuraiBUPT/KCF-tracking-sys.git
cd KCF-tracking-sys
pip install -e .

这会自动开始编译C++文件。

需要注意的是,编译需要的环境是:

  • Linux (Our: Ubuntu 20.04)
  • CMake
  • libtorch

3. 启动推理

然后您可以直接使用我们提供的基于fastapi的server进行测试:

cd server
python3 server.py

localhost:8000您可以进行交互与查看部署结果。

4. 检查我们的实验结果

您需要先下载数据集,我们已经上传至百度云

然后解压到KCF/benchmark_dataset路径下。您的项目结构应该看起来像这样:

image-20240520155941149

可以开始运行实验代码:

cd KCF
python3 eval_inference.py

Optimize

1. Analysis

要使用cuda优化,不能盲目着手,要找出来到底什么地方是整个算法的bottleneck,并且使用cuda计算还要考虑memcpy的时间,如果时间瓶颈都比较小的话,那就没必要进行cuda计算。(因为无法trade off)

  • 先找是否是精度问题
  • 再考虑profile bottleneck
  • 再考虑cuda优化

1.1 精度问题

我认为计算精确度的问题,应该是精度的问题,不是其他的。

如果尝试使用更高的精度,也许效果会更好。

结论:并没有更好

1.2 bottleneck分析

image-20240519001146457

初步profile结果:

  • NormalizeAndTruncate里面调用了func3. - rank No.3
  • getFeatureMaps调用了:func1, func2 - rank No.4, No.7
  • PCAFeatureMaps调用了:func4 - rank No.10
  • 还有一个就是gaussianCorrelation - rank No.5
    • 包括mulSpectrums
  • FFTD和dft其实是一伙的。(fftd函数内部调用的就是cv2.dft) - rank No.2

实验结果

Precision

image-20240529180603736

The kcf_v5_cuda reached the highest level of precision. [0.732]

Reason

image-20240529180930812

This is a log file when I was developing. The cuda version has higher float precision than python implementation. (Language reason, not algorithm reason.)

Inference Speed

image-20240529180752610

Unluckily, the cuda optimized version has low performance in inferencing.

Reason

image-20240529181045329

We miscalculated cuda usage and did not balance IO losses against computing benefits. Each time the data was copied to GPU and copied back. Plenty of time was wasted in IO, not computing.

Conclusion

We have achieved the goal:

  • Implement the KCF by ourself
  • Reach the level where the original paper was.
  • Explore engineering experiences, not just algorithm.

It's a little bit disappointed when we see the result. But it is worth the price.

Reference

Papers

With respect, we have read multiple awesome papers related to KCF algorithm, listed as follows:

Github Code

Blogs