blob: 7c30a942db062fce78aae756f29a7fe649b6b456 [file] [log] [blame]
giuros01c04a0e82018-10-03 12:44:35 +01001/*
Georgios Pinitas8f5802f2019-02-22 11:08:32 +00002 * Copyright (c) 2018-2019 ARM Limited.
giuros01c04a0e82018-10-03 12:44:35 +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/core/CL/kernels/CLBoundingBoxTransformKernel.h"
25
26#include "arm_compute/core/AccessWindowStatic.h"
27#include "arm_compute/core/CL/CLHelpers.h"
28#include "arm_compute/core/CL/CLKernelLibrary.h"
29#include "arm_compute/core/CL/CLValidate.h"
30#include "arm_compute/core/CL/ICLArray.h"
31#include "arm_compute/core/CL/ICLTensor.h"
32#include "arm_compute/core/CL/OpenCL.h"
33#include "arm_compute/core/Helpers.h"
34#include "arm_compute/core/TensorInfo.h"
35#include "arm_compute/core/Utils.h"
36#include "arm_compute/core/Window.h"
37
38namespace arm_compute
39{
40namespace
41{
giuros01d696cb62018-11-16 10:39:59 +000042Status validate_arguments(const ITensorInfo *boxes, const ITensorInfo *pred_boxes, const ITensorInfo *deltas, const BoundingBoxTransformInfo &info)
giuros01c04a0e82018-10-03 12:44:35 +010043{
44 ARM_COMPUTE_RETURN_ERROR_ON_NULLPTR(boxes, pred_boxes, deltas);
Georgios Pinitas8f5802f2019-02-22 11:08:32 +000045 ARM_COMPUTE_RETURN_ERROR_ON_F16_UNSUPPORTED(boxes);
giuros01c04a0e82018-10-03 12:44:35 +010046 ARM_COMPUTE_RETURN_ERROR_ON_DATA_TYPE_NOT_IN(boxes, DataType::F32, DataType::F16);
47 ARM_COMPUTE_RETURN_ERROR_ON_DATA_TYPE_NOT_IN(deltas, DataType::F32, DataType::F16);
48 ARM_COMPUTE_RETURN_ERROR_ON(deltas->tensor_shape()[1] != boxes->tensor_shape()[1]);
49 ARM_COMPUTE_RETURN_ERROR_ON(deltas->tensor_shape()[0] % 4 != 0);
50 ARM_COMPUTE_RETURN_ERROR_ON(boxes->tensor_shape()[0] != 4);
51 ARM_COMPUTE_RETURN_ERROR_ON(deltas->num_dimensions() > 2);
52 ARM_COMPUTE_RETURN_ERROR_ON(boxes->num_dimensions() > 2);
53
54 if(pred_boxes->total_size() > 0)
55 {
56 ARM_COMPUTE_RETURN_ERROR_ON_MISMATCHING_DIMENSIONS(pred_boxes->tensor_shape(), deltas->tensor_shape());
57 ARM_COMPUTE_RETURN_ERROR_ON_MISMATCHING_DATA_TYPES(pred_boxes, deltas);
58 ARM_COMPUTE_RETURN_ERROR_ON(pred_boxes->num_dimensions() > 2);
59 }
giuros01d696cb62018-11-16 10:39:59 +000060 ARM_COMPUTE_RETURN_ERROR_ON(info.scale() <= 0);
giuros01c04a0e82018-10-03 12:44:35 +010061 return Status{};
62}
63} // namespace
64
65CLBoundingBoxTransformKernel::CLBoundingBoxTransformKernel()
66 : _boxes(nullptr), _pred_boxes(nullptr), _deltas(nullptr)
67{
68}
69
70void CLBoundingBoxTransformKernel::configure(const ICLTensor *boxes, ICLTensor *pred_boxes, const ICLTensor *deltas, const BoundingBoxTransformInfo &info)
71{
72 ARM_COMPUTE_ERROR_ON_NULLPTR(boxes, pred_boxes, deltas);
73 auto_init_if_empty(*pred_boxes->info(), *deltas->info());
74
giuros01d696cb62018-11-16 10:39:59 +000075 ARM_COMPUTE_ERROR_THROW_ON(validate_arguments(boxes->info(), pred_boxes->info(), deltas->info(), info));
76
giuros01c04a0e82018-10-03 12:44:35 +010077 // Set instance variables
78 _boxes = boxes;
79 _pred_boxes = pred_boxes;
80 _deltas = deltas;
81
82 // Get image height and widht (rescaled)
83 const int img_h = floor(info.img_height() / info.scale() + 0.5f);
84 const int img_w = floor(info.img_width() / info.scale() + 0.5f);
85
86 // Set build options
87 CLBuildOptions build_opts;
88 build_opts.add_option("-DDATA_TYPE=" + get_cl_type_from_data_type(deltas->info()->data_type()));
89 build_opts.add_option("-DWEIGHT_X=" + float_to_string_with_full_precision(info.weights()[0]));
90 build_opts.add_option("-DWEIGHT_Y=" + float_to_string_with_full_precision(info.weights()[1]));
91 build_opts.add_option("-DWEIGHT_W=" + float_to_string_with_full_precision(info.weights()[2]));
92 build_opts.add_option("-DWEIGHT_H=" + float_to_string_with_full_precision(info.weights()[3]));
93 build_opts.add_option("-DBBOX_XFORM_CLIP=" + float_to_string_with_full_precision(info.bbox_xform_clip()));
94 build_opts.add_option("-DIMG_WIDTH=" + support::cpp11::to_string(img_w));
95 build_opts.add_option("-DIMG_HEIGHT=" + support::cpp11::to_string(img_h));
96 build_opts.add_option("-DBOX_FIELDS=" + support::cpp11::to_string(4));
giuros01d696cb62018-11-16 10:39:59 +000097 build_opts.add_option("-DSCALE_BEFORE=" + float_to_string_with_full_precision(info.scale()));
98 build_opts.add_option_if(info.apply_scale(), "-DSCALE_AFTER=" + float_to_string_with_full_precision(info.scale()));
99 build_opts.add_option_if(info.correct_transform_coords(), "-DOFFSET=1");
giuros01c04a0e82018-10-03 12:44:35 +0100100
101 // Create kernel
102 _kernel = static_cast<cl::Kernel>(CLKernelLibrary::get().create_kernel("bounding_box_transform", build_opts.options()));
103
104 // Since the number of columns is a multiple of 4 by definition, we don't need to pad the tensor
105 const unsigned int num_elems_processed_per_iteration = 4;
106 Window win = calculate_max_window(*deltas->info(), Steps(num_elems_processed_per_iteration));
107 ICLKernel::configure_internal(win);
108}
109
110Status CLBoundingBoxTransformKernel::validate(const ITensorInfo *boxes, const ITensorInfo *pred_boxes, const ITensorInfo *deltas, const BoundingBoxTransformInfo &info)
111{
giuros01d696cb62018-11-16 10:39:59 +0000112 ARM_COMPUTE_RETURN_ON_ERROR(validate_arguments(boxes, pred_boxes, deltas, info));
giuros01c04a0e82018-10-03 12:44:35 +0100113 return Status{};
114}
115
116void CLBoundingBoxTransformKernel::run(const Window &window, cl::CommandQueue &queue)
117{
118 ARM_COMPUTE_ERROR_ON_UNCONFIGURED_KERNEL(this);
119 ARM_COMPUTE_ERROR_ON_INVALID_SUBWINDOW(IKernel::window(), window);
120
121 Window slice = window.first_slice_window_2D();
122
123 // Set arguments
124 unsigned int idx = 0;
125 add_1D_tensor_argument(idx, _boxes, slice);
126 add_2D_tensor_argument(idx, _pred_boxes, slice);
127 add_2D_tensor_argument(idx, _deltas, slice);
128
129 // Note that we don't need to loop over the slices, as we are sure that we are dealing with all 2D tensors
130 enqueue(queue, *this, slice);
131}
132} // namespace arm_compute