From c04a0e8f93c620d05444251e1ae55dcf8c660a1b Mon Sep 17 00:00:00 2001 From: giuros01 Date: Wed, 3 Oct 2018 12:44:35 +0100 Subject: COMPMID-1327: Add support for BBoxTransform operator in CL Change-Id: I91865506166951b3bf7f06a0b2d4cde925cfefb6 Reviewed-on: https://eu-gerrit-1.euhpc.arm.com/153447 Tested-by: bsgcomp Reviewed-by: Pablo Tello --- arm_compute/core/CL/CLKernels.h | 1 + .../core/CL/kernels/CLBoundingBoxTransformKernel.h | 84 ++++++++++ arm_compute/core/Types.h | 58 +++++++ arm_compute/runtime/CL/CLFunctions.h | 1 + .../runtime/CL/functions/CLBoundingBoxTransform.h | 67 ++++++++ src/core/CL/CLKernelLibrary.cpp | 5 + src/core/CL/cl_kernels/bounding_box_transform.cl | 118 ++++++++++++++ .../CL/kernels/CLBoundingBoxTransformKernel.cpp | 127 +++++++++++++++ .../CL/functions/CLBoundingBoxTransform.cpp | 43 +++++ tests/validation/CL/BoundingBoxTransform.cpp | 127 +++++++++++++++ .../fixtures/BoundingBoxTransformFixture.h | 173 +++++++++++++++++++++ .../validation/reference/BoundingBoxTransform.cpp | 101 ++++++++++++ tests/validation/reference/BoundingBoxTransform.h | 47 ++++++ utils/TypePrinter.h | 28 ++++ 14 files changed, 980 insertions(+) create mode 100644 arm_compute/core/CL/kernels/CLBoundingBoxTransformKernel.h create mode 100644 arm_compute/runtime/CL/functions/CLBoundingBoxTransform.h create mode 100644 src/core/CL/cl_kernels/bounding_box_transform.cl create mode 100644 src/core/CL/kernels/CLBoundingBoxTransformKernel.cpp create mode 100644 src/runtime/CL/functions/CLBoundingBoxTransform.cpp create mode 100644 tests/validation/CL/BoundingBoxTransform.cpp create mode 100644 tests/validation/fixtures/BoundingBoxTransformFixture.h create mode 100644 tests/validation/reference/BoundingBoxTransform.cpp create mode 100644 tests/validation/reference/BoundingBoxTransform.h diff --git a/arm_compute/core/CL/CLKernels.h b/arm_compute/core/CL/CLKernels.h index 95b6f9b70f..298cf5241f 100644 --- a/arm_compute/core/CL/CLKernels.h +++ b/arm_compute/core/CL/CLKernels.h @@ -37,6 +37,7 @@ #include "arm_compute/core/CL/kernels/CLBitwiseNotKernel.h" #include "arm_compute/core/CL/kernels/CLBitwiseOrKernel.h" #include "arm_compute/core/CL/kernels/CLBitwiseXorKernel.h" +#include "arm_compute/core/CL/kernels/CLBoundingBoxTransformKernel.h" #include "arm_compute/core/CL/kernels/CLBox3x3Kernel.h" #include "arm_compute/core/CL/kernels/CLCannyEdgeKernel.h" #include "arm_compute/core/CL/kernels/CLChannelCombineKernel.h" diff --git a/arm_compute/core/CL/kernels/CLBoundingBoxTransformKernel.h b/arm_compute/core/CL/kernels/CLBoundingBoxTransformKernel.h new file mode 100644 index 0000000000..6f0abc1888 --- /dev/null +++ b/arm_compute/core/CL/kernels/CLBoundingBoxTransformKernel.h @@ -0,0 +1,84 @@ +/* + * Copyright (c) 2018 ARM Limited. + * + * SPDX-License-Identifier: MIT + * + * Permission is hereby granted, free of charge, to any person obtaining a copy + * of this software and associated documentation files (the "Software"), to + * deal in the Software without restriction, including without limitation the + * rights to use, copy, modify, merge, publish, distribute, sublicense, and/or + * sell copies of the Software, and to permit persons to whom the Software is + * furnished to do so, subject to the following conditions: + * + * The above copyright notice and this permission notice shall be included in all + * copies or substantial portions of the Software. + * + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR + * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, + * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE + * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER + * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, + * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE + * SOFTWARE. + */ +#ifndef __ARM_COMPUTE_CLBOUNDINGBOXTRANSFORMKERNEL_H__ +#define __ARM_COMPUTE_CLBOUNDINGBOXTRANSFORMKERNEL_H__ + +#include "arm_compute/core/CL/ICLKernel.h" + +namespace arm_compute +{ +class ICLTensor; + +/** Interface for the bounding box kernel */ +class CLBoundingBoxTransformKernel : public ICLKernel +{ +public: + /** Default constructor */ + CLBoundingBoxTransformKernel(); + /** Prevent instances of this class from being copied (As this class contains pointers) */ + CLBoundingBoxTransformKernel(const CLBoundingBoxTransformKernel &) = delete; + /** Prevent instances of this class from being copied (As this class contains pointers) */ + CLBoundingBoxTransformKernel &operator=(const CLBoundingBoxTransformKernel &) = delete; + /** Allow instances of this class to be moved */ + CLBoundingBoxTransformKernel(CLBoundingBoxTransformKernel &&) = default; + /** Allow instances of this class to be moved */ + CLBoundingBoxTransformKernel &operator=(CLBoundingBoxTransformKernel &&) = default; + /** Default destructor */ + ~CLBoundingBoxTransformKernel() = default; + + /** Set the input and output tensors. + * + * @param[in] boxes Source tensor. Bounding box proposals in pixel coordinates. Size(M, 4), format [x1, y1, x2, y2]. Data types supported: F16/F32. + * @param[out] pred_boxes Destination tensor. Pixel coordinates of the transformed bounding boxes. Size (M, 4*K), format [x1, y1, x2, y2]. Data types supported: Same as @p input + * @param[in] deltas Bounding box translations and scales. Size (M, 4*K), format [dx, dy, dw, dh], K is the number of classes. Data types supported: Same as @p input + * @param[in] info Contains BoundingBox operation information described in @ref BoundingBoxTransformInfo. + * + * @note Only single image prediction is supported. Height and Width (and scale) of the image will be contained in the BoundingBoxTransformInfo struct. + * + */ + void configure(const ICLTensor *boxes, ICLTensor *pred_boxes, const ICLTensor *deltas, const BoundingBoxTransformInfo &info); + + /** Static function to check if given info will lead to a valid configuration of @ref CLBoundingBoxTransform + * + * @param[in] boxes Source tensor info. Bounding box proposals in pixel coordinates. Size(M, 4), format [x1, y1, x2, y2]. Data types supported: F16/F32. + * @param[in] pred_boxes Destination tensor info. Pixel coordinates of the transformed bounding boxes. Size (M, 4*K), format [x1, y1, x2, y2]. Data types supported: Same as @p input + * @param[in] deltas Bounding box translations and scales. Size (M, 4*K), format [dx, dy, dw, dh], K is the number of classes. Data types supported: Same as @p input + * @param[in] info Contains BoundingBox operation information described in @ref BoundingBoxTransformInfo. + * + * @note Only single image prediction is supported. Height and Width (and scale) of the image will be contained in the BoundingBoxTransformInfo struct. + * + * @return a Status + */ + static Status validate(const ITensorInfo *boxes, const ITensorInfo *pred_boxes, const ITensorInfo *deltas, const BoundingBoxTransformInfo &info); + + // Inherited methods overridden: + void run(const Window &window, cl::CommandQueue &queue) override; + +private: + const ICLTensor *_boxes; + ICLTensor *_pred_boxes; + const ICLTensor *_deltas; +}; +} // namespace arm_compute +#endif /*__ARM_COMPUTE_CLBOUNDINGBOXTRANSFORMKERNEL_H__ */ diff --git a/arm_compute/core/Types.h b/arm_compute/core/Types.h index 8a8cd509fa..5e04bcd0f4 100644 --- a/arm_compute/core/Types.h +++ b/arm_compute/core/Types.h @@ -851,6 +851,64 @@ private: unsigned int _sampling_ratio; }; +/** Bounding Box Transform information class */ +class BoundingBoxTransformInfo +{ +public: + /** Constructor + * + * @param[in] img_width Width of the original image + * @param[in] img_height Height, of the original image + * @param[in] scale Scale of the original image + * @param[in] apply_scale (Optional)Re-apply scaling after transforming the boxes. Defaults to false + * @param[in] weights (Optional)Weights [wx, wy, ww, wh] for the deltas. Defaults to all ones + * @param[in] bbox_xform_clip (Optional)Minimum bounding box width and height after bounding box transformation in log-space. Defaults to log(1000/16) + */ + BoundingBoxTransformInfo(float img_width, float img_height, float scale, bool apply_scale = false, const std::array weights = { 1.0, 1.0, 1.0, 1.0 }, float bbox_xform_clip = + 4.135166556742356) + : _img_width(img_width), _img_height(img_height), _scale(scale), _apply_scale(apply_scale), _weights(weights), _bbox_xform_clip(bbox_xform_clip) + { + } + + std::array weights() const + { + return _weights; + } + + float bbox_xform_clip() const + { + return _bbox_xform_clip; + } + + float img_height() const + { + return _img_height; + } + + float img_width() const + { + return _img_width; + } + + float scale() const + { + return _scale; + } + + bool apply_scale() const + { + return _apply_scale; + } + +private: + float _img_width; + float _img_height; + float _scale; + bool _apply_scale; + std::array _weights; + float _bbox_xform_clip; +}; + /** Activation Layer Information class */ class ActivationLayerInfo { diff --git a/arm_compute/runtime/CL/CLFunctions.h b/arm_compute/runtime/CL/CLFunctions.h index 014e25bbb3..7e36afd6f1 100644 --- a/arm_compute/runtime/CL/CLFunctions.h +++ b/arm_compute/runtime/CL/CLFunctions.h @@ -37,6 +37,7 @@ #include "arm_compute/runtime/CL/functions/CLBitwiseNot.h" #include "arm_compute/runtime/CL/functions/CLBitwiseOr.h" #include "arm_compute/runtime/CL/functions/CLBitwiseXor.h" +#include "arm_compute/runtime/CL/functions/CLBoundingBoxTransform.h" #include "arm_compute/runtime/CL/functions/CLBox3x3.h" #include "arm_compute/runtime/CL/functions/CLCannyEdge.h" #include "arm_compute/runtime/CL/functions/CLChannelCombine.h" diff --git a/arm_compute/runtime/CL/functions/CLBoundingBoxTransform.h b/arm_compute/runtime/CL/functions/CLBoundingBoxTransform.h new file mode 100644 index 0000000000..11be4301a0 --- /dev/null +++ b/arm_compute/runtime/CL/functions/CLBoundingBoxTransform.h @@ -0,0 +1,67 @@ +/* + * Copyright (c) 2018 ARM Limited. + * + * SPDX-License-Identifier: MIT + * + * Permission is hereby granted, free of charge, to any person obtaining a copy + * of this software and associated documentation files (the "Software"), to + * deal in the Software without restriction, including without limitation the + * rights to use, copy, modify, merge, publish, distribute, sublicense, and/or + * sell copies of the Software, and to permit persons to whom the Software is + * furnished to do so, subject to the following conditions: + * + * The above copyright notice and this permission notice shall be included in all + * copies or substantial portions of the Software. + * + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR + * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, + * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE + * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER + * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, + * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE + * SOFTWARE. + */ +#ifndef __ARM_COMPUTE_CLBOUNDINGBOXTRANSOFORM_H__ +#define __ARM_COMPUTE_CLBOUNDINGBOXTRANSOFORM_H__ + +#include "arm_compute/core/CL/kernels/CLBoundingBoxTransformKernel.h" +#include "arm_compute/runtime/CL/ICLSimpleFunction.h" + +namespace arm_compute +{ +class ICLTensor; + +/** Basic function to run @ref CLBoundingBoxTransformKernel. + * + * This function calls the following OpenCL kernels: + * -# @ref CLBoundingBoxTransformKernel + */ +class CLBoundingBoxTransform : public ICLSimpleFunction +{ +public: + /** Set the input and output tensors. + * + * @param[in] boxes Source tensor. Bounding box proposals in pixel coordinates. Size(M, 4), format [x1, y1, x2, y2]. Data types supported: F16/F32. + * @param[out] pred_boxes Destination tensor. Pixel coordinates of the transformed bounding boxes. Size (M, 4*K), format [x1, y1, x2, y2]. Data types supported: Same as @p input + * @param[in] deltas Bounding box translations and scales. Size (M, 4*K), format [dx, dy, dw, dh], K is the number of classes. Data types supported: Same as @p input + * @param[in] info Contains BoundingBox operation information described in @ref BoundingBoxTransformInfo. + * + * @note Only single image prediction is supported. Height and Width (and scale) of the image will be contained in the BoundingBoxTransformInfo struct. + */ + void configure(const ICLTensor *boxes, ICLTensor *pred_boxes, const ICLTensor *deltas, const BoundingBoxTransformInfo &info); + + /** Static function to check if given info will lead to a valid configuration of @ref CLBoundingBoxTransform + * + * @param[in] boxes Source tensor info. Bounding box proposals in pixel coordinates. Size(M, 4), format [x1, y1, x2, y2]. Data types supported: F16/F32. + * @param[in] pred_boxes Destination tensor info. Pixel coordinates of the transformed bounding boxes. Size (M, 4*K), format [x1, y1, x2, y2]. Data types supported: Same as @p input + * @param[in] deltas Bounding box translations and scales. Size (M, 4*K), format [dx, dy, dw, dh], K is the number of classes. Data types supported: Same as @p input + * @param[in] info Contains BoundingBox operation information described in @ref BoundingBoxTransformInfo. + * + * @note Only single image prediction is supported. Height and Width (and scale) of the image will be contained in the BoundingBoxTransformInfo struct. + * + * @return a Status + */ + static Status validate(const ITensorInfo *boxes, const ITensorInfo *pred_boxes, const ITensorInfo *deltas, const BoundingBoxTransformInfo &info); +}; +} // namespace arm_compute +#endif /* __ARM_COMPUTE_CLBOUNDINGBOXTRANSFORM_H__ */ diff --git a/src/core/CL/CLKernelLibrary.cpp b/src/core/CL/CLKernelLibrary.cpp index 900cb04b1a..12a7c38dfd 100644 --- a/src/core/CL/CLKernelLibrary.cpp +++ b/src/core/CL/CLKernelLibrary.cpp @@ -164,6 +164,7 @@ const std::map CLKernelLibrary::_kernel_program_map = { "bitwise_and", "bitwise_op.cl" }, { "bitwise_xor", "bitwise_op.cl" }, { "bitwise_not", "bitwise_op.cl" }, + { "bounding_box_transform", "bounding_box_transform.cl" }, { "channel_combine_NV", "channel_combine.cl" }, { "channel_combine_RGB888", "channel_combine.cl" }, { "channel_combine_RGBA8888", "channel_combine.cl" }, @@ -495,6 +496,10 @@ const std::map CLKernelLibrary::_program_source_map = { "bitwise_op.cl", #include "./cl_kernels/bitwise_op.clembed" + }, + { + "bounding_box_transform.cl", +#include "./cl_kernels/bounding_box_transform.clembed" }, { "canny.cl", diff --git a/src/core/CL/cl_kernels/bounding_box_transform.cl b/src/core/CL/cl_kernels/bounding_box_transform.cl new file mode 100644 index 0000000000..a62635e052 --- /dev/null +++ b/src/core/CL/cl_kernels/bounding_box_transform.cl @@ -0,0 +1,118 @@ +/* + * Copyright (c) 2018 ARM Limited. + * + * SPDX-License-Identifier: MIT + * + * Permission is hereby granted, free of charge, to any person obtaining a copy + * of this software and associated documentation files (the "Software"), to + * deal in the Software without restriction, including without limitation the + * rights to use, copy, modify, merge, publish, distribute, sublicense, and/or + * sell copies of the Software, and to permit persons to whom the Software is + * furnished to do so, subject to the following conditions: + * + * The above copyright notice and this permission notice shall be included in all + * copies or substantial portions of the Software. + * + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR + * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, + * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE + * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER + * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, + * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE + * SOFTWARE. + */ +#include "helpers.h" + +#if defined(DATA_TYPE) && defined(WEIGHT_X) && defined(WEIGHT_Y) && defined(WEIGHT_W) && defined(WEIGHT_H) && defined(IMG_WIDTH) && defined(IMG_HEIGHT) && defined(BOX_FIELDS) // Check for compile time constants + +/** Perform a padded copy of input tensor to the output tensor. Padding values are defined at compile time + * + * @attention The following variables must be passed at compile time: + * -# -DDATA_TYPE = Tensor data type. Supported data types: F16/F32 + * -# -DWEIGHT{X,Y,W,H}= Weights [wx, wy, ww, wh] for the deltas + * -# -DIMG_WIDTH= Original image width + * -# -DIMG_HEIGHT= Original image height + * -# -DBOX_FIELDS=Number of fields that are used to represent a box in boxes + * + * @param[in] boxes_ptr Pointer to the boxes tensor. Supported data types: F16/F32 + * @param[in] boxes_stride_x Stride of the boxes tensor in X dimension (in bytes) + * @param[in] boxes_step_x boxes_stride_x * number of elements along X processed per workitem(in bytes) + * @param[in] boxes_stride_y Stride of the boxes tensor in Y dimension (in bytes) + * @param[in] boxes_step_y boxes_stride_y * number of elements along Y processed per workitem(in bytes) + * @param[in] boxes_stride_z Stride of the source tensor in Z dimension (in bytes) + * @param[in] boxes_step_z boxes_stride_z * number of elements along Z processed per workitem(in bytes) + * @param[in] boxes_offset_first_element_in_bytes The offset of the first element in the boxes tensor + * @param[out] pred_boxes_ptr Pointer to the predicted boxes. Supported data types: same as @p in_ptr + * @param[in] pred_boxes_stride_x Stride of the predicted boxes in X dimension (in bytes) + * @param[in] pred_boxes_step_x pred_boxes_stride_x * number of elements along X processed per workitem(in bytes) + * @param[in] pred_boxes_stride_y Stride of the predicted boxes in Y dimension (in bytes) + * @param[in] pred_boxes_step_y pred_boxes_stride_y * number of elements along Y processed per workitem(in bytes) + * @param[in] pred_boxes_stride_z Stride of the predicted boxes in Z dimension (in bytes) + * @param[in] pred_boxes_step_z pred_boxes_stride_z * number of elements along Z processed per workitem(in bytes) + * @param[in] pred_boxes_offset_first_element_in_bytes The offset of the first element in the predicted boxes + * @param[in] deltas_ptr Pointer to the deltas tensor. Supported data types: same as @p in_ptr + * @param[in] deltas_stride_x Stride of the deltas tensor in X dimension (in bytes) + * @param[in] deltas_step_x deltas_stride_x * number of elements along X processed per workitem(in bytes) + * @param[in] deltas_stride_y Stride of the deltas tensor in Y dimension (in bytes) + * @param[in] deltas_step_y deltas_stride_y * number of elements along Y processed per workitem(in bytes) + * @param[in] deltas_stride_z Stride of the source tensor in Z dimension (in bytes) + * @param[in] deltas_step_z deltas_stride_z * number of elements along Z processed per workitem(in bytes) + * @param[in] deltas_offset_first_element_in_bytes The offset of the first element in the deltas tensor + */ +__kernel void bounding_box_transform( + VECTOR_DECLARATION(boxes), + IMAGE_DECLARATION(pred_boxes), + IMAGE_DECLARATION(deltas)) +{ + // Get pixels pointer + Vector boxes = CONVERT_TO_VECTOR_STRUCT_NO_STEP(boxes); + Image pred_boxes = CONVERT_TO_IMAGE_STRUCT(pred_boxes); + Image deltas = CONVERT_TO_IMAGE_STRUCT(deltas); + + // Load delta and box values into registers + const DATA_TYPE one = (DATA_TYPE)1.f; + const DATA_TYPE halfone = (DATA_TYPE)0.5f; + + const int py = get_global_id(1); // box + VEC_DATA_TYPE(DATA_TYPE, 4) + delta = vload4(0, (__global DATA_TYPE *)deltas.ptr); + const VEC_DATA_TYPE(DATA_TYPE, 4) + box = vload4(0, (__global DATA_TYPE *)vector_offset(&boxes, BOX_FIELDS * py)); + + // Calculate width and centers of the old boxes + const VEC_DATA_TYPE(DATA_TYPE, 2) + dims = box.s23 - box.s01 + one; + const VEC_DATA_TYPE(DATA_TYPE, 2) + ctr = box.s01 + halfone * dims; + const VEC_DATA_TYPE(DATA_TYPE, 4) + weights = (VEC_DATA_TYPE(DATA_TYPE, 4))(WEIGHT_X, WEIGHT_Y, WEIGHT_W, WEIGHT_H); + delta /= weights; + delta.s23 = min(delta.s23, (DATA_TYPE)BBOX_XFORM_CLIP); + + // Calculate widths and centers of the new boxes (translation + aspect ratio transformation) + const VEC_DATA_TYPE(DATA_TYPE, 2) + pred_ctr = delta.s01 * dims + ctr; + const VEC_DATA_TYPE(DATA_TYPE, 2) + pred_dims = exp(delta.s23) * dims; + + // Useful vector constant definitions + const VEC_DATA_TYPE(DATA_TYPE, 4) + max_values = (VEC_DATA_TYPE(DATA_TYPE, 4))(IMG_WIDTH, IMG_HEIGHT, IMG_WIDTH, IMG_HEIGHT); + const VEC_DATA_TYPE(DATA_TYPE, 4) + sign = (VEC_DATA_TYPE(DATA_TYPE, 4))(-1, -1, 1, 1); + const VEC_DATA_TYPE(DATA_TYPE, 4) + min_values = 0; + + // Calculate the coordinates of the new boxes + VEC_DATA_TYPE(DATA_TYPE, 4) + pred_box = pred_ctr.s0101 + sign * halfone * pred_dims.s0101; + pred_box = CLAMP(pred_box, min_values, max_values); +#ifdef SCALE // Possibly scale the predicted boxes + pred_box *= SCALE; +#endif // Possibly scale the predicted boxes + + // Store them into the output + vstore4(pred_box, 0, (__global DATA_TYPE *)pred_boxes.ptr); +} + +#endif // defined(DATA_TYPE) && defined(WEIGHT_X) && defined(WEIGHT_Y) && defined(WEIGHT_W) && defined(WEIGHT_H) && defined(IMG_WIDTH) && defined(IMG_HEIGHT) && defined(BOX_FIELDS) diff --git a/src/core/CL/kernels/CLBoundingBoxTransformKernel.cpp b/src/core/CL/kernels/CLBoundingBoxTransformKernel.cpp new file mode 100644 index 0000000000..09f3d33f5b --- /dev/null +++ b/src/core/CL/kernels/CLBoundingBoxTransformKernel.cpp @@ -0,0 +1,127 @@ +/* + * Copyright (c) 2018 ARM Limited. + * + * SPDX-License-Identifier: MIT + * + * Permission is hereby granted, free of charge, to any person obtaining a copy + * of this software and associated documentation files (the "Software"), to + * deal in the Software without restriction, including without limitation the + * rights to use, copy, modify, merge, publish, distribute, sublicense, and/or + * sell copies of the Software, and to permit persons to whom the Software is + * furnished to do so, subject to the following conditions: + * + * The above copyright notice and this permission notice shall be included in all + * copies or substantial portions of the Software. + * + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR + * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, + * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE + * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER + * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, + * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE + * SOFTWARE. + */ +#include "arm_compute/core/CL/kernels/CLBoundingBoxTransformKernel.h" + +#include "arm_compute/core/AccessWindowStatic.h" +#include "arm_compute/core/CL/CLHelpers.h" +#include "arm_compute/core/CL/CLKernelLibrary.h" +#include "arm_compute/core/CL/CLValidate.h" +#include "arm_compute/core/CL/ICLArray.h" +#include "arm_compute/core/CL/ICLTensor.h" +#include "arm_compute/core/CL/OpenCL.h" +#include "arm_compute/core/Helpers.h" +#include "arm_compute/core/TensorInfo.h" +#include "arm_compute/core/Utils.h" +#include "arm_compute/core/Window.h" + +namespace arm_compute +{ +namespace +{ +Status validate_arguments(const ITensorInfo *boxes, const ITensorInfo *pred_boxes, const ITensorInfo *deltas) +{ + ARM_COMPUTE_RETURN_ERROR_ON_NULLPTR(boxes, pred_boxes, deltas); + ARM_COMPUTE_RETURN_ERROR_ON_DATA_TYPE_NOT_IN(boxes, DataType::F32, DataType::F16); + ARM_COMPUTE_RETURN_ERROR_ON_DATA_TYPE_NOT_IN(deltas, DataType::F32, DataType::F16); + ARM_COMPUTE_RETURN_ERROR_ON(deltas->tensor_shape()[1] != boxes->tensor_shape()[1]); + ARM_COMPUTE_RETURN_ERROR_ON(deltas->tensor_shape()[0] % 4 != 0); + ARM_COMPUTE_RETURN_ERROR_ON(boxes->tensor_shape()[0] != 4); + ARM_COMPUTE_RETURN_ERROR_ON(deltas->num_dimensions() > 2); + ARM_COMPUTE_RETURN_ERROR_ON(boxes->num_dimensions() > 2); + + if(pred_boxes->total_size() > 0) + { + ARM_COMPUTE_RETURN_ERROR_ON_MISMATCHING_DIMENSIONS(pred_boxes->tensor_shape(), deltas->tensor_shape()); + ARM_COMPUTE_RETURN_ERROR_ON_MISMATCHING_DATA_TYPES(pred_boxes, deltas); + ARM_COMPUTE_RETURN_ERROR_ON(pred_boxes->num_dimensions() > 2); + } + return Status{}; +} +} // namespace + +CLBoundingBoxTransformKernel::CLBoundingBoxTransformKernel() + : _boxes(nullptr), _pred_boxes(nullptr), _deltas(nullptr) +{ +} + +void CLBoundingBoxTransformKernel::configure(const ICLTensor *boxes, ICLTensor *pred_boxes, const ICLTensor *deltas, const BoundingBoxTransformInfo &info) +{ + ARM_COMPUTE_ERROR_ON_NULLPTR(boxes, pred_boxes, deltas); + auto_init_if_empty(*pred_boxes->info(), *deltas->info()); + + // Set instance variables + _boxes = boxes; + _pred_boxes = pred_boxes; + _deltas = deltas; + + // Get image height and widht (rescaled) + const int img_h = floor(info.img_height() / info.scale() + 0.5f); + const int img_w = floor(info.img_width() / info.scale() + 0.5f); + + // Set build options + CLBuildOptions build_opts; + build_opts.add_option("-DDATA_TYPE=" + get_cl_type_from_data_type(deltas->info()->data_type())); + build_opts.add_option("-DWEIGHT_X=" + float_to_string_with_full_precision(info.weights()[0])); + build_opts.add_option("-DWEIGHT_Y=" + float_to_string_with_full_precision(info.weights()[1])); + build_opts.add_option("-DWEIGHT_W=" + float_to_string_with_full_precision(info.weights()[2])); + build_opts.add_option("-DWEIGHT_H=" + float_to_string_with_full_precision(info.weights()[3])); + build_opts.add_option("-DBBOX_XFORM_CLIP=" + float_to_string_with_full_precision(info.bbox_xform_clip())); + build_opts.add_option("-DIMG_WIDTH=" + support::cpp11::to_string(img_w)); + build_opts.add_option("-DIMG_HEIGHT=" + support::cpp11::to_string(img_h)); + build_opts.add_option("-DBOX_FIELDS=" + support::cpp11::to_string(4)); + build_opts.add_option_if(info.apply_scale(), "-DSCALE=" + float_to_string_with_full_precision(info.scale())); + + // Create kernel + _kernel = static_cast(CLKernelLibrary::get().create_kernel("bounding_box_transform", build_opts.options())); + + // Since the number of columns is a multiple of 4 by definition, we don't need to pad the tensor + const unsigned int num_elems_processed_per_iteration = 4; + Window win = calculate_max_window(*deltas->info(), Steps(num_elems_processed_per_iteration)); + ICLKernel::configure_internal(win); +} + +Status CLBoundingBoxTransformKernel::validate(const ITensorInfo *boxes, const ITensorInfo *pred_boxes, const ITensorInfo *deltas, const BoundingBoxTransformInfo &info) +{ + ARM_COMPUTE_UNUSED(info); + ARM_COMPUTE_RETURN_ON_ERROR(validate_arguments(boxes, pred_boxes, deltas)); + return Status{}; +} + +void CLBoundingBoxTransformKernel::run(const Window &window, cl::CommandQueue &queue) +{ + ARM_COMPUTE_ERROR_ON_UNCONFIGURED_KERNEL(this); + ARM_COMPUTE_ERROR_ON_INVALID_SUBWINDOW(IKernel::window(), window); + + Window slice = window.first_slice_window_2D(); + + // Set arguments + unsigned int idx = 0; + add_1D_tensor_argument(idx, _boxes, slice); + add_2D_tensor_argument(idx, _pred_boxes, slice); + add_2D_tensor_argument(idx, _deltas, slice); + + // Note that we don't need to loop over the slices, as we are sure that we are dealing with all 2D tensors + enqueue(queue, *this, slice); +} +} // namespace arm_compute diff --git a/src/runtime/CL/functions/CLBoundingBoxTransform.cpp b/src/runtime/CL/functions/CLBoundingBoxTransform.cpp new file mode 100644 index 0000000000..46a6b8ea96 --- /dev/null +++ b/src/runtime/CL/functions/CLBoundingBoxTransform.cpp @@ -0,0 +1,43 @@ +/* + * Copyright (c) 2018 ARM Limited. + * + * SPDX-License-Identifier: MIT + * + * Permission is hereby granted, free of charge, to any person obtaining a copy + * of this software and associated documentation files (the "Software"), to + * deal in the Software without restriction, including without limitation the + * rights to use, copy, modify, merge, publish, distribute, sublicense, and/or + * sell copies of the Software, and to permit persons to whom the Software is + * furnished to do so, subject to the following conditions: + * + * The above copyright notice and this permission notice shall be included in all + * copies or substantial portions of the Software. + * + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR + * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, + * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE + * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER + * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, + * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE + * SOFTWARE. + */ +#include "arm_compute/runtime/CL/functions/CLBoundingBoxTransform.h" + +#include "arm_compute/core/CL/kernels/CLBoundingBoxTransformKernel.h" +#include "support/ToolchainSupport.h" + +namespace arm_compute +{ +void CLBoundingBoxTransform::configure(const ICLTensor *boxes, ICLTensor *pred_boxes, const ICLTensor *deltas, const BoundingBoxTransformInfo &info) +{ + // Configure Bounding Box kernel + auto k = arm_compute::support::cpp14::make_unique(); + k->configure(boxes, pred_boxes, deltas, info); + _kernel = std::move(k); +} + +Status CLBoundingBoxTransform::validate(const ITensorInfo *boxes, const ITensorInfo *pred_boxes, const ITensorInfo *deltas, const BoundingBoxTransformInfo &info) +{ + return CLBoundingBoxTransformKernel::validate(boxes, pred_boxes, deltas, info); +} +} // namespace arm_compute diff --git a/tests/validation/CL/BoundingBoxTransform.cpp b/tests/validation/CL/BoundingBoxTransform.cpp new file mode 100644 index 0000000000..e5e2ad8f61 --- /dev/null +++ b/tests/validation/CL/BoundingBoxTransform.cpp @@ -0,0 +1,127 @@ +/* + * Copyright (c) 2018 ARM Limited. + * + * SPDX-License-Identifier: MIT + * + * Permission is hereby granted, free of charge, to any person obtaining a copy + * of this software and associated documentation files (the "Software"), to + * deal in the Software without restriction, including without limitation the + * rights to use, copy, modify, merge, publish, distribute, sublicense, and/or + * sell copies of the Software, and to permit persons to whom the Software is + * furnished to do so, subject to the following conditions: + * + * The above copyright notice and this permission notice shall be included in all + * copies or substantial portions of the Software. + * + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR + * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, + * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE + * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER + * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, + * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE + * SOFTWARE. + */ +#include "arm_compute/runtime/CL/CLScheduler.h" +#include "arm_compute/runtime/CL/functions/CLBoundingBoxTransform.h" +#include "tests/CL/CLAccessor.h" +#include "tests/CL/CLArrayAccessor.h" +#include "tests/Globals.h" +#include "tests/framework/Macros.h" +#include "tests/framework/datasets/Datasets.h" +#include "tests/validation/Validation.h" +#include "tests/validation/fixtures/BoundingBoxTransformFixture.h" +#include "utils/TypePrinter.h" + +namespace arm_compute +{ +namespace test +{ +namespace validation +{ +namespace +{ +RelativeTolerance relative_tolerance_f32(0.01f); +AbsoluteTolerance absolute_tolerance_f32(0.001f); + +RelativeTolerance relative_tolerance_f16(half(0.2)); +AbsoluteTolerance absolute_tolerance_f16(half(0.02f)); + +const auto BboxInfoDataset = framework::dataset::make("BboxInfo", { BoundingBoxTransformInfo(20U, 20U, 2U, true), + BoundingBoxTransformInfo(128U, 128U, 4U, true), + BoundingBoxTransformInfo(800U, 600U, 1U, false), + BoundingBoxTransformInfo(800U, 600U, 2U, true, { 1.0, 0.5, 1.5, 2.0 }), + BoundingBoxTransformInfo(800U, 600U, 4U, false, { 1.0, 0.5, 1.5, 2.0 }) + }); + +const auto DeltaDataset = framework::dataset::make("DeltasShape", { TensorShape(4U, 1U), + TensorShape(4U, 2U), + TensorShape(8U, 2U), + TensorShape(36U, 1U), + TensorShape(36U, 20U), + TensorShape(36U, 100U), + TensorShape(36U, 200U) + }); + +} // namespace + +TEST_SUITE(CL) +TEST_SUITE(BBoxTransform) + +// *INDENT-OFF* +// clang-format off +DATA_TEST_CASE(Validate, framework::DatasetMode::ALL, zip(zip(zip(zip( + framework::dataset::make("BoxesInfo", { TensorInfo(TensorShape(4U, 128U), 1, DataType::F32), + TensorInfo(TensorShape(5U, 128U), 1, DataType::F32), // Wrong number of box fields + TensorInfo(TensorShape(4U, 128U), 1, DataType::F16), // Wrong data type + TensorInfo(TensorShape(4U, 128U), 1, DataType::F32), // Wrong number of classes + TensorInfo(TensorShape(4U, 128U), 1, DataType::F32)}), // Deltas and predicted boxes have different dimensions + framework::dataset::make("PredBoxesInfo",{ TensorInfo(TensorShape(128U, 128U), 1, DataType::F32), + TensorInfo(TensorShape(128U, 128U), 1, DataType::F32), + TensorInfo(TensorShape(127U, 128U), 1, DataType::F32), + TensorInfo(TensorShape(128U, 100U), 1, DataType::F32), + TensorInfo(TensorShape(128U, 100U), 1, DataType::F32)})), + framework::dataset::make("DeltasInfo", { TensorInfo(TensorShape(128U, 128U), 1, DataType::F32), + TensorInfo(TensorShape(128U, 128U), 1, DataType::F32), + TensorInfo(TensorShape(127U, 128U), 1, DataType::F32), + TensorInfo(TensorShape(128U, 100U), 1, DataType::F32), + TensorInfo(TensorShape(128U, 128U), 1, DataType::F32)})), + framework::dataset::make("BoundingBoxTransofmInfo", { BoundingBoxTransformInfo(800.f, 600.f, 1.f), + BoundingBoxTransformInfo(800.f, 600.f, 1.f), + BoundingBoxTransformInfo(800.f, 600.f, 1.f), + BoundingBoxTransformInfo(800.f, 600.f, 1.f)})), + framework::dataset::make("Expected", { true, false, false, false, false })), + boxes_info, pred_boxes_info, deltas_info, bbox_info, expected) +{ + ARM_COMPUTE_EXPECT(bool(CLBoundingBoxTransform::validate(&boxes_info.clone()->set_is_resizable(true), &pred_boxes_info.clone()->set_is_resizable(true), &deltas_info.clone()->set_is_resizable(true), bbox_info)) == expected, framework::LogLevel::ERRORS); +} +// clang-format on +// *INDENT-ON* + +template +using CLBoundingBoxTransformFixture = BoundingBoxTransformFixture; + +TEST_SUITE(Float) +TEST_SUITE(FP32) +FIXTURE_DATA_TEST_CASE(BoundingBox, CLBoundingBoxTransformFixture, framework::DatasetMode::ALL, + combine(combine(DeltaDataset, BboxInfoDataset), framework::dataset::make("DataType", { DataType::F32 }))) +{ + // Validate output + validate(CLAccessor(_target), _reference, relative_tolerance_f32, 0.f, absolute_tolerance_f32); +} +TEST_SUITE_END() // FP32 + +TEST_SUITE(FP16) +FIXTURE_DATA_TEST_CASE(BoundingBox, CLBoundingBoxTransformFixture, framework::DatasetMode::ALL, + combine(combine(DeltaDataset, BboxInfoDataset), framework::dataset::make("DataType", { DataType::F16 }))) +{ + // Validate output + validate(CLAccessor(_target), _reference, relative_tolerance_f16, 0.01f, absolute_tolerance_f16); +} +TEST_SUITE_END() // FP16 +TEST_SUITE_END() // Float + +TEST_SUITE_END() // BBoxTransform +TEST_SUITE_END() // CL +} // namespace validation +} // namespace test +} // namespace arm_compute diff --git a/tests/validation/fixtures/BoundingBoxTransformFixture.h b/tests/validation/fixtures/BoundingBoxTransformFixture.h new file mode 100644 index 0000000000..b71da8e97d --- /dev/null +++ b/tests/validation/fixtures/BoundingBoxTransformFixture.h @@ -0,0 +1,173 @@ +/* + * Copyright (c) 2018 ARM Limited. + * + * SPDX-License-Identifier: MIT + * + * Permission is hereby granted, free of charge, to any person obtaining a copy + * of this software and associated documentation files (the "Software"), to + * deal in the Software without restriction, including without limitation the + * rights to use, copy, modify, merge, publish, distribute, sublicense, and/or + * sell copies of the Software, and to permit persons to whom the Software is + * furnished to do so, subject to the following conditions: + * + * The above copyright notice and this permission notice shall be included in all + * copies or substantial portions of the Software. + * + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR + * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, + * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE + * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER + * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, + * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE + * SOFTWARE. + */ +#ifndef ARM_COMPUTE_TEST_BOUNDINGBOXTRANSFORM_FIXTURE +#define ARM_COMPUTE_TEST_BOUNDINGBOXTRANSFORM_FIXTURE + +#include "arm_compute/core/TensorShape.h" +#include "arm_compute/core/Types.h" +#include "tests/AssetsLibrary.h" +#include "tests/Globals.h" +#include "tests/IAccessor.h" +#include "tests/framework/Asserts.h" +#include "tests/framework/Fixture.h" +#include "tests/validation/Helpers.h" +#include "tests/validation/reference/BoundingBoxTransform.h" + +namespace arm_compute +{ +namespace test +{ +namespace validation +{ +template +class BoundingBoxTransformFixture : public framework::Fixture +{ +public: + template + void setup(TensorShape deltas_shape, const BoundingBoxTransformInfo &info, DataType data_type) + { + std::mt19937 gen_target(library->seed()); + _target = compute_target(deltas_shape, data_type, info, gen_target); + + std::mt19937 gen_reference(library->seed()); + _reference = compute_reference(deltas_shape, data_type, info, gen_reference); + } + +protected: + TensorType compute_target(const TensorShape &deltas_shape, DataType data_type, + const BoundingBoxTransformInfo &bbox_info, std::mt19937 &gen) + { + // Create tensors + TensorShape boxes_shape(4, deltas_shape[1]); + TensorType deltas = create_tensor(deltas_shape, data_type); + TensorType boxes = create_tensor(boxes_shape, data_type); + TensorType pred_boxes; + + // Create and configure function + FunctionType bbox_transform; + bbox_transform.configure(&boxes, &pred_boxes, &deltas, bbox_info); + + ARM_COMPUTE_EXPECT(deltas.info()->is_resizable(), framework::LogLevel::ERRORS); + ARM_COMPUTE_EXPECT(boxes.info()->is_resizable(), framework::LogLevel::ERRORS); + ARM_COMPUTE_EXPECT(pred_boxes.info()->is_resizable(), framework::LogLevel::ERRORS); + + // Allocate tensors + deltas.allocator()->allocate(); + boxes.allocator()->allocate(); + pred_boxes.allocator()->allocate(); + + ARM_COMPUTE_EXPECT(!deltas.info()->is_resizable(), framework::LogLevel::ERRORS); + ARM_COMPUTE_EXPECT(!boxes.info()->is_resizable(), framework::LogLevel::ERRORS); + + // Fill tensors + TensorShape img_shape(bbox_info.scale() * bbox_info.img_width(), bbox_info.scale() * bbox_info.img_height()); + generate_boxes(AccessorType(boxes), img_shape, boxes_shape[1], gen); + generate_deltas(AccessorType(deltas), AccessorType(boxes), img_shape, deltas_shape[1], deltas_shape[0] / 4, gen); + + // Compute function + bbox_transform.run(); + + return pred_boxes; + } + + SimpleTensor compute_reference(const TensorShape &deltas_shape, + DataType data_type, + const BoundingBoxTransformInfo &bbox_info, std::mt19937 &gen) + { + // Create reference tensor + TensorShape boxes_shape(4, deltas_shape[1]); + SimpleTensor boxes{ boxes_shape, data_type }; + SimpleTensor deltas{ deltas_shape, data_type }; + + // Fill reference tensor + TensorShape img_shape(bbox_info.scale() * bbox_info.img_width(), bbox_info.scale() * bbox_info.img_height()); + generate_boxes(boxes, img_shape, boxes_shape[1], gen); + generate_deltas(deltas, boxes, img_shape, deltas_shape[1], deltas_shape[0] / 4, gen); + + return reference::bounding_box_transform(boxes, deltas, bbox_info); + } + + TensorType _target{}; + SimpleTensor _reference{}; + +private: + template + void generate_deltas(U &&deltas, U &&boxes, const TensorShape &image_shape, size_t num_boxes, size_t num_classes, std::mt19937 &gen) + { + T *deltas_ptr = static_cast(deltas.data()); + T *boxes_ptr = static_cast(boxes.data()); + + std::uniform_int_distribution<> dist_x1(0, image_shape[0] - 1); + std::uniform_int_distribution<> dist_y1(0, image_shape[1] - 1); + std::uniform_int_distribution<> dist_w(1, image_shape[0]); + std::uniform_int_distribution<> dist_h(1, image_shape[1]); + + for(size_t i = 0; i < num_boxes; ++i) + { + const T ex_width = boxes_ptr[4 * i + 2] - boxes_ptr[4 * i] + T(1); + const T ex_height = boxes_ptr[4 * i + 3] - boxes_ptr[4 * i + 1] + T(1); + const T ex_ctr_x = boxes_ptr[4 * i] + T(0.5) * ex_width; + const T ex_ctr_y = boxes_ptr[4 * i + 1] + T(0.5) * ex_height; + + for(size_t j = 0; j < num_classes; ++j) + { + const T x1 = T(dist_x1(gen)); + const T y1 = T(dist_y1(gen)); + const T width = T(dist_w(gen)); + const T height = T(dist_h(gen)); + const T ctr_x = x1 + T(0.5) * width; + const T ctr_y = y1 + T(0.5) * height; + + deltas_ptr[4 * num_classes * i + 4 * j] = (ctr_x - ex_ctr_x) / ex_width; + deltas_ptr[4 * num_classes * i + 4 * j + 1] = (ctr_y - ex_ctr_y) / ex_height; + deltas_ptr[4 * num_classes * i + 4 * j + 2] = log(width / ex_width); + deltas_ptr[4 * num_classes * i + 4 * j + 3] = log(height / ex_height); + } + } + } + + template + void generate_boxes(U &&boxes, const TensorShape &image_shape, size_t num_boxes, std::mt19937 &gen) + { + T *boxes_ptr = (T *)boxes.data(); + + std::uniform_int_distribution<> dist_x1(0, image_shape[0] - 1); + std::uniform_int_distribution<> dist_y1(0, image_shape[1] - 1); + std::uniform_int_distribution<> dist_w(1, image_shape[0]); + std::uniform_int_distribution<> dist_h(1, image_shape[1]); + + for(size_t i = 0; i < num_boxes; ++i) + { + boxes_ptr[4 * i] = dist_x1(gen); + boxes_ptr[4 * i + 1] = dist_y1(gen); + boxes_ptr[4 * i + 2] = boxes_ptr[4 * i] + dist_w(gen) - 1; + boxes_ptr[4 * i + 3] = boxes_ptr[4 * i + 1] + dist_h(gen) - 1; + } + } +}; + +} // namespace validation +} // namespace test +} // namespace arm_compute +#endif /* ARM_COMPUTE_TEST_BOUNDINGBOXTRANSFORM_FIXTURE */ diff --git a/tests/validation/reference/BoundingBoxTransform.cpp b/tests/validation/reference/BoundingBoxTransform.cpp new file mode 100644 index 0000000000..6ac512e749 --- /dev/null +++ b/tests/validation/reference/BoundingBoxTransform.cpp @@ -0,0 +1,101 @@ +/* + * Copyright (c) 2018 ARM Limited. + * + * SPDX-License-Identifier: MIT + * + * Permission is hereby granted, free of charge, to any person obtaining a copy + * of this software and associated documentation files (the "Software"), to + * deal in the Software without restriction, including without limitation the + * rights to use, copy, modify, merge, publish, distribute, sublicense, and/or + * sell copies of the Software, and to permit persons to whom the Software is + * furnished to do so, subject to the following conditions: + * + * The above copyright notice and this permission notice shall be included in all + * copies or substantial portions of the Software. + * + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR + * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, + * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE + * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER + * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, + * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE + * SOFTWARE. + */ +#include "BoundingBoxTransform.h" + +#include "arm_compute/core/Types.h" +#include "arm_compute/core/utils/misc/ShapeCalculator.h" +#include "arm_compute/core/utils/misc/Utility.h" +#include "tests/validation/Helpers.h" + +namespace arm_compute +{ +namespace test +{ +namespace validation +{ +namespace reference +{ +template +SimpleTensor bounding_box_transform(const SimpleTensor &boxes, const SimpleTensor &deltas, const BoundingBoxTransformInfo &info) +{ + const DataType boxes_data_type = deltas.data_type(); + SimpleTensor pred_boxes(deltas.shape(), boxes_data_type); + + const size_t num_classes = deltas.shape()[0] / 4; + const size_t num_boxes = deltas.shape()[1]; + const T *deltas_ptr = deltas.data(); + T *pred_boxes_ptr = pred_boxes.data(); + + const int img_h = floor(info.img_height() / info.scale() + 0.5f); + const int img_w = floor(info.img_width() / info.scale() + 0.5f); + + const T scale = (info.apply_scale() ? T(info.scale()) : T(1)); + + const size_t box_fields = 4; + const size_t class_fields = 4; + + for(size_t i = 0; i < num_boxes; ++i) + { + // Extract ROI information + const size_t start_box = box_fields * i; + const T width = boxes[start_box + 2] - boxes[start_box] + T(1.0); + const T height = boxes[start_box + 3] - boxes[start_box + 1] + T(1.0); + const T ctr_x = boxes[start_box] + T(0.5) * width; + const T ctr_y = boxes[start_box + 1] + T(0.5) * height; + + for(size_t j = 0; j < num_classes; ++j) + { + // Extract deltas + const size_t start_delta = i * num_classes * class_fields + class_fields * j; + const T dx = deltas_ptr[start_delta] / T(info.weights()[0]); + const T dy = deltas_ptr[start_delta + 1] / T(info.weights()[1]); + T dw = deltas_ptr[start_delta + 2] / T(info.weights()[2]); + T dh = deltas_ptr[start_delta + 3] / T(info.weights()[3]); + + // Clip dw and dh + dw = std::min(dw, T(info.bbox_xform_clip())); + dh = std::min(dh, T(info.bbox_xform_clip())); + + // Determine the predictions + const T pred_ctr_x = dx * width + ctr_x; + const T pred_ctr_y = dy * height + ctr_y; + const T pred_w = T(std::exp(dw)) * width; + const T pred_h = T(std::exp(dh)) * height; + + // Store the prediction into the output tensor + pred_boxes_ptr[start_delta] = scale * utility::clamp(pred_ctr_x - T(0.5) * pred_w, T(0), T(img_w)); + pred_boxes_ptr[start_delta + 1] = scale * utility::clamp(pred_ctr_y - T(0.5) * pred_h, T(0), T(img_h)); + pred_boxes_ptr[start_delta + 2] = scale * utility::clamp(pred_ctr_x + T(0.5) * pred_w, T(0), T(img_w)); + pred_boxes_ptr[start_delta + 3] = scale * utility::clamp(pred_ctr_y + T(0.5) * pred_h, T(0), T(img_h)); + } + } + return pred_boxes; +} + +template SimpleTensor bounding_box_transform(const SimpleTensor &boxes, const SimpleTensor &deltas, const BoundingBoxTransformInfo &info); +template SimpleTensor bounding_box_transform(const SimpleTensor &boxes, const SimpleTensor &deltas, const BoundingBoxTransformInfo &info); +} // namespace reference +} // namespace validation +} // namespace test +} // namespace arm_compute diff --git a/tests/validation/reference/BoundingBoxTransform.h b/tests/validation/reference/BoundingBoxTransform.h new file mode 100644 index 0000000000..33ef9d984f --- /dev/null +++ b/tests/validation/reference/BoundingBoxTransform.h @@ -0,0 +1,47 @@ +/* + * Copyright (c) 2018 ARM Limited. + * + * SPDX-License-Identifier: MIT + * + * Permission is hereby granted, free of charge, to any person obtaining a copy + * of this software and associated documentation files (the "Software"), to + * deal in the Software without restriction, including without limitation the + * rights to use, copy, modify, merge, publish, distribute, sublicense, and/or + * sell copies of the Software, and to permit persons to whom the Software is + * furnished to do so, subject to the following conditions: + * + * The above copyright notice and this permission notice shall be included in all + * copies or substantial portions of the Software. + * + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR + * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, + * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE + * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER + * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, + * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE + * SOFTWARE. + */ +#ifndef __ARM_COMPUTE_TEST_BOUNDINGBOXTRANSFORM_H__ +#define __ARM_COMPUTE_TEST_BOUNDINGBOXTRANSFORM_H__ + +#include "BoundingBoxTransform.h" + +#include "arm_compute/core/Types.h" +#include "arm_compute/core/utils/misc/ShapeCalculator.h" +#include "tests/validation/Helpers.h" + +namespace arm_compute +{ +namespace test +{ +namespace validation +{ +namespace reference +{ +template +SimpleTensor bounding_box_transform(const SimpleTensor &boxes, const SimpleTensor &deltas, const BoundingBoxTransformInfo &info); +} // namespace reference +} // namespace validation +} // namespace test +} // namespace arm_compute +#endif /* __ARM_COMPUTE_TEST_BOUNDINGBOXTRANSFORM_H__ */ diff --git a/utils/TypePrinter.h b/utils/TypePrinter.h index b868aa94a1..571de9a4ef 100644 --- a/utils/TypePrinter.h +++ b/utils/TypePrinter.h @@ -233,6 +233,34 @@ inline std::string to_string(const ROIPoolingLayerInfo &pool_info) return str.str(); } +/** Formatted output of the BoundingBoxTransformInfo type. + * + * @param[out] os Output stream. + * @param[in] bbox_info Type to output. + * + * @return Modified output stream. + */ +inline ::std::ostream &operator<<(::std::ostream &os, const BoundingBoxTransformInfo &bbox_info) +{ + auto weights = bbox_info.weights(); + os << "(" << bbox_info.img_width() << "x" << bbox_info.img_height() << ")~" << bbox_info.scale() << "(weights = {" << weights[0] << ", " << weights[1] << ", " << weights[2] << ", " << weights[3] << + "})"; + return os; +} + +/** Formatted output of the BoundingBoxTransformInfo type. + * + * @param[in] bbox_info Type to output. + * + * @return Formatted string. + */ +inline std::string to_string(const BoundingBoxTransformInfo &bbox_info) +{ + std::stringstream str; + str << bbox_info; + return str.str(); +} + /** Formatted output of the QuantizationInfo type. * * @param[out] os Output stream. -- cgit v1.2.1