本篇爲yolov3源碼配置實現部分及 使用官方模型進行批量測試
目錄
源碼配置
- 下載源碼並編譯
git clone https://github.com/pjreddie/darknet
cd darknet
make
- 下載預訓練權重(coco)
wget https://pjreddie.com/media/files/yolov3.weights
- 單張圖片測試
darknet下
./darknet detect cfg/yolov3.cfg yolov3.weights data/dog.jpg
應該會出現這個界面:
再然後會保存到darknet下prediction.jpg,不會顯示,因爲沒有在opencv下編譯
好的,這是單次測試,然後是批量測試
批量測試
如果只是想要批量測試一下並輸出記錄目標位置信息的txt文件,可以使用valid命令,如下:
./darknet detector valid cfg/voc.data cfg/yolov3.cfg yolov3.weights
其中,cfg/voc.data 爲測試訓練路徑等配置,這裏需要修改
而cfg/yolov3.cfg爲yolov3網絡配置文件
yolov3.weights 爲剛剛下載的權重文件
classes= 20
train = /home/pjreddie/data/voc/train.txt
valid = /home/dbc/darknet/test.txt #你自己的測試圖像路徑
names = data/coco.names #注意這裏使用的是coco所訓練的模型yolov3.cfg所以這裏對應爲coco.names
backup = backup
這裏test.txt爲測試圖像的路徑,
可以使用命令
ls -R /home/dbc/DATASET/1/pic/*.jpg > files.txt
將路徑下文件的絕對路徑寫入files.txt文件中。
我的test.txt中內容如下:
/home/dbc/DATASET/1/pic/000001.jpg
/home/dbc/DATASET/1/pic/000002.jpg
/home/dbc/DATASET/1/pic/000003.jpg
/home/dbc/DATASET/1/pic/000004.jpg
/home/dbc/DATASET/1/pic/000005.jpg
/home/dbc/DATASET/1/pic/000006.jpg
/home/dbc/DATASET/1/pic/000007.jpg
/home/dbc/DATASET/1/pic/000008.jpg
……
執行完在./results/comp4_det_test_[類名].txt裏會保存測試結果,像這樣
打開comp4_det_test_[handbag].txt看一下:
按列,分別爲:圖像名稱 | 置信度 | xmin,ymin,xmax,ymax
如果想要批量測試並保存測試結果則需要修改./example/detector.c文件
這裏參考了博客
https://blog.csdn.net/mieleizhi0522/article/details/79989754
中的修改方法:
- 使用以下函數替換了detector.c中的void test_detector函數, 注意修改3處路徑
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);
//image sized = resize_image(im, net->w, net->h);
//image sized2 = resize_max(im, net->w);
//image sized = crop_image(sized2, -((net->w - sized2.w)/2), -((net->h - sized2.h)/2), net->w, net->h);
//resize_network(net, sized.w, sized.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);
//printf("%d\n", nboxes);
//if (nms) do_nms_obj(boxes, probs, l.w*l.h*l.n, l.classes, nms);
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");
#ifdef OPENCV
cvNamedWindow("predictions", CV_WINDOW_NORMAL);
if(fullscreen){
cvSetWindowProperty("predictions", CV_WND_PROP_FULLSCREEN, CV_WINDOW_FULLSCREEN);
}
show_image(im, "predictions");
cvWaitKey(0);
cvDestroyAllWindows();
#endif
}
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);
//image sized = resize_image(im, net->w, net->h);
//image sized2 = resize_max(im, net->w);
//image sized = crop_image(sized2, -((net->w - sized2.w)/2), -((net->h - sized2.h)/2), net->w, net->h);
//resize_network(net, sized.w, sized.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);
//printf("%d\n", nboxes);
//if (nms) do_nms_obj(boxes, probs, l.w*l.h*l.n, l.classes, nms);
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));
#ifdef OPENCV
cvNamedWindow("predictions", CV_WINDOW_NORMAL);
if(fullscreen){
cvSetWindowProperty("predictions", CV_WND_PROP_FULLSCREEN, CV_WINDOW_FULLSCREEN);
}
show_image(im, "predictions");
cvWaitKey(0);
cvDestroyAllWindows();
#endif
}
free_image(im);
free_image(sized);
if (filename) break;
}
}
}
}
- 在前面添加*GetFilename(char *p)函數(這裏和原博客略有不同,增加
#include <unistd.h>
不然會報錯)
#include "darknet.h"
#include <sys/stat.h>
#include <stdio.h>
#include <unistd.h>
static int coco_ids[] = {1,2,3,4,5,6,7,8,9,10,11,13,14,15,16,17,18,19,20,21,22,23,24,25,27,28,31,32,33,34,35,36,37,38,39,40,41,42,43,44,46,47,48,49,50,51,52,53,54,55,56,57,58,59,60,61,62,63,64,65,67,70,72,73,74,75,76,77,78,79,80,81,82,84,85,86,87,88,89,90};
char *GetFilename(char *p)
{
static char name[20]={""};
char *q = strrchr(p,'/') + 1;
strncpy(name,q,6); //6是你的測試圖像名稱的長度
return name;
}
- 在darknet下重新make
- 執行批量測試命令如下
./darknet detector test cfg/voc.data cfg/yolov3.cfg yolov3.weights
執行結果:
layer filters size input output
0 conv 32 3 x 3 / 1 416 x 416 x 3 -> 416 x 416 x 32 0.299 BFLOPs
1 conv 64 3 x 3 / 2 416 x 416 x 32 -> 208 x 208 x 64 1.595 BFLOPs
.......
104 conv 256 3 x 3 / 1 52 x 52 x 128 -> 52 x 52 x 256 1.595 BFLOPs
105 conv 255 1 x 1 / 1 52 x 52 x 256 -> 52 x 52 x 255 0.353 BFLOPs
106 detection
Loading weights from yolov3.weights...Done!
Enter Image Path:
然後輸入測試用圖像路徑,與上文valid參數一樣,輸入
/home/dbc/darknet/test.txt #你的測試圖像路徑
測試結果圖像會保存在./data/out中。
進入下一部分,AP及mAP的計算
yolov3 的AP,mAP計算
這裏,你需要準備:
- 測試圖像
- 測試圖像所對應的VOC格式的標記文件(xml)
- 測試圖像文件列表的txt文件
- 上文yolo批量測試所輸出的對測試圖像測試的結果文件(txt),其中包含模型所預測的位置信息。*注意:*這裏的txt文件的命名,應當與xml文件中所標記的類別名稱相同,如:handbag.txt, person.txt etc.
- 下載fasterrcnn的eval_voc.py,自行下載放在darknet目錄下
1大家肯定都有哈哈,2 我是用labelimg標記的,labelImg鏈接
3測試圖像文件列表的txt文件的格式如下:
只有文件名,不需要路徑和後綴名
在darknet目錄下新建一個compute_mAP.py,內容;
from voc_eval import voc_eval
import _pickle as cPickle
rec,prec,ap = voc_eval('/home/dbc/darknet/results/{}.txt', '/home/dbc/DATASET/1/anno/{}.xml', '/home/dbc/darknet/test.txt', 'person', '.')
print('rec',rec)
print('prec',prec)
print('ap',ap)
運行python compute_mAP.py 計算出單類別的AP
###tips:有關keyerror
每次運行前記得刪掉annots.pkl,不然會報錯