Joseph Redmon
2015-09-22 fed6d6e31d819ecae21e9c3b9315b5877deb3f42
idk man
11 files modified
157 ■■■■■ changed files
Makefile 8 ●●●● patch | view | raw | blame | history
cfg/darknet.cfg 10 ●●●● patch | view | raw | blame | history
cfg/yolo.cfg 1 ●●●● patch | view | raw | blame | history
src/box.c 10 ●●●●● patch | view | raw | blame | history
src/box.h 1 ●●●● patch | view | raw | blame | history
src/compare.c 12 ●●●●● patch | view | raw | blame | history
src/layer.h 5 ●●●●● patch | view | raw | blame | history
src/parser.c 12 ●●●●● patch | view | raw | blame | history
src/region_layer.c 94 ●●●● patch | view | raw | blame | history
src/stb_image_write.h 2 ●●● patch | view | raw | blame | history
src/swag.c 2 ●●● patch | view | raw | blame | history
Makefile
@@ -1,5 +1,5 @@
GPU=1
OPENCV=1
GPU=0
OPENCV=0
DEBUG=0
ARCH= --gpu-architecture=compute_20 --gpu-code=compute_20
@@ -12,7 +12,7 @@
NVCC=nvcc
OPTS=-Ofast
LDFLAGS= -lm -pthread -lstdc++ 
COMMON= -I/usr/local/cuda/include/
COMMON=
CFLAGS=-Wall -Wfatal-errors 
ifeq ($(DEBUG), 1) 
@@ -29,7 +29,7 @@
endif
ifeq ($(GPU), 1) 
COMMON+= -DGPU
COMMON+= -DGPU -I/usr/local/cuda/include/
CFLAGS+= -DGPU
LDFLAGS+= -L/usr/local/cuda/lib64 -lcuda -lcudart -lcublas -lcurand
endif
cfg/darknet.cfg
@@ -1,5 +1,5 @@
[net]
batch=256
batch=128
subdivisions=1
height=256
width=256
@@ -8,10 +8,10 @@
decay=0.0005
learning_rate=0.01
policy=step
scale=.1
step=100000
max_batches=400000
policy=sigmoid
gamma=.00002
step=400000
max_batches=800000
[crop]
crop_height=224
cfg/yolo.cfg
@@ -206,4 +206,5 @@
rescore=0
joint=0
objectness=1
background=0
src/box.c
@@ -2,6 +2,16 @@
#include <stdio.h>
#include <math.h>
box float_to_box(float *f)
{
    box b;
    b.x = f[0];
    b.y = f[1];
    b.w = f[2];
    b.h = f[3];
    return b;
}
dbox derivative(box a, box b)
{
    dbox d;
src/box.h
@@ -9,6 +9,7 @@
    float dx, dy, dw, dh;
} dbox;
box float_to_box(float *f);
float box_iou(box a, box b);
float box_rmse(box a, box b);
dbox diou(box a, box b);
src/compare.c
@@ -241,7 +241,8 @@
    srand(time(0));
    set_batch_network(&net, 1);
    list *plist = get_paths("data/compare.sort.list");
    //list *plist = get_paths("data/compare.sort.list");
    list *plist = get_paths("data/compare.cat.list");
    //list *plist = get_paths("data/compare.val.old");
    char **paths = (char **)list_to_array(plist);
    int N = plist->size;
