blob: f2d19ef0cc150d63d991ee6cbca6c2614e64f717 [file] [log] [blame]
Anthony Barbier6ff3b192017-09-04 18:44:23 +01001/*
Michalis Spyroua4f378d2019-04-26 14:54:54 +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 */
Anthony Barbier15d5ac82017-07-17 15:22:17 +010024#ifndef ARM_COMPUTE_CL /* Needed by Utils.cpp to handle OpenCL exceptions properly */
25#error "This example needs to be built with -DARM_COMPUTE_CL"
26#endif /* ARM_COMPUTE_CL */
27
Anthony Barbier6ff3b192017-09-04 18:44:23 +010028#include "arm_compute/core/Types.h"
29#include "arm_compute/runtime/CL/CLFunctions.h"
30#include "arm_compute/runtime/CL/CLScheduler.h"
Georgios Pinitas7c3b9242018-06-21 19:01:25 +010031#include "utils/ImageLoader.h"
Anthony Barbier6ff3b192017-09-04 18:44:23 +010032#include "utils/Utils.h"
33
34using namespace arm_compute;
35using namespace utils;
36
37/** Gaussian 3x3 matrix
38 */
Michalis Spyroua4f378d2019-04-26 14:54:54 +010039const std::array<int16_t, 9> gaussian3x3 =
Anthony Barbier6ff3b192017-09-04 18:44:23 +010040{
41 1, 2, 1,
42 2, 4, 2,
43 1, 2, 1
44};
45
46/** Gaussian 5x5 matrix
47 */
Michalis Spyroua4f378d2019-04-26 14:54:54 +010048const std::array<int16_t, 25> gaussian5x5 =
Anthony Barbier6ff3b192017-09-04 18:44:23 +010049{
50 1, 4, 6, 4, 1,
51 4, 16, 24, 16, 4,
52 6, 24, 36, 24, 6,
53 4, 16, 24, 16, 4,
54 1, 4, 6, 4, 1
55};
56
Anthony Barbier6db0ff52018-01-05 10:59:12 +000057class CLConvolutionExample : public Example
Anthony Barbier6ff3b192017-09-04 18:44:23 +010058{
Anthony Barbier6db0ff52018-01-05 10:59:12 +000059public:
Georgios Pinitas12be7ab2018-07-03 12:06:23 +010060 bool do_setup(int argc, char **argv) override
Anthony Barbier6ff3b192017-09-04 18:44:23 +010061 {
Anthony Barbier6db0ff52018-01-05 10:59:12 +000062 PPMLoader ppm;
63
64 CLScheduler::get().default_init();
65
66 if(argc < 2)
67 {
68 // Print help
69 std::cout << "Usage: ./build/cl_convolution [input_image.ppm]\n\n";
70 std::cout << "No input_image provided, creating a dummy 640x480 image\n";
71 // Create an empty grayscale 640x480 image
72 src.allocator()->init(TensorInfo(640, 480, Format::U8));
73 }
74 else
75 {
76 ppm.open(argv[1]);
77 ppm.init_image(src, Format::U8);
78 }
79
80 // Configure the temporary and destination images
81 tmp.allocator()->init(*src.info());
82 dst.allocator()->init(*src.info());
83
84 // Apply a Gaussian 3x3 filter to the source image followed by a Gaussian 5x5:
Michalis Spyroua4f378d2019-04-26 14:54:54 +010085 conv3x3.configure(&src, &tmp, gaussian3x3.data(), 0 /* Let arm_compute calculate the scale */, BorderMode::UNDEFINED);
86 conv5x5.configure(&tmp, &dst, gaussian5x5.data(), 0 /* Let arm_compute calculate the scale */, BorderMode::UNDEFINED);
Anthony Barbier6db0ff52018-01-05 10:59:12 +000087
88 // Allocate all the images
89 src.allocator()->allocate();
90 tmp.allocator()->allocate();
91 dst.allocator()->allocate();
92 // Fill the input image with the content of the PPM image if a filename was provided:
93 if(ppm.is_open())
94 {
95 ppm.fill_image(src);
96 output_filename = std::string(argv[1]) + "_out.ppm";
97 }
Georgios Pinitas12be7ab2018-07-03 12:06:23 +010098
99 return true;
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100100 }
Anthony Barbier6db0ff52018-01-05 10:59:12 +0000101 void do_run() override
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100102 {
Anthony Barbier6db0ff52018-01-05 10:59:12 +0000103 // Execute the functions:
104 conv3x3.run();
105 conv5x5.run();
106
107 // Make sure all the OpenCL jobs are done executing:
108 CLScheduler::get().sync();
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100109 }
Anthony Barbier6db0ff52018-01-05 10:59:12 +0000110 void do_teardown() override
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100111 {
Anthony Barbier6db0ff52018-01-05 10:59:12 +0000112 // Save the result to file:
113 if(!output_filename.empty())
114 {
115 save_to_ppm(dst, output_filename); // save_to_ppm maps and unmaps the image to store as PPM
116 }
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100117 }
Michalis Spyroua4f378d2019-04-26 14:54:54 +0100118
119private:
120 CLImage src{};
121 CLImage tmp{};
122 CLImage dst{};
Anthony Barbier6db0ff52018-01-05 10:59:12 +0000123 CLConvolution3x3 conv3x3{};
124 CLConvolution5x5 conv5x5{};
125 std::string output_filename{};
126};
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100127
128/** Main program for convolution test
129 *
130 * @param[in] argc Number of arguments
131 * @param[in] argv Arguments ( [optional] Path to PPM image to process )
132 */
Anthony Barbier6db0ff52018-01-05 10:59:12 +0000133int main(int argc, char **argv)
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100134{
Anthony Barbier6db0ff52018-01-05 10:59:12 +0000135 return utils::run_example<CLConvolutionExample>(argc, argv);
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100136}