Skip to content

Commit 0b189ab

Browse files
committed
[DIP] Add benchmark for Buddy & OpenCV resize2d operation.
1 parent 7db95bb commit 0b189ab

File tree

4 files changed

+489
-0
lines changed

4 files changed

+489
-0
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,219 @@
1+
//===- BuddyResize2DBenchmark.cpp -------------------------------------------===//
2+
//
3+
// Licensed under the Apache License, Version 2.0 (the "License");
4+
// you may not use this file except in compliance with the License.
5+
// You may obtain a copy of the License at
6+
//
7+
// http://www.apache.org/licenses/LICENSE-2.0
8+
//
9+
// Unless required by applicable law or agreed to in writing, software
10+
// distributed under the License is distributed on an "AS IS" BASIS,
11+
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12+
// See the License for the specific language governing permissions and
13+
// limitations under the License.
14+
//
15+
//===----------------------------------------------------------------------===//
16+
//
17+
// This file implements the benchmark for Resize2D operation.
18+
//
19+
//===----------------------------------------------------------------------===//
20+
21+
#include <benchmark/benchmark.h>
22+
#include <buddy/Core/Container.h>
23+
#include <buddy/DIP/ImageContainer.h>
24+
#include <buddy/DIP/DIP.h>
25+
#include <opencv2/opencv.hpp>
26+
27+
using namespace cv;
28+
using namespace std;
29+
30+
// Declare input image.
31+
Mat inputImageBuddyResize2D;
32+
33+
// Define the output size or factor.
34+
int outputRowsBuddyResize2DLength, outputColsBuddyResize2DLength;
35+
float outputRowsBuddyResize2DFactor, outputColsBuddyResize2DFactor;
36+
37+
// Define sizes of input and output.
38+
intptr_t sizesInputBuddyResize2D[2];
39+
intptr_t sizesOutputBuddyResize2D[2];
40+
std::vector<float> factorsOutputBuddyResize2D = {1.0, 1.0};
41+
42+
// Declare Interpolation option supported.
43+
enum InterpolationOption { bilinear_interpolation, nearest_neighbour_interpolation };
44+
45+
// Declare Scale option supported.
46+
enum ScaleOption { scale_factor, scale_length };
47+
48+
// Define Interpolation option selected.
49+
InterpolationOption InterpolationType;
50+
51+
// Define Scale option selected.
52+
ScaleOption ScaleType;
53+
54+
void initializeBuddyResize2D(char **argv) {
55+
inputImageBuddyResize2D = imread(argv[1], IMREAD_GRAYSCALE);
56+
57+
sizesInputBuddyResize2D[0] = inputImageBuddyResize2D.rows;
58+
sizesInputBuddyResize2D[1] = inputImageBuddyResize2D.cols;
59+
60+
if (static_cast<string>(argv[2]) == "SCALE_FACTOR") {
61+
ScaleType = scale_factor;
62+
} else {
63+
ScaleType = scale_length;
64+
}
65+
66+
std::string argRow = argv[3];
67+
std::string argCol = argv[4];
68+
try {
69+
if (ScaleType == scale_factor) {
70+
float outputRowsBuddyResize2DFactor = std::stof(argRow);
71+
float outputColsBuddyResize2DFactor = std::stof(argCol);
72+
factorsOutputBuddyResize2D[0] = outputRowsBuddyResize2DFactor;
73+
factorsOutputBuddyResize2D[1] = outputColsBuddyResize2DFactor;
74+
sizesOutputBuddyResize2D[0] = sizesInputBuddyResize2D[0] * outputRowsBuddyResize2DFactor;
75+
sizesOutputBuddyResize2D[1] = sizesInputBuddyResize2D[1] * outputColsBuddyResize2DFactor;
76+
} else {
77+
intptr_t outputRowsBuddyResize2DLength = std::stoi(argRow);
78+
intptr_t outputColsBuddyResize2DLength = std::stoi(argCol);
79+
sizesOutputBuddyResize2D[0] = outputRowsBuddyResize2DLength;
80+
sizesOutputBuddyResize2D[1] = outputColsBuddyResize2DLength;
81+
}
82+
} catch (const std::exception& e) {
83+
cout << "Exception converting row and col scale_factor/scale_length to number." << endl;
84+
}
85+
86+
if (static_cast<string>(argv[5]) == "NEAREST_NEIGHBOUR_INTERPOLATION") {
87+
InterpolationType = nearest_neighbour_interpolation;
88+
} else {
89+
InterpolationType = bilinear_interpolation;
90+
}
91+
}
92+
93+
static void Buddy_Resize2D_Bilinear_Interpolation_Length(benchmark::State &state) {
94+
// Define the MemRef descriptor for input.
95+
Img<float, 2> inputBuddyResize2D(inputImageBuddyResize2D);
96+
97+
for (auto _ : state) {
98+
for (int i = 0; i < state.range(0); ++i) {
99+
// Call the MLIR Resize2D function.
100+
MemRef<float, 2> output = dip::Resize2D(&inputBuddyResize2D,
101+
dip::INTERPOLATION_TYPE::BILINEAR_INTERPOLATION,
102+
sizesOutputBuddyResize2D);
103+
}
104+
}
105+
}
106+
107+
static void Buddy_Resize2D_Nearest_Neighbour_Interpolation_Length(benchmark::State &state) {
108+
// Define the MemRef descriptor for input.
109+
Img<float, 2> inputBuddyResize2D(inputImageBuddyResize2D);
110+
111+
for (auto _ : state) {
112+
for (int i = 0; i < state.range(0); ++i) {
113+
// Call the MLIR Resize2D function.
114+
MemRef<float, 2> output = dip::Resize2D(&inputBuddyResize2D,
115+
dip::INTERPOLATION_TYPE::NEAREST_NEIGHBOUR_INTERPOLATION,
116+
sizesOutputBuddyResize2D);
117+
}
118+
}
119+
}
120+
121+
static void Buddy_Resize2D_Bilinear_Interpolation_Factor(benchmark::State &state) {
122+
// Define the MemRef descriptor for input.
123+
Img<float, 2> inputBuddyResize2D(inputImageBuddyResize2D);
124+
125+
for (auto _ : state) {
126+
for (int i = 0; i < state.range(0); ++i) {
127+
// Call the MLIR Resize2D function.
128+
MemRef<float, 2> output = dip::Resize2D(&inputBuddyResize2D,
129+
dip::INTERPOLATION_TYPE::BILINEAR_INTERPOLATION,
130+
factorsOutputBuddyResize2D);
131+
}
132+
}
133+
}
134+
135+
static void Buddy_Resize2D_Nearest_Neighbour_Interpolation_Factor(benchmark::State &state) {
136+
// Define the MemRef descriptor for input.
137+
Img<float, 2> inputBuddyResize2D(inputImageBuddyResize2D);
138+
139+
for (auto _ : state) {
140+
for (int i = 0; i < state.range(0); ++i) {
141+
// Call the MLIR Resize2D function.
142+
MemRef<float, 2> output = dip::Resize2D(&inputBuddyResize2D,
143+
dip::INTERPOLATION_TYPE::NEAREST_NEIGHBOUR_INTERPOLATION,
144+
factorsOutputBuddyResize2D);
145+
}
146+
}
147+
}
148+
149+
// Register benchmarking function.
150+
void registerBenchmarkBuddyResize2D() {
151+
if (InterpolationType == nearest_neighbour_interpolation && ScaleType == scale_factor) {
152+
BENCHMARK(Buddy_Resize2D_Nearest_Neighbour_Interpolation_Factor)
153+
->Arg(1)
154+
->Unit(benchmark::kMillisecond);
155+
} else if (InterpolationType == bilinear_interpolation && ScaleType == scale_factor) {
156+
BENCHMARK(Buddy_Resize2D_Bilinear_Interpolation_Factor)
157+
->Arg(1)
158+
->Unit(benchmark::kMillisecond);
159+
} else if (InterpolationType == nearest_neighbour_interpolation && ScaleType == scale_length) {
160+
BENCHMARK(Buddy_Resize2D_Nearest_Neighbour_Interpolation_Length)
161+
->Arg(1)
162+
->Unit(benchmark::kMillisecond);
163+
} else if (InterpolationType == bilinear_interpolation && ScaleType == scale_length) {
164+
BENCHMARK(Buddy_Resize2D_Bilinear_Interpolation_Length)
165+
->Arg(1)
166+
->Unit(benchmark::kMillisecond);
167+
}
168+
}
169+
170+
// Generate result image.
171+
void generateResultBuddyResize2D() {
172+
// Define the MemRef descriptor for input.
173+
Img<float, 2> input(inputImageBuddyResize2D);
174+
MemRef<float, 2> output(sizesOutputBuddyResize2D);
175+
// Run the resize 2D operation.
176+
if (InterpolationType == nearest_neighbour_interpolation && ScaleType == scale_factor) {
177+
// Call the MLIR Resize2D function.
178+
output = dip::Resize2D(&input,
179+
dip::INTERPOLATION_TYPE::NEAREST_NEIGHBOUR_INTERPOLATION,
180+
factorsOutputBuddyResize2D);
181+
} else if (InterpolationType == bilinear_interpolation && ScaleType == scale_factor) {
182+
// Call the MLIR Resize2D function.
183+
output = dip::Resize2D(&input,
184+
dip::INTERPOLATION_TYPE::BILINEAR_INTERPOLATION,
185+
factorsOutputBuddyResize2D);
186+
} else if (InterpolationType == nearest_neighbour_interpolation && ScaleType == scale_length) {
187+
// Call the MLIR Resize2D function.
188+
output = dip::Resize2D(&input,
189+
dip::INTERPOLATION_TYPE::NEAREST_NEIGHBOUR_INTERPOLATION,
190+
sizesOutputBuddyResize2D);
191+
} else if (InterpolationType == bilinear_interpolation && ScaleType == scale_length) {
192+
// Call the MLIR Resize2D function.
193+
output = dip::Resize2D(&input,
194+
dip::INTERPOLATION_TYPE::BILINEAR_INTERPOLATION,
195+
sizesOutputBuddyResize2D);
196+
}
197+
198+
// Define a cv::Mat with the output of the resize operation.
199+
Mat outputImage(output.getSizes()[0], output.getSizes()[1], CV_32FC1,
200+
output.getData());
201+
202+
// Choose a PNG compression level
203+
vector<int> compressionParams;
204+
compressionParams.push_back(IMWRITE_PNG_COMPRESSION);
205+
compressionParams.push_back(9);
206+
207+
// Write output to PNG.
208+
bool result = true;
209+
try {
210+
result = imwrite("ResultBuddyResize2D.png", outputImage, compressionParams);
211+
} catch (const cv::Exception &ex) {
212+
fprintf(stderr, "Exception converting image to PNG format: %s\n",
213+
ex.what());
214+
}
215+
if (result)
216+
cout << "Saved PNG file." << endl;
217+
else
218+
cout << "ERROR: Can't save PNG file." << endl;
219+
}

