From 2db9fbef2bd7d35a547d0018a9850f6b249c524f Mon Sep 17 00:00:00 2001
From: Joseph Redmon <pjreddie@gmail.com>
Date: Wed, 13 Nov 2013 18:50:38 +0000
Subject: [PATCH] Parsing, image loading, lots of stuff

---
 src/convolutional_layer.c |   99 ++++++++++++++++++++++++++++++++++++++-----------
 1 files changed, 77 insertions(+), 22 deletions(-)

diff --git a/src/convolutional_layer.c b/src/convolutional_layer.c
index 7478158..d4aff73 100644
--- a/src/convolutional_layer.c
+++ b/src/convolutional_layer.c
@@ -1,52 +1,93 @@
 #include "convolutional_layer.h"
+#include <stdio.h>
 
-double convolution_activation(double x)
+image get_convolutional_image(convolutional_layer layer)
 {
-    return x*(x>0);
+    int h = (layer.h-1)/layer.stride + 1;
+    int w = (layer.w-1)/layer.stride + 1;
+    int c = layer.n;
+    return double_to_image(h,w,c,layer.output);
 }
 
-double convolution_gradient(double x)
+image get_convolutional_delta(convolutional_layer layer)
 {
-    return (x>=0);
+    int h = (layer.h-1)/layer.stride + 1;
+    int w = (layer.w-1)/layer.stride + 1;
+    int c = layer.n;
+    return double_to_image(h,w,c,layer.delta);
 }
 
-convolutional_layer *make_convolutional_layer(int h, int w, int c, int n, int size, int stride)
+convolutional_layer *make_convolutional_layer(int h, int w, int c, int n, int size, int stride, ACTIVATION activator)
 {
+    printf("Convolutional Layer: %d x %d x %d image, %d filters\n", h,w,c,n);
     int i;
     convolutional_layer *layer = calloc(1, sizeof(convolutional_layer));
+    layer->h = h;
+    layer->w = w;
+    layer->c = c;
     layer->n = n;
     layer->stride = stride;
     layer->kernels = calloc(n, sizeof(image));
     layer->kernel_updates = calloc(n, sizeof(image));
+    layer->biases = calloc(n, sizeof(double));
+    layer->bias_updates = calloc(n, sizeof(double));
     for(i = 0; i < n; ++i){
+        layer->biases[i] = .005;
         layer->kernels[i] = make_random_kernel(size, c);
         layer->kernel_updates[i] = make_random_kernel(size, c);
     }
-    layer->output = make_image((h-1)/stride+1, (w-1)/stride+1, n);
+    layer->output = calloc(((h-1)/stride+1) * ((w-1)/stride+1) * n, sizeof(double));
+    layer->delta  = calloc(((h-1)/stride+1) * ((w-1)/stride+1) * n, sizeof(double));
     layer->upsampled = make_image(h,w,n);
+
+    if(activator == SIGMOID){
+        layer->activation = sigmoid_activation;
+        layer->gradient = sigmoid_gradient;
+    }else if(activator == RELU){
+        layer->activation = relu_activation;
+        layer->gradient = relu_gradient;
+    }else if(activator == IDENTITY){
+        layer->activation = identity_activation;
+        layer->gradient = identity_gradient;
+    }
     return layer;
 }
 
-void run_convolutional_layer(const image input, const convolutional_layer layer)
+void forward_convolutional_layer(const convolutional_layer layer, double *in)
 {
-    int i;
+    image input = double_to_image(layer.h, layer.w, layer.c, in);
+    image output = get_convolutional_image(layer);
+    int i,j;
     for(i = 0; i < layer.n; ++i){
-        convolve(input, layer.kernels[i], layer.stride, i, layer.output);
+        convolve(input, layer.kernels[i], layer.stride, i, output);
     }
-    for(i = 0; i < layer.output.h*layer.output.w*layer.output.c; ++i){
-        layer.output.data[i] = convolution_activation(layer.output.data[i]);
+    for(i = 0; i < output.c; ++i){
+        for(j = 0; j < output.h*output.w; ++j){
+            int index = i*output.h*output.w + j;
+            output.data[index] += layer.biases[i];
+            output.data[index] = layer.activation(output.data[index]);
+        }
     }
 }
 
-void backpropagate_convolutional_layer(image input, convolutional_layer layer)
+void backward_convolutional_layer(convolutional_layer layer, double *input, double *delta)
 {
     int i;
-    zero_image(input);
+
+    image in_image = double_to_image(layer.h, layer.w, layer.c, input);
+    image in_delta = double_to_image(layer.h, layer.w, layer.c, delta);
+    image out_delta = get_convolutional_delta(layer);
+    zero_image(in_delta);
+
     for(i = 0; i < layer.n; ++i){
-        back_convolve(input, layer.kernels[i], layer.stride, i, layer.output);
+        back_convolve(in_delta, layer.kernels[i], layer.stride, i, out_delta);
+    }
+    for(i = 0; i < layer.h*layer.w*layer.c; ++i){
+        in_delta.data[i] *= layer.gradient(in_image.data[i]);
     }
 }
 
+/*
 void backpropagate_convolutional_layer_convolve(image input, convolutional_layer layer)
 {
     int i,j;
@@ -66,25 +107,26 @@
         rotate_image(layer.kernels[i]);
     }
 }
+*/
 
-void learn_convolutional_layer(image input, convolutional_layer layer)
+void learn_convolutional_layer(convolutional_layer layer, double *input)
 {
     int i;
+    image in_image = double_to_image(layer.h, layer.w, layer.c, input);
+    image out_delta = get_convolutional_delta(layer);
     for(i = 0; i < layer.n; ++i){
-        kernel_update(input, layer.kernel_updates[i], layer.stride, i, layer.output);
+        kernel_update(in_image, layer.kernel_updates[i], layer.stride, i, out_delta);
+        layer.bias_updates[i] += avg_image_layer(out_delta, i);
     }
-    image old_input = copy_image(input);
-    backpropagate_convolutional_layer(input, layer);
-    for(i = 0; i < input.h*input.w*input.c; ++i){
-        input.data[i] *= convolution_gradient(old_input.data[i]);
-    }
-    free_image(old_input);
 }
 
 void update_convolutional_layer(convolutional_layer layer, double step)
 {
+    return;
     int i,j;
     for(i = 0; i < layer.n; ++i){
+        layer.biases[i] += step*layer.bias_updates[i];
+        layer.bias_updates[i] = 0;
         int pixels = layer.kernels[i].h*layer.kernels[i].w*layer.kernels[i].c;
         for(j = 0; j < pixels; ++j){
             layer.kernels[i].data[j] += step*layer.kernel_updates[i].data[j];
@@ -93,3 +135,16 @@
     }
 }
 
+void visualize_convolutional_layer(convolutional_layer layer)
+{
+    int i;
+    char buff[256];
+    //image vis = make_image(layer.n*layer.size, layer.size*layer.kernels[0].c, 3);
+    for(i = 0; i < layer.n; ++i){
+        image k = layer.kernels[i];
+        sprintf(buff, "Kernel %d", i);
+        if(k.c <= 3) show_image(k, buff);
+        else show_image_layers(k, buff);
+    }
+}
+

--
Gitblit v1.10.0