原 YOLO源码详解(五)-追本溯源7*7个grid

原 YOLO源码详解(五)- YOLO中的7*7个grid和RPN中的9个anchors

原 YOLO源码详解(四)- 反向传播(back propagation)

原 YOLOv2如何fine-tuning?

原 用YOLOv2模型训练VOC数据集

原 YOLO源码详解(三)- 前向传播(forward)

原 YOLO源码详解(二)- 函数剖析

原 YOLO源码详解(一)-训练

 

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;//对应论文里面的B,表示每个cell生成的bounding box数。
    l.batch = batch;
    l.inputs = inputs;//输入detection的维度,计算方式根据论文是S*S*(B*5 + C),论文里面对应7*7*(2*5 + 20)
    l.classes = classes;//类别数,论文里面对应是20
    l.coords = coords;//坐标,(x,y,w,h), (x,y)是相对于网格的数值[0,1],(w,h),是相对于整个图片的大小。
    l.rescore = rescore;
    l.side = side;//网格side*side
    assert(side*side*((1 + l.coords)*l.n + l.classes) == inputs);
    l.cost = calloc(1, sizeof(float));
    l.outputs = l.inputs;
    l.truths = l.side*l.side*(1+l.coords+l.classes);
    l.output = calloc(batch*l.outputs, sizeof(float));
    l.delta = calloc(batch*l.outputs, sizeof(float));
