From 741ada451cc7fee1b9a4c3deaec6af87a2af7497 Mon Sep 17 00:00:00 2001 From: Joseph Redmon Date: Mon, 17 Aug 2015 09:00:12 -0700 Subject: [PATCH] Added Darknet reference model --- cfg/darknet.cfg | 154 ++++++++++++++++++++++++++++++++++++++++++++++++ src/darknet.c | 54 ++++++++++++++++- 2 files changed, 206 insertions(+), 2 deletions(-) create mode 100644 cfg/darknet.cfg diff --git a/cfg/darknet.cfg b/cfg/darknet.cfg new file mode 100644 index 00000000..8a5978e8 --- /dev/null +++ b/cfg/darknet.cfg @@ -0,0 +1,154 @@ +[net] +batch=128 +subdivisions=1 +height=256 +width=256 +channels=3 +learning_rate=0.01 +momentum=0.9 +decay=0.0005 + +[crop] +crop_height=224 +crop_width=224 +flip=1 +angle=0 +saturation=1 +exposure=1 + +[convolutional] +filters=16 +size=3 +stride=1 +pad=1 +activation=leaky + +#[convolutional] +#filters=16 +#size=3 +#stride=1 +#pad=1 +#activation=leaky + +[maxpool] +stride=2 +size=2 + +[convolutional] +filters=32 +size=3 +stride=1 +pad=1 +activation=leaky + +#[convolutional] +#filters=32 +#size=3 +#stride=1 +#pad=1 +#activation=leaky + +[maxpool] +stride=2 +size=2 + +[convolutional] +filters=64 +size=3 +stride=1 +pad=1 +activation=leaky + +#[convolutional] +#filters=64 +#size=3 +#stride=1 +#pad=1 +#activation=leaky + +[maxpool] +stride=2 +size=2 + +[convolutional] +filters=128 +size=3 +stride=1 +pad=1 +activation=leaky + +#[convolutional] +#filters=128 +#size=3 +#stride=1 +#pad=1 +#activation=leaky + +[maxpool] +stride=2 +size=2 + +[convolutional] +filters=256 +size=3 +stride=1 +pad=1 +activation=leaky + +#[convolutional] +#filters=256 +#size=3 +#stride=1 +#pad=1 +#activation=leaky + +[maxpool] +stride=2 +size=2 + +[convolutional] +filters=512 +size=3 +stride=1 +pad=1 +activation=leaky + +#[convolutional] +#filters=512 +#size=3 +#stride=1 +#pad=1 +#activation=leaky + +[maxpool] +stride=2 +size=2 + +[convolutional] +filters=1024 +size=3 +stride=1 +pad=1 +activation=leaky + +#[convolutional] +#filters=1024 +#size=3 +#stride=1 +#pad=1 +#activation=leaky + +[avgpool] + +[dropout] +probability=.5 + +[connected] +output=1000 +activation=leaky + +[softmax] + +[cost] +type=sse + diff --git a/src/darknet.c b/src/darknet.c index bd56b807..9b6eadbb 100644 --- a/src/darknet.c +++ b/src/darknet.c @@ -5,6 +5,7 @@ #include "parser.h" #include "utils.h" #include "cuda.h" +#include "blas.h" #ifdef OPENCV #include "opencv2/highgui/highgui_c.h" @@ -33,8 +34,55 @@ void change_rate(char *filename, float scale, float add) fclose(fp); } +void average(int argc, char *argv[]) +{ + char *cfgfile = argv[2]; + char *outfile = argv[3]; + gpu_index = -1; + network net = parse_network_cfg(cfgfile); + network sum = parse_network_cfg(cfgfile); + + char *weightfile = argv[4]; + load_weights(&sum, weightfile); + + int i, j; + int n = argc - 5; + for(i = 0; i < n; ++i){ + weightfile = argv[i+5]; + load_weights(&net, weightfile); + for(j = 0; j < net.n; ++j){ + layer l = net.layers[j]; + layer out = sum.layers[j]; + if(l.type == CONVOLUTIONAL){ + int num = l.n*l.c*l.size*l.size; + axpy_cpu(l.n, 1, l.biases, 1, out.biases, 1); + axpy_cpu(num, 1, l.filters, 1, out.filters, 1); + } + if(l.type == CONNECTED){ + axpy_cpu(l.outputs, 1, l.biases, 1, out.biases, 1); + axpy_cpu(l.outputs*l.inputs, 1, l.weights, 1, out.weights, 1); + } + } + } + n = n+1; + for(j = 0; j < net.n; ++j){ + layer l = sum.layers[j]; + if(l.type == CONVOLUTIONAL){ + int num = l.n*l.c*l.size*l.size; + scal_cpu(l.n, 1./n, l.biases, 1); + scal_cpu(num, 1./n, l.filters, 1); + } + if(l.type == CONNECTED){ + scal_cpu(l.outputs, 1./n, l.biases, 1); + scal_cpu(l.outputs*l.inputs, 1./n, l.weights, 1); + } + } + save_weights(sum, outfile); +} + void partial(char *cfgfile, char *weightfile, char *outfile, int max) { + gpu_index = -1; network net = parse_network_cfg(cfgfile); if(weightfile){ load_weights_upto(&net, weightfile, max); @@ -87,9 +135,9 @@ void visualize(char *cfgfile, char *weightfile) load_weights(&net, weightfile); } visualize_network(net); - #ifdef OPENCV +#ifdef OPENCV cvWaitKey(0); - #endif +#endif } int main(int argc, char **argv) @@ -114,6 +162,8 @@ int main(int argc, char **argv) if(0==strcmp(argv[1], "imagenet")){ run_imagenet(argc, argv); + } else if (0 == strcmp(argv[1], "average")){ + average(argc, argv); } else if (0 == strcmp(argv[1], "detection")){ run_detection(argc, argv); } else if (0 == strcmp(argv[1], "yolo")){