From aa6dc2b270202aaeef711e246e8361fd674597fe Mon Sep 17 00:00:00 2001 From: meshtag Date: Tue, 25 Jan 2022 18:37:42 +0800 Subject: [PATCH 1/8] Add support for testing DIP dialect with OpenCV --- README.md | 18 ++++ tests/UnitTests/CMakeLists.txt | 2 + .../UnitTests/ImageProcessing/CMakeLists.txt | 5 ++ .../ImageProcessing/DIPCorr2DTest.cpp | 90 +++++++++++++++++++ 4 files changed, 115 insertions(+) create mode 100644 tests/UnitTests/ImageProcessing/CMakeLists.txt create mode 100644 tests/UnitTests/ImageProcessing/DIPCorr2DTest.cpp diff --git a/README.md b/README.md index d6385f25..2eb1868a 100644 --- a/README.md +++ b/README.md @@ -110,3 +110,21 @@ $ cmake --build . -- $ ninja test ``` +For tests related to DIP dialect : + +``` +$ cd buddy-benchmark +$ mkdir build && cd build +$ cmake -G Ninja .. \ + -DIMAGE_PROCESSING_BENCHMARKS=ON \ + -DDEEP_LEARNING_BENCHMARKS=ON \ + -DBUILD_TESTS=ON \ + -DOpenCV_DIR=/path/to/opencv/build/ \ + -DBUDDY_OPT_BUILD_DIR=/path/to/buddy-mlir/build/ \ + -DBUDDY_OPT_STRIP_MINING= \ + -DBUDDY_OPT_ATTR= +$ ninja DIPCorr2DTest +$ cd bin && ./DIPCorr2DTest +``` + +where `` is the path of image used as first operand during 2D Correlation. diff --git a/tests/UnitTests/CMakeLists.txt b/tests/UnitTests/CMakeLists.txt index e6774589..7569f9f8 100644 --- a/tests/UnitTests/CMakeLists.txt +++ b/tests/UnitTests/CMakeLists.txt @@ -11,3 +11,5 @@ add_dependencies(test-container Container) include(GoogleTest) gtest_discover_tests(test-container) + +add_subdirectory(ImageProcessing) diff --git a/tests/UnitTests/ImageProcessing/CMakeLists.txt b/tests/UnitTests/ImageProcessing/CMakeLists.txt new file mode 100644 index 00000000..66ba85f6 --- /dev/null +++ b/tests/UnitTests/ImageProcessing/CMakeLists.txt @@ -0,0 +1,5 @@ +find_package(OpenCV REQUIRED CONFIG) +include_directories(${OpenCV_INCLUDE_DIRS}) + +add_executable(DIPCorr2DTest DIPCorr2DTest.cpp) +target_link_libraries(DIPCorr2DTest ${OpenCV_LIBS} Corr2D Container PNGImage) diff --git a/tests/UnitTests/ImageProcessing/DIPCorr2DTest.cpp b/tests/UnitTests/ImageProcessing/DIPCorr2DTest.cpp new file mode 100644 index 00000000..20f41773 --- /dev/null +++ b/tests/UnitTests/ImageProcessing/DIPCorr2DTest.cpp @@ -0,0 +1,90 @@ +#include "../../../include/ImageProcessing/Kernels.h" +#include "Utils/Container.h" + +#include +#include + +#include + +using namespace cv; +using namespace std; + +// Declare the Corr2D C interface. +extern "C" { +void _mlir_ciface_corr_2d(MemRef *input, MemRef *kernel, + MemRef *output, unsigned int centerX, + unsigned int centerY, int boundaryOption); +} + +bool equalImages(const Mat &img1, const Mat &img2) +{ + if (img1.rows != img2.rows || img1.cols != img2.cols) { + std::cout << "Produced outputs by DIP and OpenCV differ. Image dimensions are not equal\n"; + return 0; + } + + for (unsigned int y = 0; y < img1.rows; ++y) + { + for (unsigned int x = 0; x < img1.cols; ++x) + { + if (abs(img1.at(x, y) - img2.at(x, y)) > 10e-3) + { + std::cout << "Produced outputs by DIP and OpenCV differ.\n"; + return 0; + } + } + } + return 1; +} + +void testKernelImpl(const Mat& inputImage, unsigned int kernelRows, unsigned int kernelCols, float* kernelArray, + unsigned int x, unsigned int y) +{ + // Define container sizes. + intptr_t sizesInput[2] = {inputImage.rows, inputImage.cols}; + intptr_t sizesKernel[2] = {kernelRows, kernelCols}; + intptr_t sizesOutput[2] = {inputImage.rows, inputImage.cols}; + + // Define input, kernel, and output. + MemRef input(inputImage, sizesInput); + MemRef kernel(kernelArray, sizesKernel); + MemRef output(sizesOutput); + + for (int i = 0; i < inputImage.rows; i++) + for (int j = 0; j < inputImage.cols; j++) + output[i * inputImage.rows + j] = 0; + + Mat kernel1 = Mat(kernelRows, kernelCols, CV_32FC1, kernelArray); + Mat opencvOutput; + + _mlir_ciface_corr_2d(&input, &kernel, &output, x, y, 0); + + filter2D(inputImage, opencvOutput, CV_32FC1, kernel1, cv::Point(x, y), 1.0, + cv::BORDER_REPLICATE); + + // Define a cv::Mat with the output of corr_2d. + Mat dipOutput(inputImage.rows, inputImage.cols, CV_32FC1, output.getData()); + + if (!equalImages(dipOutput, opencvOutput)) + { + std::cout << "Different images produced by OpenCV and DIP for kernel :\n" << kernel1 << "\n" + "when anchor point was : (" << x << ", " << y << ").\n"; + return; + } +} + +void testKernel(const Mat& inputImage, unsigned int kernelRows, unsigned int kernelCols, float* kernelArray) +{ + for (unsigned int y = 0; y < kernelRows; ++y) + for (unsigned int x = 0; x < kernelCols; ++x) + testKernelImpl(inputImage, kernelRows, kernelCols, kernelArray, x, y); +} + +int main(int argc, char **argv) +{ + // Read input image + Mat inputImage = imread(argv[1], IMREAD_GRAYSCALE); + + for (auto kernel : kernelMap) + testKernel(inputImage, get<1>(kernel.second), get<2>(kernel.second), get<0>(kernel.second)); +} From 759147ce1bae9661c97eb57fa481829da98db37b Mon Sep 17 00:00:00 2001 From: meshtag Date: Wed, 26 Jan 2022 22:13:09 +0800 Subject: [PATCH 2/8] Apply clang-format --- .../ImageProcessing/DIPCorr2DTest.cpp | 43 +++++++++---------- 1 file changed, 21 insertions(+), 22 deletions(-) diff --git a/tests/UnitTests/ImageProcessing/DIPCorr2DTest.cpp b/tests/UnitTests/ImageProcessing/DIPCorr2DTest.cpp index 20f41773..7bd933d9 100644 --- a/tests/UnitTests/ImageProcessing/DIPCorr2DTest.cpp +++ b/tests/UnitTests/ImageProcessing/DIPCorr2DTest.cpp @@ -16,19 +16,16 @@ void _mlir_ciface_corr_2d(MemRef *input, MemRef *kernel, unsigned int centerY, int boundaryOption); } -bool equalImages(const Mat &img1, const Mat &img2) -{ +bool equalImages(const Mat &img1, const Mat &img2) { if (img1.rows != img2.rows || img1.cols != img2.cols) { - std::cout << "Produced outputs by DIP and OpenCV differ. Image dimensions are not equal\n"; + std::cout << "Produced outputs by DIP and OpenCV differ. Image dimensions " + "are not equal\n"; return 0; } - for (unsigned int y = 0; y < img1.rows; ++y) - { - for (unsigned int x = 0; x < img1.cols; ++x) - { - if (abs(img1.at(x, y) - img2.at(x, y)) > 10e-3) - { + for (unsigned int y = 0; y < img1.rows; ++y) { + for (unsigned int x = 0; x < img1.cols; ++x) { + if (abs(img1.at(x, y) - img2.at(x, y)) > 10e-3) { std::cout << "Produced outputs by DIP and OpenCV differ.\n"; return 0; } @@ -37,9 +34,9 @@ bool equalImages(const Mat &img1, const Mat &img2) return 1; } -void testKernelImpl(const Mat& inputImage, unsigned int kernelRows, unsigned int kernelCols, float* kernelArray, - unsigned int x, unsigned int y) -{ +void testKernelImpl(const Mat &inputImage, unsigned int kernelRows, + unsigned int kernelCols, float *kernelArray, unsigned int x, + unsigned int y) { // Define container sizes. intptr_t sizesInput[2] = {inputImage.rows, inputImage.cols}; intptr_t sizesKernel[2] = {kernelRows, kernelCols}; @@ -60,31 +57,33 @@ void testKernelImpl(const Mat& inputImage, unsigned int kernelRows, unsigned int _mlir_ciface_corr_2d(&input, &kernel, &output, x, y, 0); filter2D(inputImage, opencvOutput, CV_32FC1, kernel1, cv::Point(x, y), 1.0, - cv::BORDER_REPLICATE); + cv::BORDER_REPLICATE); // Define a cv::Mat with the output of corr_2d. Mat dipOutput(inputImage.rows, inputImage.cols, CV_32FC1, output.getData()); - if (!equalImages(dipOutput, opencvOutput)) - { - std::cout << "Different images produced by OpenCV and DIP for kernel :\n" << kernel1 << "\n" - "when anchor point was : (" << x << ", " << y << ").\n"; + if (!equalImages(dipOutput, opencvOutput)) { + std::cout << "Different images produced by OpenCV and DIP for kernel :\n" + << kernel1 + << "\n" + "when anchor point was : (" + << x << ", " << y << ").\n"; return; } } -void testKernel(const Mat& inputImage, unsigned int kernelRows, unsigned int kernelCols, float* kernelArray) -{ +void testKernel(const Mat &inputImage, unsigned int kernelRows, + unsigned int kernelCols, float *kernelArray) { for (unsigned int y = 0; y < kernelRows; ++y) for (unsigned int x = 0; x < kernelCols; ++x) testKernelImpl(inputImage, kernelRows, kernelCols, kernelArray, x, y); } -int main(int argc, char **argv) -{ +int main(int argc, char **argv) { // Read input image Mat inputImage = imread(argv[1], IMREAD_GRAYSCALE); for (auto kernel : kernelMap) - testKernel(inputImage, get<1>(kernel.second), get<2>(kernel.second), get<0>(kernel.second)); + testKernel(inputImage, get<1>(kernel.second), get<2>(kernel.second), + get<0>(kernel.second)); } From 1e96dec58b91ae7b8e11d805e2dffef5f03c4907 Mon Sep 17 00:00:00 2001 From: meshtag Date: Fri, 4 Feb 2022 12:28:17 +0800 Subject: [PATCH 3/8] First draft with goole test --- .../ImageProcessing/Images/test_6x6.png | Bin 0 -> 75 bytes {tests/UnitTests => include/Utils}/Test.h | 0 tests/CMakeLists.txt | 1 + tests/IntegrationTests/CMakeLists.txt | 1 + .../ImageProcessing/CMakeLists.txt | 6 ++- .../ImageProcessing/DIPCorr2DTest.cpp | 50 +++++++++++++++--- tests/UnitTests/CMakeLists.txt | 2 - tests/UnitTests/TestContainer.cpp | 2 +- 8 files changed, 51 insertions(+), 11 deletions(-) create mode 100644 benchmarks/ImageProcessing/Images/test_6x6.png rename {tests/UnitTests => include/Utils}/Test.h (100%) create mode 100644 tests/IntegrationTests/CMakeLists.txt rename tests/{UnitTests => IntegrationTests}/ImageProcessing/CMakeLists.txt (70%) rename tests/{UnitTests => IntegrationTests}/ImageProcessing/DIPCorr2DTest.cpp (74%) diff --git a/benchmarks/ImageProcessing/Images/test_6x6.png b/benchmarks/ImageProcessing/Images/test_6x6.png new file mode 100644 index 0000000000000000000000000000000000000000..ee3e72078d81a9dcbd45ddd0a52a34ca1ce4a6d3 GIT binary patch literal 75 zcmeAS@N?(olHy`uVBq!ia0vp^Y#_`5Bp8k^-!KJ833<9WhHykDrzEg2n;D#JYG-0# W^JDP8R35Y%q{`FP&t;ucLK6TC(hz(A literal 0 HcmV?d00001 diff --git a/tests/UnitTests/Test.h b/include/Utils/Test.h similarity index 100% rename from tests/UnitTests/Test.h rename to include/Utils/Test.h diff --git a/tests/CMakeLists.txt b/tests/CMakeLists.txt index e1af6d31..9a97fea5 100644 --- a/tests/CMakeLists.txt +++ b/tests/CMakeLists.txt @@ -1 +1,2 @@ add_subdirectory(UnitTests) +add_subdirectory(IntegrationTests) diff --git a/tests/IntegrationTests/CMakeLists.txt b/tests/IntegrationTests/CMakeLists.txt new file mode 100644 index 00000000..6071631f --- /dev/null +++ b/tests/IntegrationTests/CMakeLists.txt @@ -0,0 +1 @@ +add_subdirectory(ImageProcessing) diff --git a/tests/UnitTests/ImageProcessing/CMakeLists.txt b/tests/IntegrationTests/ImageProcessing/CMakeLists.txt similarity index 70% rename from tests/UnitTests/ImageProcessing/CMakeLists.txt rename to tests/IntegrationTests/ImageProcessing/CMakeLists.txt index 66ba85f6..2ac84139 100644 --- a/tests/UnitTests/ImageProcessing/CMakeLists.txt +++ b/tests/IntegrationTests/ImageProcessing/CMakeLists.txt @@ -2,4 +2,8 @@ find_package(OpenCV REQUIRED CONFIG) include_directories(${OpenCV_INCLUDE_DIRS}) add_executable(DIPCorr2DTest DIPCorr2DTest.cpp) -target_link_libraries(DIPCorr2DTest ${OpenCV_LIBS} Corr2D Container PNGImage) + +include(GoogleTest) +gtest_discover_tests(DIPCorr2DTest) + +target_link_libraries(DIPCorr2DTest ${OpenCV_LIBS} Corr2D Container PNGImage gtest_main) diff --git a/tests/UnitTests/ImageProcessing/DIPCorr2DTest.cpp b/tests/IntegrationTests/ImageProcessing/DIPCorr2DTest.cpp similarity index 74% rename from tests/UnitTests/ImageProcessing/DIPCorr2DTest.cpp rename to tests/IntegrationTests/ImageProcessing/DIPCorr2DTest.cpp index 7bd933d9..3035b60c 100644 --- a/tests/UnitTests/ImageProcessing/DIPCorr2DTest.cpp +++ b/tests/IntegrationTests/ImageProcessing/DIPCorr2DTest.cpp @@ -1,10 +1,13 @@ -#include "../../../include/ImageProcessing/Kernels.h" +#include "ImageProcessing/Kernels.h" #include "Utils/Container.h" #include #include +#include #include +#include +// #include "../../UniTests/Test.h" using namespace cv; using namespace std; @@ -16,6 +19,30 @@ void _mlir_ciface_corr_2d(MemRef *input, MemRef *kernel, unsigned int centerY, int boundaryOption); } +// Fixture for testing the dip.corr_2d operation. +class FilterTest : public ::testing::Test { +protected: + void SetUp() + { + inputImage = imread(imageName, IMREAD_GRAYSCALE); + } + +public: + static void setImageName(std::string imageNameParam) + { + imageName = imageNameParam; + } + + const Mat& getInputImage() + { + return inputImage; + } + +private: + Mat inputImage; + static std::string imageName; +}; + bool equalImages(const Mat &img1, const Mat &img2) { if (img1.rows != img2.rows || img1.cols != img2.cols) { std::cout << "Produced outputs by DIP and OpenCV differ. Image dimensions " @@ -56,7 +83,7 @@ void testKernelImpl(const Mat &inputImage, unsigned int kernelRows, _mlir_ciface_corr_2d(&input, &kernel, &output, x, y, 0); - filter2D(inputImage, opencvOutput, CV_32FC1, kernel1, cv::Point(x, y), 1.0, + filter2D(inputImage, opencvOutput, CV_32FC1, kernel1, cv::Point(x, y), 0.0, cv::BORDER_REPLICATE); // Define a cv::Mat with the output of corr_2d. @@ -79,11 +106,20 @@ void testKernel(const Mat &inputImage, unsigned int kernelRows, testKernelImpl(inputImage, kernelRows, kernelCols, kernelArray, x, y); } -int main(int argc, char **argv) { - // Read input image - Mat inputImage = imread(argv[1], IMREAD_GRAYSCALE); - +TEST_F(FilterTest, OpenCVComparison) { for (auto kernel : kernelMap) - testKernel(inputImage, get<1>(kernel.second), get<2>(kernel.second), + testKernel(getInputImage(), get<1>(kernel.second), get<2>(kernel.second), get<0>(kernel.second)); + + // testKernel(getInputImage(), 3, 3, laplacianKernelAlign); + // ASSERT_EQ(2, 2); +} + +string FilterTest::imageName = ""; + +int main(int argc, char **argv) +{ + FilterTest::setImageName(argv[1]); + ::testing::InitGoogleTest(&argc, argv); + return RUN_ALL_TESTS(); } diff --git a/tests/UnitTests/CMakeLists.txt b/tests/UnitTests/CMakeLists.txt index 7569f9f8..e6774589 100644 --- a/tests/UnitTests/CMakeLists.txt +++ b/tests/UnitTests/CMakeLists.txt @@ -11,5 +11,3 @@ add_dependencies(test-container Container) include(GoogleTest) gtest_discover_tests(test-container) - -add_subdirectory(ImageProcessing) diff --git a/tests/UnitTests/TestContainer.cpp b/tests/UnitTests/TestContainer.cpp index c567d41b..f55e6efb 100644 --- a/tests/UnitTests/TestContainer.cpp +++ b/tests/UnitTests/TestContainer.cpp @@ -18,7 +18,7 @@ // //===----------------------------------------------------------------------===// -#include "Test.h" +#include "Utils/Test.h" #include "Utils/Container.h" // Fixture for testing the MemRef class. From b66e055915a0cb35a76ed907dd7e89b55fb0f148 Mon Sep 17 00:00:00 2001 From: meshtag Date: Sat, 12 Feb 2022 01:39:33 +0800 Subject: [PATCH 4/8] Dummy commit --- benchmarks/ImageProcessing/CMakeLists.txt | 2 +- include/ImageProcessing/Kernels.h | 6 +++ .../ImageProcessing/DIPCorr2DTest.cpp | 41 +++++++++++-------- 3 files changed, 31 insertions(+), 18 deletions(-) diff --git a/benchmarks/ImageProcessing/CMakeLists.txt b/benchmarks/ImageProcessing/CMakeLists.txt index d40643e8..2b21233c 100644 --- a/benchmarks/ImageProcessing/CMakeLists.txt +++ b/benchmarks/ImageProcessing/CMakeLists.txt @@ -30,7 +30,7 @@ add_custom_command(OUTPUT corr2d.o COMMAND ${BUDDY_OPT_BUILD_DIR}/bin/buddy-opt ${BUDDY_SOURCE_DIR}/benchmarks/ImageProcessing/Corr2D.mlir -lower-dip="DIP-strip-mining=${BUDDY_OPT_STRIP_MINING}" - -lower-affine -convert-scf-to-std -convert-vector-to-llvm + -lower-affine -convert-scf-to-cf -convert-vector-to-llvm -convert-memref-to-llvm -convert-std-to-llvm='emit-c-wrappers=1' -reconcile-unrealized-casts | ${LLVM_MLIR_BINARY_DIR}/mlir-translate --mlir-to-llvmir | diff --git a/include/ImageProcessing/Kernels.h b/include/ImageProcessing/Kernels.h index fb029280..3378cc6d 100644 --- a/include/ImageProcessing/Kernels.h +++ b/include/ImageProcessing/Kernels.h @@ -22,6 +22,7 @@ #define IMAGE_PROCESSING_KERNELS // clang-format off +#include #include #include @@ -85,6 +86,11 @@ static std::map> kernelMap = { {"logKernelAlign", {logKernelAlign, logKernelRows, logKernelCols}} }; +static std::vector imageNames = { + "test_6x6.png", "YuTu.png", "YuTu4.png", "YuTu6.png", + "YuTu8.png", "YuTu16.png", "YuTu32.png", "YuTu64.png", + "YuTu128.png", "YuTu256.png", "YuTu512.png", "YuTu1024.png"}; + // clang-format on #endif // IMAGE_PROCESSING_KERNELS diff --git a/tests/IntegrationTests/ImageProcessing/DIPCorr2DTest.cpp b/tests/IntegrationTests/ImageProcessing/DIPCorr2DTest.cpp index 3035b60c..02ab7c29 100644 --- a/tests/IntegrationTests/ImageProcessing/DIPCorr2DTest.cpp +++ b/tests/IntegrationTests/ImageProcessing/DIPCorr2DTest.cpp @@ -21,26 +21,19 @@ void _mlir_ciface_corr_2d(MemRef *input, MemRef *kernel, // Fixture for testing the dip.corr_2d operation. class FilterTest : public ::testing::Test { -protected: - void SetUp() - { - inputImage = imread(imageName, IMREAD_GRAYSCALE); - } - public: - static void setImageName(std::string imageNameParam) + static void setImageNames(std::vector testImageNamesParam) { - imageName = imageNameParam; + testImageNames = testImageNamesParam; } - const Mat& getInputImage() + const std::vector& getTestImageNames() { - return inputImage; + return testImageNames; } private: - Mat inputImage; - static std::string imageName; + static std::vector testImageNames; }; bool equalImages(const Mat &img1, const Mat &img2) { @@ -107,19 +100,33 @@ void testKernel(const Mat &inputImage, unsigned int kernelRows, } TEST_F(FilterTest, OpenCVComparison) { - for (auto kernel : kernelMap) - testKernel(getInputImage(), get<1>(kernel.second), get<2>(kernel.second), - get<0>(kernel.second)); + // for (auto kernel : kernelMap) + // testKernel(getInputImage(), get<1>(kernel.second), get<2>(kernel.second), + // get<0>(kernel.second)); + std::vector testImageNames = getTestImageNames(); + for (auto testImageName : testImageNames) + { + for (auto kernel : kernelMap) + { + std::string testImagePath = "../../benchmarks/ImageProcessing/Images/" + testImageName; + cv::Mat testImage = cv::imread(testImagePath, cv::IMREAD_GRAYSCALE); + testKernel(testImage, get<1>(kernel.second), get<2>(kernel.second), get<0>(kernel.second)); + } + } // testKernel(getInputImage(), 3, 3, laplacianKernelAlign); // ASSERT_EQ(2, 2); } -string FilterTest::imageName = ""; +std::vector FilterTest::testImageNames = {}; int main(int argc, char **argv) { - FilterTest::setImageName(argv[1]); + FilterTest::setImageNames(imageNames); + + // for (auto imageName : imageNames) + // FilterTest::setImageName() + ::testing::InitGoogleTest(&argc, argv); return RUN_ALL_TESTS(); } From 653cdf75ed66e9ea4475ab13890c02f75915626d Mon Sep 17 00:00:00 2001 From: meshtag Date: Wed, 30 Mar 2022 15:41:29 +0800 Subject: [PATCH 5/8] Interface update --- benchmarks/ImageProcessing/BuddyCorr2D.mlir | 7 ++++--- benchmarks/ImageProcessing/BuddyCorr2DBenchmark.cpp | 6 +++--- 2 files changed, 7 insertions(+), 6 deletions(-) diff --git a/benchmarks/ImageProcessing/BuddyCorr2D.mlir b/benchmarks/ImageProcessing/BuddyCorr2D.mlir index 54d99126..19904da7 100644 --- a/benchmarks/ImageProcessing/BuddyCorr2D.mlir +++ b/benchmarks/ImageProcessing/BuddyCorr2D.mlir @@ -20,9 +20,10 @@ func @corr_2d(%inputImage : memref, %kernel : memref, %outputImage : memref, %centerX : index, - %centerY : index, %boundaryOption : index) { + %centerY : index, %constantValue : f32) { dip.corr_2d %inputImage, %kernel, %outputImage, %centerX, %centerY, - %boundaryOption : memref, memref, - memref, index, index, index + %constantValue {boundary_option = "CONSTANT_PADDING"} : + memref, memref, memref, index, + index, f32 return } diff --git a/benchmarks/ImageProcessing/BuddyCorr2DBenchmark.cpp b/benchmarks/ImageProcessing/BuddyCorr2DBenchmark.cpp index 869652bf..3d60f2bd 100644 --- a/benchmarks/ImageProcessing/BuddyCorr2DBenchmark.cpp +++ b/benchmarks/ImageProcessing/BuddyCorr2DBenchmark.cpp @@ -31,8 +31,8 @@ extern "C" { void _mlir_ciface_corr_2d(MemRef *inputBuddyCorr2D, MemRef *kernelBuddyCorr2D, MemRef *outputBuddyCorr2D, - unsigned int centerX, unsigned int centerY, - int boundaryOption); + unsigned int centerX, unsigned int centerY, + float constantValue); } // Declare input image and kernel. @@ -83,7 +83,7 @@ static void Buddy_Corr2D(benchmark::State &state) { for (int i = 0; i < state.range(0); ++i) { _mlir_ciface_corr_2d(&inputBuddyCorr2D, &kernelBuddyCorr2D, &outputBuddyCorr2D, 1 /* Center X */, - 1 /* Center Y */, 0 /* Boundary Option */); + 1 /* Center Y */, 0.0f); } } } From da6114d44818d941ff8f63d49484e54ea51ce88f Mon Sep 17 00:00:00 2001 From: meshtag Date: Sun, 3 Apr 2022 22:03:52 +0800 Subject: [PATCH 6/8] Rectify code --- include/ImageProcessing/Kernels.h | 5 -- .../ImageProcessing/CMakeLists.txt | 2 +- .../ImageProcessing/DIPCorr2DTest.cpp | 58 ++++++++----------- 3 files changed, 25 insertions(+), 40 deletions(-) diff --git a/include/ImageProcessing/Kernels.h b/include/ImageProcessing/Kernels.h index f1ded6c2..47d745a1 100644 --- a/include/ImageProcessing/Kernels.h +++ b/include/ImageProcessing/Kernels.h @@ -177,11 +177,6 @@ static std::map> kernelMap = { {"random15x15KernelAlign", {random15x15KernelAlign, random15x15KernelRows, random15x15KernelCols}} }; -static std::vector imageNames = { - "test_6x6.png", "YuTu.png", "YuTu4.png", "YuTu6.png", - "YuTu8.png", "YuTu16.png", "YuTu32.png", "YuTu64.png", - "YuTu128.png", "YuTu256.png", "YuTu512.png", "YuTu1024.png"}; - // clang-format on #endif // IMAGE_PROCESSING_KERNELS diff --git a/tests/IntegrationTests/ImageProcessing/CMakeLists.txt b/tests/IntegrationTests/ImageProcessing/CMakeLists.txt index 2ac84139..5b460dd6 100644 --- a/tests/IntegrationTests/ImageProcessing/CMakeLists.txt +++ b/tests/IntegrationTests/ImageProcessing/CMakeLists.txt @@ -6,4 +6,4 @@ add_executable(DIPCorr2DTest DIPCorr2DTest.cpp) include(GoogleTest) gtest_discover_tests(DIPCorr2DTest) -target_link_libraries(DIPCorr2DTest ${OpenCV_LIBS} Corr2D Container PNGImage gtest_main) +target_link_libraries(DIPCorr2DTest ${OpenCV_LIBS} BuddyCorr2D Container PNGImage gtest_main) diff --git a/tests/IntegrationTests/ImageProcessing/DIPCorr2DTest.cpp b/tests/IntegrationTests/ImageProcessing/DIPCorr2DTest.cpp index 02ab7c29..a88f1a75 100644 --- a/tests/IntegrationTests/ImageProcessing/DIPCorr2DTest.cpp +++ b/tests/IntegrationTests/ImageProcessing/DIPCorr2DTest.cpp @@ -7,35 +7,42 @@ #include #include #include -// #include "../../UniTests/Test.h" using namespace cv; using namespace std; // Declare the Corr2D C interface. extern "C" { -void _mlir_ciface_corr_2d(MemRef *input, MemRef *kernel, - MemRef *output, unsigned int centerX, - unsigned int centerY, int boundaryOption); +void _mlir_ciface_corr_2d_constant_padding(MemRef *input, MemRef *kernel, + MemRef *output, unsigned int centerX, + unsigned int centerY, float constantValue); } // Fixture for testing the dip.corr_2d operation. class FilterTest : public ::testing::Test { public: - static void setImageNames(std::vector testImageNamesParam) + static void setImageNames(int argc, char **argv) { - testImageNames = testImageNamesParam; + if (argc > 1) + testImageName = argv[1]; + else + { + testImageName = "../../benchmarks/ImageProcessing/Images/YuTu.png"; + std::cout << "Reached here\n"; + } } - const std::vector& getTestImageNames() + const std::string getTestImageName() { - return testImageNames; + return testImageName; } private: - static std::vector testImageNames; + static std::string testImageName; }; +std::string FilterTest::testImageName; + bool equalImages(const Mat &img1, const Mat &img2) { if (img1.rows != img2.rows || img1.cols != img2.cols) { std::cout << "Produced outputs by DIP and OpenCV differ. Image dimensions " @@ -45,7 +52,7 @@ bool equalImages(const Mat &img1, const Mat &img2) { for (unsigned int y = 0; y < img1.rows; ++y) { for (unsigned int x = 0; x < img1.cols; ++x) { - if (abs(img1.at(x, y) - img2.at(x, y)) > 10e-3) { + if (abs(img1.at(x, y) - img2.at(x, y)) > 10e-2) { std::cout << "Produced outputs by DIP and OpenCV differ.\n"; return 0; } @@ -58,14 +65,13 @@ void testKernelImpl(const Mat &inputImage, unsigned int kernelRows, unsigned int kernelCols, float *kernelArray, unsigned int x, unsigned int y) { // Define container sizes. - intptr_t sizesInput[2] = {inputImage.rows, inputImage.cols}; + intptr_t sizesImage[2] = {inputImage.rows, inputImage.cols}; intptr_t sizesKernel[2] = {kernelRows, kernelCols}; - intptr_t sizesOutput[2] = {inputImage.rows, inputImage.cols}; // Define input, kernel, and output. - MemRef input(inputImage, sizesInput); + MemRef input(inputImage, sizesImage); MemRef kernel(kernelArray, sizesKernel); - MemRef output(sizesOutput); + MemRef output(sizesImage); for (int i = 0; i < inputImage.rows; i++) for (int j = 0; j < inputImage.cols; j++) @@ -74,10 +80,10 @@ void testKernelImpl(const Mat &inputImage, unsigned int kernelRows, Mat kernel1 = Mat(kernelRows, kernelCols, CV_32FC1, kernelArray); Mat opencvOutput; - _mlir_ciface_corr_2d(&input, &kernel, &output, x, y, 0); + _mlir_ciface_corr_2d_constant_padding(&input, &kernel, &output, x, y, 0); filter2D(inputImage, opencvOutput, CV_32FC1, kernel1, cv::Point(x, y), 0.0, - cv::BORDER_REPLICATE); + cv::BORDER_CONSTANT); // Define a cv::Mat with the output of corr_2d. Mat dipOutput(inputImage.rows, inputImage.cols, CV_32FC1, output.getData()); @@ -100,32 +106,16 @@ void testKernel(const Mat &inputImage, unsigned int kernelRows, } TEST_F(FilterTest, OpenCVComparison) { - // for (auto kernel : kernelMap) - // testKernel(getInputImage(), get<1>(kernel.second), get<2>(kernel.second), - // get<0>(kernel.second)); - std::vector testImageNames = getTestImageNames(); - for (auto testImageName : testImageNames) - { for (auto kernel : kernelMap) { - std::string testImagePath = "../../benchmarks/ImageProcessing/Images/" + testImageName; - cv::Mat testImage = cv::imread(testImagePath, cv::IMREAD_GRAYSCALE); + cv::Mat testImage = cv::imread(getTestImageName(), cv::IMREAD_GRAYSCALE); testKernel(testImage, get<1>(kernel.second), get<2>(kernel.second), get<0>(kernel.second)); } - } - - // testKernel(getInputImage(), 3, 3, laplacianKernelAlign); - // ASSERT_EQ(2, 2); } -std::vector FilterTest::testImageNames = {}; - int main(int argc, char **argv) { - FilterTest::setImageNames(imageNames); - - // for (auto imageName : imageNames) - // FilterTest::setImageName() + FilterTest::setImageNames(argc, argv); ::testing::InitGoogleTest(&argc, argv); return RUN_ALL_TESTS(); From 4e8d514cc3e06d1ebca6680e72a08860c8951e8b Mon Sep 17 00:00:00 2001 From: meshtag Date: Thu, 7 Apr 2022 03:27:54 +0800 Subject: [PATCH 7/8] Clarify CLI command --- README.md | 21 +++++------------- .../ImageProcessing/Images/test_6x6.png | Bin 75 -> 0 bytes .../ImageProcessing/CMakeLists.txt | 16 ++++++++++--- .../ImageProcessing/DIPCorr2DTest.cpp | 5 +---- 4 files changed, 19 insertions(+), 23 deletions(-) delete mode 100644 benchmarks/ImageProcessing/Images/test_6x6.png diff --git a/README.md b/README.md index 5f880952..15383591 100644 --- a/README.md +++ b/README.md @@ -126,24 +126,13 @@ $ cmake -G Ninja .. \ -DBUDDY_OPT_STRIP_MINING= \ -DBUDDY_OPT_ATTR= $ cmake --build . -- -$ ninja test +$ ninja test && ninja image-processing-integration-tests ``` -For tests related to DIP dialect : - +For executing Image Processing integration tests : ``` -$ cd buddy-benchmark -$ mkdir build && cd build -$ cmake -G Ninja .. \ - -DIMAGE_PROCESSING_BENCHMARKS=ON \ - -DDEEP_LEARNING_BENCHMARKS=ON \ - -DBUILD_TESTS=ON \ - -DOpenCV_DIR=/path/to/opencv/build/ \ - -DBUDDY_OPT_BUILD_DIR=/path/to/buddy-mlir/build/ \ - -DBUDDY_OPT_STRIP_MINING= \ - -DBUDDY_OPT_ATTR= -$ ninja DIPCorr2DTest -$ cd bin && ./DIPCorr2DTest +cd bin +./image-processing-integration-tests ``` -where `` is the path of image used as first operand during 2D Correlation. +where `` is an optional parameter used for pointing to the image which is to be used while executing tests. diff --git a/benchmarks/ImageProcessing/Images/test_6x6.png b/benchmarks/ImageProcessing/Images/test_6x6.png deleted file mode 100644 index ee3e72078d81a9dcbd45ddd0a52a34ca1ce4a6d3..0000000000000000000000000000000000000000 GIT binary patch literal 0 HcmV?d00001 literal 75 zcmeAS@N?(olHy`uVBq!ia0vp^Y#_`5Bp8k^-!KJ833<9WhHykDrzEg2n;D#JYG-0# W^JDP8R35Y%q{`FP&t;ucLK6TC(hz(A diff --git a/tests/IntegrationTests/ImageProcessing/CMakeLists.txt b/tests/IntegrationTests/ImageProcessing/CMakeLists.txt index 5b460dd6..acb2d790 100644 --- a/tests/IntegrationTests/ImageProcessing/CMakeLists.txt +++ b/tests/IntegrationTests/ImageProcessing/CMakeLists.txt @@ -1,9 +1,19 @@ find_package(OpenCV REQUIRED CONFIG) include_directories(${OpenCV_INCLUDE_DIRS}) -add_executable(DIPCorr2DTest DIPCorr2DTest.cpp) +add_executable( + image-processing-integration-tests + DIPCorr2DTest.cpp +) include(GoogleTest) -gtest_discover_tests(DIPCorr2DTest) +gtest_discover_tests(image-processing-integration-tests) -target_link_libraries(DIPCorr2DTest ${OpenCV_LIBS} BuddyCorr2D Container PNGImage gtest_main) +target_link_libraries( + image-processing-integration-tests + ${OpenCV_LIBS} + BuddyCorr2D + Container + PNGImage + gtest_main +) diff --git a/tests/IntegrationTests/ImageProcessing/DIPCorr2DTest.cpp b/tests/IntegrationTests/ImageProcessing/DIPCorr2DTest.cpp index a88f1a75..83bfb842 100644 --- a/tests/IntegrationTests/ImageProcessing/DIPCorr2DTest.cpp +++ b/tests/IntegrationTests/ImageProcessing/DIPCorr2DTest.cpp @@ -26,10 +26,7 @@ class FilterTest : public ::testing::Test { if (argc > 1) testImageName = argv[1]; else - { - testImageName = "../../benchmarks/ImageProcessing/Images/YuTu.png"; - std::cout << "Reached here\n"; - } + testImageName = "../../benchmarks/ImageProcessing/Images/YuTu.png"; } const std::string getTestImageName() From e9cedb82ef4b0ca0a7a33a6c41f51d3c9a8bc916 Mon Sep 17 00:00:00 2001 From: meshtag Date: Thu, 7 Apr 2022 03:38:41 +0800 Subject: [PATCH 8/8] Apply clang-format --- .../ImageProcessing/CMakeLists.txt | 2 +- .../ImageProcessing/DIPCorr2DTest.cpp | 29 ++++++++----------- tests/UnitTests/TestContainer.cpp | 2 +- 3 files changed, 14 insertions(+), 19 deletions(-) diff --git a/tests/IntegrationTests/ImageProcessing/CMakeLists.txt b/tests/IntegrationTests/ImageProcessing/CMakeLists.txt index acb2d790..fa6c9ab7 100644 --- a/tests/IntegrationTests/ImageProcessing/CMakeLists.txt +++ b/tests/IntegrationTests/ImageProcessing/CMakeLists.txt @@ -14,6 +14,6 @@ target_link_libraries( ${OpenCV_LIBS} BuddyCorr2D Container - PNGImage + gtest_main ) diff --git a/tests/IntegrationTests/ImageProcessing/DIPCorr2DTest.cpp b/tests/IntegrationTests/ImageProcessing/DIPCorr2DTest.cpp index 83bfb842..1df7d209 100644 --- a/tests/IntegrationTests/ImageProcessing/DIPCorr2DTest.cpp +++ b/tests/IntegrationTests/ImageProcessing/DIPCorr2DTest.cpp @@ -13,26 +13,22 @@ using namespace std; // Declare the Corr2D C interface. extern "C" { -void _mlir_ciface_corr_2d_constant_padding(MemRef *input, MemRef *kernel, - MemRef *output, unsigned int centerX, - unsigned int centerY, float constantValue); +void _mlir_ciface_corr_2d_constant_padding( + MemRef *input, MemRef *kernel, MemRef *output, + unsigned int centerX, unsigned int centerY, float constantValue); } // Fixture for testing the dip.corr_2d operation. class FilterTest : public ::testing::Test { public: - static void setImageNames(int argc, char **argv) - { + static void setImageNames(int argc, char **argv) { if (argc > 1) testImageName = argv[1]; - else + else testImageName = "../../benchmarks/ImageProcessing/Images/YuTu.png"; } - const std::string getTestImageName() - { - return testImageName; - } + const std::string getTestImageName() { return testImageName; } private: static std::string testImageName; @@ -103,15 +99,14 @@ void testKernel(const Mat &inputImage, unsigned int kernelRows, } TEST_F(FilterTest, OpenCVComparison) { - for (auto kernel : kernelMap) - { - cv::Mat testImage = cv::imread(getTestImageName(), cv::IMREAD_GRAYSCALE); - testKernel(testImage, get<1>(kernel.second), get<2>(kernel.second), get<0>(kernel.second)); - } + for (auto kernel : kernelMap) { + cv::Mat testImage = cv::imread(getTestImageName(), cv::IMREAD_GRAYSCALE); + testKernel(testImage, get<1>(kernel.second), get<2>(kernel.second), + get<0>(kernel.second)); + } } -int main(int argc, char **argv) -{ +int main(int argc, char **argv) { FilterTest::setImageNames(argc, argv); ::testing::InitGoogleTest(&argc, argv); diff --git a/tests/UnitTests/TestContainer.cpp b/tests/UnitTests/TestContainer.cpp index cd19292a..bedd046d 100644 --- a/tests/UnitTests/TestContainer.cpp +++ b/tests/UnitTests/TestContainer.cpp @@ -18,8 +18,8 @@ // //===----------------------------------------------------------------------===// -#include "Utils/Test.h" #include "Utils/Container.h" +#include "Utils/Test.h" #include // Fixture for testing the MemRef class.