From 943f6e874b819271a87665cf41199388380989a0 Mon Sep 17 00:00:00 2001
From: AlexeyAB <alexeyab84@gmail.com>
Date: Thu, 05 Apr 2018 20:27:02 +0000
Subject: [PATCH] Added Focal Loss to yolo-layer

---
 src/network.c          |    4 +-
 src/yolo_v2_class.cpp  |    1 
 src/network_kernels.cu |   16 ++++++++
 src/parser.c           |    1 
 src/yolo_layer.c       |   40 +++++++++++++++----
 5 files changed, 51 insertions(+), 11 deletions(-)

diff --git a/src/network.c b/src/network.c
index d532b85..438829a 100644
--- a/src/network.c
+++ b/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);
 		}
 	}
 }
diff --git a/src/network_kernels.cu b/src/network_kernels.cu
index d6bb294..2e2335d 100644
--- a/src/network_kernels.cu
+++ b/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();
+		}
+*/
     }
 }
 
diff --git a/src/parser.c b/src/parser.c
index 4de8aeb..651671b 100644
--- a/src/parser.c
+++ b/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);
diff --git a/src/yolo_layer.c b/src/yolo_layer.c
index a735932..ad62426 100644
--- a/src/yolo_layer.c
+++ b/src/yolo_layer.c
@@ -109,18 +109,40 @@
 }
 
 
-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 @@
                         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;
diff --git a/src/yolo_v2_class.cpp b/src/yolo_v2_class.cpp
index be1b4ee..6cc0252 100644
--- a/src/yolo_v2_class.cpp
+++ b/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;

--
Gitblit v1.10.0