AlexeyAB
2018-04-05 943f6e874b819271a87665cf41199388380989a0
Added Focal Loss to yolo-layer
5 files modified
58 ■■■■ changed files
src/network.c 4 ●●●● patch | view | raw | blame | history
src/network_kernels.cu 16 ●●●●● patch | view | raw | blame | history
src/parser.c 1 ●●●● patch | view | raw | blame | history
src/yolo_layer.c 36 ●●●● patch | view | raw | blame | history
src/yolo_v2_class.cpp 1 ●●●● patch | view | raw | blame | history
src/network.c
@@ -757,7 +757,7 @@
        layer *l = &net.layers[j];
        if (l->type == CONVOLUTIONAL) {
            printf(" Fuse Convolutional layer \t\t l->size = %d  \n", l->size);
            //printf(" Merges Convolutional-%d and batch_norm \n", j);
            if (l->batch_normalize) {
                int f;
@@ -783,7 +783,7 @@
            }
        }
        else {
            printf(" Skip layer: %d \n", l->type);
            //printf(" Fusion skip layer type: %d \n", l->type);
        }
    }
}
src/network_kernels.cu
@@ -39,6 +39,7 @@
float * get_network_output_gpu_layer(network net, int i);
float * get_network_delta_gpu_layer(network net, int i);
float * get_network_output_gpu(network net);
#include "opencv2/highgui/highgui_c.h"
void forward_network_gpu(network net, network_state state)
{
@@ -54,6 +55,21 @@
        if(net.wait_stream)
            cudaStreamSynchronize(get_cuda_stream());
        state.input = l.output_gpu;
/*
        cuda_pull_array(l.output_gpu, l.output, l.batch*l.outputs);
        if (l.out_w >= 0 && l.out_h >= 1 && l.c >= 3) {
            int j;
            for (j = 0; j < l.out_c; ++j) {
                image img = make_image(l.out_w, l.out_h, 3);
                memcpy(img.data, l.output+ l.out_w*l.out_h*j, l.out_w*l.out_h * 1 * sizeof(float));
                char buff[256];
                sprintf(buff, "layer-%d slice-%d", i, j);
                show_image(img, buff);
            }
            cvWaitKey(0); // wait press-key in console
            cvDestroyAllWindows();
        }
*/
    }
}
src/parser.c
@@ -274,6 +274,7 @@
    //l.max_boxes = option_find_int_quiet(options, "max", 90);
    l.jitter = option_find_float(options, "jitter", .2);
    l.focal_loss = option_find_int_quiet(options, "focal_loss", 0);
    l.ignore_thresh = option_find_float(options, "ignore_thresh", .5);
    l.truth_thresh = option_find_float(options, "truth_thresh", 1);
src/yolo_layer.c
@@ -109,17 +109,39 @@
}
void delta_yolo_class(float *output, float *delta, int index, int class, int classes, int stride, float *avg_cat)
void delta_yolo_class(float *output, float *delta, int index, int class_id, int classes, int stride, float *avg_cat, int focal_loss)
{
    int n;
    if (delta[index]){
        delta[index + stride*class] = 1 - output[index + stride*class];
        if(avg_cat) *avg_cat += output[index + stride*class];
        delta[index + stride*class_id] = 1 - output[index + stride*class_id];
        if(avg_cat) *avg_cat += output[index + stride*class_id];
        return;
    }
    // Focal loss
    if (focal_loss) {
        // Focal Loss
        float alpha = 0.5;  // 0.25 or 0.5
                            //float gamma = 2;  // hardcoded in many places of the grad-formula
        int ti = index + stride*class_id;
        float pt = output[ti] + 0.000000000000001F;
        //float grad = -(1 - pt) * (2 * pt*logf(pt) + pt - 1);  // http://blog.csdn.net/linmingan/article/details/77885832
        float grad = (1 - pt) * (2 * pt*logf(pt) + pt - 1);     // https://github.com/unsky/focal-loss
    for(n = 0; n < classes; ++n){
        delta[index + stride*n] = ((n == class)?1 : 0) - output[index + stride*n];
        if(n == class && avg_cat) *avg_cat += output[index + stride*n];
            delta[index + stride*n] = (((n == class_id) ? 1 : 0) - output[index + stride*n]);
            delta[index + stride*n] *= alpha*grad;
            if (n == class_id) *avg_cat += output[index + stride*n];
        }
    }
    else {
        // default
        for (n = 0; n < classes; ++n) {
            delta[index + stride*n] = ((n == class_id) ? 1 : 0) - output[index + stride*n];
            if (n == class_id && avg_cat) *avg_cat += output[index + stride*n];
        }
    }
}
@@ -196,7 +218,7 @@
                        int class = state.truth[best_t*(4 + 1) + b*l.truths + 4];
                        if (l.map) class = l.map[class];
                        int class_index = entry_index(l, b, n*l.w*l.h + j*l.w + i, 4 + 1);
                        delta_yolo_class(l.output, l.delta, class_index, class, l.classes, l.w*l.h, 0);
                        delta_yolo_class(l.output, l.delta, class_index, class, l.classes, l.w*l.h, 0, l.focal_loss);
                        box truth = float_to_box_stride(state.truth + best_t*(4 + 1) + b*l.truths, 1);
                        delta_yolo_box(truth, l.output, l.biases, l.mask[n], box_index, i, j, l.w, l.h, state.net.w, state.net.h, l.delta, (2-truth.w*truth.h), l.w*l.h);
                    }
@@ -236,7 +258,7 @@
                int class = state.truth[t*(4 + 1) + b*l.truths + 4];
                if (l.map) class = l.map[class];
                int class_index = entry_index(l, b, mask_n*l.w*l.h + j*l.w + i, 4 + 1);
                delta_yolo_class(l.output, l.delta, class_index, class, l.classes, l.w*l.h, &avg_cat);
                delta_yolo_class(l.output, l.delta, class_index, class, l.classes, l.w*l.h, &avg_cat, l.focal_loss);
                ++count;
                ++class_count;
src/yolo_v2_class.cpp
@@ -69,6 +69,7 @@
    }
    set_batch_network(&net, 1);
    net.gpu_index = cur_gpu_id;
    fuse_conv_batchnorm(net);
    layer l = net.layers[net.n - 1];
    int j;