ROS Kinetic + Ubuntu 16.04 yolov3配置

ROS Kinetic + Ubuntu 16.04 yolov3配置

1.安装

项目Github地址:https://github.com/leggedrobotics/darknet_ros 安装到本地时选择ssh方式才能完整下载,需要先配置自己电脑上的ssh key并添加到Github账户(为github账户添加ssh key**(https://blog.csdn.net/qq_29232943/article/details/53523434)),选择http方式则会导致darknet文件夹空缺,需要自己手动安装;

2.编译

ROS下运行yolov3需要几步:

(1)修改darknet文件夹中的Makefile

,改成GPU适应(GPU,OPENCV,CUDNN,如果没有跑CPU也行),修改后的Makefile如下:

GPU=1
CUDNN=1
OPENCV=1
OPENMP=1
DEBUG=0

ARCH= -gencode arch=compute_30,code=sm_30 \
      -gencode arch=compute_35,code=sm_35 \
      -gencode arch=compute_50,code=[sm_50,compute_50] \
      -gencode arch=compute_52,code=[sm_52,compute_52] \
      -gencode arch=compute_60,code=[sm_60,compute_60]
#      -gencode arch=compute_20,code=[sm_20,sm_21] \ This one is deprecated?

# This is what I use, uncomment if you know your arch and want to specify
# ARCH= -gencode arch=compute_52,code=compute_52

VPATH=./src/:./examples
SLIB=libdarknet.so
ALIB=libdarknet.a
EXEC=darknet
OBJDIR=./obj/

CC=gcc
NVCC=nvcc 
AR=ar
ARFLAGS=rcs
OPTS=-Ofast
LDFLAGS= -lm -pthread 
COMMON= -Iinclude/ -Isrc/
CFLAGS=-Wall -Wno-unused-result -Wno-unknown-pragmas -Wfatal-errors -fPIC

ifeq ($(OPENMP), 1) 
CFLAGS+= -fopenmp
endif

ifeq ($(DEBUG), 1) 
OPTS=-O0 -g
endif

CFLAGS+=$(OPTS)

ifeq ($(OPENCV), 1) 
COMMON+= -DOPENCV
CFLAGS+= -DOPENCV
LDFLAGS+= `pkg-config --libs opencv` 
COMMON+= `pkg-config --cflags opencv` 
endif

ifeq ($(GPU), 1) 
COMMON+= -DGPU -I/usr/local/cuda/include/
CFLAGS+= -DGPU
LDFLAGS+= -L/usr/local/cuda/lib64 -lcuda -lcudart -lcublas -lcurand
endif

ifeq ($(CUDNN), 1) 
COMMON+= -DCUDNN 
CFLAGS+= -DCUDNN
LDFLAGS+= -lcudnn
endif

OBJ=gemm.o utils.o cuda.o deconvolutional_layer.o convolutional_layer.o list.o image.o activations.o im2col.o col2im.o blas.o crop_layer.o dropout_layer.o maxpool_layer.o softmax_layer.o data.o matrix.o network.o connected_layer.o cost_layer.o parser.o option_list.o detection_layer.o route_layer.o upsample_layer.o box.o normalization_layer.o avgpool_layer.o layer.o local_layer.o shortcut_layer.o logistic_layer.o activation_layer.o rnn_layer.o gru_layer.o crnn_layer.o demo.o batchnorm_layer.o region_layer.o reorg_layer.o tree.o  lstm_layer.o l2norm_layer.o yolo_layer.o
EXECOBJA=captcha.o lsd.o super.o art.o tag.o cifar.o go.o rnn.o segmenter.o regressor.o classifier.o coco.o yolo.o detector.o nightmare.o darknet.o
ifeq ($(GPU), 1) 
LDFLAGS+= -lstdc++ 
OBJ+=convolutional_kernels.o deconvolutional_kernels.o activation_kernels.o im2col_kernels.o col2im_kernels.o blas_kernels.o crop_layer_kernels.o dropout_layer_kernels.o maxpool_layer_kernels.o avgpool_layer_kernels.o
endif

