Loading may or may not work. But probably.

This commit is contained in:
Joseph Redmon 2013-11-06 16:09:41 -08:00
parent 9b1774bd39
commit d7286c2732
14 changed files with 155 additions and 57 deletions

1
.gitignore vendored
View File

@ -4,6 +4,7 @@
images/ images/
opencv/ opencv/
convnet/ convnet/
decaf/
cnn cnn
# OS Generated # # OS Generated #

View File

@ -4,7 +4,7 @@ CFLAGS=-Wall `pkg-config --cflags opencv` -O0 -g
LDFLAGS=`pkg-config --libs opencv` -lm LDFLAGS=`pkg-config --libs opencv` -lm
VPATH=./src/ VPATH=./src/
OBJ=network.o image.o tests.o convolutional_layer.o connected_layer.o maxpool_layer.o activations.o OBJ=network.o image.o tests.o convolutional_layer.o connected_layer.o maxpool_layer.o activations.o list.o option_list.o parser.o utils.o
all: cnn all: cnn

View File

@ -1,6 +1,17 @@
#include "activations.h" #include "activations.h"
#include <math.h> #include <math.h>
#include <stdio.h>
#include <string.h>
ACTIVATION get_activation(char *s)
{
if (strcmp(s, "sigmoid")==0) return SIGMOID;
if (strcmp(s, "relu")==0) return RELU;
if (strcmp(s, "identity")==0) return IDENTITY;
fprintf(stderr, "Couldn't find activation function %s, going with ReLU\n", s);
return RELU;
}
double identity_activation(double x) double identity_activation(double x)
{ {

View File

@ -1,10 +1,17 @@
#ifndef ACTIVATIONS_H
#define ACTIVATIONS_H
typedef enum{ typedef enum{
SIGMOID, RELU, IDENTITY SIGMOID, RELU, IDENTITY
}ACTIVATOR_TYPE; }ACTIVATION;
ACTIVATION get_activation(char *s);
double relu_activation(double x); double relu_activation(double x);
double relu_gradient(double x); double relu_gradient(double x);
double sigmoid_activation(double x); double sigmoid_activation(double x);
double sigmoid_gradient(double x); double sigmoid_gradient(double x);
double identity_activation(double x); double identity_activation(double x);
double identity_gradient(double x); double identity_gradient(double x);
#endif

View File

@ -4,34 +4,34 @@
#include <stdlib.h> #include <stdlib.h>
#include <string.h> #include <string.h>
connected_layer make_connected_layer(int inputs, int outputs, ACTIVATOR_TYPE activator) connected_layer *make_connected_layer(int inputs, int outputs, ACTIVATION activator)
{ {
int i; int i;
connected_layer layer; connected_layer *layer = calloc(1, sizeof(connected_layer));
layer.inputs = inputs; layer->inputs = inputs;
layer.outputs = outputs; layer->outputs = outputs;
layer.output = calloc(outputs, sizeof(double*)); layer->output = calloc(outputs, sizeof(double*));
layer.weight_updates = calloc(inputs*outputs, sizeof(double)); layer->weight_updates = calloc(inputs*outputs, sizeof(double));
layer.weights = calloc(inputs*outputs, sizeof(double)); layer->weights = calloc(inputs*outputs, sizeof(double));
for(i = 0; i < inputs*outputs; ++i) for(i = 0; i < inputs*outputs; ++i)
layer.weights[i] = .5 - (double)rand()/RAND_MAX; layer->weights[i] = .5 - (double)rand()/RAND_MAX;
layer.bias_updates = calloc(outputs, sizeof(double)); layer->bias_updates = calloc(outputs, sizeof(double));
layer.biases = calloc(outputs, sizeof(double)); layer->biases = calloc(outputs, sizeof(double));
for(i = 0; i < outputs; ++i) for(i = 0; i < outputs; ++i)
layer.biases[i] = (double)rand()/RAND_MAX; layer->biases[i] = (double)rand()/RAND_MAX;
if(activator == SIGMOID){ if(activator == SIGMOID){
layer.activation = sigmoid_activation; layer->activation = sigmoid_activation;
layer.gradient = sigmoid_gradient; layer->gradient = sigmoid_gradient;
}else if(activator == RELU){ }else if(activator == RELU){
layer.activation = relu_activation; layer->activation = relu_activation;
layer.gradient = relu_gradient; layer->gradient = relu_gradient;
}else if(activator == IDENTITY){ }else if(activator == IDENTITY){
layer.activation = identity_activation; layer->activation = identity_activation;
layer.gradient = identity_gradient; layer->gradient = identity_gradient;
} }
return layer; return layer;

View File

@ -16,7 +16,7 @@ typedef struct{
double (* gradient)(); double (* gradient)();
} connected_layer; } connected_layer;
connected_layer make_connected_layer(int inputs, int outputs, ACTIVATOR_TYPE activator); connected_layer *make_connected_layer(int inputs, int outputs, ACTIVATION activator);
void run_connected_layer(double *input, connected_layer layer); void run_connected_layer(double *input, connected_layer layer);
void learn_connected_layer(double *input, connected_layer layer); void learn_connected_layer(double *input, connected_layer layer);

View File

@ -10,20 +10,20 @@ double convolution_gradient(double x)
return (x>=0); return (x>=0);
} }
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)
{ {
int i; int i;
convolutional_layer layer; convolutional_layer *layer = calloc(1, sizeof(convolutional_layer));
layer.n = n; layer->n = n;
layer.stride = stride; layer->stride = stride;
layer.kernels = calloc(n, sizeof(image)); layer->kernels = calloc(n, sizeof(image));
layer.kernel_updates = calloc(n, sizeof(image)); layer->kernel_updates = calloc(n, sizeof(image));
for(i = 0; i < n; ++i){ for(i = 0; i < n; ++i){
layer.kernels[i] = make_random_kernel(size, c); layer->kernels[i] = make_random_kernel(size, c);
layer.kernel_updates[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 = make_image((h-1)/stride+1, (w-1)/stride+1, n);
layer.upsampled = make_image(h,w,n); layer->upsampled = make_image(h,w,n);
return layer; return layer;
} }

View File

@ -12,9 +12,12 @@ typedef struct {
image output; image output;
} convolutional_layer; } convolutional_layer;
convolutional_layer make_convolutional_layer(int w, int h, 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);
void run_convolutional_layer(const image input, const convolutional_layer layer); void run_convolutional_layer(const image input, const convolutional_layer layer);
void learn_convolutional_layer(image input, convolutional_layer layer); void learn_convolutional_layer(image input, convolutional_layer layer);
void update_convolutional_layer(convolutional_layer layer, double step);
void backpropagate_convolutional_layer(image input, convolutional_layer layer);
void backpropagate_convolutional_layer_convolve(image input, convolutional_layer layer);
#endif #endif

View File

@ -14,6 +14,7 @@ void normalize_image(image p);
void threshold_image(image p, double t); void threshold_image(image p, double t);
void zero_image(image m); void zero_image(image m);
void rotate_image(image m); void rotate_image(image m);
void subtract_image(image a, image b);
void show_image(image p, char *name); void show_image(image p, char *name);
void show_image_layers(image p, char *name); void show_image_layers(image p, char *name);

View File

@ -1,10 +1,10 @@
#include "maxpool_layer.h" #include "maxpool_layer.h"
maxpool_layer make_maxpool_layer(int h, int w, int c, int stride) maxpool_layer *make_maxpool_layer(int h, int w, int c, int stride)
{ {
maxpool_layer layer; maxpool_layer *layer = calloc(1, sizeof(maxpool_layer));
layer.stride = stride; layer->stride = stride;
layer.output = make_image((h-1)/stride+1, (w-1)/stride+1, c); layer->output = make_image((h-1)/stride+1, (w-1)/stride+1, c);
return layer; return layer;
} }

View File

@ -8,7 +8,7 @@ typedef struct {
image output; image output;
} maxpool_layer; } maxpool_layer;
maxpool_layer make_maxpool_layer(int h, int w, int c, int stride); maxpool_layer *make_maxpool_layer(int h, int w, int c, int stride);
void run_maxpool_layer(const image input, const maxpool_layer layer); void run_maxpool_layer(const image input, const maxpool_layer layer);
#endif #endif

View File

@ -5,6 +5,15 @@
#include "convolutional_layer.h" #include "convolutional_layer.h"
#include "maxpool_layer.h" #include "maxpool_layer.h"
network make_network(int n)
{
network net;
net.n = n;
net.layers = calloc(net.n, sizeof(void *));
net.types = calloc(net.n, sizeof(LAYER_TYPE));
return net;
}
void run_network(image input, network net) void run_network(image input, network net)
{ {
int i; int i;
@ -84,9 +93,9 @@ void learn_network(image input, network net)
} }
} }
double *get_network_output(network net)
double *get_network_output_layer(network net, int i)
{ {
int i = net.n-1;
if(net.types[i] == CONVOLUTIONAL){ if(net.types[i] == CONVOLUTIONAL){
convolutional_layer layer = *(convolutional_layer *)net.layers[i]; convolutional_layer layer = *(convolutional_layer *)net.layers[i];
return layer.output.data; return layer.output.data;
@ -101,6 +110,43 @@ double *get_network_output(network net)
} }
return 0; return 0;
} }
int get_network_output_size_layer(network net, int i)
{
if(net.types[i] == CONVOLUTIONAL){
convolutional_layer layer = *(convolutional_layer *)net.layers[i];
return layer.output.h*layer.output.w*layer.output.c;
}
else if(net.types[i] == MAXPOOL){
maxpool_layer layer = *(maxpool_layer *)net.layers[i];
return layer.output.h*layer.output.w*layer.output.c;
}
else if(net.types[i] == CONNECTED){
connected_layer layer = *(connected_layer *)net.layers[i];
return layer.outputs;
}
return 0;
}
double *get_network_output(network net)
{
int i = net.n-1;
return get_network_output_layer(net, i);
}
image get_network_image_layer(network net, int i)
{
if(net.types[i] == CONVOLUTIONAL){
convolutional_layer layer = *(convolutional_layer *)net.layers[i];
return layer.output;
}
else if(net.types[i] == MAXPOOL){
maxpool_layer layer = *(maxpool_layer *)net.layers[i];
return layer.output;
}
return make_image(0,0,0);
}
image get_network_image(network net) image get_network_image(network net)
{ {
int i; int i;

View File

@ -16,11 +16,15 @@ typedef struct {
LAYER_TYPE *types; LAYER_TYPE *types;
} network; } network;
network make_network(int n);
void run_network(image input, network net); void run_network(image input, network net);
double *get_network_output(network net);
void learn_network(image input, network net); void learn_network(image input, network net);
void update_network(network net, double step); void update_network(network net, double step);
double *get_network_output(network net);
double *get_network_output_layer(network net, int i);
int get_network_output_size_layer(network net, int i);
image get_network_image(network net); image get_network_image(network net);
image get_network_image_layer(network net, int i);
#endif #endif

View File

@ -3,6 +3,7 @@
#include "maxpool_layer.h" #include "maxpool_layer.h"
#include "network.h" #include "network.h"
#include "image.h" #include "image.h"
#include "parser.h"
#include <time.h> #include <time.h>
#include <stdlib.h> #include <stdlib.h>
@ -39,7 +40,7 @@ void test_convolutional_layer()
int n = 3; int n = 3;
int stride = 1; int stride = 1;
int size = 3; int size = 3;
convolutional_layer layer = make_convolutional_layer(dog.h, dog.w, dog.c, n, size, stride); convolutional_layer layer = *make_convolutional_layer(dog.h, dog.w, dog.c, n, size, stride);
char buff[256]; char buff[256];
for(i = 0; i < n; ++i) { for(i = 0; i < n; ++i) {
sprintf(buff, "Kernel %d", i); sprintf(buff, "Kernel %d", i);
@ -47,7 +48,7 @@ void test_convolutional_layer()
} }
run_convolutional_layer(dog, layer); run_convolutional_layer(dog, layer);
maxpool_layer mlayer = make_maxpool_layer(layer.output.h, layer.output.w, layer.output.c, 2); maxpool_layer mlayer = *make_maxpool_layer(layer.output.h, layer.output.w, layer.output.c, 2);
run_maxpool_layer(layer.output,mlayer); run_maxpool_layer(layer.output,mlayer);
show_image_layers(mlayer.output, "Test Maxpool Layer"); show_image_layers(mlayer.output, "Test Maxpool Layer");
@ -112,25 +113,25 @@ void test_network()
int n = 48; int n = 48;
int stride = 4; int stride = 4;
int size = 11; int size = 11;
convolutional_layer cl = make_convolutional_layer(dog.h, dog.w, dog.c, n, size, stride); convolutional_layer cl = *make_convolutional_layer(dog.h, dog.w, dog.c, n, size, stride);
maxpool_layer ml = make_maxpool_layer(cl.output.h, cl.output.w, cl.output.c, 2); maxpool_layer ml = *make_maxpool_layer(cl.output.h, cl.output.w, cl.output.c, 2);
n = 128; n = 128;
size = 5; size = 5;
stride = 1; stride = 1;
convolutional_layer cl2 = make_convolutional_layer(ml.output.h, ml.output.w, ml.output.c, n, size, stride); convolutional_layer cl2 = *make_convolutional_layer(ml.output.h, ml.output.w, ml.output.c, n, size, stride);
maxpool_layer ml2 = make_maxpool_layer(cl2.output.h, cl2.output.w, cl2.output.c, 2); maxpool_layer ml2 = *make_maxpool_layer(cl2.output.h, cl2.output.w, cl2.output.c, 2);
n = 192; n = 192;
size = 3; size = 3;
convolutional_layer cl3 = make_convolutional_layer(ml2.output.h, ml2.output.w, ml2.output.c, n, size, stride); convolutional_layer cl3 = *make_convolutional_layer(ml2.output.h, ml2.output.w, ml2.output.c, n, size, stride);
convolutional_layer cl4 = make_convolutional_layer(cl3.output.h, cl3.output.w, cl3.output.c, n, size, stride); convolutional_layer cl4 = *make_convolutional_layer(cl3.output.h, cl3.output.w, cl3.output.c, n, size, stride);
n = 128; n = 128;
convolutional_layer cl5 = make_convolutional_layer(cl4.output.h, cl4.output.w, cl4.output.c, n, size, stride); convolutional_layer cl5 = *make_convolutional_layer(cl4.output.h, cl4.output.w, cl4.output.c, n, size, stride);
maxpool_layer ml3 = make_maxpool_layer(cl5.output.h, cl5.output.w, cl5.output.c, 4); maxpool_layer ml3 = *make_maxpool_layer(cl5.output.h, cl5.output.w, cl5.output.c, 4);
connected_layer nl = make_connected_layer(ml3.output.h*ml3.output.w*ml3.output.c, 4096, RELU); connected_layer nl = *make_connected_layer(ml3.output.h*ml3.output.w*ml3.output.c, 4096, RELU);
connected_layer nl2 = make_connected_layer(4096, 4096, RELU); connected_layer nl2 = *make_connected_layer(4096, 4096, RELU);
connected_layer nl3 = make_connected_layer(4096, 1000, RELU); connected_layer nl3 = *make_connected_layer(4096, 1000, RELU);
net.layers[0] = &cl; net.layers[0] = &cl;
net.layers[1] = &ml; net.layers[1] = &ml;
@ -164,7 +165,7 @@ void test_backpropagate()
image dog = load_image("dog.jpg"); image dog = load_image("dog.jpg");
show_image(dog, "Test Backpropagate Input"); show_image(dog, "Test Backpropagate Input");
image dog_copy = copy_image(dog); image dog_copy = copy_image(dog);
convolutional_layer cl = make_convolutional_layer(dog.h, dog.w, dog.c, n, size, stride); convolutional_layer cl = *make_convolutional_layer(dog.h, dog.w, dog.c, n, size, stride);
run_convolutional_layer(dog, cl); run_convolutional_layer(dog, cl);
show_image(cl.output, "Test Backpropagate Output"); show_image(cl.output, "Test Backpropagate Output");
int i; int i;
@ -196,9 +197,9 @@ void test_ann()
net.types[1] = CONNECTED; net.types[1] = CONNECTED;
net.types[2] = CONNECTED; net.types[2] = CONNECTED;
connected_layer nl = make_connected_layer(1, 20, RELU); connected_layer nl = *make_connected_layer(1, 20, RELU);
connected_layer nl2 = make_connected_layer(20, 20, RELU); connected_layer nl2 = *make_connected_layer(20, 20, RELU);
connected_layer nl3 = make_connected_layer(20, 1, RELU); connected_layer nl3 = *make_connected_layer(20, 1, RELU);
net.layers[0] = &nl; net.layers[0] = &nl;
net.layers[1] = &nl2; net.layers[1] = &nl2;
@ -225,10 +226,34 @@ void test_ann()
} }
void test_parser()
{
network net = parse_network_cfg("test.cfg");
image t = make_image(1,1,1);
int count = 0;
double avgerr = 0;
while(1){
double v = ((double)rand()/RAND_MAX);
double truth = v*v;
set_pixel(t,0,0,0,v);
run_network(t, net);
double *out = get_network_output(net);
double err = pow((out[0]-truth),2.);
avgerr = .99 * avgerr + .01 * err;
//if(++count % 100000 == 0) printf("%f\n", avgerr);
if(++count % 100000 == 0) printf("%f %f :%f AVG %f \n", truth, out[0], err, avgerr);
out[0] = truth - out[0];
learn_network(t, net);
update_network(net, .001);
}
}
int main() int main()
{ {
test_parser();
//test_backpropagate(); //test_backpropagate();
test_ann(); //test_ann();
//test_convolve(); //test_convolve();
//test_upsample(); //test_upsample();
//test_rotate(); //test_rotate();