From 05398a948a2b43584b16d91f6efdda9eb361ec74 Mon Sep 17 00:00:00 2001 From: George Wort Date: Fri, 25 Jan 2019 15:38:33 +0000 Subject: COMPMID-1843: Implement NECrop Change-Id: I27e8b1a00c2315c72106e8e596f84ad48fb770e3 Signed-off-by: George Wort Reviewed-on: https://review.mlplatform.org/c/648 Tested-by: Arm Jenkins Reviewed-by: Pablo Marquez --- tests/datasets/CropResizeDataset.h | 141 ++++++++++++++++++ tests/validation/NEON/CropResize.cpp | 184 ++++++++++++++++++++++++ tests/validation/fixtures/CropResizeFixture.h | 139 ++++++++++++++++++ tests/validation/reference/CropResize.cpp | 199 ++++++++++++++++++++++++++ tests/validation/reference/CropResize.h | 44 ++++++ 5 files changed, 707 insertions(+) create mode 100644 tests/datasets/CropResizeDataset.h create mode 100644 tests/validation/NEON/CropResize.cpp create mode 100644 tests/validation/fixtures/CropResizeFixture.h create mode 100644 tests/validation/reference/CropResize.cpp create mode 100644 tests/validation/reference/CropResize.h (limited to 'tests') diff --git a/tests/datasets/CropResizeDataset.h b/tests/datasets/CropResizeDataset.h new file mode 100644 index 0000000000..8cee094fc8 --- /dev/null +++ b/tests/datasets/CropResizeDataset.h @@ -0,0 +1,141 @@ +/* + * Copyright (c) 2019 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_CROP_RESIZE_DATASET +#define ARM_COMPUTE_TEST_CROP_RESIZE_DATASET + +#include "utils/TypePrinter.h" + +#include "arm_compute/core/Types.h" + +namespace arm_compute +{ +namespace test +{ +namespace datasets +{ +class CropResizeDataset +{ +public: + using type = std::tuple; + + struct iterator + { + iterator(std::vector::const_iterator src_shapes_it, + std::vector::const_iterator boxes_shapes_it, + std::vector::const_iterator crop_size_values_it, + std::vector::const_iterator method_values_it, + std::vector::const_iterator extrapolation_values_it) + : _src_shapes_it{ std::move(src_shapes_it) }, + _boxes_shapes_it{ std::move(boxes_shapes_it) }, + _crop_size_values_it{ std::move(crop_size_values_it) }, + _method_values_it{ std::move(method_values_it) }, + _extrapolation_values_it{ std::move(extrapolation_values_it) } + { + } + + std::string description() const + { + std::stringstream description; + description << "Src_Shape=" << *_src_shapes_it << ":"; + description << "Boxes_Shape=" << *_boxes_shapes_it << ":"; + description << "Crop_Size=(" << (*_crop_size_values_it).x << "," << (*_crop_size_values_it).y << "):"; + description << "Method=" << *_method_values_it << ":"; + description << "Extrapolation_value=" << *_extrapolation_values_it << ":"; + return description.str(); + } + + CropResizeDataset::type operator*() const + { + return std::make_tuple(*_src_shapes_it, *_boxes_shapes_it, *_crop_size_values_it, *_method_values_it, *_extrapolation_values_it); + } + + iterator &operator++() + { + ++_src_shapes_it; + ++_boxes_shapes_it; + ++_crop_size_values_it; + ++_method_values_it; + ++_extrapolation_values_it; + return *this; + } + + private: + std::vector::const_iterator _src_shapes_it; + std::vector::const_iterator _boxes_shapes_it; + std::vector::const_iterator _crop_size_values_it; + std::vector::const_iterator _method_values_it; + std::vector::const_iterator _extrapolation_values_it; + }; + + iterator begin() const + { + return iterator(_src_shapes.begin(), _boxes_shapes.begin(), _crop_size_values.begin(), _method_values.begin(), _extrapolation_values.begin()); + } + + int size() const + { + return std::min(_src_shapes.size(), std::min(_boxes_shapes.size(), std::min(_crop_size_values.size(), std::min(_method_values.size(), _extrapolation_values.size())))); + } + + void add_config(TensorShape src_shape, TensorShape boxes_shape, Coordinates2D crop_size, InterpolationPolicy method, float extrapolation_value) + { + _src_shapes.emplace_back(std::move(src_shape)); + _boxes_shapes.emplace_back(std::move(boxes_shape)); + _crop_size_values.emplace_back(std::move(crop_size)); + _method_values.emplace_back(std::move(method)); + _extrapolation_values.emplace_back(std::move(extrapolation_value)); + } + +protected: + CropResizeDataset() = default; + CropResizeDataset(CropResizeDataset &&) = default; + +private: + std::vector _src_shapes{}; + std::vector _boxes_shapes{}; + std::vector _crop_size_values{}; + std::vector _method_values{}; + std::vector _extrapolation_values{}; +}; + +class SmallCropResizeDataset final : public CropResizeDataset +{ +public: + SmallCropResizeDataset() + { + add_config(TensorShape(1U, 5U, 5U), TensorShape(4, 5), Coordinates2D{ 2, 2 }, InterpolationPolicy::BILINEAR, 100); + add_config(TensorShape(3U, 5U, 5U), TensorShape(4, 5), Coordinates2D{ 2, 2 }, InterpolationPolicy::BILINEAR, 100); + add_config(TensorShape(1U, 5U, 5U), TensorShape(4, 5), Coordinates2D{ 10, 10 }, InterpolationPolicy::BILINEAR, 100); + add_config(TensorShape(15U, 30U, 30U, 10U), TensorShape(4, 20), Coordinates2D{ 10, 10 }, InterpolationPolicy::BILINEAR, 100); + + add_config(TensorShape(1U, 5U, 5U), TensorShape(4, 5), Coordinates2D{ 2, 2 }, InterpolationPolicy::NEAREST_NEIGHBOR, 100); + add_config(TensorShape(3U, 5U, 5U), TensorShape(4, 5), Coordinates2D{ 2, 2 }, InterpolationPolicy::NEAREST_NEIGHBOR, 100); + add_config(TensorShape(1U, 5U, 5U), TensorShape(4, 5), Coordinates2D{ 10, 10 }, InterpolationPolicy::NEAREST_NEIGHBOR, 100); + add_config(TensorShape(15U, 30U, 30U, 10U), TensorShape(4, 20), Coordinates2D{ 10, 10 }, InterpolationPolicy::NEAREST_NEIGHBOR, 100); + } +}; +} // namespace datasets +} // namespace test +} // namespace arm_compute +#endif /* ARM_COMPUTE_TEST_CROP_RESIZE_DATASET */ \ No newline at end of file diff --git a/tests/validation/NEON/CropResize.cpp b/tests/validation/NEON/CropResize.cpp new file mode 100644 index 0000000000..1feed3d9d2 --- /dev/null +++ b/tests/validation/NEON/CropResize.cpp @@ -0,0 +1,184 @@ +/* + * Copyright (c) 2019 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/NECropResize.h" +#include "arm_compute/runtime/Tensor.h" +#include "arm_compute/runtime/TensorAllocator.h" + +#include "tests/NEON/Accessor.h" +#include "tests/datasets/CropResizeDataset.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/CropResizeFixture.h" + +namespace arm_compute +{ +namespace test +{ +namespace validation +{ +TEST_SUITE(NEON) +TEST_SUITE(CropResize) + +RelativeTolerance tolerance_fp32(0.001f); + +template +using NECropResizeFixture = CropResizeFixture; + +// *INDENT-OFF* +// clang-format off +DATA_TEST_CASE(Validate, framework::DatasetMode::ALL, zip(zip(zip(zip( + framework::dataset::make("InputInfo", { TensorInfo(TensorShape(15U, 30U, 40U, 10U), 1, DataType::S32), + TensorInfo(TensorShape(15U, 30U, 40U, 10U), 1, DataType::U8), // Invalid input data type. + TensorInfo(TensorShape(15U, 30U, 40U, 10U), 1, DataType::S32), // Invalid box_ind shape. + TensorInfo(TensorShape(15U, 30U, 40U, 10U), 1, DataType::S32), // Invalid output shape. + TensorInfo(TensorShape(15U, 30U, 40U, 10U), 1, DataType::S32), // Invalid output data type. + TensorInfo(TensorShape(15U, 30U, 40U, 10U), 1, DataType::S32), // Invalid output shape. + TensorInfo(TensorShape(15U, 30U, 40U, 10U), 1, DataType::S32), // Invalid boxes shape. + }), + framework::dataset::make("BoxesInfo",{ TensorInfo(TensorShape(4, 20), 1, DataType::F32), + TensorInfo(TensorShape(4, 20), 1, DataType::F32), + TensorInfo(TensorShape(4, 20), 1, DataType::F32), + TensorInfo(TensorShape(4, 20), 1, DataType::F32), + TensorInfo(TensorShape(4, 20), 1, DataType::F32), + TensorInfo(TensorShape(4, 20), 1, DataType::F32), + TensorInfo(TensorShape(3, 20), 1, DataType::F32), + })), + framework::dataset::make("BoxIndInfo",{ TensorInfo(TensorShape(20), 1, DataType::S32), + TensorInfo(TensorShape(20), 1, DataType::S32), + TensorInfo(TensorShape(10), 1, DataType::S32), + TensorInfo(TensorShape(20), 1, DataType::S32), + TensorInfo(TensorShape(20), 1, DataType::S32), + TensorInfo(TensorShape(20), 1, DataType::S32), + TensorInfo(TensorShape(20), 1, DataType::S32), + })), + framework::dataset::make("OutputInfo",{ TensorInfo(TensorShape(15U, 5, 5, 20U), 1, DataType::F32), + TensorInfo(TensorShape(15U, 5, 5, 20U), 1, DataType::F32), + TensorInfo(TensorShape(15U, 5, 5, 20U), 1, DataType::F32), + TensorInfo(TensorShape(15U, 5, 5, 10U), 1, DataType::F32), + TensorInfo(TensorShape(15U, 5, 5, 20U), 1, DataType::S32), + TensorInfo(TensorShape(5U, 5, 5, 20U), 1, DataType::F32), + TensorInfo(TensorShape(15U, 5, 5, 20U), 1, DataType::F32), + })), + framework::dataset::make("Expected", { true, false, false, false, false, false, false})), + input, boxes, box_ind, output, expected) +{ + ARM_COMPUTE_EXPECT(bool(NECropResize::validate(&input.clone()->set_data_layout(DataLayout::NHWC).set_is_resizable(false), + &boxes.clone()->set_is_resizable(false), + &box_ind.clone()->set_is_resizable(false), + &output.clone()->set_data_layout(DataLayout::NHWC).set_is_resizable(false), + Coordinates2D{ 5, 5 }, InterpolationPolicy::BILINEAR, 100)) == expected, framework::LogLevel::ERRORS); +} +// clang-format on +// *INDENT-ON* + +TEST_SUITE(Float) +#ifdef __ARM_FEATURE_FP16_VECTOR_ARITHMETIC +TEST_SUITE(F16) +FIXTURE_DATA_TEST_CASE(RunSmall, + NECropResizeFixture, + framework::DatasetMode::PRECOMMIT, + combine(datasets::SmallCropResizeDataset(), + combine(framework::dataset::make("IsOutOfBounds", { true, false }), + framework::dataset::make("DataType", DataType::F16)))) +{ + // Validate output + validate(Accessor(_target), _reference, tolerance_fp32, 0.01); +} +TEST_SUITE_END() // F16 +#endif /* __ARM_FEATURE_FP16_VECTOR_ARITHMETIC */ + +TEST_SUITE(F32) +FIXTURE_DATA_TEST_CASE(RunSmall, + NECropResizeFixture, + framework::DatasetMode::PRECOMMIT, + combine(datasets::SmallCropResizeDataset(), + combine(framework::dataset::make("IsOutOfBounds", { true, false }), + framework::dataset::make("DataType", DataType::F32)))) +{ + // Validate output + validate(Accessor(_target), _reference, tolerance_fp32, 0.01); +} +TEST_SUITE_END() // F32 +TEST_SUITE_END() // Float + +TEST_SUITE(U16) +FIXTURE_DATA_TEST_CASE(RunSmall, + NECropResizeFixture, + framework::DatasetMode::PRECOMMIT, + combine(datasets::SmallCropResizeDataset(), + combine(framework::dataset::make("IsOutOfBounds", { true, false }), + framework::dataset::make("DataType", DataType::U16)))) +{ + // Validate output + validate(Accessor(_target), _reference, tolerance_fp32, 0.01); +} +TEST_SUITE_END() // U16 + +TEST_SUITE(S16) +FIXTURE_DATA_TEST_CASE(RunSmall, + NECropResizeFixture, + framework::DatasetMode::PRECOMMIT, + combine(datasets::SmallCropResizeDataset(), + combine(framework::dataset::make("IsOutOfBounds", { true, false }), + framework::dataset::make("DataType", DataType::S16)))) +{ + // Validate output + validate(Accessor(_target), _reference, tolerance_fp32, 0.01); +} +TEST_SUITE_END() // S16 + +TEST_SUITE(U32) +FIXTURE_DATA_TEST_CASE(RunSmall, + NECropResizeFixture, + framework::DatasetMode::PRECOMMIT, + combine(datasets::SmallCropResizeDataset(), + combine(framework::dataset::make("IsOutOfBounds", { true, false }), + framework::dataset::make("DataType", DataType::U32)))) +{ + // Validate output + validate(Accessor(_target), _reference, tolerance_fp32, 0.01); +} +TEST_SUITE_END() // U32 + +TEST_SUITE(S32) +FIXTURE_DATA_TEST_CASE(RunSmall, + NECropResizeFixture, + framework::DatasetMode::PRECOMMIT, + combine(datasets::SmallCropResizeDataset(), + combine(framework::dataset::make("IsOutOfBounds", { true, false }), + framework::dataset::make("DataType", DataType::S32)))) +{ + // Validate output + validate(Accessor(_target), _reference, tolerance_fp32, 0.01); +} +TEST_SUITE_END() // S32 + +TEST_SUITE_END() // CropResize +TEST_SUITE_END() // NEON +} // namespace validation +} // namespace test +} // namespace arm_compute diff --git a/tests/validation/fixtures/CropResizeFixture.h b/tests/validation/fixtures/CropResizeFixture.h new file mode 100644 index 0000000000..d83c4113f5 --- /dev/null +++ b/tests/validation/fixtures/CropResizeFixture.h @@ -0,0 +1,139 @@ +/* + * Copyright (c) 2019 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_SLICE_OPERATIONS_FIXTURE +#define ARM_COMPUTE_TEST_SLICE_OPERATIONS_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/RawLutAccessor.h" +#include "tests/framework/Asserts.h" +#include "tests/framework/Fixture.h" +#include "tests/validation/Helpers.h" +#include "tests/validation/reference/CropResize.h" +#include "tests/validation/reference/Permute.h" + +namespace arm_compute +{ +namespace test +{ +namespace validation +{ +template +class CropResizeFixture : public framework::Fixture +{ +public: + template + void setup(TensorShape src_shape, TensorShape boxes_shape, Coordinates2D crop_size, InterpolationPolicy method, + float extrapolation_value, bool is_outside_bounds, DataType data_type) + { + _target = compute_target(src_shape, boxes_shape, crop_size, method, extrapolation_value, is_outside_bounds, data_type); + _reference = compute_reference(src_shape, boxes_shape, crop_size, method, extrapolation_value, is_outside_bounds, data_type); + } + +protected: + template + void fill(U &&tensor, int i) + { + library->fill_tensor_uniform(tensor, i); + } + + template + void fill(U &&tensor, int i, V min, V max) + { + library->fill_tensor_uniform(tensor, i, min, max); + } + + TensorType compute_target(const TensorShape &src_shape, const TensorShape &boxes_shape, const Coordinates2D &crop_size, InterpolationPolicy method, + float extrapolation_value, bool is_outside_bounds, DataType data_type) + { + TensorShape dst_shape(src_shape[0], crop_size.x, crop_size.y, boxes_shape[1]); + + // Create tensors + TensorType src = create_tensor(src_shape, data_type, 1, QuantizationInfo(), DataLayout::NHWC); + TensorType boxes = create_tensor(boxes_shape, DataType::F32); + TensorType boxes_ind = create_tensor(TensorShape(boxes_shape[1]), DataType::S32); + TensorType dst = create_tensor(dst_shape, DataType::F32, 1, QuantizationInfo(), DataLayout::NHWC); + + // Create and configure function + FunctionType crop; + crop.configure(&src, &boxes, &boxes_ind, &dst, crop_size, method, extrapolation_value); + + ARM_COMPUTE_EXPECT(src.info()->is_resizable(), framework::LogLevel::ERRORS); + ARM_COMPUTE_EXPECT(boxes.info()->is_resizable(), framework::LogLevel::ERRORS); + ARM_COMPUTE_EXPECT(boxes_ind.info()->is_resizable(), framework::LogLevel::ERRORS); + ARM_COMPUTE_EXPECT(dst.info()->is_resizable(), framework::LogLevel::ERRORS); + + // Allocate tensors + src.allocator()->allocate(); + boxes.allocator()->allocate(); + boxes_ind.allocator()->allocate(); + dst.allocator()->allocate(); + + ARM_COMPUTE_EXPECT(!src.info()->is_resizable(), framework::LogLevel::ERRORS); + ARM_COMPUTE_EXPECT(!boxes.info()->is_resizable(), framework::LogLevel::ERRORS); + ARM_COMPUTE_EXPECT(!boxes_ind.info()->is_resizable(), framework::LogLevel::ERRORS); + ARM_COMPUTE_EXPECT(!dst.info()->is_resizable(), framework::LogLevel::ERRORS); + + // Fill tensors + fill(AccessorType(src), 0); + fill(AccessorType(boxes), 1, is_outside_bounds ? 0.0f - out_of_bounds_reach : 0.0f, is_outside_bounds ? 1.0f + out_of_bounds_reach : 1.0f); + fill(AccessorType(boxes_ind), 2, 0, static_cast(src_shape[3] - 1)); + + // Compute function + crop.run(); + return dst; + } + + SimpleTensor compute_reference(const TensorShape &src_shape, const TensorShape &boxes_shape, const Coordinates2D &crop_size, InterpolationPolicy method, + float extrapolation_value, bool is_outside_bounds, DataType data_type) + { + // Create reference + SimpleTensor src{ src_shape, data_type, 1, QuantizationInfo(), DataLayout::NHWC }; + SimpleTensor boxes{ boxes_shape, DataType::F32 }; + SimpleTensor boxes_ind{ TensorShape(boxes_shape[1]), DataType::S32 }; + + // Fill reference + fill(src, 0); + fill(boxes, 1, is_outside_bounds ? 0.0f - out_of_bounds_reach : 0.0f, is_outside_bounds ? 1.0f + out_of_bounds_reach : 1.0f); + fill(boxes_ind, 2, 0, static_cast(src.shape()[3] - 1)); + + SimpleTensor output = reference::crop_and_resize(src, boxes, boxes_ind, crop_size, method, extrapolation_value); + + SimpleTensor permuted = reference::permute(output, PermutationVector(1, 2U, 0U)); + return permuted; + } + + constexpr static float out_of_bounds_reach = 2.0f; + + TensorType _target{}; + SimpleTensor _reference{}; +}; +} // namespace validation +} // namespace test +} // namespace arm_compute +#endif /* ARM_COMPUTE_TEST_SLICE_OPERATIONS_FIXTURE */ diff --git a/tests/validation/reference/CropResize.cpp b/tests/validation/reference/CropResize.cpp new file mode 100644 index 0000000000..8cfce97eec --- /dev/null +++ b/tests/validation/reference/CropResize.cpp @@ -0,0 +1,199 @@ +/* + * Copyright (c) 2019 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 "CropResize.h" +#include "Utils.h" + +namespace arm_compute +{ +namespace test +{ +namespace validation +{ +namespace reference +{ +namespace +{ +SimpleTensor scale_image(const SimpleTensor &in, const TensorShape &out_shape, InterpolationPolicy policy, float extrapolation_value) +{ + ARM_COMPUTE_ERROR_ON(in.data_layout() != DataLayout::NHWC); + + SimpleTensor out{ out_shape, DataType::F32, 1, QuantizationInfo(), DataLayout::NHWC }; + // Compute the ratio between source width/height and destination width/height + const auto wr = static_cast(in.shape()[1]) / static_cast(out_shape[1]); + const auto hr = static_cast(in.shape()[2]) / static_cast(out_shape[2]); + + const auto width = static_cast(in.shape().y()); + const auto height = static_cast(in.shape().z()); + + Window win; + win.use_tensor_dimensions(out_shape); + execute_window_loop(win, [&](const Coordinates & out_id) + { + Coordinates in_id(out_id); + int idw = in_id.y(); + int idh = in_id.z(); + + switch(policy) + { + case InterpolationPolicy::NEAREST_NEIGHBOR: + { + //Calculate the source coords without -0.5f is equivalent to round the x_scr/y_src coords + float x_src = (idw + 0.5f) * wr; + float y_src = (idh + 0.5f) * hr; + in_id.set(1, x_src); + in_id.set(2, y_src); + + // If coordinates in range of tensor's width or height + if(is_valid_pixel_index(x_src, y_src, width, height, 0)) + { + *reinterpret_cast(out(out_id)) = tensor_elem_at(in, in_id, BorderMode::CONSTANT, extrapolation_value); + } + else + { + *reinterpret_cast(out(out_id)) = extrapolation_value; + } + break; + } + case InterpolationPolicy::BILINEAR: + { + float x_src = idw * wr; + float y_src = idh * hr; + in_id.set(1, std::floor(x_src)); + in_id.set(2, std::floor(y_src)); + if(is_valid_pixel_index(x_src, y_src, width, height, 0)) + { + const int id_w = in_id[1]; + const int id_h = in_id[2]; + + const float dx = x_src - id_w; + const float dy = y_src - id_h; + const float dx_1 = 1.0f - dx; + const float dy_1 = 1.0f - dy; + + in_id.set(1, id_w); + in_id.set(2, id_h); + const float tl = tensor_elem_at(in, in_id, BorderMode::CONSTANT, extrapolation_value); + in_id.set(1, id_w + 1); + in_id.set(2, id_h); + const float tr = tensor_elem_at(in, in_id, BorderMode::CONSTANT, extrapolation_value); + in_id.set(1, id_w); + in_id.set(2, id_h + 1); + const float bl = tensor_elem_at(in, in_id, BorderMode::CONSTANT, extrapolation_value); + in_id.set(1, id_w + 1); + in_id.set(2, id_h + 1); + const float br = tensor_elem_at(in, in_id, BorderMode::CONSTANT, extrapolation_value); + + *reinterpret_cast(out(out_id)) = tl * (dx_1 * dy_1) + tr * (dx * dy_1) + bl * (dx_1 * dy) + br * (dx * dy); + } + else + { + *reinterpret_cast(out(out_id)) = extrapolation_value; + } + break; + } + default: + ARM_COMPUTE_ERROR("Unsupported interpolation mode"); + } + }); + + return out; +} + +template +SimpleTensor crop_image(const SimpleTensor &src, Coordinates start, Coordinates end, int32_t batch_index, float extrapolation_value) +{ + TensorShape out_shape(src.shape()[0], abs(end[0] - start[0]) + 1, abs(end[1] - start[1]) + 1); + + SimpleTensor out{ out_shape, DataType::F32, 1, QuantizationInfo(), DataLayout::NHWC }; + + Window win; + win.use_tensor_dimensions(out_shape); + execute_window_loop(win, [&](const Coordinates & id) + { + bool out_of_bounds = false; + Coordinates offset(id[0], 0, 0, batch_index); + for(uint32_t i = 1; i < 3; ++i) + { + offset.set(i, end[i - 1] < start[i - 1] ? start[i - 1] - id[i] : start[i - 1] + id[i]); + if(offset[i] < 0 || static_cast(offset[i]) > src.shape()[i] - 1) + { + out_of_bounds = true; + break; + } + } + if(!out_of_bounds) + { + *reinterpret_cast(out(id)) = static_cast(*reinterpret_cast(src(offset))); + } + else + { + *reinterpret_cast(out(id)) = extrapolation_value; + } + }); + return out; +} + +} // namespace + +template +SimpleTensor crop_and_resize(const SimpleTensor &src, const SimpleTensor &boxes, SimpleTensor box_ind, + Coordinates2D crop_size, InterpolationPolicy method, float extrapolation_value) +{ + ARM_COMPUTE_ERROR_ON(src.shape().num_dimensions() > 4); + ARM_COMPUTE_ERROR_ON(src.data_layout() != DataLayout::NHWC); + + const TensorShape out_shape(src.shape()[0], crop_size.x, crop_size.y, boxes.shape()[1]); + SimpleTensor out{ out_shape, DataType::F32, 1, QuantizationInfo(), DataLayout::NHWC }; + + const TensorShape scaled_image_shape(src.shape()[0], crop_size.x, crop_size.y); + + for(uint32_t i = 0; i < boxes.shape()[1]; ++i) + { + Coordinates start = Coordinates(std::floor((*reinterpret_cast(boxes(Coordinates(1, i)))) * (src.shape()[1] - 1) + 0.5f), + std::floor((*reinterpret_cast(boxes(Coordinates(0, i)))) * (src.shape()[2] - 1) + 0.5f)); + Coordinates end = Coordinates(std::floor((*reinterpret_cast(boxes(Coordinates(3, i)))) * (src.shape()[1] - 1) + 0.5f), + std::floor((*reinterpret_cast(boxes(Coordinates(2, i)))) * (src.shape()[2] - 1) + 0.5f)); + SimpleTensor cropped = crop_image(src, start, end, box_ind[i], extrapolation_value); + SimpleTensor scaled = scale_image(cropped, scaled_image_shape, method, extrapolation_value); + std::copy_n(reinterpret_cast(scaled.data()), scaled.num_elements(), reinterpret_cast(out(Coordinates(0, 0, 0, i)))); + } + return out; +} + +template SimpleTensor crop_and_resize(const SimpleTensor &src, const SimpleTensor &boxes, SimpleTensor box_ind, + Coordinates2D crop_size, InterpolationPolicy method, float extrapolation_value); +template SimpleTensor crop_and_resize(const SimpleTensor &src, const SimpleTensor &boxes, SimpleTensor box_ind, + Coordinates2D crop_size, InterpolationPolicy method, float extrapolation_value); +template SimpleTensor crop_and_resize(const SimpleTensor &src, const SimpleTensor &boxes, SimpleTensor box_ind, + Coordinates2D crop_size, InterpolationPolicy method, float extrapolation_value); +template SimpleTensor crop_and_resize(const SimpleTensor &src, const SimpleTensor &boxes, SimpleTensor box_ind, + Coordinates2D crop_size, InterpolationPolicy method, float extrapolation_value); +template SimpleTensor crop_and_resize(const SimpleTensor &src, const SimpleTensor &boxes, SimpleTensor box_ind, + Coordinates2D crop_size, InterpolationPolicy method, float extrapolation_value); +template SimpleTensor crop_and_resize(const SimpleTensor &src, const SimpleTensor &boxes, SimpleTensor box_ind, + Coordinates2D crop_size, InterpolationPolicy method, float extrapolation_value); +} // namespace reference +} // namespace validation +} // namespace test +} // namespace arm_compute diff --git a/tests/validation/reference/CropResize.h b/tests/validation/reference/CropResize.h new file mode 100644 index 0000000000..517c24bd32 --- /dev/null +++ b/tests/validation/reference/CropResize.h @@ -0,0 +1,44 @@ +/* + * Copyright (c) 2019 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_CROP_RESIZE_H__ +#define __ARM_COMPUTE_TEST_CROP_RESIZE_H__ + +#include "tests/SimpleTensor.h" + +namespace arm_compute +{ +namespace test +{ +namespace validation +{ +namespace reference +{ +template +SimpleTensor crop_and_resize(const SimpleTensor &src, const SimpleTensor &boxes, SimpleTensor box_ind, + Coordinates2D crop_size, InterpolationPolicy method, float extrapolation_value); +} // namespace reference +} // namespace validation +} // namespace test +} // namespace arm_compute +#endif /* __ARM_COMPUTE_TEST_CROP_RESIZE_H__ */ -- cgit v1.2.1