diff --git a/src/data.c b/src/data.c index 8dd7d9a6..70692487 100644 --- a/src/data.c +++ b/src/data.c @@ -137,18 +137,20 @@ void fill_truth_detection(char *path, float *truth, int classes, int height, int 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); } diff --git a/src/detection.c b/src/detection.c index 522a3219..1800ca6c 100644 --- a/src/detection.c +++ b/src/detection.c @@ -50,7 +50,7 @@ void train_detection(char *cfgfile, char *weightfile) 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 @@ void train_detection(char *cfgfile, char *weightfile) 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 @@ void validate_detection(char *cfgfile, char *weightfile) 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 @@ void validate_detection(char *cfgfile, char *weightfile) 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); } } } diff --git a/src/detection_layer.c b/src/detection_layer.c index 0a754fd4..27a4daf3 100644 --- a/src/detection_layer.c +++ b/src/detection_layer.c @@ -16,7 +16,7 @@ int get_detection_layer_output_size(detection_layer layer) 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 @@ detection_layer *make_detection_layer(int batch, int inputs, int classes, int co 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 @@ void forward_detection_layer(const detection_layer layer, network_state state) 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 @@ void forward_detection_layer(const detection_layer layer, network_state state) 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 forward_detection_layer(const detection_layer layer, network_state state) } } } + */ } void backward_detection_layer(const detection_layer layer, network_state state) @@ -107,13 +116,15 @@ void backward_detection_layer(const detection_layer layer, network_state state) 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++]; } diff --git a/src/detection_layer.h b/src/detection_layer.h index 2ad1ef23..7be576e8 100644 --- a/src/detection_layer.h +++ b/src/detection_layer.h @@ -10,6 +10,7 @@ typedef struct { int coords; int background; int rescore; + int nuisance; float *output; float *delta; #ifdef GPU @@ -18,7 +19,7 @@ typedef struct { #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); diff --git a/src/parser.c b/src/parser.c index 6ff978c7..e4ee17e4 100644 --- a/src/parser.c +++ b/src/parser.c @@ -165,8 +165,9 @@ 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", 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_softmax_cfg(FILE *fp, softmax_layer *l, network net, int count) 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"); } diff --git a/src/utils.c b/src/utils.c index 6fb0e439..bc39cecc 100644 --- a/src/utils.c +++ b/src/utils.c @@ -276,10 +276,10 @@ float variance_array(float *a, int n) 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; } diff --git a/src/utils.h b/src/utils.h index 4c6b2a9f..578abc37 100644 --- a/src/utils.h +++ b/src/utils.h @@ -26,7 +26,7 @@ void normalize_array(float *a, int n); 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();