-
Notifications
You must be signed in to change notification settings - Fork 50
[DIP] Add benchmark for Buddy & OpenCV resize2d operation. #61
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
taiqzheng
wants to merge
6
commits into
buddy-compiler:main
Choose a base branch
from
taiqzheng:resize_dev
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
6 commits
Select commit
Hold shift + click to select a range
9fd2c3e
[DIP] Add benchmark for Buddy & OpenCV resize2d operation.
taiqzheng ad8799e
Adjust dimension format in OpenCV benchmark.
taiqzheng 1af0a0a
Delete duplicate enum class for interpolation type.
taiqzheng 0fb7ef0
Cange dimension order in initializeBuddyResize2D().
taiqzheng 4e2d9d9
Reformat code with clang-format.
taiqzheng 9ef53f3
Remove duplicatte enum class for OpenCVResize2DBenchmark.cpp.
taiqzheng File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,243 @@ | ||
| //===- BuddyResize2DBenchmark.cpp -----------------------------------------===// | ||
| // | ||
| // Licensed under the Apache License, Version 2.0 (the "License"); | ||
| // you may not use this file except in compliance with the License. | ||
| // You may obtain a copy of the License at | ||
| // | ||
| // http://www.apache.org/licenses/LICENSE-2.0 | ||
| // | ||
| // Unless required by applicable law or agreed to in writing, software | ||
| // distributed under the License is distributed on an "AS IS" BASIS, | ||
| // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
| // See the License for the specific language governing permissions and | ||
| // limitations under the License. | ||
| // | ||
| //===----------------------------------------------------------------------===// | ||
| // | ||
| // This file implements the benchmark for Resize2D operation. | ||
| // | ||
| //===----------------------------------------------------------------------===// | ||
|
|
||
| #include <benchmark/benchmark.h> | ||
| #include <buddy/Core/Container.h> | ||
| #include <buddy/DIP/DIP.h> | ||
| #include <buddy/DIP/ImageContainer.h> | ||
| #include <opencv2/opencv.hpp> | ||
|
|
||
| using namespace cv; | ||
| using namespace std; | ||
|
|
||
| // Declare input image. | ||
| Mat inputImageBuddyResize2D; | ||
|
|
||
| // Define the output size or factor. | ||
| int outputRowsBuddyResize2DLength, outputColsBuddyResize2DLength; | ||
| float outputRowsBuddyResize2DFactor, outputColsBuddyResize2DFactor; | ||
|
|
||
| // Define sizes of input and output. | ||
| intptr_t sizesInputBuddyResize2D[2]; | ||
| intptr_t sizesOutputBuddyResize2D[2]; | ||
| std::vector<float> factorsOutputBuddyResize2D = {1.0, 1.0}; | ||
|
|
||
| // Declare Scale option supported. | ||
| enum ScaleOption { scale_factor, scale_length }; | ||
|
|
||
| // Define Interpolation option selected. | ||
| dip::INTERPOLATION_TYPE InterpolationType; | ||
|
|
||
| // Define Scale option selected. | ||
| ScaleOption ScaleType; | ||
|
|
||
| void initializeBuddyResize2D(char **argv) { | ||
| inputImageBuddyResize2D = imread(argv[1], IMREAD_GRAYSCALE); | ||
|
|
||
| sizesInputBuddyResize2D[0] = inputImageBuddyResize2D.rows; | ||
| sizesInputBuddyResize2D[1] = inputImageBuddyResize2D.cols; | ||
|
|
||
| if (static_cast<string>(argv[2]) == "SCALE_FACTOR") { | ||
| ScaleType = scale_factor; | ||
| } else { | ||
| ScaleType = scale_length; | ||
| } | ||
|
|
||
| std::string argRow = argv[3]; | ||
| std::string argCol = argv[4]; | ||
| try { | ||
| if (ScaleType == scale_factor) { | ||
| float outputRowsBuddyResize2DFactor = std::stof(argRow); | ||
| float outputColsBuddyResize2DFactor = std::stof(argCol); | ||
| factorsOutputBuddyResize2D[0] = outputColsBuddyResize2DFactor; | ||
| factorsOutputBuddyResize2D[1] = outputRowsBuddyResize2DFactor; | ||
| sizesOutputBuddyResize2D[0] = | ||
| sizesInputBuddyResize2D[0] * outputColsBuddyResize2DFactor; | ||
| sizesOutputBuddyResize2D[1] = | ||
| sizesInputBuddyResize2D[1] * outputRowsBuddyResize2DFactor; | ||
| } else { | ||
| intptr_t outputRowsBuddyResize2DLength = std::stoi(argRow); | ||
| intptr_t outputColsBuddyResize2DLength = std::stoi(argCol); | ||
| sizesOutputBuddyResize2D[0] = outputColsBuddyResize2DLength; | ||
| sizesOutputBuddyResize2D[1] = outputRowsBuddyResize2DLength; | ||
| } | ||
| } catch (const std::exception &e) { | ||
| cout << "Exception converting row and col scale_factor/scale_length to " | ||
| "number." | ||
| << endl; | ||
| } | ||
|
|
||
| if (static_cast<string>(argv[5]) == "NEAREST_NEIGHBOUR_INTERPOLATION") { | ||
| InterpolationType = | ||
| dip::INTERPOLATION_TYPE::NEAREST_NEIGHBOUR_INTERPOLATION; | ||
| } else { | ||
| InterpolationType = dip::INTERPOLATION_TYPE::BILINEAR_INTERPOLATION; | ||
| } | ||
| } | ||
|
|
||
| static void | ||
| Buddy_Resize2D_Bilinear_Interpolation_Length(benchmark::State &state) { | ||
| // Define the MemRef descriptor for input. | ||
| Img<float, 2> inputBuddyResize2D(inputImageBuddyResize2D); | ||
|
|
||
| for (auto _ : state) { | ||
| for (int i = 0; i < state.range(0); ++i) { | ||
| // Call the MLIR Resize2D function. | ||
| MemRef<float, 2> output = dip::Resize2D( | ||
| &inputBuddyResize2D, dip::INTERPOLATION_TYPE::BILINEAR_INTERPOLATION, | ||
| sizesOutputBuddyResize2D); | ||
| } | ||
| } | ||
| } | ||
|
|
||
| static void | ||
| Buddy_Resize2D_Nearest_Neighbour_Interpolation_Length(benchmark::State &state) { | ||
| // Define the MemRef descriptor for input. | ||
| Img<float, 2> inputBuddyResize2D(inputImageBuddyResize2D); | ||
|
|
||
| for (auto _ : state) { | ||
| for (int i = 0; i < state.range(0); ++i) { | ||
| // Call the MLIR Resize2D function. | ||
| MemRef<float, 2> output = dip::Resize2D( | ||
| &inputBuddyResize2D, | ||
| dip::INTERPOLATION_TYPE::NEAREST_NEIGHBOUR_INTERPOLATION, | ||
| sizesOutputBuddyResize2D); | ||
| } | ||
| } | ||
| } | ||
|
|
||
| static void | ||
| Buddy_Resize2D_Bilinear_Interpolation_Factor(benchmark::State &state) { | ||
| // Define the MemRef descriptor for input. | ||
| Img<float, 2> inputBuddyResize2D(inputImageBuddyResize2D); | ||
|
|
||
| for (auto _ : state) { | ||
| for (int i = 0; i < state.range(0); ++i) { | ||
| // Call the MLIR Resize2D function. | ||
| MemRef<float, 2> output = dip::Resize2D( | ||
| &inputBuddyResize2D, dip::INTERPOLATION_TYPE::BILINEAR_INTERPOLATION, | ||
| factorsOutputBuddyResize2D); | ||
| } | ||
| } | ||
| } | ||
|
|
||
| static void | ||
| Buddy_Resize2D_Nearest_Neighbour_Interpolation_Factor(benchmark::State &state) { | ||
| // Define the MemRef descriptor for input. | ||
| Img<float, 2> inputBuddyResize2D(inputImageBuddyResize2D); | ||
|
|
||
| for (auto _ : state) { | ||
| for (int i = 0; i < state.range(0); ++i) { | ||
| // Call the MLIR Resize2D function. | ||
| MemRef<float, 2> output = dip::Resize2D( | ||
| &inputBuddyResize2D, | ||
| dip::INTERPOLATION_TYPE::NEAREST_NEIGHBOUR_INTERPOLATION, | ||
| factorsOutputBuddyResize2D); | ||
| } | ||
| } | ||
| } | ||
|
|
||
| // Register benchmarking function. | ||
| void registerBenchmarkBuddyResize2D() { | ||
| if (InterpolationType == | ||
| dip::INTERPOLATION_TYPE::NEAREST_NEIGHBOUR_INTERPOLATION && | ||
| ScaleType == scale_factor) { | ||
| BENCHMARK(Buddy_Resize2D_Nearest_Neighbour_Interpolation_Factor) | ||
| ->Arg(1) | ||
| ->Unit(benchmark::kMillisecond); | ||
| } else if (InterpolationType == | ||
| dip::INTERPOLATION_TYPE::BILINEAR_INTERPOLATION && | ||
| ScaleType == scale_factor) { | ||
| BENCHMARK(Buddy_Resize2D_Bilinear_Interpolation_Factor) | ||
| ->Arg(1) | ||
| ->Unit(benchmark::kMillisecond); | ||
| } else if (InterpolationType == | ||
| dip::INTERPOLATION_TYPE::NEAREST_NEIGHBOUR_INTERPOLATION && | ||
| ScaleType == scale_length) { | ||
| BENCHMARK(Buddy_Resize2D_Nearest_Neighbour_Interpolation_Length) | ||
| ->Arg(1) | ||
| ->Unit(benchmark::kMillisecond); | ||
| } else if (InterpolationType == | ||
| dip::INTERPOLATION_TYPE::BILINEAR_INTERPOLATION && | ||
| ScaleType == scale_length) { | ||
| BENCHMARK(Buddy_Resize2D_Bilinear_Interpolation_Length) | ||
| ->Arg(1) | ||
| ->Unit(benchmark::kMillisecond); | ||
| } | ||
| } | ||
|
|
||
| // Generate result image. | ||
| void generateResultBuddyResize2D() { | ||
| // Define the MemRef descriptor for input. | ||
| Img<float, 2> input(inputImageBuddyResize2D); | ||
| MemRef<float, 2> output(sizesOutputBuddyResize2D); | ||
| // Run the resize 2D operation. | ||
| if (InterpolationType == | ||
| dip::INTERPOLATION_TYPE::NEAREST_NEIGHBOUR_INTERPOLATION && | ||
| ScaleType == scale_factor) { | ||
| // Call the MLIR Resize2D function. | ||
| output = dip::Resize2D( | ||
| &input, dip::INTERPOLATION_TYPE::NEAREST_NEIGHBOUR_INTERPOLATION, | ||
| factorsOutputBuddyResize2D); | ||
| } else if (InterpolationType == | ||
| dip::INTERPOLATION_TYPE::BILINEAR_INTERPOLATION && | ||
| ScaleType == scale_factor) { | ||
| // Call the MLIR Resize2D function. | ||
| output = | ||
| dip::Resize2D(&input, dip::INTERPOLATION_TYPE::BILINEAR_INTERPOLATION, | ||
| factorsOutputBuddyResize2D); | ||
| } else if (InterpolationType == | ||
| dip::INTERPOLATION_TYPE::NEAREST_NEIGHBOUR_INTERPOLATION && | ||
| ScaleType == scale_length) { | ||
| // Call the MLIR Resize2D function. | ||
| output = dip::Resize2D( | ||
| &input, dip::INTERPOLATION_TYPE::NEAREST_NEIGHBOUR_INTERPOLATION, | ||
| sizesOutputBuddyResize2D); | ||
| } else if (InterpolationType == | ||
| dip::INTERPOLATION_TYPE::BILINEAR_INTERPOLATION && | ||
| ScaleType == scale_length) { | ||
| // Call the MLIR Resize2D function. | ||
| output = | ||
| dip::Resize2D(&input, dip::INTERPOLATION_TYPE::BILINEAR_INTERPOLATION, | ||
| sizesOutputBuddyResize2D); | ||
| } | ||
|
|
||
| // Define a cv::Mat with the output of the resize operation. | ||
| Mat outputImage(output.getSizes()[0], output.getSizes()[1], CV_32FC1, | ||
| output.getData()); | ||
|
|
||
| // Choose a PNG compression level | ||
| vector<int> compressionParams; | ||
| compressionParams.push_back(IMWRITE_PNG_COMPRESSION); | ||
| compressionParams.push_back(9); | ||
|
|
||
| // Write output to PNG. | ||
| bool result = true; | ||
| try { | ||
| result = imwrite("ResultBuddyResize2D.png", outputImage, compressionParams); | ||
| } catch (const cv::Exception &ex) { | ||
| fprintf(stderr, "Exception converting image to PNG format: %s\n", | ||
| ex.what()); | ||
| } | ||
| if (result) | ||
| cout << "Saved PNG file." << endl; | ||
| else | ||
| cout << "ERROR: Can't save PNG file." << endl; | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,62 @@ | ||
| //===- MainResize.cpp -----------------------------------------------------===// | ||
| // | ||
| // Licensed under the Apache License, Version 2.0 (the "License"); | ||
| // you may not use this file except in compliance with the License. | ||
| // You may obtain a copy of the License at | ||
| // | ||
| // http://www.apache.org/licenses/LICENSE-2.0 | ||
| // | ||
| // Unless required by applicable law or agreed to in writing, software | ||
| // distributed under the License is distributed on an "AS IS" BASIS, | ||
| // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
| // See the License for the specific language governing permissions and | ||
| // limitations under the License. | ||
| // | ||
| //===----------------------------------------------------------------------===// | ||
| // | ||
| // This is the main file of the image processing resize benchmark. | ||
| // | ||
| //===----------------------------------------------------------------------===// | ||
|
|
||
| #include <benchmark/benchmark.h> | ||
| #include <stdexcept> | ||
|
|
||
| void initializeBuddyResize2D(char **); | ||
| void initializeOpenCVResize2D(char **); | ||
|
|
||
| void generateResultBuddyResize2D(); | ||
| void generateResultOpenCVResize2D(); | ||
|
|
||
| void registerBenchmarkBuddyResize2D(); | ||
| void registerBenchmarkOpenCVResize2D(); | ||
|
|
||
| // Run benchmarks. | ||
| int main(int argc, char **argv) { | ||
| if (argc != 6) { | ||
| throw std::invalid_argument( | ||
| "Wrong format of command line arguments.\n" | ||
| "Correct format is ./image-processing-resize-benchmark <image path> " | ||
| "<Scale option> <RowNum> <ColNum> <InterpolationOption>\n where " | ||
| "image path provides path of the image to be processed, Scale option " | ||
| "available are SCALE_FACTOR, SCALE_LENGTH. " | ||
| "RowNum and ColNum are the " | ||
| "scale_factors/scale_length for row and col, " | ||
| "Interpolation option available " | ||
| "are NEAREST_NEIGHBOUR_INTERPOLATION, BILINEAR_INTERPOLATION.\n"); | ||
| } | ||
|
|
||
| initializeBuddyResize2D(argv); | ||
| initializeOpenCVResize2D(argv); | ||
|
|
||
| registerBenchmarkBuddyResize2D(); | ||
| registerBenchmarkOpenCVResize2D(); | ||
|
|
||
| ::benchmark::Initialize(&argc, argv); | ||
| ::benchmark::RunSpecifiedBenchmarks(); | ||
|
|
||
| // Generate result image. | ||
| generateResultBuddyResize2D(); | ||
| generateResultOpenCVResize2D(); | ||
|
|
||
| return 0; | ||
| } |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.