|
| 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 | +} |
0 commit comments