以下为我参考JK Jung’s blog YOLOv3 on Jetson TX2在自己的TX2上测试yolo v3的过程。
0 刷机安装JetPack-3.2
1 安装opencv3.4.0(因为目前安装的3.4.1不能跑yolo)
- Step1 Remove all old opencv stuffs installed bt JetPack
$ sudo apt-get purge libopencv*
- Step2 换到最新的numpy,因此要删掉老的numpy
- Step3
$ sudo apt-get update
$ sudo apt-get dist-upgrade
2.安装Yolo3
- Step1 下载源代码
$ git clone https://github.com/pjreddie/darknet yolo3
$ cd yolo3
- Step2 直接编译,此时是只使用CPU
$ make
- Step3 下载预训练模型
$ wget https://pjreddie.com/media/files/yolov3.weights
- Step4 运行代码,检测示例图片
$ ./darknet detect cfg/yolov3.cfg yolov3.weights data/dog.jpg
格式: ./darknet detect .cfg 预训练权重 预测权重
以上出现问题,代码被kill,原因是TX2内存溢出,因此对Step2进行修改,开启GPU
- Step2+:
$ vim Makefile
修改为:
GPU=1 #开启GPU
CUDNN=1 #
OPENCV=1 #开启Opencv
然后重新编译:
$ make
修改后仍溢出内存,发现是batch size太大了,不适合在TX2上跑,因此修改yolov3/cfg/yolov3.cfg
文件,将Training注释掉,改Testing的batch为1,即:
# Testing
batch=1
subdivisions=1
# Training
#batch=64
#subdivisions=16
重新编译成功!
成功!平均每张0.5s左右
三、跑Yolo于图片小tips
1. 检测指定路径的图像
$ ./darknet detect cfg/yolov3.cfg yolov3.weights
会输出:Enter Image Path:
2.改变阈值:
By default, YOLO only displays objects detected with a confidence of .25
or higher. You can change this by passing the -thresh flag to the yolo command.
For example, to display all detection you can set the threshold to 0:
$ ./darknet detect cfg/yolov3.cfg yolov3.weights data/dog.jpg -thresh 0
3. Tiny YOLOv3
在保证精度不损失太多的前提下,有个轻量级的网络—— yolov3-tiny.
- Step1:下载权重
$ wget https://pjreddie.com/media/files/yolov3-tiny.weights
- Step2:跑起来!
$ ./darknet detect cfg/yolov3-tiny.cfg yolov3-tiny.weights data/dog.jpg
这是个轻量级的网络,因此耗时较短:0.034424s
4. 使用摄像头
- Step1: 首先需要编译 Darknet with CUDA and OpenCV.
其实我们已经编译过了 - Step2: 运行代码
$ ./darknet detector demo cfg/coco.data cfg/yolov3.cfg yolov3.weights
报错:
Couldn't connect to wecam. Resource temporarily unavaliable YOLO will display the current FPS and predicted classes as well as the image with bounding boxes drawn on top of it.
原因是OPENCV默认采用0号摄像头,TX2的0号摄像头是板子上自带的板上摄像头,而我们的usb摄像头是1号,故使用如下代码,解决了问题:
$ ./darknet detector demo cfg/coco.data cfg/yolov3.cfg yolov3.weights -c 1//使用1号摄像头