EXECOBJ = $(addprefix $(OBJDIR), $(EXECOBJA))
OBJS = $(addprefix $(OBJDIR), $(OBJ))
DEPS = $(wildcard src/*.h) Makefile include/darknet.h

#all: obj backup results $(SLIB) $(ALIB) $(EXEC)
all: obj  results $(SLIB) $(ALIB) $(EXEC)


$(EXEC): $(EXECOBJ) $(ALIB)
	$(CC) $(COMMON) $(CFLAGS) $^ -o $@ $(LDFLAGS) $(ALIB)

$(ALIB): $(OBJS)
	$(AR) $(ARFLAGS) $@ $^

$(SLIB): $(OBJS)
	$(CC) $(CFLAGS) -shared $^ -o $@ $(LDFLAGS)

$(OBJDIR)%.o: %.c $(DEPS)
	$(CC) $(COMMON) $(CFLAGS) -c $< -o $@

$(OBJDIR)%.o: %.cu $(DEPS)
	$(NVCC) $(ARCH) $(COMMON) --compiler-options "$(CFLAGS)" -c $< -o $@

obj:
	mkdir -p obj
backup:
	mkdir -p backup
results:
	mkdir -p results

.PHONY: clean

clean:
	rm -rf $(OBJS) $(SLIB) $(ALIB) $(EXEC) $(EXECOBJ) $(OBJDIR)/*

需要注意的是第七行,显卡计算能力需要参考英伟达网址上对应本机显卡型号的信息进行更改,添加到最后一行即可,修改Makefile后在该文件夹终端输入make;

(2)编译darknet_ros

darknet是YOLO基于C的深度学习框架,编译通过后可以开始darknet_ros包的安装,参考作者的github安装说明,catkin编译命令——catkin_make -DCMAKE_BUILD_TYPE=Release
编译过程中会自动在/darknet_ros/darknet_ros/yolo_network_config/weights下载几个网络权重文件(默认COCO数据集,80类),如果需要别的数据集权重文件譬如PASCAL VOC(这两个数据集在作者主页都有镜像可以下载):

wget http://pjreddie.com/media/files/yolov2-voc.weights
wget http://pjreddie.com/media/files/yolov2-tiny-voc.weights
wget http://pjreddie.com/media/files/yolov3-voc.weights  #貌似已经不提供下载
wget http://pjreddie.com/media/files/yolov3.weights

可能遇到问题:
1.CMakeLists.txt Line100 : 找不到${DARKNET_PATH}/src/activation_layer.c ,这是因为DARKNET_PATH找不到,找到Line8 :

find_path(DARKNET_PATH
  NAMES "README.md"
  HINTS "${CMAKE_CURRENT_SOURCE_DIR}/../darknet/")

其实是因为没有下载完全,把darknet下载编译通过即可
2.error:/usr/bin/ld: 找不到 -lippicv
Ubuntu默认使用ROS Kinetic附带的OPENCV 3.3.1,之前安装深度学习环境自己配置了OPENCV 3.1,在文件夹的以下目录找到libippicv.a
/home/ds/opencv-3.1.0/3rdparty/ippicv/unpack/ippicv_lnx/lib/intel64
并拷贝到/usr/local/lib目录下,即可解决问题;

3.使用

(1)修改配置文件

yolo v3运行主要需要两个文件,即/home/whu-hk/catkin_yolo/src/darknet_ros/darknet_ros/yolo_network_config目录下的cfg与weight,分别是网络定义的权重文件,预先下载或自己训练需要的两个文件,在/darknet_ros/config下编辑yaml文件,将对应的cfg、weight选择写入,yaml文件中主要包括网络定义文件路径、权重文件路径、置信度阈值、检测类别;
修改ros.yaml:此文件定义了ros中的话题订阅和发布,主要修改图片订阅话题,本人使用

/camera/bgr/image_raw

其它视具体情况修改,darknet_ros节点发布的话题有三个:包括

 /darknet_ros/found_object  #检测到物体个数
 /darknet_ros/bounding_boxes  #检测到物体的包围盒
 /darknet_ros/detection_image  #检测结果图像

包围盒话题的输出如下,由左上角坐标和右下角坐标组成,可以视自己需要修改成ROS中的ROI类型,源代码为/darknet_ros/darknet_ros/src/YoloObjectDetector.cpp——Line 612至Line 625

header: 
  seq: 1734
  stamp: 
    secs: 1539656263
    nsecs: 109147646
  frame_id: "detection"
image_header: 
  seq: 895
  stamp: 
    secs: 1539656263
    nsecs:  24454550
  frame_id: "head_camera"
bounding_boxes: 
  - 
    Class: "person"
    probability: 0.737510383129
    xmin: 335
    ymin: 36
    xmax: 488
    ymax: 344
---

(2)ros运行

修改launch文件夹下的launch文件,主要修改:

   #cfg和weight
roscore
roslaunch usb_cam usb_cam.launch
roslaunch darknet_ros daknet_ros,launch

你可能感兴趣的:(环境配置)