void test_box() { test_dintersect(); test_dunion(); box a = {0, 0, 1, 1}; box dxa= {0+.00001, 0, 1, 1}; box dya= {0, 0+.00001, 1, 1}; box dwa= {0, 0, 1+.00001, 1}; box dha= {0, 0, 1, 1+.00001}; box b = {.5, 0, .2, .2}; float iou = box_iou(a,b); iou = (1-iou)*(1-iou); printf("%f\n", iou); dbox d = diou(a, b); printf("%f %f %f %f\n", d.dx, d.dy, d.dw, d.dh); float xiou = box_iou(dxa, b); float yiou = box_iou(dya, b); float wiou = box_iou(dwa, b); float hiou = box_iou(dha, b); xiou = ((1-xiou)*(1-xiou) - iou)/(.00001); yiou = ((1-yiou)*(1-yiou) - iou)/(.00001); wiou = ((1-wiou)*(1-wiou) - iou)/(.00001); hiou = ((1-hiou)*(1-hiou) - iou)/(.00001); printf("manual %f %f %f %f\n", xiou, yiou, wiou, hiou); }
void forward_detection_layer(const detection_layer l, network_state state) { int in_i = 0; int out_i = 0; int locations = get_detection_layer_locations(l); int i,j; for(i = 0; i < l.batch*locations; ++i){ int mask = (!state.truth || state.truth[out_i + (l.background || l.objectness) + l.classes + 2]); float scale = 1; if(l.joint) scale = state.input[in_i++]; else if(l.objectness){ l.output[out_i++] = 1-state.input[in_i++]; scale = mask; } else if(l.background) l.output[out_i++] = scale*state.input[in_i++]; for(j = 0; j < l.classes; ++j){ l.output[out_i++] = scale*state.input[in_i++]; } if(l.objectness){ }else if(l.background){ softmax_array(l.output + out_i - l.classes-l.background, l.classes+l.background, l.output + out_i - l.classes-l.background); activate_array(state.input+in_i, l.coords, LOGISTIC); } for(j = 0; j < l.coords; ++j){ l.output[out_i++] = mask*state.input[in_i++]; } } float avg_iou = 0; int count = 0; if(l.does_cost && state.train){ *(l.cost) = 0; int size = get_detection_layer_output_size(l) * l.batch; memset(l.delta, 0, size * sizeof(float)); for (i = 0; i < l.batch*locations; ++i) { int classes = l.objectness+l.classes; int offset = i*(classes+l.coords); for (j = offset; j < offset+classes; ++j) { *(l.cost) += pow(state.truth[j] - l.output[j], 2); l.delta[j] = state.truth[j] - l.output[j]; } box truth; truth.x = state.truth[j+0]/7; truth.y = state.truth[j+1]/7; truth.w = pow(state.truth[j+2], 2); truth.h = pow(state.truth[j+3], 2); box out; out.x = l.output[j+0]/7; out.y = l.output[j+1]/7; out.w = pow(l.output[j+2], 2); out.h = pow(l.output[j+3], 2); if(!(truth.w*truth.h)) continue; float iou = box_iou(out, truth); avg_iou += iou; ++count; dbox delta = diou(out, truth); l.delta[j+0] = 10 * delta.dx/7; l.delta[j+1] = 10 * delta.dy/7; l.delta[j+2] = 10 * delta.dw * 2 * sqrt(out.w); l.delta[j+3] = 10 * delta.dh * 2 * sqrt(out.h); *(l.cost) += pow((1-iou), 2); l.delta[j+0] = 4 * (state.truth[j+0] - l.output[j+0]); l.delta[j+1] = 4 * (state.truth[j+1] - l.output[j+1]); l.delta[j+2] = 4 * (state.truth[j+2] - l.output[j+2]); l.delta[j+3] = 4 * (state.truth[j+3] - l.output[j+3]); if(l.rescore){ for (j = offset; j < offset+classes; ++j) { if(state.truth[j]) state.truth[j] = iou; l.delta[j] = state.truth[j] - l.output[j]; } } } printf("Avg IOU: %f\n", avg_iou/count); } }