本文采用的百度aistudio服务器配置为NVIDIA-SMI 396.37+ubuntu16.04.1+cuda 9.0+cudnn 7.6.0
wget https://developer.nvidia.com/compute/cuda/9.0/Prod/local_installers/cuda_9.0.176_384.81_linux-run
sh cuda_9.0.176_linux-run --silent --toolkit --toolkitpath=$HOME/cuda-9.0 (HOME改为自己的路径)
export PATH=/home/aistudio/cuda-9.0/bin${PATH:+:${PATH}}
export LD_LIBRARY_PATH=/home/aistudio/cuda-9.0/lib64${LD_LIBRARY_PATH:+:${LD_LIBRARY_PATH}}
tar -zxvf /home/aistudio/data/data25688/cudnn.tgz
cp /home/aistudio/cudnn/cuda/include/cudnn.h /home/aistudio/cuda-9.0/include/
cp /home/aistudio/cudnn/cuda/lib64/libcudnn* /home/aistudio/cuda-9.0/lib64/
接下来安装用于yolo3的opencv
unzip opencv-3.4.0.zip
cd opencv-3.4.0
mkdir build
cd build
cmake -D CMAKE_BUILD_TYPE=Release -D CMAKE_INSTALL_PREFIX=/home/aistudio/opencv_install ..
make -j8 (j后面的数字是cpu核数的两倍)
make install
等待opencv安装完毕
7. 配置opencv环境变量
export PKG_CONFIG_PATH=$PKG_CONFIG_PATH:/home/aistudio/opencv_install/lib/pkgconfig
export LD_LIBRARY_PATH=/home/aistudio/opencv_install/lib:/home/aistudio/opencv-3.4.0/build/lib
source ~/.bashrc
yolo3的官网 https://pjreddie.com/darknet/yolo/.
里面有一些简单测试图片的教程
首先从GitHub上下载项目
git clone https://github.com/pjreddie/darknet
cd darknet
make
下载yolo3官网训练好的权重参数
wget https://pjreddie.com/media/files/yolov3.weights
打开配置文件
vim Makefile
输入i对文档进行编辑,将前三行都改为1
同时文档中还有几处需要修改为自己相应的路径
ARCH= -gencode arch=compute_70,code=[sm_70,compute_70]
NVCC=/home/aistudio/cuda-9.0/bin/nvcc
/home/aistudio/cuda-9.0/include/
/home/aistudio/cuda-9.0/lib64
编辑好Makefile后输入esc键退出编辑模式,然后键入:wq保存并退出文档
然后输入make生成文件
make
./darknet detect cfg/yolov3.cfg yolov3.weights data/dog.jpg
首先修改darknet/examples路径下的detector.c文件中的test_detector函数,使用以下函数替换,(其中有三处路径需要修改)
void test_detector(char *datacfg, char *cfgfile, char *weightfile, char *filename, float thresh, float hier_thresh, char *outfile, int fullscreen)
{
list *options = read_data_cfg(datacfg);
char *name_list = option_find_str(options, "names", "data/names.list");
char **names = get_labels(name_list);
image **alphabet = load_alphabet();
network *net = load_network(cfgfile, weightfile, 0);
set_batch_network(net, 1);
srand(2222222);
double time;
char buff[256];
char *input = buff;
float nms=.45;
int i=0;
while(1){
if(filename){
strncpy(input, filename, 256);
image im = load_image_color(input,0,0);
image sized = letterbox_image(im, net->w, net->h);
layer l = net->layers[net->n-1];
float *X = sized.data;
time=what_time_is_it_now();
network_predict(net, X);
printf("%s: Predicted in %f seconds.\n", input, what_time_is_it_now()-time);
int nboxes = 0;
detection *dets = get_network_boxes(net, im.w, im.h, thresh, hier_thresh, 0, 1, &nboxes);
if (nms) do_nms_sort(dets, nboxes, l.classes, nms);
draw_detections(im, dets, nboxes, thresh, names, alphabet, l.classes);
free_detections(dets, nboxes);
if(outfile)
{
save_image(im, outfile);
}
else{
save_image(im, "predictions");
}
free_image(im);
free_image(sized);
if (filename) break;
}
else {
printf("Enter Image Path: ");
fflush(stdout);
input = fgets(input, 256, stdin);
if(!input) return;
strtok(input, "\n");
list *plist = get_paths(input);
char **paths = (char **)list_to_array(plist);
printf("Start Testing!\n");
int m = plist->size;
if(access("/home/FENGsl/darknet/data/out",0)==-1)//"/home/FENGsl/darknet/data"修改成自己的路径
{
if (mkdir("/home/FENGsl/darknet/data/out",0777))//"/home/FENGsl/darknet/data"修改成自己的路径
{
printf("creat file bag failed!!!");
}
}
for(i = 0; i < m; ++i){
char *path = paths[i];
image im = load_image_color(path,0,0);
image sized = letterbox_image(im, net->w, net->h);
layer l = net->layers[net->n-1];
float *X = sized.data;
time=what_time_is_it_now();
network_predict(net, X);
printf("Try Very Hard:");
printf("%s: Predicted in %f seconds.\n", path, what_time_is_it_now()-time);
int nboxes = 0;
detection *dets = get_network_boxes(net, im.w, im.h, thresh, hier_thresh, 0, 1, &nboxes);
if (nms) do_nms_sort(dets, nboxes, l.classes, nms);
draw_detections(im, dets, nboxes, thresh, names, alphabet, l.classes);
free_detections(dets, nboxes);
if(outfile){
save_image(im, outfile);
}
else{
char b[2048];
sprintf(b,"/home/FENGsl/darknet/data/out/%s",GetFilename(path));//"/home/FENGsl/darknet/data"修改成自己的路径
save_image(im, b);
printf("save %s successfully!\n",GetFilename(path));
}
free_image(im);
free_image(sized);
if (filename) break;
}
}
}
}
然后在detector.c文件的开头加入函数
char *GetFilename(char *p)
{
static char name[20]={""};
char *q = strrchr(p,'/') + 1;
strncpy(name,q,4);//注意后面的6,如果你的测试集的图片的名字字符(不包括后缀)是其他长度,请改为你需要的长度(官方的默认的长度是6)
return name;
}
在darknet下重新make
首先在windonws下生成包含图片路径的txt文件
import os
data_path = 'C:/Users/Apple/Desktop/test3' #包含图片的文件夹路径
img_names = os.listdir(data_path)
list_file = open('C:/Users/Apple/Desktop/test1.txt', 'w')#创建的txt文件路径
for img_name in img_names:
list_file.write('/home/aistudio/test3/%s\n'%img_name) #要生成的图片路径名
list_file.close()
这时一定不要直接copy到linux服务器上,要先使用notepad++转换格式,否则会报错。
进入notepad++,选择菜单栏中的View->Show Symbol->Show All Characters,可以看到目前的格式
然后选择菜单栏中Edit->EOL Conversion->Unix(LF),文档格式改变为
保存文档上传到服务器
./darknet detect cfg/yolov3.cfg yolov3.weights
【"annot load image "/home/aistudio/test/test2/0001.jpg】
“cannot load image”可能有两方面原因,一是图片路径有错误,另外就是包含路径的.txt文件格式有问题。
【检测生成的图片中没有框】
可以将darknet的Makefile中的cudnn置为0,这样可以保证一定的速度;还可以将gpu和cudnn全部置为0,这样完全使用cpu速度较慢。注意修改完以后要重新make进行编译。
https://blog.csdn.net/mieleizhi0522/article/details/79989754.
https://blog.csdn.net/firesolider/article/details/105023062