From 1edcf73a73d2007afc61289245763f5cf0c29e10 Mon Sep 17 00:00:00 2001
From: Joseph Redmon <pjreddie@gmail.com>
Date: Thu, 04 Dec 2014 07:20:29 +0000
Subject: [PATCH] Detection good, split up col images

---
 src/network.c |  168 ++++++++++++++++---------------------------------------
 1 files changed, 50 insertions(+), 118 deletions(-)

diff --git a/src/network.c b/src/network.c
index 5833166..3a6a184 100644
--- a/src/network.c
+++ b/src/network.c
@@ -1,4 +1,5 @@
 #include <stdio.h>
+#include <time.h>
 #include "network.h"
 #include "image.h"
 #include "data.h"
@@ -24,122 +25,12 @@
     net.outputs = 0;
     net.output = 0;
     #ifdef GPU
-    net.input_cl = 0;
+    net.input_cl = calloc(1, sizeof(cl_mem));
+    net.truth_cl = calloc(1, sizeof(cl_mem));
     #endif
     return net;
 }
 
-#ifdef GPU
-void forward_network_gpu(network net, cl_mem input, cl_mem truth, int train)
-{
-    int i;
-    for(i = 0; i < net.n; ++i){
-        if(net.types[i] == CONVOLUTIONAL){
-            convolutional_layer layer = *(convolutional_layer *)net.layers[i];
-            forward_convolutional_layer_gpu(layer, input);
-            input = layer.output_cl;
-        }
-        else if(net.types[i] == COST){
-            cost_layer layer = *(cost_layer *)net.layers[i];
-            forward_cost_layer_gpu(layer, input, truth);
-        }
-        /*
-        else if(net.types[i] == CONNECTED){
-            connected_layer layer = *(connected_layer *)net.layers[i];
-            forward_connected_layer(layer, input, train);
-            input = layer.output;
-        }
-        else if(net.types[i] == SOFTMAX){
-            softmax_layer layer = *(softmax_layer *)net.layers[i];
-            forward_softmax_layer(layer, input);
-            input = layer.output;
-        }
-        else if(net.types[i] == CROP){
-            crop_layer layer = *(crop_layer *)net.layers[i];
-            forward_crop_layer(layer, input);
-            input = layer.output;
-        }
-        else if(net.types[i] == MAXPOOL){
-            maxpool_layer layer = *(maxpool_layer *)net.layers[i];
-            forward_maxpool_layer(layer, input);
-            input = layer.output;
-        }
-        else if(net.types[i] == NORMALIZATION){
-            normalization_layer layer = *(normalization_layer *)net.layers[i];
-            forward_normalization_layer(layer, input);
-            input = layer.output;
-        }
-        */
-    }
-}
-
-void backward_network_gpu(network net, cl_mem input)
-{
-    int i;
-    cl_mem prev_input;
-    cl_mem prev_delta;
-    for(i = net.n-1; i >= 0; --i){
-        if(i == 0){
-            prev_input = input;
-            prev_delta = 0;
-        }else{
-            prev_input = get_network_output_cl_layer(net, i-1);
-            prev_delta = get_network_delta_cl_layer(net, i-1);
-        }
-        if(net.types[i] == CONVOLUTIONAL){
-            convolutional_layer layer = *(convolutional_layer *)net.layers[i];
-            backward_convolutional_layer_gpu(layer, prev_delta);
-        }
-        else if(net.types[i] == COST){
-            cost_layer layer = *(cost_layer *)net.layers[i];
-            backward_cost_layer_gpu(layer, prev_input, prev_delta);
-        }
-    }
-}
-
-void update_network_gpu(network net)
-{
-    int i;
-    for(i = 0; i < net.n; ++i){
-        if(net.types[i] == CONVOLUTIONAL){
-            convolutional_layer layer = *(convolutional_layer *)net.layers[i];
-            update_convolutional_layer_gpu(layer);
-        }
-        else if(net.types[i] == MAXPOOL){
-            //maxpool_layer layer = *(maxpool_layer *)net.layers[i];
-        }
-        else if(net.types[i] == SOFTMAX){
-            //maxpool_layer layer = *(maxpool_layer *)net.layers[i];
-        }
-        else if(net.types[i] == NORMALIZATION){
-            //maxpool_layer layer = *(maxpool_layer *)net.layers[i];
-        }
-        else if(net.types[i] == CONNECTED){
-            connected_layer layer = *(connected_layer *)net.layers[i];
-            update_connected_layer(layer);
-        }
-    }
-}
-
-cl_mem get_network_output_cl_layer(network net, int i)
-{
-    if(net.types[i] == CONVOLUTIONAL){
-        convolutional_layer layer = *(convolutional_layer *)net.layers[i];
-        return layer.output_cl;
-    }
-    return 0;
-}
-
-cl_mem get_network_delta_cl_layer(network net, int i)
-{
-    if(net.types[i] == CONVOLUTIONAL){
-        convolutional_layer layer = *(convolutional_layer *)net.layers[i];
-        return layer.delta_cl;
-    }
-    return 0;
-}
-
-#endif
 
 void forward_network(network net, float *input, float *truth, int train)
 {
@@ -229,6 +120,8 @@
         return layer.output;
     } else if(net.types[i] == DROPOUT){
         return get_network_output_layer(net, i-1);
+    } else if(net.types[i] == FREEWEIGHT){
+        return get_network_output_layer(net, i-1);
     } else if(net.types[i] == CONNECTED){
         connected_layer layer = *(connected_layer *)net.layers[i];
         return layer.output;
@@ -258,6 +151,8 @@
         return layer.delta;
     } else if(net.types[i] == DROPOUT){
         return get_network_delta_layer(net, i-1);
+    } else if(net.types[i] == FREEWEIGHT){
+        return get_network_delta_layer(net, i-1);
     } else if(net.types[i] == CONNECTED){
         connected_layer layer = *(connected_layer *)net.layers[i];
         return layer.delta;
@@ -318,11 +213,11 @@
         }
         if(net.types[i] == CONVOLUTIONAL){
             convolutional_layer layer = *(convolutional_layer *)net.layers[i];
-            backward_convolutional_layer(layer, prev_delta);
+            backward_convolutional_layer(layer, prev_input, prev_delta);
         }
         else if(net.types[i] == MAXPOOL){
             maxpool_layer layer = *(maxpool_layer *)net.layers[i];
-            if(i != 0) backward_maxpool_layer(layer, prev_input, prev_delta);
+            if(i != 0) backward_maxpool_layer(layer, prev_delta);
         }
         else if(net.types[i] == NORMALIZATION){
             normalization_layer layer = *(normalization_layer *)net.layers[i];
@@ -330,7 +225,7 @@
         }
         else if(net.types[i] == SOFTMAX){
             softmax_layer layer = *(softmax_layer *)net.layers[i];
-            if(i != 0) backward_softmax_layer(layer, prev_input, prev_delta);
+            if(i != 0) backward_softmax_layer(layer, prev_delta);
         }
         else if(net.types[i] == CONNECTED){
             connected_layer layer = *(connected_layer *)net.layers[i];
@@ -343,6 +238,9 @@
     }
 }
 
