GithubHelp home page GithubHelp logo

ncdhz / yolo-libtorch Goto Github PK

View Code? Open in Web Editor NEW
184.0 5.0 56.0 36.93 MB

一个 LibTorch 版本的 Yolo 封装库

Home Page: https://ncdhz.github.io/Yolo-LibTorch

C++ 97.02% CMake 2.98%
opencv libtorch yolov5 object-detection cmake cpp yolov6 yolov7 yolov8

yolo-libtorch's Introduction

文档

Yolo-LibTorch旨在使用LibTorch轻松调用Yolo的各种版本YoloV5YoloV6YoloV7YoloV8等(以下结果通过yolov8n模型预测)。此项目需要torchscript的模型格式

快速开始

Windows 中使用(cmake)

  1. 下载所需环境

  2. 配置环境变量

    # 添加libtorch的lib目录到path
    \path\to\libtorch\lib
    # 添加opencv的bin目录到环境变量
    # vc14 vc15 vc16 都可以
    \path\to\opencv\build\x64\vc16\bin
  3. 修改根目录下 CMakeLists.txt 文件

    # Torch_DIR 路径为 libtorch 下面的 share/cmake/Torch/
    set(Torch_DIR /path/to/libtorch/share/cmake/Torch/)
    # OpenCV_DIR 路径为 opencv/build/
    set(OpenCV_DIR /path/to/opencv/build/)
  4. 编译

    mkdir build
    cd build
    cmake ..
    
    # 根据你 LibTorch 版本选择执行下面语句
    # Debug
    cmake --build . --config Debug
    cmake --install . --config Debug
    
    # Release
    cmake --build . --config Release
    cmake --install . --config Release
  5. 测试

    cd bin
    # 默认会打开电脑摄像头捕捉画面
    # 如果电脑没有摄像头可以通过参数引入视频或图片
    test.exe

yolo-libtorch's People

Contributors

ncdhz avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar  avatar

yolo-libtorch's Issues

讀取pt時報錯

您好,麻煩一下
利用cpu運行時沒問題
使用gpu運行至讀取pt檔的時候發生以下報錯(有使用gpu進行export torchscript檔案)

0x00007FFA55ECCF19 中 (於 Libtorch_yolov5_DLLtest.exe) 發生未處理的例外狀況: Microsoft C++ 發生例外狀況: c10::NotImplementedError (記憶體位置 0x000000C0766F9530)。

此報錯發生在OperatorEntry.h底下的
const KernelFunction& lookup(DispatchKey k) const {
const auto& kernel = dispatchTable_[static_cast<uint8_t>(k)];
// A valid kernel always has a boxed kernel and may have an
// unboxed kernel. However, we typically do unboxed calls in at::
// APIs, where the kernel 1) will very likely be valid and 2)
// should have an unboxed kernel. Checking the unboxed kernel
// first will allow us to avoid touching the boxed kernel at all
// in the common case.
if (C10_UNLIKELY(!kernel.isValidUnboxed())) {
if (!kernel.isValid()) {
reportError(k);
}
}
return kernel;
}

想請問這是甚麼問題,謝謝

Ubuntu20.04无法应用生成的libyolov5.so库

编译环境
Ubuntu 20.04
gcc-5
cmake 3.21.3
cuda:10.2
cudnn:8.2.1
pytorch:libtorch-shared-with-deps-1.9.1+cu102
opencv:4.5.3

