From 9802287b5890d9b2cc250adba1b9810657a95c9c Mon Sep 17 00:00:00 2001 From: Joseph Redmon Date: Fri, 18 Dec 2015 15:55:58 -0800 Subject: [PATCH] some fixes --- src/blas.c | 6 ++++++ src/blas.h | 1 + src/classifier.c | 26 ++++++++++++++------------ src/convolutional_layer.c | 30 ++++++++++++++++++++++++------ src/convolutional_layer.h | 2 +- src/deconvolutional_layer.c | 3 ++- 6 files changed, 48 insertions(+), 20 deletions(-) diff --git a/src/blas.c b/src/blas.c index 941109ef..556603cf 100644 --- a/src/blas.c +++ b/src/blas.c @@ -92,6 +92,12 @@ void scal_cpu(int N, float ALPHA, float *X, int INCX) for(i = 0; i < N; ++i) X[i*INCX] *= ALPHA; } +void fill_cpu(int N, float ALPHA, float *X, int INCX) +{ + int i; + for(i = 0; i < N; ++i) X[i*INCX] = ALPHA; +} + void copy_cpu(int N, float *X, int INCX, float *Y, int INCY) { int i; diff --git a/src/blas.h b/src/blas.h index 023024a4..208fdaa5 100644 --- a/src/blas.h +++ b/src/blas.h @@ -13,6 +13,7 @@ void mul_cpu(int N, float *X, int INCX, float *Y, int INCY); void axpy_cpu(int N, float ALPHA, float *X, int INCX, float *Y, int INCY); void copy_cpu(int N, float *X, int INCX, float *Y, int INCY); void scal_cpu(int N, float ALPHA, float *X, int INCX); +void fill_cpu(int N, float ALPHA, float * X, int INCX); float dot_cpu(int N, float *X, int INCX, float *Y, int INCY); void test_gpu_blas(); void shortcut_cpu(float *out, int w, int h, int c, int batch, int sample, float *add, int stride, int c2); diff --git a/src/classifier.c b/src/classifier.c index 8a3ae5af..ddd88b19 100644 --- a/src/classifier.c +++ b/src/classifier.c @@ -143,7 +143,7 @@ void validate_classifier(char *datacfg, char *filename, char *weightfile) clock_t time; float avg_acc = 0; float avg_topk = 0; - int splits = 50; + int splits = m/1000; int num = (i+1)*m/splits - i*m/splits; data val, buffer; @@ -201,7 +201,7 @@ void predict_classifier(char *datacfg, char *cfgfile, char *weightfile, char *fi int i = 0; char **names = get_labels(name_list); clock_t time; - int indexes[10]; + int *indexes = calloc(top, sizeof(int)); char buff[256]; char *input = buff; while(1){ @@ -214,7 +214,7 @@ void predict_classifier(char *datacfg, char *cfgfile, char *weightfile, char *fi if(!input) return; strtok(input, "\n"); } - image im = load_image_color(input, 256, 256); + image im = load_image_color(input, net.w, net.h); float *X = im.data; time=clock(); float *predictions = network_predict(net, X); @@ -229,10 +229,10 @@ void predict_classifier(char *datacfg, char *cfgfile, char *weightfile, char *fi } } -void test_classifier(char *datacfg, char *cfgfile, char *weightfile, char *filename, int target_layer) +void test_classifier(char *datacfg, char *cfgfile, char *weightfile, int target_layer) { int curr = 0; - network net = parse_network_cfg(filename); + network net = parse_network_cfg(cfgfile); if(weightfile){ load_weights(&net, weightfile); } @@ -241,10 +241,8 @@ void test_classifier(char *datacfg, char *cfgfile, char *weightfile, char *filen list *options = read_data_cfg(datacfg); char *test_list = option_find_str(options, "test", "data/test.list"); - char *label_list = option_find_str(options, "labels", "data/labels.list"); int classes = option_find_int(options, "classes", 2); - char **labels = get_labels(label_list); list *plist = get_paths(test_list); char **paths = (char **)list_to_array(plist); @@ -262,7 +260,7 @@ void test_classifier(char *datacfg, char *cfgfile, char *weightfile, char *filen args.classes = classes; args.n = net.batch; args.m = 0; - args.labels = labels; + args.labels = 0; args.d = &buffer; args.type = CLASSIFICATION_DATA; @@ -283,13 +281,17 @@ void test_classifier(char *datacfg, char *cfgfile, char *weightfile, char *filen time=clock(); matrix pred = network_predict_data(net, val); - int i; + int i, j; if (target_layer >= 0){ //layer l = net.layers[target_layer]; } - for(i = 0; i < val.X.rows; ++i){ - + for(i = 0; i < pred.rows; ++i){ + printf("%s", paths[curr-net.batch+i]); + for(j = 0; j < pred.cols; ++j){ + printf("\t%g", pred.vals[i][j]); + } + printf("\n"); } free_matrix(pred); @@ -315,7 +317,7 @@ void run_classifier(int argc, char **argv) int layer = layer_s ? atoi(layer_s) : -1; if(0==strcmp(argv[2], "predict")) predict_classifier(data, cfg, weights, filename); else if(0==strcmp(argv[2], "train")) train_classifier(data, cfg, weights); - else if(0==strcmp(argv[2], "test")) test_classifier(data, cfg, weights,filename, layer); + else if(0==strcmp(argv[2], "test")) test_classifier(data, cfg, weights, layer); else if(0==strcmp(argv[2], "valid")) validate_classifier(data, cfg, weights); } diff --git a/src/convolutional_layer.c b/src/convolutional_layer.c index ec571a69..e97b00d7 100644 --- a/src/convolutional_layer.c +++ b/src/convolutional_layer.c @@ -194,13 +194,25 @@ void resize_convolutional_layer(convolutional_layer *l, int w, int h) #endif } -void bias_output(float *output, float *biases, int batch, int n, int size) +void add_bias(float *output, float *biases, int batch, int n, int size) { int i,j,b; for(b = 0; b < batch; ++b){ for(i = 0; i < n; ++i){ for(j = 0; j < size; ++j){ - output[(b*n + i)*size + j] = biases[i]; + output[(b*n + i)*size + j] += biases[i]; + } + } + } +} + +void scale_bias(float *output, float *scales, int batch, int n, int size) +{ + int i,j,b; + for(b = 0; b < batch; ++b){ + for(i = 0; i < n; ++i){ + for(j = 0; j < size; ++j){ + output[(b*n + i)*size + j] *= scales[i]; } } } @@ -222,7 +234,7 @@ void forward_convolutional_layer(const convolutional_layer l, network_state stat int out_w = convolutional_out_width(l); int i; - bias_output(l.output, l.biases, l.batch, l.n, out_h*out_w); + fill_cpu(l.outputs*l.batch, 0, l.output, 1); int m = l.n; int k = l.size*l.size*l.c; @@ -241,10 +253,16 @@ void forward_convolutional_layer(const convolutional_layer l, network_state stat } if(l.batch_normalize){ - mean_cpu(l.output, l.batch, l.n, l.out_h*l.out_w, l.mean); - variance_cpu(l.output, l.mean, l.batch, l.n, l.out_h*l.out_w, l.variance); - normalize_cpu(l.output, l.mean, l.variance, l.batch, l.n, l.out_h*l.out_w); + if(state.train){ + mean_cpu(l.output, l.batch, l.n, l.out_h*l.out_w, l.mean); + variance_cpu(l.output, l.mean, l.batch, l.n, l.out_h*l.out_w, l.variance); + normalize_cpu(l.output, l.mean, l.variance, l.batch, l.n, l.out_h*l.out_w); + } else { + normalize_cpu(l.output, l.rolling_mean, l.rolling_variance, l.batch, l.n, l.out_h*l.out_w); + } + scale_bias(l.output, l.scales, l.batch, l.n, out_h*out_w); } + add_bias(l.output, l.biases, l.batch, l.n, out_h*out_w); activate_array(l.output, m*n*l.batch, l.activation); } diff --git a/src/convolutional_layer.h b/src/convolutional_layer.h index 436ed7e9..e22a3960 100644 --- a/src/convolutional_layer.h +++ b/src/convolutional_layer.h @@ -31,7 +31,7 @@ image *visualize_convolutional_layer(convolutional_layer layer, char *window, im void backward_convolutional_layer(convolutional_layer layer, network_state state); -void bias_output(float *output, float *biases, int batch, int n, int size); +void add_bias(float *output, float *biases, int batch, int n, int size); void backward_bias(float *bias_updates, float *delta, int batch, int n, int size); image get_convolutional_image(convolutional_layer layer); diff --git a/src/deconvolutional_layer.c b/src/deconvolutional_layer.c index 0f4e1e85..d4769577 100644 --- a/src/deconvolutional_layer.c +++ b/src/deconvolutional_layer.c @@ -134,7 +134,7 @@ void forward_deconvolutional_layer(const deconvolutional_layer l, network_state int n = l.h*l.w; int k = l.c; - bias_output(l.output, l.biases, l.batch, l.n, size); + fill_cpu(l.outputs*l.batch, 0, l.output, 1); for(i = 0; i < l.batch; ++i){ float *a = l.filters; @@ -145,6 +145,7 @@ void forward_deconvolutional_layer(const deconvolutional_layer l, network_state col2im_cpu(c, l.n, out_h, out_w, l.size, l.stride, 0, l.output+i*l.n*size); } + add_bias(l.output, l.biases, l.batch, l.n, size); activate_array(l.output, l.batch*l.n*size, l.activation); }