l = parse_detection(options, params);函数主体
detection_layer parse_detection(list *options, size_params params)
{
int coords = option_find_int(options, "coords", 1);//设置位置损失项前缀因子参数
int classes = option_find_int(options, "classes", 1);//设置类别
int rescore = option_find_int(options, "rescore", 0);//设置得分
int num = option_find_int(options, "num", 1);//设置框的数量
int side = option_find_int(options, "side", 7);//设置划分网格数side*side
detection_layer layer = make_detection_layer(params.batch, params.inputs, num, side, classes, coords, rescore);
layer.softmax = option_find_int(options, "softmax", 0);
layer.sqrt = option_find_int(options, "sqrt", 0);
layer.max_boxes = option_find_int_quiet(options, "max",90);
layer.coord_scale = option_find_float(options, "coord_scale", 1);
layer.forced = option_find_int(options, "forced", 0);
layer.object_scale = option_find_float(options, "object_scale", 1);
layer.noobject_scale = option_find_float(options, "noobject_scale", 1);
layer.class_scale = option_find_float(options, "class_scale", 1);
layer.jitter = option_find_float(options, "jitter", .2);
layer.random = option_find_int_quiet(options, "random", 0);
layer.reorg = option_find_int_quiet(options, "reorg", 0);
return layer;
}
detection_layer layer = make_detection_layer(params.batch, params.inputs, num, side, classes, coords, rescore);
detection_layer make_detection_layer(int batch, int inputs, int n, int side, int classes, int coords, int rescore)
{
//设置网络参数
detection_layer l = {0};
l.type = DETECTION;
l.n = n;
l.batch = batch;
l.inputs = inputs;
l.classes = classes;
l.coords = coords;
l.rescore = rescore;
l.side = side;
l.w = side;
l.h = side;
assert(side*side*((1 + l.coords)*l.n + l.classes) == inputs);//判断s*s*(num*(4+1)+classes)是否等于输入
l.cost = calloc(1, sizeof(float));//为损失数据分配空间
l.outputs = l.inputs;//输出等于输入,yolo1即l.side*l.side*(num*(1+l.coords)+l.classes)
l.truths = l.side*l.side*(1+l.coords+l.classes);//设置标签,例如yolo1为7x7x(1+4+20),因为一个网格只有一个标签
l.output = calloc(batch*l.outputs, sizeof(float));//为min_batch所有的输出分配空间
l.delta = calloc(batch*l.outputs, sizeof(float));
l.forward = forward_detection_layer;//为函数指针设置指向函数入口地址
l.backward = backward_detection_layer;
#ifdef GPU//若是定义了则运行下面代码,功能与上面相同
l.forward_gpu = forward_detection_layer_gpu;
l.backward_gpu = backward_detection_layer_gpu;
l.output_gpu = cuda_make_array(l.output, batch*l.outputs);
l.delta_gpu = cuda_make_array(l.delta, batch*l.outputs);
#endif
fprintf(stderr, "Detection Layer\n");
srand(0);
return l;
}