some fixes

This commit is contained in:
Joseph Redmon 2015-12-18 15:55:58 -08:00
parent 81c23650e1
commit 9802287b58
6 changed files with 48 additions and 20 deletions

View File

@ -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; 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) void copy_cpu(int N, float *X, int INCX, float *Y, int INCY)
{ {
int i; int i;

View File

@ -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 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 copy_cpu(int N, float *X, int INCX, float *Y, int INCY);
void scal_cpu(int N, float ALPHA, float *X, int INCX); 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); float dot_cpu(int N, float *X, int INCX, float *Y, int INCY);
void test_gpu_blas(); 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); void shortcut_cpu(float *out, int w, int h, int c, int batch, int sample, float *add, int stride, int c2);

View File

@ -143,7 +143,7 @@ void validate_classifier(char *datacfg, char *filename, char *weightfile)
clock_t time; clock_t time;
float avg_acc = 0; float avg_acc = 0;
float avg_topk = 0; float avg_topk = 0;
int splits = 50; int splits = m/1000;
int num = (i+1)*m/splits - i*m/splits; int num = (i+1)*m/splits - i*m/splits;
data val, buffer; data val, buffer;
@ -201,7 +201,7 @@ void predict_classifier(char *datacfg, char *cfgfile, char *weightfile, char *fi
int i = 0; int i = 0;
char **names = get_labels(name_list); char **names = get_labels(name_list);
clock_t time; clock_t time;
int indexes[10]; int *indexes = calloc(top, sizeof(int));
char buff[256]; char buff[256];
char *input = buff; char *input = buff;
while(1){ while(1){
@ -214,7 +214,7 @@ void predict_classifier(char *datacfg, char *cfgfile, char *weightfile, char *fi
if(!input) return; if(!input) return;
strtok(input, "\n"); 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; float *X = im.data;
time=clock(); time=clock();
float *predictions = network_predict(net, X); 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; int curr = 0;
network net = parse_network_cfg(filename); network net = parse_network_cfg(cfgfile);
if(weightfile){ if(weightfile){
load_weights(&net, 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); list *options = read_data_cfg(datacfg);
char *test_list = option_find_str(options, "test", "data/test.list"); 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); int classes = option_find_int(options, "classes", 2);
char **labels = get_labels(label_list);
list *plist = get_paths(test_list); list *plist = get_paths(test_list);
char **paths = (char **)list_to_array(plist); 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.classes = classes;
args.n = net.batch; args.n = net.batch;
args.m = 0; args.m = 0;
args.labels = labels; args.labels = 0;
args.d = &buffer; args.d = &buffer;
args.type = CLASSIFICATION_DATA; args.type = CLASSIFICATION_DATA;
@ -283,13 +281,17 @@ void test_classifier(char *datacfg, char *cfgfile, char *weightfile, char *filen
time=clock(); time=clock();
matrix pred = network_predict_data(net, val); matrix pred = network_predict_data(net, val);
int i; int i, j;
if (target_layer >= 0){ if (target_layer >= 0){
//layer l = net.layers[target_layer]; //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); free_matrix(pred);
@ -315,7 +317,7 @@ void run_classifier(int argc, char **argv)
int layer = layer_s ? atoi(layer_s) : -1; int layer = layer_s ? atoi(layer_s) : -1;
if(0==strcmp(argv[2], "predict")) predict_classifier(data, cfg, weights, filename); 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], "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); else if(0==strcmp(argv[2], "valid")) validate_classifier(data, cfg, weights);
} }

View File

@ -194,13 +194,25 @@ void resize_convolutional_layer(convolutional_layer *l, int w, int h)
#endif #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; int i,j,b;
for(b = 0; b < batch; ++b){ for(b = 0; b < batch; ++b){
for(i = 0; i < n; ++i){ for(i = 0; i < n; ++i){
for(j = 0; j < size; ++j){ 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 out_w = convolutional_out_width(l);
int i; 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 m = l.n;
int k = l.size*l.size*l.c; 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){ if(l.batch_normalize){
mean_cpu(l.output, l.batch, l.n, l.out_h*l.out_w, l.mean); if(state.train){
variance_cpu(l.output, l.mean, l.batch, l.n, l.out_h*l.out_w, l.variance); mean_cpu(l.output, l.batch, l.n, l.out_h*l.out_w, l.mean);
normalize_cpu(l.output, l.mean, l.variance, l.batch, l.n, l.out_h*l.out_w); 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); activate_array(l.output, m*n*l.batch, l.activation);
} }

View File

@ -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 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); void backward_bias(float *bias_updates, float *delta, int batch, int n, int size);
image get_convolutional_image(convolutional_layer layer); image get_convolutional_image(convolutional_layer layer);

View File

@ -134,7 +134,7 @@ void forward_deconvolutional_layer(const deconvolutional_layer l, network_state
int n = l.h*l.w; int n = l.h*l.w;
int k = l.c; 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){ for(i = 0; i < l.batch; ++i){
float *a = l.filters; 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); 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); activate_array(l.output, l.batch*l.n*size, l.activation);
} }