blob: bfa53f33797c122d1b4bdf14e39b86d6d9cb02b3 [file] [log] [blame]
Anthony Barbier6ff3b192017-09-04 18:44:23 +01001/*
Sang-Hoon Parkbef7fa22020-10-21 15:58:54 +01002 * Copyright (c) 2016-2020 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
Sang-Hoon Parkbef7fa22020-10-21 15:58:54 +010028#include "arm_compute/core/CL/OpenCL.h"
Anthony Barbier6ff3b192017-09-04 18:44:23 +010029#include "arm_compute/core/Types.h"
Anthony Barbier6ff3b192017-09-04 18:44:23 +010030#include "arm_compute/runtime/CL/CLScheduler.h"
Sang-Hoon Parkbef7fa22020-10-21 15:58:54 +010031#include "arm_compute/runtime/CL/functions/CLConvolution.h"
Georgios Pinitas7c3b9242018-06-21 19:01:25 +010032#include "utils/ImageLoader.h"
Anthony Barbier6ff3b192017-09-04 18:44:23 +010033#include "utils/Utils.h"
34
35using namespace arm_compute;
36using namespace utils;
37
38/** Gaussian 3x3 matrix
39 */
Michalis Spyroua4f378d2019-04-26 14:54:54 +010040const std::array<int16_t, 9> gaussian3x3 =
Anthony Barbier6ff3b192017-09-04 18:44:23 +010041{
42 1, 2, 1,
43 2, 4, 2,
44 1, 2, 1
45};
46
47/** Gaussian 5x5 matrix
48 */
Michalis Spyroua4f378d2019-04-26 14:54:54 +010049const std::array<int16_t, 25> gaussian5x5 =
Anthony Barbier6ff3b192017-09-04 18:44:23 +010050{
51 1, 4, 6, 4, 1,
52 4, 16, 24, 16, 4,
53 6, 24, 36, 24, 6,
54 4, 16, 24, 16, 4,
55 1, 4, 6, 4, 1
56};
57
Anthony Barbier6db0ff52018-01-05 10:59:12 +000058class CLConvolutionExample : public Example
Anthony Barbier6ff3b192017-09-04 18:44:23 +010059{
Anthony Barbier6db0ff52018-01-05 10:59:12 +000060public:
Georgios Pinitas12be7ab2018-07-03 12:06:23 +010061 bool do_setup(int argc, char **argv) override
Anthony Barbier6ff3b192017-09-04 18:44:23 +010062 {
Anthony Barbier6db0ff52018-01-05 10:59:12 +000063 PPMLoader ppm;
64
65 CLScheduler::get().default_init();
66
67 if(argc < 2)
68 {
69 // Print help
70 std::cout << "Usage: ./build/cl_convolution [input_image.ppm]\n\n";
71 std::cout << "No input_image provided, creating a dummy 640x480 image\n";
72 // Create an empty grayscale 640x480 image
73 src.allocator()->init(TensorInfo(640, 480, Format::U8));
74 }
75 else
76 {
77 ppm.open(argv[1]);
78 ppm.init_image(src, Format::U8);
79 }
80
81 // Configure the temporary and destination images
82 tmp.allocator()->init(*src.info());
83 dst.allocator()->init(*src.info());
84
85 // Apply a Gaussian 3x3 filter to the source image followed by a Gaussian 5x5:
Michalis Spyroua4f378d2019-04-26 14:54:54 +010086 conv3x3.configure(&src, &tmp, gaussian3x3.data(), 0 /* Let arm_compute calculate the scale */, BorderMode::UNDEFINED);
87 conv5x5.configure(&tmp, &dst, gaussian5x5.data(), 0 /* Let arm_compute calculate the scale */, BorderMode::UNDEFINED);
Anthony Barbier6db0ff52018-01-05 10:59:12 +000088
89 // Allocate all the images
90 src.allocator()->allocate();
91 tmp.allocator()->allocate();
92 dst.allocator()->allocate();
93 // Fill the input image with the content of the PPM image if a filename was provided:
94 if(ppm.is_open())
95 {
96 ppm.fill_image(src);
97 output_filename = std::string(argv[1]) + "_out.ppm";
98 }
Georgios Pinitas12be7ab2018-07-03 12:06:23 +010099
100 return true;
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100101 }
Anthony Barbier6db0ff52018-01-05 10:59:12 +0000102 void do_run() override
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100103 {
Anthony Barbier6db0ff52018-01-05 10:59:12 +0000104 // Execute the functions:
105 conv3x3.run();
106 conv5x5.run();
107
108 // Make sure all the OpenCL jobs are done executing:
109 CLScheduler::get().sync();
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100110 }
Anthony Barbier6db0ff52018-01-05 10:59:12 +0000111 void do_teardown() override
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100112 {
Anthony Barbier6db0ff52018-01-05 10:59:12 +0000113 // Save the result to file:
114 if(!output_filename.empty())
115 {
116 save_to_ppm(dst, output_filename); // save_to_ppm maps and unmaps the image to store as PPM
117 }
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100118 }
Michalis Spyroua4f378d2019-04-26 14:54:54 +0100119
120private:
121 CLImage src{};
122 CLImage tmp{};
123 CLImage dst{};
Anthony Barbier6db0ff52018-01-05 10:59:12 +0000124 CLConvolution3x3 conv3x3{};
125 CLConvolution5x5 conv5x5{};
126 std::string output_filename{};
127};
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100128
129/** Main program for convolution test
130 *
131 * @param[in] argc Number of arguments
132 * @param[in] argv Arguments ( [optional] Path to PPM image to process )
133 */
Anthony Barbier6db0ff52018-01-05 10:59:12 +0000134int main(int argc, char **argv)
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100135{
Anthony Barbier6db0ff52018-01-05 10:59:12 +0000136 return utils::run_example<CLConvolutionExample>(argc, argv);
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100137}