From b8e6e80c6d411d05a9e09f1e3676eb9a7f3ea0e8 Mon Sep 17 00:00:00 2001
From: AlexeyAB <alexeyab84@gmail.com>
Date: Fri, 03 Aug 2018 11:35:03 +0000
Subject: [PATCH] Added spatial Yolo v3 yolov3-spp.cfg

---
 src/yolo_layer.c |  125 +++++++++++++++++++++++++++++------------
 1 files changed, 87 insertions(+), 38 deletions(-)

diff --git a/src/yolo_layer.c b/src/yolo_layer.c
index c8e2ff5..f0bc073 100644
--- a/src/yolo_layer.c
+++ b/src/yolo_layer.c
@@ -10,7 +10,7 @@
 #include <string.h>
 #include <stdlib.h>
 
-layer make_yolo_layer(int batch, int w, int h, int n, int total, int *mask, int classes)
+layer make_yolo_layer(int batch, int w, int h, int n, int total, int *mask, int classes, int max_boxes)
 {
     int i;
     layer l = {0};
@@ -38,7 +38,8 @@
     l.bias_updates = calloc(n*2, sizeof(float));
     l.outputs = h*w*n*(classes + 4 + 1);
     l.inputs = l.outputs;
-    l.truths = 90*(4 + 1);
+    l.max_boxes = max_boxes;
+    l.truths = l.max_boxes*(4 + 1);    // 90*(4 + 1);
     l.delta = calloc(batch*l.outputs, sizeof(float));
     l.output = calloc(batch*l.outputs, sizeof(float));
     for(i = 0; i < total*2; ++i){
@@ -54,7 +55,7 @@
     l.delta_gpu = cuda_make_array(l.delta, batch*l.outputs);
 #endif
 
-    fprintf(stderr, "detection\n");
+    fprintf(stderr, "yolo\n");
     srand(0);
 
     return l;
@@ -108,17 +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];
+    if (delta[index + stride*class_id]){
+        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;
+        // http://fooplot.com/#W3sidHlwZSI6MCwiZXEiOiItKDEteCkqKDIqeCpsb2coeCkreC0xKSIsImNvbG9yIjoiIzAwMDAwMCJ9LHsidHlwZSI6MTAwMH1d
+        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];
+        }
     }
 }
 
@@ -131,12 +155,12 @@
 
 static box float_to_box_stride(float *f, int stride)
 {
-	box b = { 0 };
-	b.x = f[0];
-	b.y = f[1 * stride];
-	b.w = f[2 * stride];
-	b.h = f[3 * stride];
-	return b;
+    box b = { 0 };
+    b.x = f[0];
+    b.y = f[1 * stride];
+    b.w = f[2 * stride];
+    b.h = f[3 * stride];
+    return b;
 }
 
 void forward_yolo_layer(const layer l, network_state state)
@@ -176,6 +200,12 @@
                     int best_t = 0;
                     for(t = 0; t < l.max_boxes; ++t){
                         box truth = float_to_box_stride(state.truth + t*(4 + 1) + b*l.truths, 1);
+                        int class_id = state.truth[t*(4 + 1) + b*l.truths + 4];
+                        if (class_id >= l.classes) {
+                            printf(" Warning: in txt-labels class_id=%d >= classes=%d in cfg-file. In txt-labels class_id should be [from 0 to %d] \n", class_id, l.classes, l.classes - 1);
+                            getchar();
+                            continue; // if label contains class_id more than number of classes in the cfg-file
+                        }
                         if(!truth.x) break;
                         float iou = box_iou(pred, truth);
                         if (iou > best_iou) {
@@ -192,10 +222,10 @@
                     if (best_iou > l.truth_thresh) {
                         l.delta[obj_index] = 1 - l.output[obj_index];
 
-                        int class = state.truth[best_t*(4 + 1) + b*l.truths + 4];
-                        if (l.map) class = l.map[class];
+                        int class_id = state.truth[best_t*(4 + 1) + b*l.truths + 4];
+                        if (l.map) class_id = l.map[class_id];
                         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_id, 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);
                     }
@@ -204,6 +234,8 @@
         }
         for(t = 0; t < l.max_boxes; ++t){
             box truth = float_to_box_stride(state.truth + t*(4 + 1) + b*l.truths, 1);
+            int class_id = state.truth[t*(4 + 1) + b*l.truths + 4];
+            if (class_id >= l.classes) continue; // if label contains class_id more than number of classes in the cfg-file
 
             if(!truth.x) break;
             float best_iou = 0;
@@ -232,10 +264,10 @@
                 avg_obj += l.output[obj_index];
                 l.delta[obj_index] = 1 - l.output[obj_index];
 
-                int class = state.truth[t*(4 + 1) + b*l.truths + 4];
-                if (l.map) class = l.map[class];
+                int class_id = state.truth[t*(4 + 1) + b*l.truths + 4];
+                if (l.map) class_id = l.map[class_id];
                 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_id, l.classes, l.w*l.h, &avg_cat, l.focal_loss);
 
                 ++count;
                 ++class_count;
@@ -259,20 +291,20 @@
     int i;
     int new_w=0;
     int new_h=0;
-	if (letter) {
-		if (((float)netw / w) < ((float)neth / h)) {
-			new_w = netw;
-			new_h = (h * netw) / w;
-		}
-		else {
-			new_h = neth;
-			new_w = (w * neth) / h;
-		}
-	}
-	else {
-		new_w = netw;
-		new_h = neth;
-	}
+    if (letter) {
+        if (((float)netw / w) < ((float)neth / h)) {
+            new_w = netw;
+            new_h = (h * netw) / w;
+        }
+        else {
+            new_h = neth;
+            new_w = (w * neth) / h;
+        }
+    }
+    else {
+        new_w = netw;
+        new_h = neth;
+    }
     for (i = 0; i < n; ++i){
         box b = dets[i].bbox;
         b.x =  (b.x - (netw - new_w)/2./netw) / ((float)new_w/netw); 
@@ -378,9 +410,26 @@
         return;
     }
 
-    cuda_pull_array(l.output_gpu, state.input, l.batch*l.inputs);
-    forward_yolo_layer(l, state);
+    //cuda_pull_array(l.output_gpu, state.input, l.batch*l.inputs);
+    float *in_cpu = calloc(l.batch*l.inputs, sizeof(float));
+    cuda_pull_array(l.output_gpu, in_cpu, l.batch*l.inputs);
+    float *truth_cpu = 0;
+    if (state.truth) {
+        int num_truth = l.batch*l.truths;
+        truth_cpu = calloc(num_truth, sizeof(float));
+        cuda_pull_array(state.truth, truth_cpu, num_truth);
+    }
+    network_state cpu_state = state;
+    cpu_state.net = state.net;
+    cpu_state.index = state.index;
+    cpu_state.train = state.train;
+    cpu_state.truth = truth_cpu;
+    cpu_state.input = in_cpu;
+    forward_yolo_layer(l, cpu_state);
+    //forward_yolo_layer(l, state);
     cuda_push_array(l.delta_gpu, l.delta, l.batch*l.outputs);
+    free(in_cpu);
+    if (cpu_state.truth) free(cpu_state.truth);
 }
 
 void backward_yolo_layer_gpu(const layer l, network_state state)

--
Gitblit v1.10.0