From 064d4462d3890fd5dda5c7845119e1a3854d477b Mon Sep 17 00:00:00 2001
From: Joseph Redmon <pjreddie@gmail.com>
Date: Thu, 26 Nov 2015 21:45:12 +0000
Subject: [PATCH] stuff

---
 src/network.c |   90 +++++++++++++++++++++++++++++++++++----------
 1 files changed, 70 insertions(+), 20 deletions(-)

diff --git a/src/network.c b/src/network.c
index 70bcb58..d9585c4 100644
--- a/src/network.c
+++ b/src/network.c
@@ -8,10 +8,10 @@
 
 #include "crop_layer.h"
 #include "connected_layer.h"
+#include "local_layer.h"
 #include "convolutional_layer.h"
 #include "deconvolutional_layer.h"
 #include "detection_layer.h"
-#include "region_layer.h"
 #include "normalization_layer.h"
 #include "maxpool_layer.h"
 #include "avgpool_layer.h"
@@ -20,11 +20,60 @@
 #include "dropout_layer.h"
 #include "route_layer.h"
 
+int get_current_batch(network net)
+{
+    int batch_num = (*net.seen)/(net.batch*net.subdivisions);
+    return batch_num;
+}
+
+void reset_momentum(network net)
+{
+    if (net.momentum == 0) return;
+    net.learning_rate = 0;
+    net.momentum = 0;
+    net.decay = 0;
+    #ifdef GPU
+        if(gpu_index >= 0) update_network_gpu(net);
+    #endif
+}
+
+float get_current_rate(network net)
+{
+    int batch_num = get_current_batch(net);
+    int i;
+    float rate;
+    switch (net.policy) {
+        case CONSTANT:
+            return net.learning_rate;
+        case STEP:
+            return net.learning_rate * pow(net.scale, batch_num/net.step);
+        case STEPS:
+            rate = net.learning_rate;
+            for(i = 0; i < net.num_steps; ++i){
+                if(net.steps[i] > batch_num) return rate;
+                rate *= net.scales[i];
+                if(net.steps[i] > batch_num - 1) reset_momentum(net);
+            }
+            return rate;
+        case EXP:
+            return net.learning_rate * pow(net.gamma, batch_num);
+        case POLY:
+            return net.learning_rate * pow(1 - (float)batch_num / net.max_batches, net.power);
+        case SIG:
+            return net.learning_rate * (1./(1.+exp(net.gamma*(batch_num - net.step))));
+        default:
+            fprintf(stderr, "Policy is weird!\n");
+            return net.learning_rate;
+    }
+}
+
 char *get_layer_string(LAYER_TYPE a)
 {
     switch(a){
         case CONVOLUTIONAL:
             return "convolutional";
+        case LOCAL:
+            return "local";
         case DECONVOLUTIONAL:
             return "deconvolutional";
         case CONNECTED:
@@ -37,8 +86,6 @@
             return "softmax";
         case DETECTION:
             return "detection";
-        case REGION:
-            return "region";
         case DROPOUT:
             return "dropout";
         case CROP:
@@ -60,6 +107,7 @@
     network net = {0};
     net.n = n;
     net.layers = calloc(net.n, sizeof(layer));
+    net.seen = calloc(1, sizeof(int));
     #ifdef GPU
     net.input_gpu = calloc(1, sizeof(float *));
     net.truth_gpu = calloc(1, sizeof(float *));
@@ -79,12 +127,12 @@
             forward_convolutional_layer(l, state);
         } else if(l.type == DECONVOLUTIONAL){
             forward_deconvolutional_layer(l, state);
+        } else if(l.type == LOCAL){
+            forward_local_layer(l, state);
         } else if(l.type == NORMALIZATION){
             forward_normalization_layer(l, state);
         } else if(l.type == DETECTION){
             forward_detection_layer(l, state);
-        } else if(l.type == REGION){
-            forward_region_layer(l, state);
         } else if(l.type == CONNECTED){
             forward_connected_layer(l, state);
         } else if(l.type == CROP){
@@ -110,14 +158,17 @@
 {
     int i;
     int update_batch = net.batch*net.subdivisions;
+    float rate = get_current_rate(net);
     for(i = 0; i < net.n; ++i){
         layer l = net.layers[i];
         if(l.type == CONVOLUTIONAL){
-            update_convolutional_layer(l, update_batch, net.learning_rate, net.momentum, net.decay);
+            update_convolutional_layer(l, update_batch, rate, net.momentum, net.decay);
         } else if(l.type == DECONVOLUTIONAL){
-            update_deconvolutional_layer(l, net.learning_rate, net.momentum, net.decay);
+            update_deconvolutional_layer(l, rate, net.momentum, net.decay);
         } else if(l.type == CONNECTED){
-            update_connected_layer(l, update_batch, net.learning_rate, net.momentum, net.decay);
+            update_connected_layer(l, update_batch, rate, net.momentum, net.decay);
+        } else if(l.type == LOCAL){
+            update_local_layer(l, update_batch, rate, net.momentum, net.decay);
         }
     }
 }
@@ -143,10 +194,6 @@
             sum += net.layers[i].cost[0];
             ++count;
         }
-        if(net.layers[i].type == REGION){
-            sum += net.layers[i].cost[0];
-            ++count;
-        }
     }
     return sum/count;
 }