+
+
+
 float train_network_datum(network net, float *x, float *y)
 {
     forward_network(net, x, y, 1);
@@ -363,7 +261,7 @@
     int i;
     float sum = 0;
     for(i = 0; i < n; ++i){
-        get_batch(d, batch, X, y);
+        get_random_batch(d, batch, X, y);
         float err = train_network_datum(net, X, y);
         sum += err;
     }
@@ -371,6 +269,7 @@
     free(y);
     return (float)sum/(n*batch);
 }
+
 float train_network_batch(network net, data d, int n)
 {
     int i,j;
@@ -390,6 +289,23 @@
     return (float)sum/(n*batch);
 }
 
+float train_network_data_cpu(network net, data d, int n)
+{
+    int batch = net.batch;
+    float *X = calloc(batch*d.X.cols, sizeof(float));
+    float *y = calloc(batch*d.y.cols, sizeof(float));
+
+    int i;
+    float sum = 0;
+    for(i = 0; i < n; ++i){
+        get_next_batch(d, batch, i*batch, X, y);
+        float err = train_network_datum(net, X, y);
+        sum += err;
+    }
+    free(X);
+    free(y);
+    return (float)sum/(n*batch);
+}
 
 void train_network(network net, data d)
 {
@@ -424,6 +340,10 @@
         dropout_layer layer = *(dropout_layer *) net.layers[i];
         return layer.inputs;
     }
+    else if(net.types[i] == FREEWEIGHT){
+        freeweight_layer layer = *(freeweight_layer *) net.layers[i];
+        return layer.inputs;
+    }
     else if(net.types[i] == SOFTMAX){
         softmax_layer layer = *(softmax_layer *)net.layers[i];
         return layer.inputs;
@@ -451,6 +371,10 @@
         dropout_layer layer = *(dropout_layer *) net.layers[i];
         return layer.inputs;
     }
+    else if(net.types[i] == FREEWEIGHT){
+        freeweight_layer layer = *(freeweight_layer *) net.layers[i];
+        return layer.inputs;
+    }
     else if(net.types[i] == SOFTMAX){
         softmax_layer layer = *(softmax_layer *)net.layers[i];
         return layer.inputs;
@@ -538,7 +462,7 @@
     image *prev = 0;
     int i;
     char buff[256];
-    show_image(get_network_image_layer(net, 0), "Crop");
+    //show_image(get_network_image_layer(net, 0), "Crop");
     for(i = 0; i < net.n; ++i){
         sprintf(buff, "Layer %d", i);
         if(net.types[i] == CONVOLUTIONAL){
@@ -552,6 +476,14 @@
     } 
 }
 
+void top_predictions(network net, int k, int *index)
+{
+    int size = get_network_output_size(net);
+    float *out = get_network_output(net);
+    top_k(out, size, k, index);
+}
+
+
 float *network_predict(network net, float *input)
 {
     forward_network(net, input, 0, 0);
@@ -589,7 +521,7 @@
     int i,j,b;
     int k = get_network_output_size(net);
     matrix pred = make_matrix(test.X.rows, k);
-    float *X = calloc(net.batch*test.X.rows, sizeof(float));
+    float *X = calloc(net.batch*test.X.cols, sizeof(float));
     for(i = 0; i < test.X.rows; i += net.batch){
         for(b = 0; b < net.batch; ++b){
             if(i+b == test.X.rows) break;

--
Gitblit v1.10.0