From 726cebd3fb67d65ec6d2d49fa6bfba4c053085df Mon Sep 17 00:00:00 2001
From: AlexeyAB <alexeyab84@gmail.com>
Date: Mon, 02 Apr 2018 12:02:53 +0000
Subject: [PATCH] Fixed detector recall

---
 src/yolo_layer.c |   42 +++++++++++++++++++++++++++++++++++-------
 1 files changed, 35 insertions(+), 7 deletions(-)

diff --git a/src/yolo_layer.c b/src/yolo_layer.c
index 46846ef..a735932 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){
@@ -129,6 +130,16 @@
     return batch*l.outputs + n*l.w*l.h*(4+l.classes+1) + entry*l.w*l.h + loc;
 }
 
+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;
+}
+
 void forward_yolo_layer(const layer l, network_state state)
 {
     int i,j,b,t,n;
@@ -165,7 +176,7 @@
                     float best_iou = 0;
                     int best_t = 0;
                     for(t = 0; t < l.max_boxes; ++t){
-                        box truth = float_to_box(state.truth + t*(4 + 1) + b*l.truths, 1);
+                        box truth = float_to_box_stride(state.truth + t*(4 + 1) + b*l.truths, 1);
                         if(!truth.x) break;
                         float iou = box_iou(pred, truth);
                         if (iou > best_iou) {
@@ -186,14 +197,14 @@
                         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);
-                        box truth = float_to_box(state.truth + best_t*(4 + 1) + b*l.truths, 1);
+                        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);
                     }
                 }
             }
         }
         for(t = 0; t < l.max_boxes; ++t){
-            box truth = float_to_box(state.truth + t*(4 + 1) + b*l.truths, 1);
+            box truth = float_to_box_stride(state.truth + t*(4 + 1) + b*l.truths, 1);
 
             if(!truth.x) break;
             float best_iou = 0;
@@ -368,9 +379,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