代码小白,第一次认真看大段大段的代码,基础知识严重欠缺,如有错误望指正。
进入正题:
测试命令示例:
$./darknet detector test cfg/voc.data cfg/tiny-yolo-voc.cfg results/tiny-yolo-voc_6000.weights data/images.jpg
目前最新版本的darknet中,darknet.c和detector.c都在examples文件夹中。
首先,从主函数开始解析命令行参数,然后根据不同的命令行参数进入不同的调用方法。(本文不考虑GPU部分代码)
//darknet.c
int main(int argc, char **argv)
{
//test_resize("data/bad.jpg");
//test_box();
//test_convolutional_layer();
//如果没有任何命令行参数,则打印一句提示信息:Usage: [应用程序名称] ,即告诉你要指定一个命令行参数。
if(argc < 2){
fprintf(stderr, "usage: %s \n" , argv[0]);
return 0;
}
if (0 == strcmp(argv[1], "average")){
average(argc, argv);
} else if (0 == strcmp(argv[1], "detector")){
run_detector(argc, argv);
}//第一个参数是detector,跳转到run_detector函数
return 0;
}
附:1、fprintf函数
作用:格式化输出到一个流/文件中;
函数原型:
int fprintf( FILE *stream, const char *format, [ argument ]...)
fprintf()函数根据指定的格式(format)向输出流(stream)写入数据(argument)。
2、stderr – 标准错误输出流
作为程序运行过程中的错误显示出来的,默认像屏幕输出,具体看程序执行时stderr重定向到哪。
3、if(argc < 2)语句
argv[0]:第一个命令行参数darknet就是应用程序的名称,因此如果指定了一个命令行参数,那argc就为2。
//detector.c
void run_detector(int argc, char **argv)
{
//检查是否有参数prefix,默认值是0
char *prefix = find_char_arg(argc, argv, "-prefix", 0);
//检查是否有参数thresh参数,thresh为输出的阈值,默认值是0.24
float thresh = find_float_arg(argc, argv, "-thresh", .24);
//检查是否有参数hier_thresh,默认为0.5
float hier_thresh = find_float_arg(argc, argv, "-hier", .5);
//检查是否有参数cam_index,默认为0
int cam_index = find_int_arg(argc, argv, "-c", 0);
//检查是否有参数frame_skip,默认为0
int frame_skip = find_int_arg(argc, argv, "-s", 0);
//检查是否有参数avg,默认为3
int avg = find_int_arg(argc, argv, "-avg", 3);
//如果输入参数小于4个,输出正确的命令格式:[应用程序名称][yolo/detector...][train/test/valid][cfg][weights (optional)]
if(argc < 4){
fprintf(stderr, "usage: %s %s [train/test/valid] [cfg] [weights (optional)]\n", argv[0], argv[1]);
return;
}
//检查是否指定GPU运算
char *gpu_list = find_char_arg(argc, argv, "-gpus", 0);
char *outfile = find_char_arg(argc, argv, "-out", 0);
int *gpus = 0;
int gpu = 0;
int ngpus = 0;
if(gpu_list){
printf("%s\n", gpu_list);
int len = strlen(gpu_list);
ngpus = 1;
int i;
for(i = 0; i < len; ++i){
if (gpu_list[i] == ',') ++ngpus;
}
gpus = calloc(ngpus, sizeof(int));
for(i = 0; i < ngpus; ++i){
gpus[i] = atoi(gpu_list);
gpu_list = strchr(gpu_list, ',')+1;
}
} else {
gpu = gpu_index;
gpus = &gpu;
ngpus = 1;
}
//检查clear参数
int clear = find_arg(argc, argv, "-clear");
int fullscreen = find_arg(argc, argv, "-fullscreen");
int width = find_int_arg(argc, argv, "-w", 0);
int height = find_int_arg(argc, argv, "-h", 0);
int fps = find_int_arg(argc, argv, "-fps", 0);
//data文件的路径存为argv数组的第四个元素
char *datacfg = argv[3];
//cfg文件的路径存为argv数组的第五个元素
char *cfg = argv[4];
//当参数大于5个时,权重为argv数组的第六个元素的内容
char *weights = (argc > 5) ? argv[5] : 0;
//当参数大于6个时,权重为argv数组的第七个元素的内容,即示例中需检测图片的路径
char *filename = (argc > 6) ? argv[6]: 0;
//根据第三个参数(即argv[2])的内容,调用不同的函数,并传入datacfg,cfg等参数
//示例中第三个参数是test,进入test_detector参数
if(0==strcmp(argv[2], "test")) test_detector(datacfg, cfg, weights, filename, thresh, hier_thresh, outfile, fullscreen);
else if(0==strcmp(argv[2], "train")) train_detector(datacfg, cfg, weights, gpus, ngpus, clear);
else if(0==strcmp(argv[2], "valid")) validate_detector(datacfg, cfg, weights, outfile);
else if(0==strcmp(argv[2], "valid2")) validate_detector_flip(datacfg, cfg, weights, outfile);
else if(0==strcmp(argv[2], "recall")) validate_detector_recall(cfg, weights);
else if(0==strcmp(argv[2], "demo")) {
list *options = read_data_cfg(datacfg);
int classes = option_find_int(options, "classes", 20);
char *name_list = option_find_str(options, "names", "data/names.list");
char **names = get_labels(name_list);
demo(cfg, weights, thresh, cam_index, filename, names, classes, frame_skip, prefix, avg, hier_thresh, width, height, fps, fullscreen);
}
}
//detector.c
void test_detector(char *datacfg, char *cfgfile, char *weightfile, char *filename, float thresh, float hier_thresh, char *outfile, int fullscreen)
{
//options读取data文件的内容,其中包含类别文件(.names)所在路径,两个txt文件(分别记录训练集、测试集图片的绝对路径)所在路径等。
list *options = read_data_cfg(datacfg);
//从option中查找names对应的value,如没有,使用默认值“data/names.list”
char *name_list = option_find_str(options, "names", "data/names.list");
//从.names文件中得到标签(即xml文件中的类别)名称
char **names = get_labels(name_list);
//加载位于data/labels下ASCII码为32-127的8种尺寸的图片,用于显示标签用的字符图片
image **alphabet = load_alphabet();
//用netweork.h中自定义的network结构体存储yolo模型文件(cfg文件),parse_network_cfg函数位于parser.c
network net = parse_network_cfg(cfgfile);
///读取yolo模型训练得到的权重文件
if(weightfile){
load_weights(&net, weightfile);
}
//每层batch设置为1,表示一张一张地去检测图片
set_batch_network(&net, 1);
//随机数种子
srand(2222222);
double time;
//定义input指针指向buff数组,用于存放需检测图片的路径
char buff[256];
char *input = buff;
int j;
float nms=.3;
while(1){
if(filename){
strncpy(input, filename, 256);
} else { //命令行参数中没有指定检测图片的路径
printf("Enter Image Path: ");
fflush(stdout);
input = fgets(input, 256, stdin);
if(!input) return;
strtok(input, "\n");
}
//加载图片,默认当做彩色处理
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];
box *boxes = calloc(l.w*l.h*l.n, sizeof(box));
float **probs = calloc(l.w*l.h*l.n, sizeof(float *));
for(j = 0; j < l.w*l.h*l.n; ++j) probs[j] = calloc(l.classes + 1, sizeof(float *));
float **masks = 0;
if (l.coords > 4){
masks = calloc(l.w*l.h*l.n, sizeof(float*));
for(j = 0; j < l.w*l.h*l.n; ++j) masks[j] = calloc(l.coords-4, sizeof(float *));
}
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);
get_region_boxes(l, im.w, im.h, net.w, net.h, thresh, probs, boxes, masks, 0, 0, hier_thresh, 1);
if (nms) do_nms_obj(boxes, probs, l.w*l.h*l.n, l.classes, nms);
//else if (nms) do_nms_sort(boxes, probs, l.w*l.h*l.n, l.classes, nms);
//画预测结果
draw_detections(im, l.w*l.h*l.n, thresh, boxes, probs, masks, names, alphabet, l.classes);
//保存标记了预测标签的图片
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);
free(boxes);
free_ptrs((void **)probs, l.w*l.h*l.n);
if (filename) break;
}
}
其中重要的函数下一篇详解。