diff --git a/src/crop_layer.c b/src/crop_layer.c index df6eb41d..3f0011d4 100644 --- a/src/crop_layer.c +++ b/src/crop_layer.c @@ -28,14 +28,19 @@ crop_layer *make_crop_layer(int batch, int h, int w, int c, int crop_height, int return layer; } -void forward_crop_layer(const crop_layer layer, float *input) +void forward_crop_layer(const crop_layer layer, int train, float *input) { int i,j,c,b,row,col; int index; int count = 0; int flip = (layer.flip && rand()%2); - int dh = rand()%(layer.h - layer.crop_height); - int dw = rand()%(layer.w - layer.crop_width); + int dh = rand()%(layer.h - layer.crop_height + 1); + int dw = rand()%(layer.w - layer.crop_width + 1); + if(!train){ + flip = 0; + dh = (layer.h - layer.crop_height)/2; + dw = (layer.w - layer.crop_width)/2; + } for(b = 0; b < layer.batch; ++b){ for(c = 0; c < layer.c; ++c){ for(i = 0; i < layer.crop_height; ++i){ diff --git a/src/crop_layer.h b/src/crop_layer.h index 4b4ec875..0d2f03b7 100644 --- a/src/crop_layer.h +++ b/src/crop_layer.h @@ -17,10 +17,10 @@ typedef struct { image get_crop_image(crop_layer layer); crop_layer *make_crop_layer(int batch, int h, int w, int c, int crop_height, int crop_width, int flip); -void forward_crop_layer(const crop_layer layer, float *input); +void forward_crop_layer(const crop_layer layer, int train, float *input); #ifdef GPU -void forward_crop_layer_gpu(crop_layer layer, float *input); +void forward_crop_layer_gpu(crop_layer layer, int train, float *input); #endif #endif diff --git a/src/crop_layer_kernels.cu b/src/crop_layer_kernels.cu index 00ecca5e..628c7000 100644 --- a/src/crop_layer_kernels.cu +++ b/src/crop_layer_kernels.cu @@ -24,11 +24,16 @@ __global__ void forward_crop_layer_kernel(float *input, int size, int c, int h, output[count] = input[index]; } -extern "C" void forward_crop_layer_gpu(crop_layer layer, float *input) +extern "C" void forward_crop_layer_gpu(crop_layer layer, int train, float *input) { int flip = (layer.flip && rand()%2); - int dh = rand()%(layer.h - layer.crop_height); - int dw = rand()%(layer.w - layer.crop_width); + int dh = rand()%(layer.h - layer.crop_height + 1); + int dw = rand()%(layer.w - layer.crop_width + 1); + if(!train){ + flip = 0; + dh = (layer.h - layer.crop_height)/2; + dw = (layer.w - layer.crop_width)/2; + } int size = layer.batch*layer.c*layer.crop_width*layer.crop_height; dim3 dimBlock(BLOCK, 1, 1); diff --git a/src/cuda.c b/src/cuda.c index 27153eac..8849fb1f 100644 --- a/src/cuda.c +++ b/src/cuda.c @@ -1,9 +1,12 @@ +int gpu_index = 0; + +#ifdef GPU + #include "cuda.h" #include "utils.h" #include "blas.h" #include -int gpu_index = 0; void check_error(cudaError_t status) { @@ -96,4 +99,4 @@ void cuda_pull_array(float *x_gpu, float *x, int n) check_error(status); } - +#endif diff --git a/src/cuda.h b/src/cuda.h index 08c03401..cbe79755 100644 --- a/src/cuda.h +++ b/src/cuda.h @@ -1,13 +1,15 @@ #ifndef CUDA_H #define CUDA_H +extern int gpu_index; + +#ifdef GPU + #define BLOCK 256 #include "cuda_runtime.h" #include "cublas_v2.h" -extern int gpu_index; - void check_error(cudaError_t status); cublasHandle_t blas_handle(); float *cuda_make_array(float *x, int n); @@ -19,3 +21,4 @@ float cuda_compare(float *x_gpu, float *x, int n, char *s); dim3 cuda_gridsize(size_t n); #endif +#endif diff --git a/src/darknet.c b/src/darknet.c index 4f575dc4..64012e0d 100644 --- a/src/darknet.c +++ b/src/darknet.c @@ -209,13 +209,12 @@ void train_imagenet_distributed(char *address) void train_imagenet(char *cfgfile) { float avg_loss = 1; - //network net = parse_network_cfg("/home/pjreddie/imagenet_backup/alexnet_1270.cfg"); srand(time(0)); network net = parse_network_cfg(cfgfile); //test_learn_bias(*(convolutional_layer *)net.layers[1]); //set_learning_network(&net, net.learning_rate, 0, net.decay); printf("Learning Rate: %g, Momentum: %g, Decay: %g\n", net.learning_rate, net.momentum, net.decay); - int imgs = 3072; + int imgs = 1024; int i = net.seen/imgs; char **labels = get_labels("/home/pjreddie/data/imagenet/cls.labels.list"); list *plist = get_paths("/data/imagenet/cls.train.list"); @@ -231,9 +230,6 @@ void train_imagenet(char *cfgfile) time=clock(); pthread_join(load_thread, 0); train = buffer; - //normalize_data_rows(train); - //translate_data_rows(train, -128); - //scale_data_rows(train, 1./128); load_thread = load_data_thread(paths, imgs, plist->size, labels, 1000, 256, 256, &buffer); printf("Loaded: %lf seconds\n", sec(clock()-time)); time=clock(); @@ -244,7 +240,7 @@ void train_imagenet(char *cfgfile) free_data(train); if(i%100==0){ char buff[256]; - sprintf(buff, "/home/pjreddie/imagenet_backup/alexnet_%d.cfg", i); + sprintf(buff, "/home/pjreddie/imagenet_backup/vgg_%d.cfg", i); save_network(net, buff); } } @@ -347,10 +343,28 @@ void test_init(char *cfgfile) } free_image(im); } - -void test_imagenet() +void test_dog(char *cfgfile) { - network net = parse_network_cfg("cfg/imagenet_test.cfg"); + image im = load_image_color("data/dog.jpg", 224, 224); + translate_image(im, -128); + print_image(im); + float *X = im.data; + network net = parse_network_cfg(cfgfile); + set_batch_network(&net, 1); + float *predictions = network_predict(net, X); + image crop = get_network_image_layer(net, 0); + //show_image(crop, "cropped"); + // print_image(crop); + //show_image(im, "orig"); + float * inter = get_network_output(net); + pm(1000, 1, inter); + //cvWaitKey(0); +} + +void test_imagenet(char *cfgfile) +{ + network net = parse_network_cfg(cfgfile); + set_batch_network(&net, 1); //imgs=1; srand(2222222); int i = 0; @@ -362,7 +376,8 @@ void test_imagenet() fgets(filename, 256, stdin); strtok(filename, "\n"); image im = load_image_color(filename, 256, 256); - z_normalize_image(im); + translate_image(im, -128); + //scale_image(im, 1/128.); printf("%d %d %d\n", im.h, im.w, im.c); float *X = im.data; time=clock(); @@ -472,28 +487,28 @@ void train_nist(char *cfgfile) } /* -void train_nist_distributed(char *address) -{ - srand(time(0)); - network net = parse_network_cfg("cfg/nist.client"); - data train = load_categorical_data_csv("data/mnist/mnist_train.csv", 0, 10); - //data test = load_categorical_data_csv("data/mnist/mnist_test.csv",0,10); - normalize_data_rows(train); - //normalize_data_rows(test); - int count = 0; - int iters = 50000/net.batch; - iters = 1000/net.batch + 1; - while(++count <= 2000){ - clock_t start = clock(), end; - float loss = train_network_sgd(net, train, iters); - client_update(net, address); - end = clock(); - //float test_acc = network_accuracy_gpu(net, test); - //float test_acc = 0; - printf("%d: Loss: %f, Time: %lf seconds\n", count, loss, (float)(end-start)/CLOCKS_PER_SEC); - } + void train_nist_distributed(char *address) + { + srand(time(0)); + network net = parse_network_cfg("cfg/nist.client"); + data train = load_categorical_data_csv("data/mnist/mnist_train.csv", 0, 10); +//data test = load_categorical_data_csv("data/mnist/mnist_test.csv",0,10); +normalize_data_rows(train); +//normalize_data_rows(test); +int count = 0; +int iters = 50000/net.batch; +iters = 1000/net.batch + 1; +while(++count <= 2000){ +clock_t start = clock(), end; +float loss = train_network_sgd(net, train, iters); +client_update(net, address); +end = clock(); +//float test_acc = network_accuracy_gpu(net, test); +//float test_acc = 0; +printf("%d: Loss: %f, Time: %lf seconds\n", count, loss, (float)(end-start)/CLOCKS_PER_SEC); } -*/ +} + */ void test_ensemble() { @@ -535,7 +550,7 @@ void test_ensemble() void visualize_cat() { network net = parse_network_cfg("cfg/voc_imagenet.cfg"); - image im = load_image("data/cat.png", 0, 0); + image im = load_image_color("data/cat.png", 0, 0); printf("Processing %dx%d image\n", im.h, im.w); resize_network(net, im.h, im.w, im.c); forward_network(net, im.data, 0, 0); @@ -544,6 +559,7 @@ void visualize_cat() cvWaitKey(0); } +#ifdef GPU void test_convolutional_layer() { network net = parse_network_cfg("cfg/nist_conv.cfg"); @@ -561,6 +577,7 @@ void test_convolutional_layer() bias_output_gpu(layer); cuda_compare(layer.output_gpu, layer.output, out_size, "biased output"); } +#endif void test_correct_nist() { @@ -586,7 +603,7 @@ void test_correct_nist() gpu_index = -1; count = 0; srand(222222); - net = parse_network_cfg("cfg/nist_conv.cfg"); + net = parse_network_cfg("cfg/nist_conv.cfg"); while(++count <= 5){ clock_t start = clock(), end; float loss = train_network_sgd(net, train, iters); @@ -641,27 +658,27 @@ void test_correct_alexnet() } /* -void run_server() -{ - srand(time(0)); - network net = parse_network_cfg("cfg/net.cfg"); - set_batch_network(&net, 1); - server_update(net); -} + void run_server() + { + srand(time(0)); + network net = parse_network_cfg("cfg/net.cfg"); + set_batch_network(&net, 1); + server_update(net); + } -void test_client() -{ - network net = parse_network_cfg("cfg/alexnet.client"); - clock_t time=clock(); - client_update(net, "localhost"); - printf("1\n"); - client_update(net, "localhost"); - printf("2\n"); - client_update(net, "localhost"); - printf("3\n"); - printf("Transfered: %lf seconds\n", sec(clock()-time)); -} -*/ + void test_client() + { + network net = parse_network_cfg("cfg/alexnet.client"); + clock_t time=clock(); + client_update(net, "localhost"); + printf("1\n"); + client_update(net, "localhost"); + printf("2\n"); + client_update(net, "localhost"); + printf("3\n"); + printf("Transfered: %lf seconds\n", sec(clock()-time)); + } + */ void del_arg(int argc, char **argv, int index) { @@ -713,7 +730,6 @@ int main(int argc, char **argv) if(0==strcmp(argv[1], "test_correct")) test_correct_alexnet(); else if(0==strcmp(argv[1], "test_correct_nist")) test_correct_nist(); - else if(0==strcmp(argv[1], "test")) test_imagenet(); //else if(0==strcmp(argv[1], "server")) run_server(); #ifdef GPU @@ -725,6 +741,8 @@ int main(int argc, char **argv) return 0; } else if(0==strcmp(argv[1], "detection")) train_detection_net(argv[2]); + else if(0==strcmp(argv[1], "test")) test_imagenet(argv[2]); + else if(0==strcmp(argv[1], "dog")) test_dog(argv[2]); else if(0==strcmp(argv[1], "ctrain")) train_cifar10(argv[2]); else if(0==strcmp(argv[1], "nist")) train_nist(argv[2]); else if(0==strcmp(argv[1], "ctest")) test_cifar10(argv[2]); diff --git a/src/data.c b/src/data.c index 87097b6c..3a374118 100644 --- a/src/data.c +++ b/src/data.c @@ -239,7 +239,7 @@ void *load_in_thread(void *ptr) { struct load_args a = *(struct load_args*)ptr; *a.d = load_data(a.paths, a.n, a.m, a.labels, a.k, a.h, a.w); - translate_data_rows(*a.d, -144); + translate_data_rows(*a.d, -128); scale_data_rows(*a.d, 1./128); free(ptr); return 0; diff --git a/src/image.c b/src/image.c index ddb5bf52..a686a3e0 100644 --- a/src/image.c +++ b/src/image.c @@ -484,7 +484,7 @@ image load_image(char *filename, int h, int w) exit(0); } if(h && w ){ - IplImage *resized = resizeImage(src, h, w, 1); + IplImage *resized = resizeImage(src, h, w, 0); cvReleaseImage(&src); src = resized; } @@ -702,10 +702,21 @@ void back_convolve(image m, image kernel, int stride, int channel, image out, in void print_image(image m) { - int i; - for(i =0 ; i < m.h*m.w*m.c; ++i) printf("%lf, ", m.data[i]); + int i, j, k; + for(i =0 ; i < m.c; ++i){ + for(j =0 ; j < m.h; ++j){ + for(k = 0; k < m.w; ++k){ + printf("%.2lf, ", m.data[i*m.h*m.w + j*m.w + k]); + if(k > 30) break; + } + printf("\n"); + if(j > 30) break; + } + printf("\n"); + } printf("\n"); } + image collapse_images_vert(image *ims, int n) { int color = 1; diff --git a/src/network.c b/src/network.c index f554090e..b6285611 100644 --- a/src/network.c +++ b/src/network.c @@ -75,7 +75,7 @@ void forward_network(network net, float *input, float *truth, int train) } else if(net.types[i] == CROP){ crop_layer layer = *(crop_layer *)net.layers[i]; - forward_crop_layer(layer, input); + forward_crop_layer(layer, train, input); input = layer.output; } else if(net.types[i] == COST){ @@ -536,6 +536,9 @@ image get_network_image_layer(network net, int i) normalization_layer layer = *(normalization_layer *)net.layers[i]; return get_normalization_image(layer); } + else if(net.types[i] == DROPOUT){ + return get_network_image_layer(net, i-1); + } else if(net.types[i] == CROP){ crop_layer layer = *(crop_layer *)net.layers[i]; return get_crop_image(layer); diff --git a/src/network_kernels.cu b/src/network_kernels.cu index 7909e464..de8f659a 100644 --- a/src/network_kernels.cu +++ b/src/network_kernels.cu @@ -58,7 +58,7 @@ void forward_network_gpu(network net, float * input, float * truth, int train) } else if(net.types[i] == CROP){ crop_layer layer = *(crop_layer *)net.layers[i]; - forward_crop_layer_gpu(layer, input); + forward_crop_layer_gpu(layer, train, input); input = layer.output_gpu; } //printf("Forward %d %s %f\n", i, get_layer_string(net.types[i]), sec(clock() - time)); diff --git a/src/utils.c b/src/utils.c index 96062b08..26354944 100644 --- a/src/utils.c +++ b/src/utils.c @@ -11,6 +11,7 @@ void pm(int M, int N, float *A) { int i,j; for(i =0 ; i < M; ++i){ + printf("%d ", i+1); for(j = 0; j < N; ++j){ printf("%10.6f, ", A[i*N+j]); }