Added Focal Loss to yolo-layer

pull/604/merge
AlexeyAB 7 years ago
parent be9d971ddb
commit 943f6e874b
  1. 4
      src/network.c
  2. 16
      src/network_kernels.cu
  3. 1
      src/parser.c
  4. 40
      src/yolo_layer.c
  5. 1
      src/yolo_v2_class.cpp

@ -757,7 +757,7 @@ void fuse_conv_batchnorm(network net)
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 @@ void fuse_conv_batchnorm(network net)
}
}
else {
printf(" Skip layer: %d \n", l->type);
//printf(" Fusion skip layer type: %d \n", l->type);
}
}
}

@ -39,6 +39,7 @@ extern "C" {
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 @@ void forward_network_gpu(network net, network_state state)
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();
}
*/
}
}

@ -274,6 +274,7 @@ layer parse_yolo(list *options, size_params params)
//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);

@ -109,18 +109,40 @@ float delta_yolo_box(box truth, float *x, float *biases, int n, int index, int i
}
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;
}
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];
}
// 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_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];
}
}
}
static int entry_index(layer l, int batch, int location, int entry)
@ -196,7 +218,7 @@ void forward_yolo_layer(const layer l, network_state state)
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 @@ void forward_yolo_layer(const layer l, network_state state)
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;

@ -69,6 +69,7 @@ YOLODLL_API Detector::Detector(std::string cfg_filename, std::string weight_file
}
set_batch_network(&net, 1);
net.gpu_index = cur_gpu_id;
fuse_conv_batchnorm(net);
layer l = net.layers[net.n - 1];
int j;

Loading…
Cancel
Save