Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 1 | /* |
Anthony Barbier | 6db0ff5 | 2018-01-05 10:59:12 +0000 | [diff] [blame] | 2 | * Copyright (c) 2016, 2018 ARM Limited. |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 3 | * |
| 4 | * SPDX-License-Identifier: MIT |
| 5 | * |
| 6 | * Permission is hereby granted, free of charge, to any person obtaining a copy |
| 7 | * of this software and associated documentation files (the "Software"), to |
| 8 | * deal in the Software without restriction, including without limitation the |
| 9 | * rights to use, copy, modify, merge, publish, distribute, sublicense, and/or |
| 10 | * sell copies of the Software, and to permit persons to whom the Software is |
| 11 | * furnished to do so, subject to the following conditions: |
| 12 | * |
| 13 | * The above copyright notice and this permission notice shall be included in all |
| 14 | * copies or substantial portions of the Software. |
| 15 | * |
| 16 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR |
| 17 | * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, |
| 18 | * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE |
| 19 | * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER |
| 20 | * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, |
| 21 | * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE |
| 22 | * SOFTWARE. |
| 23 | */ |
| 24 | #include "arm_compute/runtime/NEON/NEFunctions.h" |
| 25 | |
| 26 | #include "arm_compute/core/Types.h" |
| 27 | #include "utils/Utils.h" |
| 28 | |
| 29 | using namespace arm_compute; |
| 30 | using namespace utils; |
| 31 | |
| 32 | /** Gaussian 3x3 matrix |
| 33 | */ |
| 34 | const int16_t gaussian3x3[] = |
| 35 | { |
| 36 | 1, 2, 1, |
| 37 | 2, 4, 2, |
| 38 | 1, 2, 1 |
| 39 | }; |
| 40 | |
| 41 | /** Gaussian 5x5 matrix |
| 42 | */ |
| 43 | const int16_t gaussian5x5[] = |
| 44 | { |
| 45 | 1, 4, 6, 4, 1, |
| 46 | 4, 16, 24, 16, 4, |
| 47 | 6, 24, 36, 24, 6, |
| 48 | 4, 16, 24, 16, 4, |
| 49 | 1, 4, 6, 4, 1 |
| 50 | }; |
| 51 | |
Michalis Spyrou | 2b5f0f2 | 2018-01-10 14:08:50 +0000 | [diff] [blame] | 52 | class NEONConvolutionExample : public Example |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 53 | { |
Michalis Spyrou | 2b5f0f2 | 2018-01-10 14:08:50 +0000 | [diff] [blame] | 54 | public: |
| 55 | void do_setup(int argc, char **argv) override |
| 56 | { |
| 57 | /** [Accurate padding] **/ |
| 58 | PPMLoader ppm; |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 59 | |
Michalis Spyrou | 2b5f0f2 | 2018-01-10 14:08:50 +0000 | [diff] [blame] | 60 | if(argc < 2) |
| 61 | { |
| 62 | // Print help |
| 63 | std::cout << "Usage: ./build/neon_convolution [input_image.ppm]\n\n"; |
| 64 | std::cout << "No input_image provided, creating a dummy 640x480 image\n"; |
| 65 | // Initialize just the dimensions and format of your buffers: |
| 66 | src.allocator()->init(TensorInfo(640, 480, Format::U8)); |
| 67 | } |
| 68 | else |
| 69 | { |
| 70 | ppm.open(argv[1]); |
| 71 | // Initialize just the dimensions and format of your buffers: |
| 72 | ppm.init_image(src, Format::U8); |
| 73 | } |
| 74 | |
| 75 | // Initialize just the dimensions and format of the temporary and destination images: |
| 76 | tmp.allocator()->init(*src.info()); |
| 77 | dst.allocator()->init(*src.info()); |
| 78 | |
| 79 | // Apply a Gaussian 3x3 filter to the source image followed by a Gaussian 5x5: |
| 80 | // The function will automatically update the padding information inside input and output to match its requirements |
| 81 | conv3x3.configure(&src, &tmp, gaussian3x3, 0 /* Let arm_compute calculate the scale */, BorderMode::UNDEFINED); |
| 82 | conv5x5.configure(&tmp, &dst, gaussian5x5, 0 /* Let arm_compute calculate the scale */, BorderMode::UNDEFINED); |
| 83 | |
| 84 | // Now that the padding requirements are known we can allocate the images: |
| 85 | src.allocator()->allocate(); |
| 86 | tmp.allocator()->allocate(); |
| 87 | dst.allocator()->allocate(); |
| 88 | |
| 89 | // Fill the input image with the content of the PPM image if a filename was provided: |
| 90 | if(ppm.is_open()) |
| 91 | { |
| 92 | ppm.fill_image(src); |
| 93 | output_filename = std::string(argv[1]) + "_out.ppm"; |
| 94 | } |
| 95 | /** [Accurate padding] **/ |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 96 | } |
Michalis Spyrou | 2b5f0f2 | 2018-01-10 14:08:50 +0000 | [diff] [blame] | 97 | void do_run() override |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 98 | { |
Michalis Spyrou | 2b5f0f2 | 2018-01-10 14:08:50 +0000 | [diff] [blame] | 99 | //Execute the functions: |
| 100 | conv3x3.run(); |
| 101 | conv5x5.run(); |
| 102 | } |
| 103 | void do_teardown() override |
| 104 | { |
| 105 | // Save the result to file: |
| 106 | if(!output_filename.empty()) |
| 107 | { |
| 108 | save_to_ppm(dst, output_filename); // save_to_ppm maps and unmaps the image to store as PPM |
| 109 | } |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 110 | } |
| 111 | |
Michalis Spyrou | 2b5f0f2 | 2018-01-10 14:08:50 +0000 | [diff] [blame] | 112 | private: |
| 113 | Image src{}, tmp{}, dst{}; |
| 114 | NEConvolution3x3 conv3x3{}; |
| 115 | NEConvolution5x5 conv5x5{}; |
| 116 | std::string output_filename{}; |
| 117 | }; |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 118 | |
| 119 | /** Main program for convolution test |
| 120 | * |
| 121 | * @param[in] argc Number of arguments |
| 122 | * @param[in] argv Arguments ( [optional] Path to PPM image to process ) |
| 123 | */ |
Anthony Barbier | 6db0ff5 | 2018-01-05 10:59:12 +0000 | [diff] [blame] | 124 | int main(int argc, char **argv) |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 125 | { |
Michalis Spyrou | 2b5f0f2 | 2018-01-10 14:08:50 +0000 | [diff] [blame] | 126 | return utils::run_example<NEONConvolutionExample>(argc, argv); |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 127 | } |