AlexeyAB / darknet

YOLOv4 / Scaled-YOLOv4 / YOLO - Neural Networks for Object Detection (Windows and Linux version of Darknet )
http://pjreddie.com/darknet/
Other
21.74k stars 7.96k forks source link

How to fill in the value of l.delta when I implement triplet loss? #2706

Open oldwolf204 opened 5 years ago

oldwolf204 commented 5 years ago

I try to write a triplet loss in DARKNET。

oldwolf204 commented 5 years ago

According to triplet loss, there is only one loss in the three sets of data, but I need to fill in 3 * L. inputs and L. delta. How should I fill in?

oldwolf204 commented 5 years ago

void forward_cost_layer(cost_layer l, network_state state) { if (state.truth) { memcpy(l.output, state.input, l.outputsl.batchsizeof(float)); int i; int j; int k; float loss = 0; float fa = calloc(l.inputs, sizeof(float)); float fb = calloc(l.inputs, sizeof(float)); float * fc = calloc(l.inputs, sizeof(float)); for (i = 0; i < l.batch / 3; i++) {

        int apos = (i * 3)*l.inputs;
        int bpos = (i * 3 + 1)*l.inputs;
        int cpos = (i * 3 + 2)*l.inputs;

        memcpy(fa, &state.input[apos], l.inputs * sizeof(float));
        memcpy(fb, &state.input[bpos], l.inputs * sizeof(float));
        memcpy(fc, &state.input[cpos], l.inputs * sizeof(float));

        //l2_normalize  output = x/sqrt(sun(max(x*x)),0,0000000001);
        float ma = 0;
        float mb = 0;
        float mc = 0;
        for (k = 0; k < l.inputs; k++)
        {
            float matmp = pow(state.input[apos + k], 2);
            ma = ma + matmp;

            float mbtmp = pow(state.input[bpos + k], 2);
            mb = mb + mbtmp;

            float mctmp = pow(state.input[cpos + k], 2);
            mc = mc + mctmp;
        }

        ma = sqrt(fmax(ma, 0.00000000001));
        mb = sqrt(fmax(mb, 0.00000000001));
        mc = sqrt(fmax(mc, 0.00000000001));

        for (k = 0; k < l.inputs; k++)
        {
            fa[k] = state.input[apos + k] / ma;
            fb[k] = state.input[bpos + k] / mb;
            fc[k] = state.input[cpos + k] / mc;
        }
        float LAB = 0 ;
        float LAC = 0;

        for (j = 0; j < l.inputs; j++)
        {
            float ab = pow(fa[j] - fb[j], 2);
            float ac = pow(fa[j] - fc[j], 2);
            LAB = LAB + ab;
            LAC = LAC + ac;
        }

        float Lfs = LAB - LAC + 0.2f;
        loss = fmax(Lfs, 0);
        if (loss>0)
        {
            for (j = 0; j < l.inputs; j++)
            {
                l.delta[apos + j] = 2 * (fc[j] - fb[j]);
                l.delta[bpos + j] = 2 * (fb[j] - fa[j]);
                l.delta[cpos + j] = 2 * (fa[j] - fc[j]);
            }
        }
        else
        {
            for (j = 0; j < l.inputs; j++)
            {
                l.delta[apos + j] = 0;
                l.delta[bpos + j] = 0;
                l.delta[cpos + j] = 0;
            }
        }

    }
    free(fa);
    free(fb);
    free(fc);
    //l.cost[0] = sum_array(l.output, l.batch*l.inputs);
    //l.cost[0] = sum_array(l.delta, l.batch*l.inputs);
    l.cost[0] = loss;
    return;
}

}

oldwolf204 commented 5 years ago

void backward_cost_layer(const cost_layer l, network_state state) { axpy_cpu(l.batch*l.inputs, 1, l.delta, 1, state.delta, 1); return; }

oldwolf204 commented 5 years ago

I wrote the above two functions, but LOSS keeps increasing, I think float Lfs = LAB - LAC + 0.2f; loss = fmax(Lfs, 0); if (loss>0) { for (j = 0; j < l.inputs; j++) { l.delta[apos + j] = 2 (fc[j] - fb[j]); l.delta[bpos + j] = 2 (fb[j] - fa[j]); l.delta[cpos + j] = 2 * (fa[j] - fc[j]); } } else { for (j = 0; j < l.inputs; j++) { l.delta[apos + j] = 0; l.delta[bpos + j] = 0; l.delta[cpos + j] = 0; } } Here's a question. What should I do?