From 1cfe70365fd1a78462082275935033a4de74458e Mon Sep 17 00:00:00 2001 From: Francesco Gatti Date: Tue, 1 Aug 2017 17:12:29 +0200 Subject: [PATCH] yolo test --- CMakeLists.txt | 3 ++ src/LayerWgs.cpp | 8 +-- src/utils.cpp | 2 +- tests/mnist/test.cpp | 12 ++--- tests/test/test.cpp | 10 ++-- tests/yolo/yolo.cpp | 125 +++++++++++++++++++++++++++++++++++++++++++ 6 files changed, 144 insertions(+), 16 deletions(-) create mode 100644 tests/yolo/yolo.cpp diff --git a/CMakeLists.txt b/CMakeLists.txt index 71f8908..ce0f405 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -22,3 +22,6 @@ target_link_libraries(test_simple tkDNN) add_executable(test_mnist tests/mnist/test.cpp) target_link_libraries(test_mnist tkDNN) + +add_executable(test_yolo tests/yolo/yolo.cpp) +target_link_libraries(test_yolo tkDNN) \ No newline at end of file diff --git a/src/LayerWgs.cpp b/src/LayerWgs.cpp index 84bb39b..58d40a7 100644 --- a/src/LayerWgs.cpp +++ b/src/LayerWgs.cpp @@ -15,16 +15,16 @@ LayerWgs::LayerWgs(Network *net, dataDim_t in_dim, std::cout<<"Reading weights: I="<batchnorm = batchnorm; if(batchnorm) { - seek += outputs*4; + seek += outputs; readBinaryFile(weights_path.c_str(), outputs, &scales_h, &scales_d, seek); - seek += outputs*4; + seek += outputs; readBinaryFile(weights_path.c_str(), outputs, &mean_h, &mean_d, seek); - seek += outputs*4; + seek += outputs; readBinaryFile(weights_path.c_str(), outputs, &variance_h, &variance_d, seek); } } diff --git a/src/utils.cpp b/src/utils.cpp index 3806b5c..749071c 100644 --- a/src/utils.cpp +++ b/src/utils.cpp @@ -11,7 +11,7 @@ void readBinaryFile(const char* fname, int size, value_type** data_h, value_type } if(seek != 0) { - dataFile.seekg(seek, dataFile.cur); + dataFile.seekg(seek*sizeof(value_type), dataFile.cur); } int size_b = size*sizeof(value_type); diff --git a/tests/mnist/test.cpp b/tests/mnist/test.cpp index 699b440..0542649 100644 --- a/tests/mnist/test.cpp +++ b/tests/mnist/test.cpp @@ -2,10 +2,10 @@ #include "tkdnn.h" const char *input_bin = "../tests/mnist/input.bin"; -const char *c0_bin = "../tests/mnist/layers/Convolution0.bin"; -const char *c1_bin = "../tests/mnist/layers/Convolution1.bin"; -const char *d2_bin = "../tests/mnist/layers/InnerProduct2.bin"; -const char *d3_bin = "../tests/mnist/layers/InnerProduct3.bin"; +const char *c0_bin = "../tests/mnist/layers/c0.bin"; +const char *c1_bin = "../tests/mnist/layers/c1.bin"; +const char *d2_bin = "../tests/mnist/layers/d2.bin"; +const char *d3_bin = "../tests/mnist/layers/d3.bin"; const char *output_bin = "../tests/mnist/output.bin"; int main() { @@ -14,9 +14,9 @@ int main() { tkDNN::Network net; tkDNN::dataDim_t dim(1, 1, 28, 28, 1); tkDNN::Layer *l; - l = new tkDNN::Conv2d (&net, dim, 20, 5, 5, 1, 1, 1, 1, c0_bin); + l = new tkDNN::Conv2d (&net, dim, 20, 5, 5, 1, 1, 0, 0, c0_bin); l = new tkDNN::Pooling (&net, l->output_dim, 2, 2, 2, 2, tkDNN::POOLING_MAX); - l = new tkDNN::Conv2d (&net, l->output_dim, 50, 5, 5, 1, 1, 1, 1, c1_bin); + l = new tkDNN::Conv2d (&net, l->output_dim, 50, 5, 5, 1, 1, 0, 0, c1_bin); l = new tkDNN::Pooling (&net, l->output_dim, 2, 2, 2, 2, tkDNN::POOLING_MAX); l = new tkDNN::Dense (&net, l->output_dim, 500, d2_bin); l = new tkDNN::Activation (&net, l->output_dim, CUDNN_ACTIVATION_RELU); diff --git a/tests/test/test.cpp b/tests/test/test.cpp index da0f1b3..73f049e 100644 --- a/tests/test/test.cpp +++ b/tests/test/test.cpp @@ -2,9 +2,9 @@ #include "tkdnn.h" const char *input_bin = "../tests/test/input.bin"; -const char *c0_bin = "../tests/test/layers/conv0.bin"; -const char *c1_bin = "../tests/test/layers/conv1.bin"; -const char *d2_bin = "../tests/test/layers/dense2.bin"; +const char *c0_bin = "../tests/test/layers/c0.bin"; +const char *c1_bin = "../tests/test/layers/c1.bin"; +const char *d2_bin = "../tests/test/layers/d2.bin"; const char *output_bin = "../tests/test/output.bin"; int main() { @@ -13,9 +13,9 @@ int main() { tkDNN::Network net; tkDNN::dataDim_t dim(1, 1, 10, 10, 1); tkDNN::Layer *l; - l = new tkDNN::Conv2d (&net, dim, 2, 4, 4, 2, 2, 1, 1, c0_bin); + l = new tkDNN::Conv2d (&net, dim, 2, 4, 4, 2, 2, 0, 0, c0_bin); l = new tkDNN::Activation (&net, l->output_dim, CUDNN_ACTIVATION_RELU); - l = new tkDNN::Conv2d (&net, l->output_dim, 4, 2, 2, 1, 1, 1, 1, c1_bin); + l = new tkDNN::Conv2d (&net, l->output_dim, 4, 2, 2, 1, 1, 0, 0, c1_bin); l = new tkDNN::Activation (&net, l->output_dim, CUDNN_ACTIVATION_RELU); l = new tkDNN::Flatten (&net, l->output_dim); l = new tkDNN::Dense (&net, l->output_dim, 4, d2_bin); diff --git a/tests/yolo/yolo.cpp b/tests/yolo/yolo.cpp new file mode 100644 index 0000000..8e4c23f --- /dev/null +++ b/tests/yolo/yolo.cpp @@ -0,0 +1,125 @@ +#include +#include "tkdnn.h" + +const char *input_bin = "../tests/yolo/layers/input.bin"; +const char *c0_bin = "../tests/yolo/layers/c0.bin"; +const char *c2_bin = "../tests/yolo/layers/c2.bin"; +const char *c4_bin = "../tests/yolo/layers/c4.bin"; +const char *c5_bin = "../tests/yolo/layers/c5.bin"; +const char *c6_bin = "../tests/yolo/layers/c6.bin"; +const char *c8_bin = "../tests/yolo/layers/c8.bin"; +const char *c9_bin = "../tests/yolo/layers/c9.bin"; +const char *c10_bin = "../tests/yolo/layers/c10.bin"; +const char *c12_bin = "../tests/yolo/layers/c12.bin"; +const char *c13_bin = "../tests/yolo/layers/c13.bin"; +const char *c14_bin = "../tests/yolo/layers/c14.bin"; +const char *c15_bin = "../tests/yolo/layers/c15.bin"; +const char *c16_bin = "../tests/yolo/layers/c16.bin"; +const char *c18_bin = "../tests/yolo/layers/c18.bin"; +const char *c19_bin = "../tests/yolo/layers/c19.bin"; +const char *c20_bin = "../tests/yolo/layers/c20.bin"; +const char *c21_bin = "../tests/yolo/layers/c21.bin"; +const char *c22_bin = "../tests/yolo/layers/c22.bin"; +const char *c23_bin = "../tests/yolo/layers/c23.bin"; +const char *c24_bin = "../tests/yolo/layers/c24.bin"; +const char *c26_bin = "../tests/yolo/layers/c26.bin"; +const char *c29_bin = "../tests/yolo/layers/c29.bin"; +const char *c30_bin = "../tests/yolo/layers/c30.bin"; +const char *output_bin = "../tests/yolo/layers/output.bin"; + +int main() { + + // Network layout + tkDNN::Network net; + tkDNN::dataDim_t dim(1, 3, 608, 608, 1); + tkDNN::Layer *l; + l = new tkDNN::Conv2d (&net, dim, 32, 3, 3, 1, 1, 1, 1, c0_bin, true); + l = new tkDNN::Activation (&net, l->output_dim, tkDNN::ACTIVATION_LEAKY); + l = new tkDNN::Pooling (&net, l->output_dim, 2, 2, 2, 2, tkDNN::POOLING_MAX); + + l = new tkDNN::Conv2d (&net, l->output_dim, 64, 3, 3, 1, 1, 1, 1, c2_bin, true); + l = new tkDNN::Activation (&net, l->output_dim, tkDNN::ACTIVATION_LEAKY); + l = new tkDNN::Pooling (&net, l->output_dim, 2, 2, 2, 2, tkDNN::POOLING_MAX); + + l = new tkDNN::Conv2d (&net, l->output_dim, 128, 3, 3, 1, 1, 1, 1, c4_bin, true); + l = new tkDNN::Activation (&net, l->output_dim, tkDNN::ACTIVATION_LEAKY); + l = new tkDNN::Conv2d (&net, l->output_dim, 64, 1, 1, 1, 1, 0, 0, c5_bin, true); + l = new tkDNN::Activation (&net, l->output_dim, tkDNN::ACTIVATION_LEAKY); + l = new tkDNN::Conv2d (&net, l->output_dim, 128, 3, 3, 1, 1, 1, 1, c6_bin, true); + l = new tkDNN::Activation (&net, l->output_dim, tkDNN::ACTIVATION_LEAKY); + l = new tkDNN::Pooling (&net, l->output_dim, 2, 2, 2, 2, tkDNN::POOLING_MAX); + + l = new tkDNN::Conv2d (&net, l->output_dim, 256, 3, 3, 1, 1, 1, 1, c8_bin, true); + l = new tkDNN::Activation (&net, l->output_dim, tkDNN::ACTIVATION_LEAKY); + l = new tkDNN::Conv2d (&net, l->output_dim, 128, 1, 1, 1, 1, 0, 0, c9_bin, true); + l = new tkDNN::Activation (&net, l->output_dim, tkDNN::ACTIVATION_LEAKY); + l = new tkDNN::Conv2d (&net, l->output_dim, 256, 3, 3, 1, 1, 1, 1, c10_bin, true); + l = new tkDNN::Activation (&net, l->output_dim, tkDNN::ACTIVATION_LEAKY); + l = new tkDNN::Pooling (&net, l->output_dim, 2, 2, 2, 2, tkDNN::POOLING_MAX); + + l = new tkDNN::Conv2d (&net, l->output_dim, 512, 3, 3, 1, 1, 1, 1, c12_bin, true); + l = new tkDNN::Activation (&net, l->output_dim, tkDNN::ACTIVATION_LEAKY); + l = new tkDNN::Conv2d (&net, l->output_dim, 256, 1, 1, 1, 1, 0, 0, c13_bin, true); + l = new tkDNN::Activation (&net, l->output_dim, tkDNN::ACTIVATION_LEAKY); + l = new tkDNN::Conv2d (&net, l->output_dim, 512, 3, 3, 1, 1, 1, 1, c14_bin, true); + l = new tkDNN::Activation (&net, l->output_dim, tkDNN::ACTIVATION_LEAKY); + l = new tkDNN::Conv2d (&net, l->output_dim, 256, 1, 1, 1, 1, 0, 0, c15_bin, true); + l = new tkDNN::Activation (&net, l->output_dim, tkDNN::ACTIVATION_LEAKY); + l = new tkDNN::Conv2d (&net, l->output_dim, 512, 3, 3, 1, 1, 1, 1, c16_bin, true); + l = new tkDNN::Activation (&net, l->output_dim, tkDNN::ACTIVATION_LEAKY); //29 + l = new tkDNN::Pooling (&net, l->output_dim, 2, 2, 2, 2, tkDNN::POOLING_MAX); + + l = new tkDNN::Conv2d (&net, l->output_dim, 1024, 3, 3, 1, 1, 1, 1, c18_bin, true); + l = new tkDNN::Activation (&net, l->output_dim, tkDNN::ACTIVATION_LEAKY); + l = new tkDNN::Conv2d (&net, l->output_dim, 512, 1, 1, 1, 1, 0, 0, c19_bin, true); + l = new tkDNN::Activation (&net, l->output_dim, tkDNN::ACTIVATION_LEAKY); + l = new tkDNN::Conv2d (&net, l->output_dim, 1024, 3, 3, 1, 1, 1, 1, c20_bin, true); + l = new tkDNN::Activation (&net, l->output_dim, tkDNN::ACTIVATION_LEAKY); + l = new tkDNN::Conv2d (&net, l->output_dim, 512, 1, 1, 1, 1, 0, 0, c21_bin, true); + l = new tkDNN::Activation (&net, l->output_dim, tkDNN::ACTIVATION_LEAKY); + l = new tkDNN::Conv2d (&net, l->output_dim, 1024, 3, 3, 1, 1, 1, 1, c22_bin, true); + l = new tkDNN::Activation (&net, l->output_dim, tkDNN::ACTIVATION_LEAKY); + l = new tkDNN::Conv2d (&net, l->output_dim, 1024, 3, 3, 1, 1, 1, 1, c23_bin, true); + l = new tkDNN::Activation (&net, l->output_dim, tkDNN::ACTIVATION_LEAKY); + l = new tkDNN::Conv2d (&net, l->output_dim, 1024, 3, 3, 1, 1, 1, 1, c24_bin, true); + l = new tkDNN::Activation (&net, l->output_dim, tkDNN::ACTIVATION_LEAKY); //44 + + int rlayers[1] = {29}; + l = new tkDNN::Route (&net, rlayers, 1); + l = new tkDNN::Conv2d (&net, l->output_dim, 64, 1, 1, 1, 1, 0, 0, c26_bin, true); + l = new tkDNN::Activation (&net, l->output_dim, tkDNN::ACTIVATION_LEAKY); + l = new tkDNN::Reorg (&net, l->output_dim, 2); //48 + + int rlayers2[2] = {48,44}; + l = new tkDNN::Route (&net, rlayers2, 2); + + l = new tkDNN::Conv2d (&net, l->output_dim, 1024, 3, 3, 1, 1, 1, 1, c29_bin, true); + l = new tkDNN::Activation (&net, l->output_dim, tkDNN::ACTIVATION_LEAKY); + l = new tkDNN::Conv2d (&net, l->output_dim, 425, 1, 1, 1, 1, 0, 0, c30_bin, false); + + l = new tkDNN::Region (&net, l->output_dim, 80, 4, 5, 0.6f); + + // Load input + value_type *data; + value_type *input_h; + readBinaryFile(input_bin, dim.tot(), &input_h, &data); + + dim.print(); //print initial dimension + + TIMER_START + + // Inference + data = net.infer(dim, data); + + TIMER_STOP + dim.print(); + + // Print real test + std::cout<<"\n==== CHECK RESULT ====\n"; + value_type *out; + value_type *out_h; + readBinaryFile(output_bin, dim.tot(), &out_h, &out); + int diff = checkResult(dim.tot(), data, out); + printf("Output diffs: %d\n", diff); + return 0; +}