• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (c) 2016-2019 Arm Limited.
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/ImageLoader.h"
28 #include "utils/Utils.h"
29 
30 using namespace arm_compute;
31 using namespace utils;
32 
33 /** Gaussian 3x3 matrix
34  */
35 const std::array<int16_t, 9> gaussian3x3 =
36 {
37     1, 2, 1,
38     2, 4, 2,
39     1, 2, 1
40 };
41 
42 /** Gaussian 5x5 matrix
43  */
44 const std::array<int16_t, 25> gaussian5x5 =
45 {
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 
53 class NEONConvolutionExample : public Example
54 {
55 public:
do_setup(int argc,char ** argv)56     bool do_setup(int argc, char **argv) override
57     {
58         /** [Accurate padding] **/
59         PPMLoader ppm;
60 
61         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
82         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);
84 
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] **/
97 
98         return true;
99     }
do_run()100     void do_run() override
101     {
102         //Execute the functions:
103         conv3x3.run();
104         conv5x5.run();
105     }
do_teardown()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         }
113     }
114 
115 private:
116     Image            src{}, tmp{}, dst{};
117     NEConvolution3x3 conv3x3{};
118     NEConvolution5x5 conv5x5{};
119     std::string      output_filename{};
120 };
121 
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  */
main(int argc,char ** argv)127 int main(int argc, char **argv)
128 {
129     return utils::run_example<NEONConvolutionExample>(argc, argv);
130 }
131