Joseph Redmon
2015-03-24 56b6561ae4f1e238ba1a65701f91b40636037cc2
stuff changed probably
7 files modified
68 ■■■■■ changed files
src/data.c 12 ●●●●● patch | view | raw | blame | history
src/detection.c 19 ●●●●● patch | view | raw | blame | history
src/detection_layer.c 21 ●●●● patch | view | raw | blame | history
src/detection_layer.h 3 ●●●● patch | view | raw | blame | history
src/parser.c 5 ●●●●● patch | view | raw | blame | history
src/utils.c 6 ●●●● patch | view | raw | blame | history
src/utils.h 2 ●●● patch | view | raw | blame | history
src/data.c
@@ -137,18 +137,20 @@
        if(j < 0) j = 0;
        if(j >= num_height) j = num_height-1;
        
        float dw = (x - i*box_width)/box_width;
        float dh = (y - j*box_height)/box_height;
        float dw = constrain(0,1, (x - i*box_width)/box_width );
        float dh = constrain(0,1, (y - j*box_height)/box_height );
        float th = constrain(0,1, h*(height+jitter)/height );
        float tw = constrain(0,1, w*(width+jitter)/width );
        int index = (i+j*num_width)*(4+classes+background);
        if(truth[index+classes+background]) continue;
        if(truth[index+classes+background+2]) continue;
        if(background) truth[index++] = 0;
        truth[index+id] = 1;
        index += classes;
        truth[index++] = dh;
        truth[index++] = dw;
        truth[index++] = h*(height+jitter)/height;
        truth[index++] = w*(width+jitter)/width;
        truth[index++] = th;
        truth[index++] = tw;
    }
    free(boxes);
}
src/detection.c
@@ -50,7 +50,7 @@
    if(weightfile){
        load_weights(&net, weightfile);
    }
    net.seen = 0;
    //net.seen = 0;
    printf("Learning Rate: %g, Momentum: %g, Decay: %g\n", net.learning_rate, net.momentum, net.decay);
    int imgs = 128;
    srand(time(0));
@@ -63,7 +63,7 @@
    int im_dim = 512;
    int jitter = 64;
    int classes = 20;
    int background = 0;
    int background = 1;
    pthread_t load_thread = load_data_detection_thread(imgs, paths, plist->size, classes, im_dim, im_dim, 7, 7, jitter, background, &buffer);
    clock_t time;
    while(1){
@@ -109,8 +109,9 @@
    char **paths = (char **)list_to_array(plist);
    int im_size = 448;
    int classes = 20;
    int background = 0;
    int num_output = 7*7*(4+classes+background);
    int background = 1;
    int nuisance = 0;
    int num_output = 7*7*(4+classes+background+nuisance);
    int m = plist->size;
    int i = 0;
@@ -134,17 +135,19 @@
        matrix pred = network_predict_data(net, val);
        int j, k, class;
        for(j = 0; j < pred.rows; ++j){
            for(k = 0; k < pred.cols; k += classes+4+background){
            for(k = 0; k < pred.cols; k += classes+4+background+nuisance){
                float scale = 1.;
                if(nuisance) scale = pred.vals[j][k];
                for(class = 0; class < classes; ++class){
                    int index = (k)/(classes+4+background);
                    int index = (k)/(classes+4+background+nuisance);
                    int r = index/7;
                    int c = index%7;
                    int ci = k+classes+background;
                    int ci = k+classes+background+nuisance;
                    float y = (r + pred.vals[j][ci + 0])/7.;
                    float x = (c + pred.vals[j][ci + 1])/7.;
                    float h = pred.vals[j][ci + 2];
                    float w = pred.vals[j][ci + 3];
                    printf("%d %d %f %f %f %f %f\n", (i-1)*m/splits + j, class, pred.vals[j][k+class+background], y, x, h, w);
                    printf("%d %d %f %f %f %f %f\n", (i-1)*m/splits + j, class, scale*pred.vals[j][k+class+background+nuisance], y, x, h, w);
                }
            }
        }
src/detection_layer.c
@@ -16,7 +16,7 @@
    return get_detection_layer_locations(layer)*(layer.background + layer.classes + layer.coords);
}
detection_layer *make_detection_layer(int batch, int inputs, int classes, int coords, int rescore, int background)
detection_layer *make_detection_layer(int batch, int inputs, int classes, int coords, int rescore, int background, int nuisance)
{
    detection_layer *layer = calloc(1, sizeof(detection_layer));
    
@@ -25,6 +25,7 @@
    layer->classes = classes;
    layer->coords = coords;
    layer->rescore = rescore;
    layer->nuisance = nuisance;
    layer->background = background;
    int outputs = get_detection_layer_output_size(*layer);
    layer->output = calloc(batch*outputs, sizeof(float));
@@ -72,12 +73,18 @@
        int mask = (!state.truth || state.truth[out_i + layer.background + layer.classes + 2]);
        float scale = 1;
        if(layer.rescore) scale = state.input[in_i++];
        if(layer.background) layer.output[out_i++] = scale*state.input[in_i++];
        else if(layer.nuisance){
            layer.output[out_i++] = 1-state.input[in_i++];
            scale = mask;
        }
        else if(layer.background) layer.output[out_i++] = scale*state.input[in_i++];
        for(j = 0; j < layer.classes; ++j){
            layer.output[out_i++] = scale*state.input[in_i++];
        }
        if(layer.background){
        if(layer.nuisance){
        }else if(layer.background){
            softmax_array(layer.output + out_i - layer.classes-layer.background, layer.classes+layer.background, layer.output + out_i - layer.classes-layer.background);
            activate_array(state.input+in_i, layer.coords, LOGISTIC);
        }
@@ -85,6 +92,7 @@
            layer.output[out_i++] = mask*state.input[in_i++];
        }
    }
    /*
    if(layer.background || 1){
        for(i = 0; i < layer.batch*locations; ++i){
            int index = i*(layer.classes+layer.coords+layer.background);
@@ -95,6 +103,7 @@
            }
        }
    }
    */
}
void backward_detection_layer(const detection_layer layer, network_state state)
@@ -107,13 +116,15 @@
        float scale = 1;
        float latent_delta = 0;
        if(layer.rescore) scale = state.input[in_i++];
        if(layer.background) state.delta[in_i++] = scale*layer.delta[out_i++];
        else if (layer.nuisance)   state.delta[in_i++] = -layer.delta[out_i++];
        else if (layer.background) state.delta[in_i++] = scale*layer.delta[out_i++];
        for(j = 0; j < layer.classes; ++j){
            latent_delta += state.input[in_i]*layer.delta[out_i];
            state.delta[in_i++] = scale*layer.delta[out_i++];
        }
        if (layer.background) gradient_array(layer.output + out_i, layer.coords, LOGISTIC, layer.delta + out_i);
        if (layer.nuisance) ;
        else if (layer.background) gradient_array(layer.output + out_i, layer.coords, LOGISTIC, layer.delta + out_i);
        for(j = 0; j < layer.coords; ++j){
            state.delta[in_i++] = layer.delta[out_i++];
        }