@@ -256,15 +257,16 @@
    }
    int round;
    clock_t time=clock();
    for(round = 1; round <= 40; ++round){
    for(round = 1; round <= 500; ++round){
        clock_t round_time=clock();
        printf("Round: %d\n", round);
        qsort(boxes, N, sizeof(sortable_bbox), elo_comparator);
        sorta_shuffle(boxes, N, sizeof(sortable_bbox), 10);
        shuffle(boxes, N, sizeof(sortable_bbox));
        for(i = 0; i < N/2; ++i){
            bbox_fight(boxes+i*2, boxes+i*2+1);
        }
        if(round >= 4){
        if(round >= 4 && 0){
            qsort(boxes, N, sizeof(sortable_bbox), elo_comparator);
            if(round == 4){
                N = N/2;
@@ -275,9 +277,11 @@
        printf("Round: %f secs, %d remaining\n", sec(clock()-round_time), N);
    }
    qsort(boxes, N, sizeof(sortable_bbox), elo_comparator);
    FILE *outfp = fopen("results/battle.log", "w");
    for(i = 0; i < N; ++i){
        printf("%s %f\n", boxes[i].filename, boxes[i].elo);
        fprintf(outfp, "%s %f\n", boxes[i].filename, boxes[i].elo);
    }
    fclose(outfp);
    printf("Tournament in %d compares, %f secs\n", total_compares, sec(clock()-time));
}
src/layer.h
@@ -60,6 +60,11 @@
    float beta;
    float kappa;
    float coord_scale;
    float object_scale;
    float noobject_scale;
    float class_scale;
    int dontload;
    float probability;
src/parser.c
@@ -182,10 +182,14 @@
    int num = option_find_int(options, "num", 1);
    int side = option_find_int(options, "side", 7);
    region_layer layer = make_region_layer(params.batch, params.inputs, num, side, classes, coords, rescore);
    int softmax = option_find_int(options, "softmax", 0);
    int sqrt = option_find_int(options, "sqrt", 0);
    layer.softmax = softmax;
    layer.sqrt = sqrt;
    layer.softmax = option_find_int(options, "softmax", 0);
    layer.sqrt = option_find_int(options, "sqrt", 0);
    layer.coord_scale = option_find_float(options, "coord_scale", 1);
    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);
    return layer;
}
src/region_layer.c
@@ -44,15 +44,20 @@
    int locations = l.side*l.side;
    int i,j;
    memcpy(l.output, state.input, l.outputs*l.batch*sizeof(float));
    for(i = 0; i < l.batch*locations; ++i){
        int index = i*((1+l.coords)*l.n + l.classes);
    int b;
        if(l.softmax){
            activate_array(l.output + index, l.n*(1+l.coords), LOGISTIC);
            int offset = l.n*(1+l.coords);
        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,
                    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;
@@ -62,46 +67,41 @@
        *(l.cost) = 0;
        int size = l.inputs * l.batch;
        memset(l.delta, 0, size * sizeof(float));
        for (i = 0; i < l.batch*locations; ++i) {
            int index = i*((1+l.coords)*l.n + l.classes);
        for (b = 0; b < l.batch; ++b){
            int index = b*l.inputs;
            for (i = 0; i < locations; ++i) {
                int truth_index = (b*locations + i)*(1+l.coords+l.classes);
                int is_obj = state.truth[truth_index];
            for(j = 0; j < l.n; ++j){
                int prob_index = index + j*(1 + l.coords);
                l.delta[prob_index] = (1./l.n)*(0-l.output[prob_index]);
                if(l.softmax){
                    l.delta[prob_index] = 1./(l.n*l.side)*(0-l.output[prob_index]);
                }
                *(l.cost) += (1./l.n)*pow(l.output[prob_index], 2);
                //printf("%f\n", l.output[prob_index]);
                avg_anyobj += l.output[prob_index];
                    int p_index = index + locations*l.classes + i*l.n + j;
                    l.delta[p_index] = l.noobject_scale*(0 - l.output[p_index]);
                    *(l.cost) += l.noobject_scale*pow(l.output[p_index], 2);
                    avg_anyobj += l.output[p_index];
            }
            int truth_index = i*(1 + l.coords + l.classes);
            int best_index = -1;
            float best_iou = 0;
            float best_rmse = 4;
            int bg = !state.truth[truth_index];
            if(bg) {
                continue;
            }
                if (!is_obj) continue;
            int class_index = index + l.n*(1+l.coords);
                int class_index = index + i*l.classes;
            for(j = 0; j < l.classes; ++j) {
                l.delta[class_index+j] = state.truth[truth_index+1+j] - l.output[class_index+j];
                *(l.cost) += pow(state.truth[truth_index+1+j] - l.output[class_index+j], 2);
                    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];
            }
            truth_index += l.classes + 1;
            box truth = {state.truth[truth_index+0], state.truth[truth_index+1], state.truth[truth_index+2], state.truth[truth_index+3]};
                box truth = float_to_box(state.truth + truth_index + 1 + l.classes);
            truth.x /= l.side;
            truth.y /= l.side;
            for(j = 0; j < l.n; ++j){
                int out_index = index + j*(1+l.coords);
                box out = {l.output[out_index+1], l.output[out_index+2], l.output[out_index+3], l.output[out_index+4]};
                    int box_index = index + locations*(l.classes + l.n) + (i*l.n + j) * l.coords;
                    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;
@@ -121,35 +121,37 @@
                    }
                }
            }
            //printf("%d", best_index);
            int in_index = index + best_index*(1+l.coords);
            *(l.cost) -= pow(l.output[in_index], 2);
            *(l.cost) += pow(1-l.output[in_index], 2);
            avg_obj += l.output[in_index];
            l.delta[in_index+0] = (1.-l.output[in_index]);
            if(l.softmax){
                l.delta[in_index+0] = 5*(1.-l.output[in_index]);
            }
            //printf("%f\n", l.output[in_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+0] = l.object_scale * (1.-l.output[p_index]);
            l.delta[in_index+1] = 5*(state.truth[truth_index+0] - l.output[in_index+1]);
            l.delta[in_index+2] = 5*(state.truth[truth_index+1] - l.output[in_index+2]);
                if(l.rescore){
                    l.delta[p_index+0] = l.object_scale * (best_iou - l.output[p_index]);
                }
                int box_index = index + locations*(l.classes + l.n) + (i*l.n + best_index) * l.coords;
                int tbox_index = truth_index + 1 + l.classes;
                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[in_index+3] = 5*(sqrt(state.truth[truth_index+2]) - l.output[in_index+3]);
                l.delta[in_index+4] = 5*(sqrt(state.truth[truth_index+3]) - l.output[in_index+4]);
            }else{
                l.delta[in_index+3] = 5*(state.truth[truth_index+2] - l.output[in_index+3]);
                l.delta[in_index+4] = 5*(state.truth[truth_index+3] - l.output[in_index+4]);
                    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-best_iou, 2);
            avg_iou += best_iou;
            ++count;
            }
            if(l.softmax){
                gradient_array(l.output + index, l.n*(1+l.coords), LOGISTIC, l.delta + index);
                gradient_array(l.output + index + locations*l.classes, locations*l.n*(1+l.coords),
                        LOGISTIC, l.delta + index + locations*l.classes);
            }
        }
        printf("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, 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);
    }
}
src/stb_image_write.h
@@ -279,7 +279,7 @@
{
   unsigned char scanlineheader[4] = { 2, 2, 0, 0 };
   unsigned char rgbe[4];
   float linear[3];
   float linear[3] = {0};
   int x;
   scanlineheader[2] = (width&0xff00)>>8;
src/swag.c
@@ -77,7 +77,7 @@
    int classes = l.classes;
    list *plist = get_paths(train_images);
    int N = plist->size;
    //int N = plist->size;
    char **paths = (char **)list_to_array(plist);
    load_args args = {0};