From 01bec657080d123a5b44a10ec94ebe132e20b1f3 Mon Sep 17 00:00:00 2001
From: IlyaOvodov <b@ovdv.ru>
Date: Wed, 30 May 2018 16:51:55 +0000
Subject: [PATCH] "channel" parameter of [net] is used in detector when preparing images for net.

---
 src/softmax_layer.c |  101 +++++++++++++++++++++++++++++++++++++++++---------
 1 files changed, 83 insertions(+), 18 deletions(-)

diff --git a/src/softmax_layer.c b/src/softmax_layer.c
index b213e5b..27f73fd 100644
--- a/src/softmax_layer.c
+++ b/src/softmax_layer.c
@@ -1,35 +1,100 @@
 #include "softmax_layer.h"
+#include "blas.h"
+#include "cuda.h"
+#include <float.h>
 #include <math.h>
 #include <stdlib.h>
 #include <stdio.h>
+#include <assert.h>
 
-softmax_layer *make_softmax_layer(int inputs)
+softmax_layer make_softmax_layer(int batch, int inputs, int groups)
 {
-    fprintf(stderr, "Softmax Layer: %d inputs\n", inputs);
-    softmax_layer *layer = calloc(1, sizeof(softmax_layer));
-    layer->inputs = inputs;
-    layer->output = calloc(inputs, sizeof(double));
-    layer->delta = calloc(inputs, sizeof(double));
-    return layer;
+    assert(inputs%groups == 0);
+    fprintf(stderr, "softmax                                        %4d\n",  inputs);
+    softmax_layer l = {0};
+    l.type = SOFTMAX;
+    l.batch = batch;
+    l.groups = groups;
+    l.inputs = inputs;
+    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 forward_softmax_layer(const softmax_layer layer, double *input)
+void softmax_tree(float *input, int batch, int inputs, float temp, tree *hierarchy, float *output)
+{
+    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;
+        }
+    }
+}
+
+void forward_softmax_layer(const softmax_layer l, network_state state)
+{
+    int b;
+    int inputs = l.inputs / l.groups;
+    int batch = l.batch * l.groups;
+    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);
+        }
+    }
+}
+
+void backward_softmax_layer(const softmax_layer l, network_state state)
 {
     int i;
-    double sum = 0;
-    for(i = 0; i < layer.inputs; ++i){
-        sum += exp(input[i]);
-    }
-    for(i = 0; i < layer.inputs; ++i){
-        layer.output[i] = exp(input[i])/sum;
+    for(i = 0; i < l.inputs*l.batch; ++i){
+        state.delta[i] += l.delta[i];
     }
 }
 
-void backward_softmax_layer(const softmax_layer layer, double *input, double *delta)
+#ifdef GPU
+
+void pull_softmax_layer_output(const softmax_layer layer)
 {
-    int i;
-    for(i = 0; i < layer.inputs; ++i){
-        delta[i] = layer.delta[i];
+    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