src/detection_layer.h
@@ -10,6 +10,7 @@
    int coords;
    int background;
    int rescore;
    int nuisance;
    float *output;
    float *delta;
    #ifdef GPU
@@ -18,7 +19,7 @@
    #endif
} detection_layer;
detection_layer *make_detection_layer(int batch, int inputs, int classes, int coords, int rescore, int background);
detection_layer *make_detection_layer(int batch, int inputs, int classes, int coords, int rescore, int background, int nuisance);
void forward_detection_layer(const detection_layer layer, network_state state);
void backward_detection_layer(const detection_layer layer, network_state state);
int get_detection_layer_output_size(detection_layer layer);
src/parser.c
@@ -165,8 +165,9 @@
    int coords = option_find_int(options, "coords", 1);
    int classes = option_find_int(options, "classes", 1);
    int rescore = option_find_int(options, "rescore", 1);
    int nuisance = option_find_int(options, "nuisance", 0);
    int background = option_find_int(options, "background", 1);
    detection_layer *layer = make_detection_layer(params.batch, params.inputs, classes, coords, rescore, background);
    detection_layer *layer = make_detection_layer(params.batch, params.inputs, classes, coords, rescore, background, nuisance);
    option_unused(options);
    return layer;
}
@@ -550,7 +551,7 @@
void print_detection_cfg(FILE *fp, detection_layer *l, network net, int count)
{
    fprintf(fp, "[detection]\n");
    fprintf(fp, "classes=%d\ncoords=%d\nrescore=%d\n", l->classes, l->coords, l->rescore);
    fprintf(fp, "classes=%d\ncoords=%d\nrescore=%d\nnuisance=%d\n", l->classes, l->coords, l->rescore, l->nuisance);
    fprintf(fp, "\n");
}
src/utils.c
@@ -276,10 +276,10 @@
    return variance;
}
float constrain(float a, float max)
float constrain(float min, float max, float a)
{
    if(a > abs(max)) return abs(max);
    if(a < -abs(max)) return -abs(max);
    if (a < min) return min;
    if (a > max) return max;
    return a;
}
src/utils.h
@@ -26,7 +26,7 @@
void scale_array(float *a, int n, float s);
void translate_array(float *a, int n, float s);
int max_index(float *a, int n);
float constrain(float a, float max);
float constrain(float min, float max, float a);
float mse_array(float *a, int n);
float rand_normal();
float rand_uniform();