| | |
| | | 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); |
| | | } |
| | |
| | | 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)); |
| | |
| | | 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){ |
| | |
| | | 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; |
| | |
| | | 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); |
| | | } |
| | | } |
| | | } |
| | |
| | | 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)); |
| | | |
| | |
| | | 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)); |
| | |
| | | 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); |
| | | } |
| | |
| | | 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); |
| | |
| | | } |
| | | } |
| | | } |
| | | */ |
| | | } |
| | | |
| | | 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++]; |
| | | } |
| | |
| | | int coords; |
| | | int background; |
| | | int rescore; |
| | | int nuisance; |
| | | float *output; |
| | | float *delta; |
| | | #ifdef GPU |
| | |
| | | #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); |
| | |
| | | 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; |
| | | } |
| | |
| | | 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"); |
| | | } |
| | | |
| | |
| | | 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; |
| | | } |
| | | |
| | |
| | | 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(); |