## Introduction
A LibTorch inference implementation of the [yolov5](https://github.com/ultralytics/yolov5) object detection algorithm. Both GPU and CPU are supported.
## Dependencies
- Ubuntu 16.04
- CUDA 10.2
- OpenCV 3.4.12
- LibTorch 1.6.0
## TorchScript Model Export
Please refer to the official document here: https://github.com/ultralytics/yolov5/issues/251
**Mandatory Update**: developer needs to modify following code from the original [export.py in yolov5](https://github.com/ultralytics/yolov5/blob/master/models/export.py)
```bash
# line 29
model.model[-1].export = False
```
**Add GPU support**: Note that the current export script in [yolov5](https://github.com/ultralytics/yolov5) **uses CPU by default**, the "export.py" needs to be modified as following to support GPU:
```python
# line 28
img = torch.zeros((opt.batch_size, 3, *opt.img_size)).to(device='cuda')
# line 31
model = attempt_load(opt.weights, map_location=torch.device('cuda'))
```
Export a trained yolov5 model:
```bash
cd yolov5
export PYTHONPATH="$PWD" # add path
python models/export.py --weights yolov5s.pt --img 640 --batch 1 # export
```
## Setup
```bash
$ cd /path/to/libtorch-yolo5
$ wget https://download.pytorch.org/libtorch/cu102/libtorch-cxx11-abi-shared-with-deps-1.6.0.zip
$ unzip libtorch-cxx11-abi-shared-with-deps-1.6.0.zip
$ mkdir build && cd build
$ cmake .. && make
```
To run inference on examples in the `./images` folder:
```bash
# CPU
$ ./libtorch-yolov5 --source ../images/bus.jpg --weights ../weights/yolov5s.torchscript.pt --view-img
# GPU
$ ./libtorch-yolov5 --source ../images/bus.jpg --weights ../weights/yolov5s.torchscript.pt --gpu --view-img
# Profiling
$ CUDA_LAUNCH_BLOCKING=1 ./libtorch-yolov5 --source ../images/bus.jpg --weights ../weights/yolov5s.torchscript.pt --gpu --view-img
```
## Demo
![Bus](images/bus_out.jpg)
![Zidane](images/zidane_out.jpg)
## FAQ
1. terminate called after throwing an instance of 'c10::Error' what(): isTuple() INTERNAL ASSERT FAILED
- Make sure "model.model[-1].export = False" when running export script.
2. Why the first "inference takes" so long from the log?
- The first inference is slower as well due to the initial optimization that the JIT (Just-in-time compilation) is doing on your code. This is similar to "warm up" in other JIT compilers. Typically, production services will warm up a model using representative inputs before marking it as available.
- It may take longer time for the first cycle. The [yolov5 python version](https://github.com/ultralytics/yolov5) run the inference once with an empty image before the actual detection pipeline. User can modify the code to process the same image multiple times or process a video to get the valid processing time.
## References
1. https://github.com/ultralytics/yolov5
2. [Question about the code in non_max_suppression](https://github.com/ultralytics/yolov5/issues/422)
3. https://github.com/walktree/libtorch-yolov3
4. https://pytorch.org/cppdocs/index.html
5. https://github.com/pytorch/vision
6. [PyTorch.org - CUDA SEMANTICS](https://pytorch.org/docs/stable/notes/cuda.html)
7. [PyTorch.org - add synchronization points](https://discuss.pytorch.org/t/why-is-the-const-time-with-fp32-and-fp16-almost-the-same-in-libtorchs-forward/45792/5)
8. [PyTorch - why first inference is slower](https://github.com/pytorch/pytorch/issues/2694)
没有合适的资源?快使用搜索试试~ 我知道了~
温馨提示
介绍 对象检测算法的LibTorch推理实现。 GPU和CPU均受支持。 依存关系 Ubuntu 16.04 CUDA 10.2 OpenCV 3.4.12 LibTorch 1.6.0 TorchScript模型导出 请在此处参考官方文档: : 强制更新:开发人员需要修改原始以下代码 # line 29 model.model[-1].export = False 添加GPU支持:请注意, 当前的导出脚本默认情况下使用CPU ,需要对“ export.py”进行如下修改以支持GPU: # line 28 img = torch . zeros (( opt . batch_size , 3 , * opt . img_size )). to ( device = 'cuda' ) # line 31 model = attempt_load ( opt . weigh
资源详情
资源评论
资源推荐
收起资源包目录
libtorch-yolov5-master.zip (14个子文件)
libtorch-yolov5-master
.gitignore 330B
images
bus_out.jpg 485KB
zidane_out.jpg 249KB
bus.jpg 476KB
zidane.jpg 165KB
src
main.cpp 4KB
detector.cpp 10KB
LICENSE 1KB
README.md 3KB
include
detector.h 3KB
utils.h 194B
cxxopts.hpp 45KB
weights
coco.names 621B
CMakeLists.txt 1KB
共 14 条
- 1
kolten
- 粉丝: 50
- 资源: 4558
上传资源 快速赚钱
- 我的内容管理 展开
- 我的资源 快来上传第一个资源
- 我的收益 登录查看自己的收益
- 我的积分 登录查看自己的积分
- 我的C币 登录后查看C币余额
- 我的收藏
- 我的下载
- 下载帮助
最新资源
资源上传下载、课程学习等过程中有任何疑问或建议,欢迎提出宝贵意见哦~我们会及时处理!
点击此处反馈
安全验证
文档复制为VIP权益,开通VIP直接复制
信息提交成功
评论1