benchmarks/ImageProcessing/CMakeLists.txt

+28
Original file line numberDiff line numberDiff line change
@@ -117,3 +117,31 @@ target_link_libraries(image-processing-benchmark
117117
# Link Buddy MLIR DIP Library.
118118
BuddyLibDIP
119119
)
120+
121+
#-------------------------------------------------------------------------------
122+
# Image Processing Resize Benchmark Target
123+
#-------------------------------------------------------------------------------
124+
125+
add_executable(image-processing-resize-benchmark
126+
MainResize.cpp
127+
BuddyResize2DBenchmark.cpp
128+
OpenCVResize2DBenchmark.cpp
129+
)
130+
131+
target_include_directories(image-processing-resize-benchmark
132+
PRIVATE
133+
${BUDDY_SOURCE_DIR}/benchmarks/ImageProcessing/include/
134+
)
135+
136+
target_link_directories(image-processing-resize-benchmark
137+
PRIVATE
138+
${BUDDY_MLIR_LIB_DIR}
139+
)
140+
141+
target_link_libraries(image-processing-resize-benchmark
142+
GoogleBenchmark
143+
${OpenCV_LIBS}
144+
# Link Buddy MLIR DIP Library.
145+
BuddyLibDIP
146+
)
147+
+62
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,62 @@
1+
//===- MainResize.cpp -----------------------------------------------------------===//
2+
//
3+
// Licensed under the Apache License, Version 2.0 (the "License");
4+
// you may not use this file except in compliance with the License.
5+
// You may obtain a copy of the License at
6+
//
7+
// http://www.apache.org/licenses/LICENSE-2.0
8+
//
9+
// Unless required by applicable law or agreed to in writing, software
10+
// distributed under the License is distributed on an "AS IS" BASIS,
11+
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12+
// See the License for the specific language governing permissions and
13+
// limitations under the License.
14+
//
15+
//===----------------------------------------------------------------------===//
16+
//
17+
// This is the main file of the image processing resize benchmark.
18+
//
19+
//===----------------------------------------------------------------------===//
20+
21+
#include <benchmark/benchmark.h>
22+
#include <stdexcept>
23+
24+
void initializeBuddyResize2D(char **);
25+
void initializeOpenCVResize2D(char **);
26+
27+
void generateResultBuddyResize2D();
28+
void generateResultOpenCVResize2D();
29+
30+
void registerBenchmarkBuddyResize2D();
31+
void registerBenchmarkOpenCVResize2D();
32+
33+
// Run benchmarks.
34+
int main(int argc, char **argv) {
35+
if (argc != 6) {
36+
throw std::invalid_argument(
37+
"Wrong format of command line arguments.\n"
38+
"Correct format is ./image-processing-resize-benchmark <image path> <Scale "
39+
"option> <RowNum> <ColNum> <InterpolationOption>\n where "
40+
"image path provides path of the image to be processed, Scale option "
41+
"available are SCALE_FACTOR, SCALE_LENGTH. "
42+
"RowNum and ColNum are the "
43+
"scale_factors/scale_length for row and col, "
44+
"Interpolation option available "
45+
"are NEAREST_NEIGHBOUR_INTERPOLATION, BILINEAR_INTERPOLATION.\n");
46+
}
47+
48+
initializeBuddyResize2D(argv);
49+
initializeOpenCVResize2D(argv);
50+
51+
registerBenchmarkBuddyResize2D();
52+
registerBenchmarkOpenCVResize2D();
53+
54+
::benchmark::Initialize(&argc, argv);
55+
::benchmark::RunSpecifiedBenchmarks();
56+
57+
// Generate result image.
58+
generateResultBuddyResize2D();
59+
generateResultOpenCVResize2D();
60+
61+
return 0;
62+
}

0 commit comments

Comments
 (0)