From 1b5afb45838e603fa6780762eb8cc59246dc2d81 Mon Sep 17 00:00:00 2001
From: IlyaOvodov <b@ovdv.ru>
Date: Tue, 08 May 2018 11:09:35 +0000
Subject: [PATCH] Output improvements for detector results: When printing detector results, output was done in random order, obfuscating results for interpreting. Now: 1. Text output includes coordinates of rects in (left,right,top,bottom in pixels) along with label and score 2. Text output is sorted by rect lefts to simplify finding appropriate rects on image 3. If several class probs are > thresh for some detection, the most probable is written first and coordinates for others are not repeated 4. Rects are imprinted in image in order by their best class prob, so most probable rects are always on top and not overlayed by less probable ones 5. Most probable label for rect is always written first Also: 6. Message about low GPU memory include required amount

---
 src/softmax_layer.c |   70 ++++++++++++++++++++++++++--------
 1 files changed, 53 insertions(+), 17 deletions(-)

diff --git a/src/softmax_layer.c b/src/softmax_layer.c
index e189701..27f73fd 100644
--- a/src/softmax_layer.c
+++ b/src/softmax_layer.c
@@ -10,7 +10,7 @@
 softmax_layer make_softmax_layer(int batch, int inputs, int groups)
 {
     assert(inputs%groups == 0);
-    fprintf(stderr, "Softmax Layer: %d inputs\n", inputs);
+    fprintf(stderr, "softmax                                        %4d\n",  inputs);
     softmax_layer l = {0};
     l.type = SOFTMAX;
     l.batch = batch;
@@ -19,28 +19,30 @@
     l.outputs = inputs;
     l.output = calloc(inputs*batch, sizeof(float));
     l.delta = calloc(inputs*batch, sizeof(float));
+
+    l.forward = forward_softmax_layer;
+    l.backward = backward_softmax_layer;
     #ifdef GPU
+    l.forward_gpu = forward_softmax_layer_gpu;
+    l.backward_gpu = backward_softmax_layer_gpu;
+
     l.output_gpu = cuda_make_array(l.output, inputs*batch); 
     l.delta_gpu = cuda_make_array(l.delta, inputs*batch); 
     #endif
     return l;
 }
 
-void softmax_array(float *input, int n, float temp, float *output)
+void softmax_tree(float *input, int batch, int inputs, float temp, tree *hierarchy, float *output)
 {
-    int i;
-    float sum = 0;
-    float largest = -FLT_MAX;
-    for(i = 0; i < n; ++i){
-        if(input[i] > largest) largest = input[i];
-    }
-    for(i = 0; i < n; ++i){
-        sum += exp(input[i]/temp-largest/temp);
-    }
-    if(sum) sum = largest/temp+log(sum);
-    else sum = largest-100;
-    for(i = 0; i < n; ++i){
-        output[i] = exp(input[i]/temp-sum);
+    int b;
+    for(b = 0; b < batch; ++b){
+        int i;
+        int count = 0;
+        for(i = 0; i < hierarchy->groups; ++i){
+            int group_size = hierarchy->group_size[i];
+            softmax(input+b*inputs + count, group_size, temp, output+b*inputs + count, 1);
+            count += group_size;
+        }
     }
 }
 
@@ -49,8 +51,12 @@
     int b;
     int inputs = l.inputs / l.groups;
     int batch = l.batch * l.groups;
-    for(b = 0; b < batch; ++b){
-        softmax_array(state.input+b*inputs, inputs, l.temperature, l.output+b*inputs);
+    if(l.softmax_tree){
+        softmax_tree(state.input, batch, inputs, l.temperature, l.softmax_tree, l.output);
+    } else {
+        for(b = 0; b < batch; ++b){
+            softmax(state.input+b*inputs, inputs, l.temperature, l.output+b*inputs, 1);
+        }
     }
 }
 
@@ -62,3 +68,33 @@
     }
 }
 
+#ifdef GPU
+
+void pull_softmax_layer_output(const softmax_layer layer)
+{
+    cuda_pull_array(layer.output_gpu, layer.output, layer.inputs*layer.batch);
+}
+
+void forward_softmax_layer_gpu(const softmax_layer l, network_state state)
+{
+    int inputs = l.inputs / l.groups;
+    int batch = l.batch * l.groups;
+    if(l.softmax_tree){
+        int i;
+        int count = 0;
+        for (i = 0; i < l.softmax_tree->groups; ++i) {
+            int group_size = l.softmax_tree->group_size[i];
+            softmax_gpu(state.input+count, group_size, inputs, batch, l.temperature, l.output_gpu + count);
+            count += group_size;
+        }
+    } else {
+        softmax_gpu(state.input, inputs, inputs, batch, l.temperature, l.output_gpu);
+    }
+}
+
+void backward_softmax_layer_gpu(const softmax_layer layer, network_state state)
+{
+    axpy_ongpu(layer.batch*layer.inputs, 1, layer.delta_gpu, 1, state.delta, 1);
+}
+
+#endif

--
Gitblit v1.10.0