@@ -187,12 +234,12 @@
             backward_dropout_layer(l, state);
         } else if(l.type == DETECTION){
             backward_detection_layer(l, state);
-        } else if(l.type == REGION){
-            backward_region_layer(l, state);
         } else if(l.type == SOFTMAX){
             if(i != 0) backward_softmax_layer(l, state);
         } else if(l.type == CONNECTED){
             backward_connected_layer(l, state);
+        } else if(l.type == LOCAL){
+            backward_local_layer(l, state);
         } else if(l.type == COST){
             backward_cost_layer(l, state);
         } else if(l.type == ROUTE){
@@ -203,6 +250,7 @@
 
 float train_network_datum(network net, float *x, float *y)
 {
+    *net.seen += net.batch;
 #ifdef GPU
     if(gpu_index >= 0) return train_network_datum_gpu(net, x, y);
 #endif
@@ -214,7 +262,7 @@
     forward_network(net, state);
     backward_network(net, state);
     float error = get_network_cost(net);
-    if((net.seen/net.batch)%net.subdivisions == 0) update_network(net);
+    if(((*net.seen)/net.batch)%net.subdivisions == 0) update_network(net);
     return error;
 }
 
@@ -227,7 +275,6 @@
     int i;
     float sum = 0;
     for(i = 0; i < n; ++i){
-        net.seen += batch;
         get_random_batch(d, batch, X, y);
         float err = train_network_datum(net, X, y);
         sum += err;
@@ -248,7 +295,6 @@
     float sum = 0;
     for(i = 0; i < n; ++i){
         get_next_batch(d, batch, i*batch, X, y);
-        net.seen += batch;
         float err = train_network_datum(net, X, y);
         sum += err;
     }
@@ -294,6 +340,7 @@
     //if(w == net->w && h == net->h) return 0;
     net->w = w;
     net->h = h;
+    int inputs = 0;
     //fprintf(stderr, "Resizing to %d x %d...", w, h);
     //fflush(stderr);
     for (i = 0; i < net->n; ++i){
@@ -307,9 +354,12 @@
             break;
         }else if(l.type == NORMALIZATION){
             resize_normalization_layer(&l, w, h);
+        }else if(l.type == COST){
+            resize_cost_layer(&l, inputs);
         }else{
             error("Cannot resize this type of layer");
         }
+        inputs = l.outputs;
         net->layers[i] = l;
         w = l.out_w;
         h = l.out_h;
@@ -500,12 +550,12 @@
     return acc;
 }
 
-float *network_accuracies(network net, data d)
+float *network_accuracies(network net, data d, int n)
 {
     static float acc[2];
     matrix guess = network_predict_data(net, d);
-    acc[0] = matrix_topk_accuracy(d.y, guess,1);
-    acc[1] = matrix_topk_accuracy(d.y, guess,5);
+    acc[0] = matrix_topk_accuracy(d.y, guess, 1);
+    acc[1] = matrix_topk_accuracy(d.y, guess, n);
     free_matrix(guess);
     return acc;
 }

--
Gitblit v1.10.0