-- Found CUDA: /usr/local/cuda (found suitable exact version "10.2")
-- Found CUDA: /usr/local/cuda (found version "10.2")
-- Caffe2: CUDA detected: 10.2
-- Caffe2: CUDA nvcc is: /usr/local/cuda/bin/nvcc
-- Caffe2: CUDA toolkit directory: /usr/local/cuda
-- Caffe2: Header version is: 10.2
-- Found cuDNN: v8.2.1 (include: /usr/local/cuda/include, library: /usr/local/cuda/lib64/libcudnn.so)
-- /usr/local/cuda/lib64/libnvrtc.so shorthash is 08c4863f
-- Autodetected CUDA architecture(s): 5.0
-- Added CUDA NVCC flags for: -gencode;arch=compute_50,code=sm_50
-- Found CUDA: /usr/local/cuda (found suitable exact version "10.2")
-- Found CUDA: /usr/local/cuda (found version "10.2")
-- Caffe2: CUDA detected: 10.2
-- Caffe2: CUDA nvcc is: /usr/local/cuda/bin/nvcc
-- Caffe2: CUDA toolkit directory: /usr/local/cuda
-- Caffe2: Header version is: 10.2
-- Found cuDNN: v8.2.1 (include: /usr/local/cuda/include, library: /usr/local/cuda/lib64/libcudnn.so)
-- /usr/local/cuda/lib64/libnvrtc.so shorthash is 08c4863f
-- Autodetected CUDA architecture(s): 5.0
-- Added CUDA NVCC flags for: -gencode;arch=compute_50,code=sm_50
-- Configuring done
-- Generating done
-- Build files have been written to: /home/du/SoftWave/Project/YoloV5-LibTorch/cmake-build-debug

生成test的过程中,libyolov5.so正常生成但连接到test时导致opencv库引用失败。

/home/du/SoftWave/clion-2021.2.2/bin/cmake/linux/bin/cmake --build /home/du/SoftWave/Project/YoloV5-LibTorch/cmake-build-debug --target test -- -j 3
Scanning dependencies of target YoloV5
[ 25%] Building CXX object src/CMakeFiles/YoloV5.dir/YoloV5.cpp.o
/home/du/SoftWave/Project/YoloV5-LibTorch/src/YoloV5.cpp: In member function ‘cv::Mat YoloV5::drawRectangle(cv::Mat, at::Tensor, std::map<int, cv::Scalar_ >, std::map<int, std::basic_string >, int)’:
/home/du/SoftWave/Project/YoloV5-LibTorch/src/YoloV5.cpp:321:22: warning: passing NULL to non-pointer argument 1 of ‘cv::Scalar_<Tp>::Scalar(_Tp) [with _Tp = double]’ [-Wconversion-null]
cv::Scalar color = NULL;
^
[ 50%] Linking CXX shared library libYoloV5.so
[ 50%] Built target YoloV5
Scanning dependencies of target test
[ 75%] Linking CXX executable test
/usr/bin/ld: CMakeFiles/test.dir/test.cpp.o: in function main': /home/du/SoftWave/Project/YoloV5-LibTorch/test/test.cpp:49: undefined reference to cv::imshow(std::string const&, cv::_InputArray const&)'
/usr/bin/ld: ../src/libYoloV5.so: undefined reference to cv::putText(cv::_InputOutputArray const&, std::string const&, cv::Point_<int>, int, double, cv::Scalar_<double>, int, int, bool)' /usr/bin/ld: ../src/libYoloV5.so: undefined reference to cv::imread(std::string const&, int)'
collect2: error: ld returned 1 exit status
make[3]: *** [test/CMakeFiles/test.dir/build.make:238:test/test] 错误 1
make[2]: *** [CMakeFiles/Makefile2:142:test/CMakeFiles/test.dir/all] 错误 2
make[1]: *** [CMakeFiles/Makefile2:149:test/CMakeFiles/test.dir/rule] 错误 2
make: *** [Makefile:182:test] 错误 2

为什么会有这些报错信息

C:\devResources\gitproject\Yolo-LibTorch-main\Yolo-LibTorch-main\src\Yolo.cpp(11,8): error C2039: "device": 不是 "Yolo" 的
成员 [C:\devResources\gitproject\Yolo-LibTorch-main\Yolo-LibTorch-main\build\src\Yolo.vcxproj]
C:\devResources\gitproject\Yolo-LibTorch-main\Yolo-LibTorch-main\include\Yolo.h(53,7):
参见“Yolo”的声明

