diff options
author | Michalis Spyrou <michalis.spyrou@arm.com> | 2018-09-04 15:27:25 +0100 |
---|---|---|
committer | Isabella Gottardi <isabella.gottardi@arm.com> | 2018-11-16 09:49:04 +0000 |
commit | 721c4cb0e1fbf1f151a79cd4c9d67e832f94f9bf (patch) | |
tree | 5727ec968bc80550912e4c8f1db778ab381af057 | |
parent | e413d255b18fad1553bd3550a98f707c9c6e8aec (diff) | |
download | ComputeLibrary-721c4cb0e1fbf1f151a79cd4c9d67e832f94f9bf.tar.gz |
COMPMID-1461 SSD support: Create NEON PriorBox
Change-Id: I99e1c3939cfea4b9cb0ddfa313706f31b213ca89
-rw-r--r-- | arm_compute/core/NEON/NEKernels.h | 1 | ||||
-rw-r--r-- | arm_compute/core/NEON/kernels/NEPriorBoxLayerKernel.h | 105 | ||||
-rw-r--r-- | arm_compute/core/Types.h | 4 | ||||
-rw-r--r-- | arm_compute/runtime/CL/functions/CLPriorBoxLayer.h | 2 | ||||
-rw-r--r-- | arm_compute/runtime/NEON/NEFunctions.h | 1 | ||||
-rw-r--r-- | arm_compute/runtime/NEON/functions/NEPriorBoxLayer.h | 59 | ||||
-rw-r--r-- | src/core/NEON/kernels/NEPriorBoxLayerKernel.cpp | 358 | ||||
-rw-r--r-- | src/runtime/NEON/functions/NEPriorBoxLayer.cpp | 47 | ||||
-rw-r--r-- | tests/datasets/PriorBoxLayerDataset.h | 1 | ||||
-rw-r--r-- | tests/validation/NEON/PriorBoxLayer.cpp | 100 |
10 files changed, 673 insertions, 5 deletions
diff --git a/arm_compute/core/NEON/NEKernels.h b/arm_compute/core/NEON/NEKernels.h index 76eb5cb5f2..0cfde722d9 100644 --- a/arm_compute/core/NEON/NEKernels.h +++ b/arm_compute/core/NEON/NEKernels.h @@ -99,6 +99,7 @@ #include "arm_compute/core/NEON/kernels/NEPermuteKernel.h" #include "arm_compute/core/NEON/kernels/NEPixelWiseMultiplicationKernel.h" #include "arm_compute/core/NEON/kernels/NEPoolingLayerKernel.h" +#include "arm_compute/core/NEON/kernels/NEPriorBoxLayerKernel.h" #include "arm_compute/core/NEON/kernels/NEQuantizationLayerKernel.h" #include "arm_compute/core/NEON/kernels/NEROIPoolingLayerKernel.h" #include "arm_compute/core/NEON/kernels/NEReductionOperationKernel.h" diff --git a/arm_compute/core/NEON/kernels/NEPriorBoxLayerKernel.h b/arm_compute/core/NEON/kernels/NEPriorBoxLayerKernel.h new file mode 100644 index 0000000000..ac61b61142 --- /dev/null +++ b/arm_compute/core/NEON/kernels/NEPriorBoxLayerKernel.h @@ -0,0 +1,105 @@ +/* + * 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_NEPRIORBOXLAYERKERNEL_H__ +#define __ARM_COMPUTE_NEPRIORBOXLAYERKERNEL_H__ + +#include "arm_compute/core/NEON/INEKernel.h" + +namespace arm_compute +{ +class ITensor; + +/** Interface for the kernel to calculate prior boxes */ +class NEPriorBoxLayerKernel : public INEKernel +{ +public: + const char *name() const override + { + return "NEPriorBoxLayerKernel"; + } + /** Default constructor */ + NEPriorBoxLayerKernel(); + /** Prevent instances of this class from being copied (As this class contains pointers) */ + NEPriorBoxLayerKernel(const NEPriorBoxLayerKernel &) = delete; + /** Prevent instances of this class from being copied (As this class contains pointers) */ + NEPriorBoxLayerKernel &operator=(const NEPriorBoxLayerKernel &) = delete; + /** Allow instances of this class to be moved */ + NEPriorBoxLayerKernel(NEPriorBoxLayerKernel &&) = default; + /** Allow instances of this class to be moved */ + NEPriorBoxLayerKernel &operator=(NEPriorBoxLayerKernel &&) = default; + /** Set the input and output tensors. + * + * @param[in] input1 First source tensor. Data types supported: F32. Data layouts supported: NCHW/NHWC. + * @param[in] input2 Second source tensor. Data types and layouts supported: same as @p input1 + * @param[out] output Destination tensor. Output dimensions are [W * H * num_priors * 4, 2]. Data type supported: same as @p input + * @param[in] info Prior box layer info. + */ + void configure(const ITensor *input1, const ITensor *input2, ITensor *output, const PriorBoxLayerInfo &info); + /** Static function to check if given info will lead to a valid configuration of @ref NEPriorBoxLayerKernel + * + * @param[in] input1 First source tensor info. Data types supported: F32. Data layouts supported: NCHW/NHWC. + * @param[in] input2 Second source tensor info. Data types and layouts supported: same as @p input1 + * @param[in] output Destination tensor info. Output dimensions are [W * H * num_priors * 4, 2]. Data type supported: same as @p input + * @param[in] info Prior box layer info. + * + * @return a status + */ + static Status validate(const ITensorInfo *input1, const ITensorInfo *input2, const ITensorInfo *output, const PriorBoxLayerInfo &info); + + // Inherited methods overridden: + void run(const Window &window, const ThreadInfo &info) override; + +private: + /** Stores the coordinates of the calculated prior boxes. + * + * @param[out] out Output pointer. + * @param[in] offset Output offset to write to. + * @param[in] center_x Center pixel value on x-axis. + * @param[in] center_y Center pixel value on y-axis. + * @param[in] box_width Prior box width. + * @param[in] box_height Prior box height. + * @param[in] width Input width. + * @param[in] height Input height. + */ + template <DataLayout DL> + void store_coordinates(float *out, const int offset, const float center_x, const float center_y, const float box_width, const float box_height, const int width, const int height); + /** Function to calculate prior boxes. + * + * @param[in] window Input region on which to execute the kernel. + */ + template <DataLayout DL> + void calculate_prior_boxes(const Window &window); + /** Common signature for all the specialised PriorBox functions + * + * @param[in] window Input region on which to execute the kernel.. + */ + using PriorBoxFunction = void (NEPriorBoxLayerKernel::*)(const Window &window); + PriorBoxFunction _func; + const ITensor *_input1; + const ITensor *_input2; + ITensor *_output; + PriorBoxLayerInfo _info; +}; +} // namespace arm_compute +#endif /* __ARM_COMPUTE_NEPRIORBOXLAYERKERNEL_H__ */ diff --git a/arm_compute/core/Types.h b/arm_compute/core/Types.h index 1c9571463b..fb277584fd 100644 --- a/arm_compute/core/Types.h +++ b/arm_compute/core/Types.h @@ -847,7 +847,7 @@ public: /** Constructor * * @param[in] min_sizes Min sizes vector. - * @param[in] variances Variances vector. Size must be equal to 4. + * @param[in] variances Variances vector. * @param[in] offset Offset value. * @param[in] flip (Optional) Flip the aspect ratios. * @param[in] clip (Optional) Clip coordinates so that they're within [0,1]. @@ -864,7 +864,7 @@ public: _flip(flip), _clip(clip), _max_sizes(max_sizes), - _aspect_ratios(aspect_ratios), + _aspect_ratios(), _img_size(img_size), _steps(steps) { diff --git a/arm_compute/runtime/CL/functions/CLPriorBoxLayer.h b/arm_compute/runtime/CL/functions/CLPriorBoxLayer.h index 2376cd3fc6..6adf7f9dd1 100644 --- a/arm_compute/runtime/CL/functions/CLPriorBoxLayer.h +++ b/arm_compute/runtime/CL/functions/CLPriorBoxLayer.h @@ -24,8 +24,6 @@ #ifndef __ARM_COMPUTE_CLPRIORBOXLAYER_H__ #define __ARM_COMPUTE_CLPRIORBOXLAYER_H__ -#include "arm_compute/runtime/IFunction.h" - #include "arm_compute/core/CL/kernels/CLPriorBoxLayerKernel.h" #include "arm_compute/core/Types.h" #include "arm_compute/runtime/CL/ICLSimpleFunction.h" diff --git a/arm_compute/runtime/NEON/NEFunctions.h b/arm_compute/runtime/NEON/NEFunctions.h index 555d7d7551..d9c8355178 100644 --- a/arm_compute/runtime/NEON/NEFunctions.h +++ b/arm_compute/runtime/NEON/NEFunctions.h @@ -99,6 +99,7 @@ #include "arm_compute/runtime/NEON/functions/NEPhase.h" #include "arm_compute/runtime/NEON/functions/NEPixelWiseMultiplication.h" #include "arm_compute/runtime/NEON/functions/NEPoolingLayer.h" +#include "arm_compute/runtime/NEON/functions/NEPriorBoxLayer.h" #include "arm_compute/runtime/NEON/functions/NEQuantizationLayer.h" #include "arm_compute/runtime/NEON/functions/NERNNLayer.h" #include "arm_compute/runtime/NEON/functions/NEROIPoolingLayer.h" diff --git a/arm_compute/runtime/NEON/functions/NEPriorBoxLayer.h b/arm_compute/runtime/NEON/functions/NEPriorBoxLayer.h new file mode 100644 index 0000000000..34ba39d960 --- /dev/null +++ b/arm_compute/runtime/NEON/functions/NEPriorBoxLayer.h @@ -0,0 +1,59 @@ +/* + * 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_NEPRIORBOXLAYER_H__ +#define __ARM_COMPUTE_NEPRIORBOXLAYER_H__ + +#include "arm_compute/core/NEON/kernels/NEPriorBoxLayerKernel.h" +#include "arm_compute/core/Types.h" +#include "arm_compute/runtime/NEON/INESimpleFunction.h" + +namespace arm_compute +{ +class ITensor; + +/** Basic function to run @ref NEPriorBoxLayerKernel. */ +class NEPriorBoxLayer : public INESimpleFunction +{ +public: + /** Set the input and output tensors. + * + * @param[in] input1 First source tensor. Data types supported: F32. Data layouts supported: NCHW/NHWC. + * @param[in] input2 Second source tensor. Data types and layouts supported: same as @p input1 + * @param[out] output Destination tensor. Output dimensions are [W * H * num_priors * 4, 2]. Data type supported: same as @p input + * @param[in] info Prior box layer info. + */ + void configure(const ITensor *input1, const ITensor *input2, ITensor *output, const PriorBoxLayerInfo &info); + /** Static function to check if given info will lead to a valid configuration of @ref NEPriorBoxLayer + * + * @param[in] input1 First source tensor info. Data types supported: F32. Data layouts supported: NCHW/NHWC. + * @param[in] input2 Second source tensor info. Data types and layouts supported: same as @p input1 + * @param[in] output Destination tensor info. Output dimensions are [W * H * num_priors * 4, 2]. Data type supported: same as @p input + * @param[in] info Prior box layer info. + * + * @return a status + */ + static Status validate(const ITensorInfo *input1, const ITensorInfo *input2, const ITensorInfo *output, const PriorBoxLayerInfo &info); +}; +} // namespace arm_compute +#endif /* __ARM_COMPUTE_NEPRIORBOXLAYER_H__ */ diff --git a/src/core/NEON/kernels/NEPriorBoxLayerKernel.cpp b/src/core/NEON/kernels/NEPriorBoxLayerKernel.cpp new file mode 100644 index 0000000000..2f6317921b --- /dev/null +++ b/src/core/NEON/kernels/NEPriorBoxLayerKernel.cpp @@ -0,0 +1,358 @@ +/* + * 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/NEON/kernels/NEPriorBoxLayerKernel.h" + +#include "arm_compute/core/Helpers.h" +#include "arm_compute/core/ITensor.h" +#include "arm_compute/core/Types.h" +#include "arm_compute/core/Validate.h" + +#include <arm_neon.h> +#include <cstdint> + +namespace arm_compute +{ +namespace +{ +Status validate_arguments(const ITensorInfo *input1, const ITensorInfo *input2, const ITensorInfo *output, const PriorBoxLayerInfo &info) +{ + ARM_COMPUTE_RETURN_ERROR_ON_NULLPTR(input1, input2, output); + ARM_COMPUTE_RETURN_ERROR_ON_DATA_TYPE_CHANNEL_NOT_IN(input1, 1, DataType::F32); + ARM_COMPUTE_RETURN_ERROR_ON_MISMATCHING_DATA_LAYOUT(input1, input2); + ARM_COMPUTE_RETURN_ERROR_ON_MISMATCHING_DATA_TYPES(input1, input2); + + // Check variances + const int var_size = info.variances().size(); + if(var_size > 1) + { + ARM_COMPUTE_RETURN_ERROR_ON_MSG(var_size != 4, "Must provide 4 variance values"); + for(int i = 0; i < var_size; ++i) + { + ARM_COMPUTE_RETURN_ERROR_ON_MSG(var_size <= 0, "Must be greater than 0"); + } + } + ARM_COMPUTE_RETURN_ERROR_ON_MSG(info.steps()[0] < 0.f, "Step x should be greater or equal to 0"); + ARM_COMPUTE_RETURN_ERROR_ON_MSG(info.steps()[1] < 0.f, "Step y should be greater or equal to 0"); + + if(!info.max_sizes().empty()) + { + ARM_COMPUTE_RETURN_ERROR_ON_MSG(info.max_sizes().size() != info.min_sizes().size(), "Max and min sizes dimensions should match"); + } + + for(unsigned int i = 0; i < info.max_sizes().size(); ++i) + { + ARM_COMPUTE_RETURN_ERROR_ON_MSG(info.max_sizes()[i] < info.min_sizes()[i], "Max size should be greater than min size"); + } + + if(output != nullptr && output->total_size() != 0) + { + ARM_COMPUTE_RETURN_ERROR_ON(output->dimension(get_data_layout_dimension_index(input1->data_layout(), DataLayoutDimension::HEIGHT)) != 2); + ARM_COMPUTE_RETURN_ERROR_ON_MISMATCHING_DATA_LAYOUT(input1, output); + } + + return Status{}; +} + +std::pair<Status, Window> validate_and_configure_window(const ITensorInfo *input1, const ITensorInfo *input2, ITensorInfo *output, const PriorBoxLayerInfo &info) +{ + ARM_COMPUTE_UNUSED(input2); + + Window win = {}; + bool window_changed = false; + switch(input1->data_layout()) + { + case DataLayout::NCHW: + { + const int num_priors = info.aspect_ratios().size() * info.min_sizes().size() + info.max_sizes().size(); + const unsigned int num_elems_processed_per_iteration = 4 * num_priors; + win = calculate_max_window(*output, Steps(num_elems_processed_per_iteration)); + AccessWindowHorizontal output_access(output, 0, num_elems_processed_per_iteration); + window_changed = update_window_and_padding(win, output_access); + break; + } + case DataLayout::NHWC: + { + win = calculate_max_window(*output, Steps()); + break; + } + default: + ARM_COMPUTE_ERROR("Not implemented"); + }; + + Status err = (window_changed) ? ARM_COMPUTE_CREATE_ERROR(ErrorCode::RUNTIME_ERROR, "Insufficient Padding!") : Status{}; + return std::make_pair(err, win); +} +} // namespace + +NEPriorBoxLayerKernel::NEPriorBoxLayerKernel() + : _func(nullptr), _input1(nullptr), _input2(nullptr), _output(nullptr), _info() +{ +} + +template <DataLayout DL> +void NEPriorBoxLayerKernel::store_coordinates(float *out, const int offset, const float center_x, const float center_y, const float box_width, const float box_height, const int width, + const int height) +{ + float xmin = (center_x - box_width / 2.f) / width; + float ymin = (center_y - box_height / 2.f) / height; + float xmax = (center_x + box_width / 2.f) / width; + float ymax = (center_y + box_height / 2.f) / height; + + switch(DL) + { + case DataLayout::NCHW: + { + float32x4_t vec_elements = { xmin, ymin, xmax, ymax }; + if(_info.clip()) + { + static const float32x4_t CONST_0 = vdupq_n_f32(0.f); + static const float32x4_t CONST_1 = vdupq_n_f32(1.f); + vec_elements = vmaxq_f32(vminq_f32(vec_elements, CONST_1), CONST_0); + } + vst1q_f32(out + offset, vec_elements); + } + break; + case DataLayout::NHWC: + { + const int output_offset = _output->info()->strides_in_bytes()[1] / _output->info()->element_size(); + if(_info.clip()) + { + xmin = std::min(std::max(xmin, 0.f), 1.f); + ymin = std::min(std::max(ymin, 0.f), 1.f); + xmax = std::min(std::max(xmax, 0.f), 1.f); + ymax = std::min(std::max(ymax, 0.f), 1.f); + } + + *(out + output_offset * offset) = xmin; + *(out + output_offset * (offset + 1)) = ymin; + *(out + output_offset * (offset + 2)) = xmax; + *(out + output_offset * (offset + 3)) = ymax; + } + break; + default: + ARM_COMPUTE_ERROR("Not implemented"); + } +} + +template <DataLayout DL> +void NEPriorBoxLayerKernel::calculate_prior_boxes(const Window &window) +{ + const int num_priors = _info.aspect_ratios().size() * _info.min_sizes().size() + _info.max_sizes().size(); + + const int width_idx = get_data_layout_dimension_index(DL, DataLayoutDimension::WIDTH); + const int height_idx = get_data_layout_dimension_index(DL, DataLayoutDimension::HEIGHT); + + const int layer_width = _input1->info()->dimension(width_idx); + const int layer_height = _input1->info()->dimension(height_idx); + + int img_width = _info.img_size().x; + int img_height = _info.img_size().y; + if(img_width == 0 || img_height == 0) + { + img_width = _input2->info()->dimension(width_idx); + img_height = _input2->info()->dimension(height_idx); + } + + float step_x = _info.steps()[0]; + float step_y = _info.steps()[1]; + if(step_x == 0.f || step_y == 0.f) + { + step_x = static_cast<float>(img_width) / layer_width; + step_y = static_cast<float>(img_height) / layer_height; + } + + Window slice = {}; + + switch(DL) + { + case DataLayout::NCHW: + slice = window.first_slice_window_2D(); + slice.set(Window::DimY, Window::Dimension(0, _output->info()->dimension(1), 2)); + break; + case DataLayout::NHWC: + slice = window.first_slice_window_3D(); + slice.set(Window::DimY, Window::Dimension(0, _output->info()->dimension(1), 4 * num_priors)); + slice.set(Window::DimZ, Window::Dimension(0, _output->info()->dimension(2), 2)); + break; + default: + ARM_COMPUTE_ERROR("Not implemented"); + } + + Iterator output(_output, slice); + execute_window_loop(slice, [&](const Coordinates & id) + { + float center_x = 0; + float center_y = 0; + int idx = 0; + switch(DL) + { + case DataLayout::NCHW: + idx = id.x() / (4 * num_priors); + center_x = (static_cast<float>(idx % layer_width) + _info.offset()) * step_x; + center_y = (static_cast<float>(idx / layer_width) + _info.offset()) * step_y; + break; + case DataLayout::NHWC: + idx = id.y() / (4 * num_priors); + center_x = (static_cast<float>(idx % layer_width) + _info.offset()) * step_x; + center_y = (static_cast<float>(idx / layer_width) + _info.offset()) * step_y; + break; + default: + ARM_COMPUTE_ERROR("Not implemented"); + } + + float box_width; + float box_height; + int offset = 0; + + auto out = reinterpret_cast<float *>(output.ptr()); + for(unsigned int i = 0; i < _info.min_sizes().size(); ++i) + { + const float min_size = _info.min_sizes().at(i); + box_width = min_size; + box_height = min_size; + store_coordinates<DL>(out, offset, center_x, center_y, box_width, box_height, img_width, img_height); + offset += 4; + + if(!_info.max_sizes().empty()) + { + const float max_size = _info.max_sizes().at(i); + box_width = std::sqrt(min_size * max_size); + box_height = box_width; + + store_coordinates<DL>(out, offset, center_x, center_y, box_width, box_height, img_width, img_height); + offset += 4; + } + + // rest of priors + for(auto ar : _info.aspect_ratios()) + { + if(fabs(ar - 1.) < 1e-6) + { + continue; + } + + box_width = min_size * sqrt(ar); + box_height = min_size / sqrt(ar); + + store_coordinates<DL>(out, offset, center_x, center_y, box_width, box_height, img_width, img_height); + offset += 4; + } + } + + // set the variance + switch(DL) + { + case DataLayout::NCHW: + { + out = reinterpret_cast<float *>(_output->ptr_to_element(Coordinates(id.x(), 1))); + float32x4_t var; + if(_info.variances().size() == 1) + { + var = vdupq_n_f32(_info.variances().at(0)); + } + else + { + const float32x4_t vars = { _info.variances().at(0), _info.variances().at(1), _info.variances().at(2), _info.variances().at(3) }; + var = vars; + } + for(int i = 0; i < num_priors; ++i) + { + vst1q_f32(out + 4 * i, var); + } + } + break; + case DataLayout::NHWC: + { + for(int i = 0; i < num_priors; ++i) + { + const int prior_offset = 4 * i; + const bool single_var = _info.variances().size() == 1; + *(reinterpret_cast<float *>(_output->ptr_to_element(Coordinates(0, id.y() + prior_offset + 0, 1)))) = _info.variances().at(0); + *(reinterpret_cast<float *>(_output->ptr_to_element(Coordinates(0, id.y() + prior_offset + 1, 1)))) = single_var ? _info.variances().at(0) : _info.variances().at(1); + *(reinterpret_cast<float *>(_output->ptr_to_element(Coordinates(0, id.y() + prior_offset + 2, 1)))) = single_var ? _info.variances().at(0) : _info.variances().at(2); + *(reinterpret_cast<float *>(_output->ptr_to_element(Coordinates(0, id.y() + prior_offset + 3, 1)))) = single_var ? _info.variances().at(0) : _info.variances().at(3); + } + } + break; + default: + ARM_COMPUTE_ERROR("Not implemented"); + } + + }, + output); +} + +void NEPriorBoxLayerKernel::configure(const ITensor *input1, const ITensor *input2, ITensor *output, const PriorBoxLayerInfo &info) +{ + ARM_COMPUTE_ERROR_ON_NULLPTR(input1, input2, output); + + ARM_COMPUTE_ERROR_THROW_ON(validate_arguments(input1->info(), input2->info(), output->info(), info)); + + _input1 = input1; + _input2 = input2; + _info = info; + _output = output; + + switch(input1->info()->data_layout()) + { + case DataLayout::NCHW: + { + _func = &NEPriorBoxLayerKernel::calculate_prior_boxes<DataLayout::NCHW>; + break; + } + case DataLayout::NHWC: + { + _func = &NEPriorBoxLayerKernel::calculate_prior_boxes<DataLayout::NHWC>; + break; + } + default: + ARM_COMPUTE_ERROR("Not implemented."); + } + + // Configure kernel window + auto win_config = validate_and_configure_window(input1->info(), input2->info(), output->info(), info); + ARM_COMPUTE_ERROR_THROW_ON(win_config.first); + INEKernel::configure(win_config.second); +} + +Status NEPriorBoxLayerKernel::validate(const ITensorInfo *input1, const ITensorInfo *input2, const ITensorInfo *output, const PriorBoxLayerInfo &info) +{ + ARM_COMPUTE_RETURN_ERROR_ON_NULLPTR(input1, input2, output); + ARM_COMPUTE_RETURN_ON_ERROR(validate_arguments(input1, input2, output, info)); + ARM_COMPUTE_RETURN_ON_ERROR(validate_and_configure_window(input1->clone().get(), input2->clone().get(), output->clone().get(), info) + .first); + + return Status{}; +} +void NEPriorBoxLayerKernel::run(const Window &window, const ThreadInfo &info) +{ + ARM_COMPUTE_UNUSED(info); + ARM_COMPUTE_ERROR_ON_UNCONFIGURED_KERNEL(this); + ARM_COMPUTE_ERROR_ON_INVALID_SUBWINDOW(INEKernel::window(), window); + ARM_COMPUTE_ERROR_ON(_func == nullptr); + + // Run function + (this->*_func)(window); +} +} // namespace arm_compute
\ No newline at end of file diff --git a/src/runtime/NEON/functions/NEPriorBoxLayer.cpp b/src/runtime/NEON/functions/NEPriorBoxLayer.cpp new file mode 100644 index 0000000000..6e7d4ab667 --- /dev/null +++ b/src/runtime/NEON/functions/NEPriorBoxLayer.cpp @@ -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. + */ + +#include "arm_compute/runtime/NEON/functions/NEPriorBoxLayer.h" + +#include "arm_compute/core/Error.h" +#include "arm_compute/core/TensorInfo.h" +#include "arm_compute/core/Types.h" +#include "arm_compute/core/Validate.h" +#include "arm_compute/core/utils/misc/ShapeCalculator.h" +#include "arm_compute/runtime/NEON/NEScheduler.h" + +namespace arm_compute +{ +void NEPriorBoxLayer::configure(const ITensor *input1, const ITensor *input2, ITensor *output, const PriorBoxLayerInfo &info) +{ + auto k = arm_compute::support::cpp14::make_unique<NEPriorBoxLayerKernel>(); + k->configure(input1, input2, output, info); + _kernel = std::move(k); +} + +Status NEPriorBoxLayer::validate(const ITensorInfo *input1, const ITensorInfo *input2, const ITensorInfo *output, const PriorBoxLayerInfo &info) +{ + return NEPriorBoxLayerKernel::validate(input1, input2, output, info); +} +} // namespace arm_compute diff --git a/tests/datasets/PriorBoxLayerDataset.h b/tests/datasets/PriorBoxLayerDataset.h index 22a0d97057..c63e941171 100644 --- a/tests/datasets/PriorBoxLayerDataset.h +++ b/tests/datasets/PriorBoxLayerDataset.h @@ -110,7 +110,6 @@ public: std::vector<float> max_val = { 60.f }; std::vector<float> aspect_ratio = { 2.f }; std::array<float, 2> steps = { 8.f, 8.f }; - add_config(TensorShape(4U, 4U), PriorBoxLayerInfo(min_val, var, 0.5f, true, false, max_val, aspect_ratio, Coordinates2D{ 8, 8 }, steps)); } }; diff --git a/tests/validation/NEON/PriorBoxLayer.cpp b/tests/validation/NEON/PriorBoxLayer.cpp new file mode 100644 index 0000000000..ed11120a10 --- /dev/null +++ b/tests/validation/NEON/PriorBoxLayer.cpp @@ -0,0 +1,100 @@ +/* + * 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/Types.h" +#include "arm_compute/runtime/NEON/functions/NEPriorBoxLayer.h" +#include "arm_compute/runtime/Tensor.h" +#include "arm_compute/runtime/TensorAllocator.h" +#include "tests/NEON/Accessor.h" +#include "tests/datasets/PriorBoxLayerDataset.h" +#include "tests/framework/Asserts.h" +#include "tests/framework/Macros.h" +#include "tests/framework/datasets/Datasets.h" +#include "tests/validation/Validation.h" +#include "tests/validation/fixtures/PriorBoxLayerFixture.h" + +namespace arm_compute +{ +namespace test +{ +namespace validation +{ +namespace +{ +constexpr AbsoluteTolerance<float> tolerance_f32(0.00001f); /**< Tolerance value for comparing reference's output against implementation's output for DataType::F32 */ +} // namespace + +TEST_SUITE(NEON) +TEST_SUITE(PriorBoxLayer) + +template <typename T> +using NEPriorBoxLayerFixture = PriorBoxLayerValidationFixture<Tensor, Accessor, NEPriorBoxLayer, T>; + +// *INDENT-OFF* +// clang-format off +DATA_TEST_CASE(Validate, framework::DatasetMode::ALL, zip(zip(zip(zip( + framework::dataset::make("Input1Info", { TensorInfo(TensorShape(10U, 10U, 2U), 1, DataType::F32), + TensorInfo(TensorShape(10U, 10U, 2U), 1, DataType::F32), // Window shrink + }), + framework::dataset::make("Input2Info", { TensorInfo(TensorShape(10U, 10U, 2U), 1, DataType::F32), + TensorInfo(TensorShape(10U, 10U, 2U), 1, DataType::F32), + })), + framework::dataset::make("OutputInfo",{ TensorInfo(TensorShape(1200U, 2U), 1, DataType::F32), + TensorInfo(TensorShape(1000U, 2U), 1, DataType::F32), + })), + framework::dataset::make("PriorBoxInfo",{ PriorBoxLayerInfo(std::vector<float>(1), std::vector<float>(1), 0, true, true, std::vector<float>(1), std::vector<float>(1), Coordinates2D{8, 8}, std::array<float, 2>()), + PriorBoxLayerInfo(std::vector<float>(1), std::vector<float>(1), 0, true, true, std::vector<float>(1), std::vector<float>(1), Coordinates2D{8, 8}, std::array<float, 2>()), + })), + framework::dataset::make("Expected", { true, false})), + input1_info, input2_info, output_info, info, expected) +{ + bool has_error = bool(NEPriorBoxLayer::validate(&input1_info.clone()->set_is_resizable(false), &input2_info.clone()->set_is_resizable(false), &output_info.clone()->set_is_resizable(false), info)); + ARM_COMPUTE_EXPECT(has_error == expected, framework::LogLevel::ERRORS); +} +// clang-format on +// *INDENT-ON* + +TEST_SUITE(Float) +TEST_SUITE(FP32) +FIXTURE_DATA_TEST_CASE(RunSmall, NEPriorBoxLayerFixture<float>, framework::DatasetMode::PRECOMMIT, combine(combine(datasets::SmallPriorBoxLayerDataset(), + framework::dataset::make("DataType", DataType::F32)), + framework::dataset::make("DataLayout", { DataLayout::NCHW, DataLayout::NHWC }))) +{ + // Validate output + validate(Accessor(_target), _reference, tolerance_f32, 0); +} +FIXTURE_DATA_TEST_CASE(RunLarge, NEPriorBoxLayerFixture<float>, framework::DatasetMode::NIGHTLY, combine(combine(datasets::LargePriorBoxLayerDataset(), + framework::dataset::make("DataType", DataType::F32)), + framework::dataset::make("DataLayout", { DataLayout::NCHW, DataLayout::NHWC }))) +{ + // Validate output + validate(Accessor(_target), _reference, tolerance_f32, 0); +} +TEST_SUITE_END() // Float +TEST_SUITE_END() // FP32 + +TEST_SUITE_END() // PriorBoxLayer +TEST_SUITE_END() // NEON +} // namespace validation +} // namespace test +} // namespace arm_compute |