#ifdef 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;
}
void forward_detection_layer(const detection_layer l, network_state state)
{
    int locations = l.side*l.side;
    int i, j;
    memcpy(l.output, state.input, l.outputs*l.batch*sizeof(float));
    int b;
    if (l.softmax){
        for (b = 0; b < l.batch; ++b){
            int index = b*l.inputs;
            for (i = 0; i < locations; ++i) {
                int offset = i*l.classes;
                softmax_array(l.output + index + offset, l.classes, 1,
                    l.output + index + offset);
            }
            int offset = locations*l.classes;
            activate_array(l.output + index + offset, locations*l.n*(1 + l.coords), LOGISTIC);
        }
    }
    if (state.train){
        float avg_iou = 0;
        float avg_cat = 0;
        float avg_allcat = 0;
        float avg_obj = 0;
        float avg_anyobj = 0;
        int count = 0;
        *(l.cost) = 0;
        int size = l.inputs * l.batch;
        memset(l.delta, 0, size * sizeof(float));
        for (b = 0; b < l.batch; ++b){
            int index = b*l.inputs;
            for (i = 0; i < locations; ++i) {
                //truth 的存储方式是 [l.side*l.side*(1+l.classes+l.coords)]
                int truth_index = (b*locations + i)*(1 + l.coords + l.classes);//这个地方表示的是输入图片的gound truth的index
                int is_obj = state.truth[truth_index];//表示这个cell是否是object,数据存放在state里面
                for (j = 0; j < l.n; ++j) {//计算noobj这一项的损失
                    int p_index = index + locations*l.classes + i*l.n + j;
                    //b*l.inputs + locations*l.classes + i*l.n + j;
                    //inputs 的存储方式是 [l.side*l.side*l.classes][l.side*l.side*l.n][l.side*l.side*l.n*l.coords]
                    l.delta[p_index] = l.noobject_scale*(0 - l.output[p_index]);//对应论文损失的第四项。这个计算很是奇怪,论文里面还是要判断一下是否是noobj????他怎么没有判断就直接计算了
                    *(l.cost) += l.noobject_scale*pow(l.output[p_index], 2);//同上
                    avg_anyobj += l.output[p_index];
                }

                int best_index = -1;
                float best_iou = 0;
                float best_rmse = 20;

                if (!is_obj){
                    continue;
                }
                //后面都是对应于object的情况。
                int class_index = index + i*l.classes;
                for (j = 0; j < l.classes; ++j) {
                    l.delta[class_index + j] = l.class_scale * (state.truth[truth_index + 1 + j] - l.output[class_index + j]);//对应论文五项损失的第五项
                    *(l.cost) += l.class_scale * pow(state.truth[truth_index + 1 + j] - l.output[class_index + j], 2);
                    if (state.truth[truth_index + 1 + j]) avg_cat += l.output[class_index + j];//float类型做判断这样写不太好。
                    avg_allcat += l.output[class_index + j];
                }//l.classes

                box truth = float_to_box(state.truth + truth_index + 1 + l.classes);//进行了loaction 对应 truth的获取。
                truth.x /= l.side;//这个地方的计算不清楚。
                truth.y /= l.side;

                for (j = 0; j < l.n; ++j){
                    int box_index = index + locations*(l.classes + l.n) + (i*l.n + j) * l.coords;
                    box out = float_to_box(l.output + box_index);//cell里面预测的boundingbox
                    out.x /= l.side;//同上面的不清楚
                    out.y /= l.side;

                    if (l.sqrt){
                        out.w = out.w*out.w;
                        out.h = out.h*out.h;
                    }//if

                    float iou = box_iou(out, truth);//计算iou值,即out与truth的交除以他们的并。
                    //iou = 0;
                    float rmse = box_rmse(out, truth);//这个是计算boundingbox各项之间的差的平方和的开根号。
                    if (best_iou > 0 || iou > 0){
                        if (iou > best_iou){
                            best_iou = iou;
                            best_index = j;//存放cell里面最好的预测的boundingbox的idx
                        }
                    }
                    else{
                        if (rmse < best_rmse){
                            best_rmse = rmse;
                            best_index = j;
                        }
                    }//if
                }//for

                if (l.forced){//??
                    if (truth.w*truth.h < .1){
                        best_index = 1;
                    }
                    else{
                        best_index = 0;
                    }
                }

                int box_index = index + locations*(l.classes + l.n) + (i*l.n + best_index) * l.coords;
                int tbox_index = truth_index + 1 + l.classes;

                box out = float_to_box(l.output + box_index);
                out.x /= l.side;
                out.y /= l.side;
                if (l.sqrt) {
                    out.w = out.w*out.w;
                    out.h = out.h*out.h;
                }
                float iou = box_iou(out, truth);

                //printf("%d,", best_index);
                int p_index = index + locations*l.classes + i*l.n + best_index;
                //对应于论文里面的第三项。
                *(l.cost) -= l.noobject_scale * pow(l.output[p_index], 2);//这里揭开前面的问题,这里相当于已经判定是objcet,就把之前算在里面noobjcet的减掉。
                *(l.cost) += l.object_scale * pow(1 - l.output[p_index], 2);
                avg_obj += l.output[p_index];
                l.delta[p_index] = l.object_scale * (1. - l.output[p_index]);//这里就覆盖到之前被noobject赋值的数。

                if (l.rescore){
                    l.delta[p_index] = l.object_scale * (iou - l.output[p_index]);
                }
                //下面的四个对应于论文里面损失的第一项和第二项。
                l.delta[box_index + 0] = l.coord_scale*(state.truth[tbox_index + 0] - l.output[box_index + 0]);
                l.delta[box_index + 1] = l.coord_scale*(state.truth[tbox_index + 1] - l.output[box_index + 1]);
                l.delta[box_index + 2] = l.coord_scale*(state.truth[tbox_index + 2] - l.output[box_index + 2]);
                l.delta[box_index + 3] = l.coord_scale*(state.truth[tbox_index + 3] - l.output[box_index + 3]);
                if (l.sqrt){
                    l.delta[box_index + 2] = l.coord_scale*(sqrt(state.truth[tbox_index + 2]) - l.output[box_index + 2]);
                    l.delta[box_index + 3] = l.coord_scale*(sqrt(state.truth[tbox_index + 3]) - l.output[box_index + 3]);
                }

                *(l.cost) += pow(1 - iou, 2);
                avg_iou += iou;
                ++count;
            }
            if (l.softmax){
                gradient_array(l.output + index + locations*l.classes, locations*l.n*(1 + l.coords),
                    LOGISTIC, l.delta + index + locations*l.classes);
            }
        }//loaction
        printf("Detection Avg IOU: %f, Pos Cat: %f, All Cat: %f, Pos Obj: %f, Any Obj: %f, count: %d\n", avg_iou / count, avg_cat / count, avg_allcat / (count*l.classes), avg_obj / count, avg_anyobj / (l.batch*locations*l.n), count);
    }//batchsize
}