C:\devResources\gitproject\Yolo-LibTorch-main\Yolo-LibTorch-main\src\Yolo.cpp(15,8): error C2039: "confThres": 不是 "Yolo
" 的成员 [C:\devResources\gitproject\Yolo-LibTorch-main\Yolo-LibTorch-main\build\src\Yolo.vcxproj]
C:\devResources\gitproject\Yolo-LibTorch-main\Yolo-LibTorch-main\include\Yolo.h(53,7):
参见“Yolo”的声明

C:\devResources\gitproject\Yolo-LibTorch-main\Yolo-LibTorch-main\src\Yolo.cpp(16,8): error C2039: "isHalf": 不是 "Yolo" 的
成员 [C:\devResources\gitproject\Yolo-LibTorch-main\Yolo-LibTorch-main\build\src\Yolo.vcxproj]
C:\devResources\gitproject\Yolo-LibTorch-main\Yolo-LibTorch-main\include\Yolo.h(53,7):
参见“Yolo”的声明

C:\devResources\gitproject\Yolo-LibTorch-main\Yolo-LibTorch-main\src\Yolo.cpp(23,34): error C2039: "non_max_suppression
": 不是 "Yolo" 的成员 [C:\devResources\gitproject\Yolo-LibTorch-main\Yolo-LibTorch-main\build\src\Yolo.vcxproj]
C:\devResources\gitproject\Yolo-LibTorch-main\Yolo-LibTorch-main\include\Yolo.h(53,7):
参见“Yolo”的声明

C:\devResources\gitproject\Yolo-LibTorch-main\Yolo-LibTorch-main\src\Yolo.cpp(40,23): error C3861: “xywh2xyxy”: 找不到标识符
[C:\devResources\gitproject\Yolo-LibTorch-main\Yolo-LibTorch-main\build\src\Yolo.vcxproj]
C:\devResources\gitproject\Yolo-LibTorch-main\Yolo-LibTorch-main\src\Yolo.cpp(55,22): error C3861: “nms”: 找不到标识符 [C:\de
vResources\gitproject\Yolo-LibTorch-main\Yolo-LibTorch-main\build\src\Yolo.vcxproj]
C:\devResources\gitproject\Yolo-LibTorch-main\Yolo-LibTorch-main\src\Yolo.cpp(61,18): error C2039: "getRandScalar": 不是

"Yolo" 的成员 [C:\devResources\gitproject\Yolo-LibTorch-main\Yolo-LibTorch-main\build\src\Yolo.vcxproj]
C:\devResources\gitproject\Yolo-LibTorch-main\Yolo-LibTorch-main\include\Yolo.h(53,7):
参见“Yolo”的声明

关于非极大值抑制函数的一个问题

请问
std::vectortorch::Tensor YoloV5::non_max_suppression(torch::Tensor prediction, float confThres, float iouThres)
函数里面,torch::Tensor c = x.slice(1, 5, 6) * maxWh;执行的作用是什么?

你好,为啥报错c10:error


