blob: 0b33c76d51c32635280b02a216507ee343dfda73 [file] [log] [blame]
Anthony Barbier6ff3b192017-09-04 18:44:23 +01001/*
Michele Di Giorgiod9eaf612020-07-08 11:12:57 +01002 * Copyright (c) 2016-2019 Arm Limited.
Anthony Barbier6ff3b192017-09-04 18:44:23 +01003 *
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"
Georgios Pinitas7c3b9242018-06-21 19:01:25 +010027#include "utils/ImageLoader.h"
Anthony Barbier6ff3b192017-09-04 18:44:23 +010028#include "utils/Utils.h"
29
30using namespace arm_compute;
31using namespace utils;
32
33/** Gaussian 3x3 matrix
34 */
Michalis Spyroubcfd09a2019-05-01 13:03:59 +010035const std::array<int16_t, 9> gaussian3x3 =
Anthony Barbier6ff3b192017-09-04 18:44:23 +010036{
37 1, 2, 1,
38 2, 4, 2,
39 1, 2, 1
40};
41
42/** Gaussian 5x5 matrix
43 */
Michalis Spyroubcfd09a2019-05-01 13:03:59 +010044const std::array<int16_t, 25> gaussian5x5 =
Anthony Barbier6ff3b192017-09-04 18:44:23 +010045{
46 1, 4, 6, 4, 1,
47 4, 16, 24, 16, 4,
48 6, 24, 36, 24, 6,
49 4, 16, 24, 16, 4,
50 1, 4, 6, 4, 1
51};
52
Michalis Spyrou2b5f0f22018-01-10 14:08:50 +000053class NEONConvolutionExample : public Example
Anthony Barbier6ff3b192017-09-04 18:44:23 +010054{
Michalis Spyrou2b5f0f22018-01-10 14:08:50 +000055public:
Georgios Pinitas12be7ab2018-07-03 12:06:23 +010056 bool do_setup(int argc, char **argv) override
Michalis Spyrou2b5f0f22018-01-10 14:08:50 +000057 {
58 /** [Accurate padding] **/
59 PPMLoader ppm;
Anthony Barbier6ff3b192017-09-04 18:44:23 +010060
Michalis Spyrou2b5f0f22018-01-10 14:08:50 +000061 if(argc < 2)
62 {
63 // Print help
64 std::cout << "Usage: ./build/neon_convolution [input_image.ppm]\n\n";
65 std::cout << "No input_image provided, creating a dummy 640x480 image\n";
66 // Initialize just the dimensions and format of your buffers:
67 src.allocator()->init(TensorInfo(640, 480, Format::U8));
68 }
69 else
70 {
71 ppm.open(argv[1]);
72 // Initialize just the dimensions and format of your buffers:
73 ppm.init_image(src, Format::U8);
74 }
75
76 // Initialize just the dimensions and format of the temporary and destination images:
77 tmp.allocator()->init(*src.info());
78 dst.allocator()->init(*src.info());
79
80 // Apply a Gaussian 3x3 filter to the source image followed by a Gaussian 5x5:
81 // The function will automatically update the padding information inside input and output to match its requirements
Michalis Spyroubcfd09a2019-05-01 13:03:59 +010082 conv3x3.configure(&src, &tmp, gaussian3x3.data(), 0 /* Let arm_compute calculate the scale */, BorderMode::UNDEFINED);
83 conv5x5.configure(&tmp, &dst, gaussian5x5.data(), 0 /* Let arm_compute calculate the scale */, BorderMode::UNDEFINED);
Michalis Spyrou2b5f0f22018-01-10 14:08:50 +000084
85 // Now that the padding requirements are known we can allocate the images:
86 src.allocator()->allocate();
87 tmp.allocator()->allocate();
88 dst.allocator()->allocate();
89
90 // Fill the input image with the content of the PPM image if a filename was provided:
91 if(ppm.is_open())
92 {
93 ppm.fill_image(src);
94 output_filename = std::string(argv[1]) + "_out.ppm";
95 }
96 /** [Accurate padding] **/
Georgios Pinitas12be7ab2018-07-03 12:06:23 +010097
98 return true;
Anthony Barbier6ff3b192017-09-04 18:44:23 +010099 }
Michalis Spyrou2b5f0f22018-01-10 14:08:50 +0000100 void do_run() override
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100101 {
Michalis Spyrou2b5f0f22018-01-10 14:08:50 +0000102 //Execute the functions:
103 conv3x3.run();
104 conv5x5.run();
105 }
106 void do_teardown() override
107 {
108 // Save the result to file:
109 if(!output_filename.empty())
110 {
111 save_to_ppm(dst, output_filename); // save_to_ppm maps and unmaps the image to store as PPM
112 }
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100113 }
114
Michalis Spyrou2b5f0f22018-01-10 14:08:50 +0000115private:
116 Image src{}, tmp{}, dst{};
117 NEConvolution3x3 conv3x3{};
118 NEConvolution5x5 conv5x5{};
119 std::string output_filename{};
120};
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100121
122/** Main program for convolution test
123 *
124 * @param[in] argc Number of arguments
125 * @param[in] argv Arguments ( [optional] Path to PPM image to process )
126 */
Anthony Barbier6db0ff52018-01-05 10:59:12 +0000127int main(int argc, char **argv)
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100128{
Michalis Spyrou2b5f0f22018-01-10 14:08:50 +0000129 return utils::run_example<NEONConvolutionExample>(argc, argv);
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100130}