-
Notifications
You must be signed in to change notification settings - Fork 40
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[DIP] Add benchmark for Buddy rotation operation.
- Loading branch information
Showing
3 changed files
with
221 additions
and
0 deletions.
There are no files selected for viewing
This file contains 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,141 @@ | ||
//===- BuddyRotate2DBenchmark.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 Rotate2D operation. | ||
// | ||
//===----------------------------------------------------------------------===// | ||
|
||
#include <benchmark/benchmark.h> | ||
#include <buddy/Core/Container.h> | ||
#include <buddy/DIP/ImageContainer.h> | ||
#include <buddy/DIP/DIP.h> | ||
#include <opencv2/opencv.hpp> | ||
|
||
using namespace cv; | ||
using namespace std; | ||
|
||
// Declare input image. | ||
Mat inputImageBuddyRotate2D; | ||
|
||
// Define the angle. | ||
float BuddyRotate2DAngle; | ||
|
||
// Define sizes of input. | ||
intptr_t sizesInputBuddyRotate2D[2]; | ||
|
||
// Declare Angle option supported. | ||
enum AngleOption { ANGLE_DEGREE, ANGLE_RADIAN }; | ||
|
||
// Define Angle option selected. | ||
AngleOption AngleType; | ||
|
||
void initializeBuddyRotate2D(char **argv) { | ||
inputImageBuddyRotate2D = imread(argv[1], IMREAD_GRAYSCALE); | ||
|
||
sizesInputBuddyRotate2D[0] = inputImageBuddyRotate2D.rows; | ||
sizesInputBuddyRotate2D[1] = inputImageBuddyRotate2D.cols; | ||
|
||
if (static_cast<string>(argv[2]) == "DEGREE") { | ||
AngleType = ANGLE_DEGREE; | ||
} else { | ||
AngleType = ANGLE_RADIAN; | ||
} | ||
|
||
std::string argAngle = argv[3]; | ||
try { | ||
BuddyRotate2DAngle = std::stof(argAngle); | ||
} catch (const std::exception& e) { | ||
cout << "Exception converting rotation angle to float." << endl; | ||
} | ||
} | ||
|
||
static void Buddy_Rotate2D_ANGLE_DEGREE(benchmark::State &state) { | ||
// Define the MemRef descriptor for input. | ||
Img<float, 2> inputBuddyRotate2D(inputImageBuddyRotate2D); | ||
|
||
for (auto _ : state) { | ||
for (int i = 0; i < state.range(0); ++i) { | ||
// Call the MLIR Rotate2D function. | ||
MemRef<float, 2> output = dip::Rotate2D(&inputBuddyRotate2D, BuddyRotate2DAngle, | ||
dip::ANGLE_TYPE::DEGREE); | ||
} | ||
} | ||
} | ||
|
||
static void Buddy_Rotate2D_ANGLE_RADIAN(benchmark::State &state) { | ||
// Define the MemRef descriptor for input. | ||
Img<float, 2> inputBuddyRotate2D(inputImageBuddyRotate2D); | ||
|
||
for (auto _ : state) { | ||
for (int i = 0; i < state.range(0); ++i) { | ||
// Call the MLIR Rotate2D function. | ||
MemRef<float, 2> output = dip::Rotate2D(&inputBuddyRotate2D, BuddyRotate2DAngle, | ||
dip::ANGLE_TYPE::RADIAN); | ||
} | ||
} | ||
} | ||
|
||
// Register benchmarking function. | ||
void registerBenchmarkBuddyRotate2D() { | ||
if (AngleType == ANGLE_DEGREE) { | ||
BENCHMARK(Buddy_Rotate2D_ANGLE_DEGREE) | ||
->Arg(1) | ||
->Unit(benchmark::kMillisecond); | ||
} else { | ||
BENCHMARK(Buddy_Rotate2D_ANGLE_RADIAN) | ||
->Arg(1) | ||
->Unit(benchmark::kMillisecond); | ||
} | ||
} | ||
|
||
// Generate result image. | ||
void generateResultBuddyRotate2D() { | ||
// Define the MemRef descriptor for input. | ||
Img<float, 2> input(inputImageBuddyRotate2D); | ||
MemRef<float, 2> output(sizesInputBuddyRotate2D); | ||
// Run the resize 2D operation. | ||
if (AngleType == ANGLE_DEGREE) { | ||
// Call the MLIR Rotate2D function. | ||
output = dip::Rotate2D(&input, BuddyRotate2DAngle, | ||
dip::ANGLE_TYPE::DEGREE); | ||
} else { | ||
// Call the MLIR Rotate2D function. | ||
output = dip::Rotate2D(&input, BuddyRotate2DAngle, | ||
dip::ANGLE_TYPE::RADIAN); | ||
} | ||
|
||
// 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("ResultBuddyRotate2D.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 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 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,54 @@ | ||
//===- MainRotate.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 rotate benchmark. | ||
// | ||
//===----------------------------------------------------------------------===// | ||
|
||
#include <benchmark/benchmark.h> | ||
#include <stdexcept> | ||
|
||
void initializeBuddyRotate2D(char **); | ||
|
||
void generateResultBuddyRotate2D(); | ||
|
||
void registerBenchmarkBuddyRotate2D(); | ||
|
||
// Run benchmarks. | ||
int main(int argc, char **argv) { | ||
if (argc != 4) { | ||
throw std::invalid_argument( | ||
"Wrong format of command line arguments.\n" | ||
"Correct format is ./image-processing-rotate-benchmark <image path> <Rotate " | ||
"option> <RotateAngle> \n where " | ||
"image path provides path of the image to be processed, Rotate option " | ||
"available are DEGREE, RADIAN. " | ||
"RotateAngle accepts a " | ||
"float number for Rotate option.\n"); | ||
} | ||
|
||
initializeBuddyRotate2D(argv); | ||
|
||
registerBenchmarkBuddyRotate2D(); | ||
|
||
::benchmark::Initialize(&argc, argv); | ||
::benchmark::RunSpecifiedBenchmarks(); | ||
|
||
// Generate result image. | ||
generateResultBuddyRotate2D(); | ||
|
||
return 0; | ||
} |