inline c10::intrusive_ptr<ivalue::Tuple> IValue::toTuple() && { AT_ASSERT(isTuple(), "Expected Tuple but got ", tagKind()); return moveToIntrusivePtr<ivalue::Tuple>();
报错:0x00007FFA5F0FCD29 处(位于 kuka_socket_new_1.exe 中)有未经处理的异常: Microsoft C++ 异常: c10::Error,位于内存位置 0x000000A1EE6FF160 处。

请问batch size的问题,以及如何才能支持GPU半精度模型?[问题请教]

请忽略开始的问题,发现自己犯了个错,YOLO export的时候用错了一些设置。
我希望检测一个视频,不知道怎样才能最大利用GPU。Batch size是可以大一点吗?
如果Yolo导出一个batch size 大于1的模型,不知道怎么在这里使用。
没有看到关于batch size的设定,是否在test.cpp里面预读几个image再塞到模型里面?
另外,如何才能支持GPU半精度模型?
谢谢指教!

yolov8和yolov5区别

作者您好,我在导出torchscript格式的文件时,发现Yolov8和yolov5在第三维(代表各种属性信息)少了一行。 Yolov8 导出尺寸(1、10、8400); 而 yolov5 导出尺寸 (1 ,11,XXX)。 我的数据包含五个类别。 缺失的一行数据代表什么呢?我把您代码中关于Yolov8的处理转移到我的代码中,但是发现不能检测最后一类缺陷。

Output function for yolox?

None of output function work for yolox. Do u have suitable code for the yolox output that can be pass to the nms function of your work?

关于图片输入模型时的预处理

你好就是我最近在尝试部署我的yoloV5模型,我阅读你的源码后认为你的letterbox操作跟python源码的好像不太一样。但是我按照官方的思路写letterbox的话输入模型会报错,有时还会产生python端不会预测的框,按照你的思路写的话就不会报那个错误,产生的框也是python端产生的框。

ubuntu 18.04 make 报错

CMake Error at test/CMakeLists.txt:6 (add_executable):
The target name "test" is reserved or not valid for certain CMake features,
such as generator expressions, and may result in undefined behavior.

CMake Error at test/CMakeLists.txt:8 (target_link_libraries):
Cannot specify link libraries for target "test" which is not built by this
project.

无法正确读取pt文件

  • 麻烦,请教下
  • py那边训练好的pt,c++程序读取后,运行,卡在

torch::Tensor pred = model.forward({ data }).toTuple()->elements()[0].toTensor();

  • 请问可能是什么问题
  • 已经用export.py对pt文件进行了转换

std::vector<torch::Tensor> YoloV5::prediction(torch::Tensor data)
{
	if (!data.is_cuda() && this->isCuda)
	{
		data = data.cuda();
	}
	if (data.is_cuda() && !this->isCuda)
	{
		data = data.cpu();
	}
	torch::Tensor pred = model.forward({ data }).toTuple()->elements()[0].toTensor();
	return non_max_suppression(pred, confThres, iouThres);
}

关于yolov5版本和精度问题

您好,请问你这个项目针对yolov5的哪个版本呢?还是通用的?
另外一个问题是转换后的精度相比较yolov5有下降么?

编译的时候找不到OpenCVConfig.cmake opencv-config.cmake?

已经在Cmake list中添加了相应路径:
cmake_minimum_required(VERSION 3.10)

Windows 系统下指定 pytorch 路径和 opencv 路径

set(F:/programs/opencv/build)
set(F:/programs/libtorch/hare/cmake/Torch)

project(YoloV5LibTorch)

include_directories(./include)

add_subdirectory(./src)
add_subdirectory(./test)

install(TARGETS test YoloV5
RUNTIME DESTINATION ${CMAKE_SOURCE_DIR}/bin
ARCHIVE DESTINATION ${CMAKE_SOURCE_DIR}/lib
)

进行第三步的时候,提示下面错误。
CMake Error at src/CMakeLists.txt:1 (find_package):
By not providing "FindOpenCV.cmake" in CMAKE_MODULE_PATH this project has
asked CMake to find a package configuration file provided by "OpenCV", but
CMake did not find one.

Could not find a package configuration file provided by "OpenCV" with any
of the following names:

OpenCVConfig.cmake
opencv-config.cmake

Add the installation prefix of "OpenCV" to CMAKE_PREFIX_PATH or set
"OpenCV_DIR" to a directory containing one of the above files. If "OpenCV"
provides a separate development package or SDK, be sure it has been
installed.
-- Configuring incomplete, errors occurred!
See also "F:/programs/YoloV5-LibTorch-main/build/CMakeFiles/CMakeOutput.log".

多gpu推理

您好,代码可以实现多gpu加载yolo模型,实现多gpu推理吗

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.