| | |
| | | show_image_layers(edge, "Test Convolve"); |
| | | } |
| | | |
| | | #ifdef GPU |
| | | |
| | | void test_convolutional_layer() |
| | | { |
| | | int i; |
| | | image dog = load_image("data/dog.jpg",256,256); |
| | | network net = parse_network_cfg("cfg/convolutional.cfg"); |
| | | // data test = load_cifar10_data("data/cifar10/test_batch.bin"); |
| | | // float *X = calloc(net.batch*test.X.cols, sizeof(float)); |
| | | // float *y = calloc(net.batch*test.y.cols, sizeof(float)); |
| | | int in_size = get_network_input_size(net)*net.batch; |
| | | int size = get_network_output_size(net)*net.batch; |
| | | float *X = calloc(in_size, sizeof(float)); |
| | | for(i = 0; i < in_size; ++i){ |
| | | X[i] = dog.data[i%get_network_input_size(net)]; |
| | | } |
| | | // get_batch(test, net.batch, X, y); |
| | | clock_t start, end; |
| | | cl_mem input_cl = cl_make_array(X, in_size); |
| | | |
| | | forward_network_gpu(net, input_cl, 1); |
| | | start = clock(); |
| | | forward_network_gpu(net, input_cl, 1); |
| | | end = clock(); |
| | | float gpu_sec = (float)(end-start)/CLOCKS_PER_SEC; |
| | | float *gpu_out = calloc(size, sizeof(float)); |
| | | memcpy(gpu_out, get_network_output(net), size*sizeof(float)); |
| | | |
| | | start = clock(); |
| | | forward_network(net, X, 1); |
| | | end = clock(); |
| | | float cpu_sec = (float)(end-start)/CLOCKS_PER_SEC; |
| | | float *cpu_out = calloc(size, sizeof(float)); |
| | | memcpy(cpu_out, get_network_output(net), size*sizeof(float)); |
| | | |
| | | float sum = 0; |
| | | for(i = 0; i < size; ++i) { |
| | | //printf("%f, %f\n", gpu_out[i], cpu_out[i]); |
| | | sum += pow(gpu_out[i] - cpu_out[i], 2); |
| | | } |
| | | printf("gpu: %f sec, cpu: %f sec, diff: %f, size: %d\n", gpu_sec, cpu_sec, sum, size); |
| | | } |
| | | |
| | | #endif |
| | | |
| | | void test_convolve_matrix() |
| | | { |
| | | image dog = load_image("dog.jpg",300,400); |
| | |
| | | |
| | | void test_cifar10() |
| | | { |
| | | srand(222222); |
| | | |
| | | network net = parse_network_cfg("cfg/cifar10_part5.cfg"); |
| | | data test = load_cifar10_data("data/cifar10/test_batch.bin"); |
| | | clock_t start = clock(), end; |
| | | float test_acc = network_accuracy(net, test); |
| | | end = clock(); |
| | | printf("%f in %f Sec\n", test_acc, (float)(end-start)/CLOCKS_PER_SEC); |
| | | visualize_network(net); |
| | | cvWaitKey(0); |
| | | } |
| | | |
| | | void train_cifar10() |
| | | { |
| | | srand(555555); |
| | | network net = parse_network_cfg("cfg/cifar10.cfg"); |
| | | //data test = load_cifar10_data("data/cifar10/test_batch.bin"); |
| | | data test = load_cifar10_data("data/cifar10/test_batch.bin"); |
| | | int count = 0; |
| | | int iters = 10000/net.batch; |
| | | data train = load_all_cifar10(); |
| | |
| | | clock_t start = clock(), end; |
| | | float loss = train_network_sgd(net, train, iters); |
| | | end = clock(); |
| | | //visualize_network(net); |
| | | //cvWaitKey(1000); |
| | | visualize_network(net); |
| | | cvWaitKey(5000); |
| | | |
| | | //float test_acc = network_accuracy(net, test); |
| | | //printf("%d: Loss: %f, Test Acc: %f, Time: %lf seconds, LR: %f, Momentum: %f, Decay: %f\n", count, loss, test_acc,(float)(end-start)/CLOCKS_PER_SEC, net.learning_rate, net.momentum, net.decay); |
| | | printf("%d: Loss: %f, Time: %lf seconds, LR: %f, Momentum: %f, Decay: %f\n", count, loss, (float)(end-start)/CLOCKS_PER_SEC, net.learning_rate, net.momentum, net.decay); |
| | | if(count%10 == 0){ |
| | | float test_acc = network_accuracy(net, test); |
| | | printf("%d: Loss: %f, Test Acc: %f, Time: %lf seconds, LR: %f, Momentum: %f, Decay: %f\n", count, loss, test_acc,(float)(end-start)/CLOCKS_PER_SEC, net.learning_rate, net.momentum, net.decay); |
| | | char buff[256]; |
| | | sprintf(buff, "/home/pjreddie/cifar/cifar2_%d.cfg", count); |
| | | save_network(net, buff); |
| | | }else{ |
| | | printf("%d: Loss: %f, Time: %lf seconds, LR: %f, Momentum: %f, Decay: %f\n", count, loss, (float)(end-start)/CLOCKS_PER_SEC, net.learning_rate, net.momentum, net.decay); |
| | | } |
| | | } |
| | | free_data(train); |
| | | } |
| | |
| | | void test_nist() |
| | | { |
| | | srand(222222); |
| | | network net = parse_network_cfg("cfg/nist_final.cfg"); |
| | | data test = load_categorical_data_csv("data/mnist/mnist_test.csv",0,10); |
| | | translate_data_rows(test, -144); |
| | | clock_t start = clock(), end; |
| | | float test_acc = network_accuracy_multi(net, test,16); |
| | | end = clock(); |
| | | printf("Accuracy: %f, Time: %lf seconds\n", test_acc,(float)(end-start)/CLOCKS_PER_SEC); |
| | | } |
| | | |
| | | void train_nist() |
| | | { |
| | | srand(222222); |
| | | network net = parse_network_cfg("cfg/nist.cfg"); |
| | | 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); |
| | | translate_data_rows(train, -144); |
| | | //scale_data_rows(train, 1./128); |
| | | translate_data_rows(test, -144); |
| | | //scale_data_rows(test, 1./128); |
| | | translate_data_rows(train, -144); |
| | | //scale_data_rows(train, 1./128); |
| | | translate_data_rows(test, -144); |
| | | //scale_data_rows(test, 1./128); |
| | | //randomize_data(train); |
| | | int count = 0; |
| | | //clock_t start = clock(), end; |
| | |
| | | //float test_acc = 0; |
| | | printf("%d: Loss: %f, Test Acc: %f, Time: %lf seconds, LR: %f, Momentum: %f, Decay: %f\n", count, loss, test_acc,(float)(end-start)/CLOCKS_PER_SEC, net.learning_rate, net.momentum, net.decay); |
| | | /*printf("%f %f %f %f %f\n", mean_array(get_network_output_layer(net,0), 100), |
| | | mean_array(get_network_output_layer(net,1), 100), |
| | | mean_array(get_network_output_layer(net,2), 100), |
| | | mean_array(get_network_output_layer(net,3), 100), |
| | | mean_array(get_network_output_layer(net,4), 100)); |
| | | */ |
| | | //save_network(net, "cfg/nist_basic_trained.cfg"); |
| | | mean_array(get_network_output_layer(net,1), 100), |
| | | mean_array(get_network_output_layer(net,2), 100), |
| | | mean_array(get_network_output_layer(net,3), 100), |
| | | mean_array(get_network_output_layer(net,4), 100)); |
| | | */ |
| | | //save_network(net, "cfg/nist_final2.cfg"); |
| | | |
| | | //printf("%5d Training Loss: %lf, Params: %f %f %f, ",count*1000, loss, lr, momentum, decay); |
| | | //end = clock(); |
| | |
| | | { |
| | | //train_full(); |
| | | //test_distribution(); |
| | | feenableexcept(FE_DIVBYZERO | FE_INVALID | FE_OVERFLOW); |
| | | //feenableexcept(FE_DIVBYZERO | FE_INVALID | FE_OVERFLOW); |
| | | |
| | | //test_blas(); |
| | | //test_visualize(); |
| | |
| | | //test_split(); |
| | | //test_ensemble(); |
| | | //test_nist_single(); |
| | | test_nist(); |
| | | //test_nist(); |
| | | train_nist(); |
| | | //test_convolutional_layer(); |
| | | //test_cifar10(); |
| | | //train_cifar10(); |
| | | //test_vince(); |
| | | //test_full(); |
| | | //tune_VOC(); |