Joseph Redmon
2015-11-04 8fd18add6e060a433629fae3fa2a7ef75df4644e
src/region_layer.c
@@ -57,10 +57,33 @@
            activate_array(l.output + index + offset, locations*l.n*(1+l.coords), LOGISTIC);
        }
    }
    if (l.object_logistic) {
        for(b = 0; b < l.batch; ++b){
            int index = b*l.inputs;
            int p_index = index + locations*l.classes;
            activate_array(l.output + p_index, locations*l.n, LOGISTIC);
        }
    }
    if (l.coord_logistic) {
        for(b = 0; b < l.batch; ++b){
            int index = b*l.inputs;
            int coord_index = index + locations*(l.classes + l.n);
            activate_array(l.output + coord_index, locations*l.n*l.coords, LOGISTIC);
        }
    }
    if (l.class_logistic) {
        for(b = 0; b < l.batch; ++b){
            int class_index = b*l.inputs;
            activate_array(l.output + class_index, locations*l.classes, 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;
@@ -81,15 +104,18 @@
                int best_index = -1;
                float best_iou = 0;
                float best_rmse = 4;
                float best_rmse = 20;
                if (!is_obj) continue;
                if (!is_obj){
                    continue;
                }
                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];
                    avg_allcat += l.output[class_index+j];
                }
                box truth = float_to_box(state.truth + truth_index + 1 + l.classes);
@@ -108,6 +134,7 @@
                    }
                    float iou  = box_iou(out, truth);
                    //iou = 0;
                    float rmse = box_rmse(out, truth);
                    if(best_iou > 0 || iou > 0){
                        if(iou > best_iou){
@@ -121,18 +148,38 @@
                        }
                    }
                }
                int p_index = index + locations*l.classes + i*l.n + best_index;
                *(l.cost) -= l.noobject_scale * pow(l.output[p_index], 2);
                *(l.cost) += l.object_scale * pow(1-l.output[p_index], 2);
                avg_obj += l.output[p_index];
                l.delta[p_index+0] = l.object_scale * (1.-l.output[p_index]);
                if(l.rescore){
                    l.delta[p_index+0] = l.object_scale * (best_iou - l.output[p_index]);
                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);
                *(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]);
                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]);
@@ -142,16 +189,31 @@
                    l.delta[box_index+3] = l.coord_scale*(sqrt(state.truth[tbox_index + 3]) - l.output[box_index + 3]);
                }
                *(l.cost) += pow(1-best_iou, 2);
                avg_iou += best_iou;
                *(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);
            }
            if (l.object_logistic) {
                int p_index = index + locations*l.classes;
                gradient_array(l.output + p_index, locations*l.n, LOGISTIC, l.delta + p_index);
            }
            if (l.class_logistic) {
                int class_index = index;
                gradient_array(l.output + class_index, locations*l.classes, LOGISTIC, l.delta + class_index);
            }
            if (l.coord_logistic) {
                    int coord_index = index + locations*(l.classes + l.n);
                gradient_array(l.output + coord_index, locations*l.n*l.coords, LOGISTIC, l.delta + coord_index);
            }
            //printf("\n");
        }
        printf("Region Avg IOU: %f, Avg Cat Pred: %f, Avg Obj: %f, Avg Any: %f, count: %d\n", avg_iou/count, avg_cat/count, avg_obj/count, avg_anyobj/(l.batch*locations*l.n), count);
        printf("Region 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);
    }
}
@@ -164,6 +226,11 @@
void forward_region_layer_gpu(const region_layer l, network_state state)
{
    if(!state.train){
        copy_ongpu(l.batch*l.inputs, state.input, 1, l.output_gpu, 1);
        return;
    }
    float *in_cpu = calloc(l.batch*l.inputs, sizeof(float));
    float *truth_cpu = 0;
    if(state.truth){