From f5f34bb068565bf9435ba5561aae1c9280db8bbe Mon Sep 17 00:00:00 2001 From: Pablo Tello Date: Tue, 22 Aug 2017 13:34:13 +0100 Subject: COMPMID-470: Neon Deconvolution. Implemented by up-sampling the input with zeros insertions between the input samples and convolving the Deconvolution kernels on the up-sampled result. The upsampling is performed by the function NEDeconvolutionLayerUpsample. Convolving is done by NEDirectConvolutionLayer. Change-Id: I25f7ba7c6b99cd9310797972ede40aeff4a54900 Reviewed-on: http://mpd-gerrit.cambridge.arm.com/85319 Tested-by: Kaizen Reviewed-by: Anthony Barbier --- tests/datasets/ShapeDatasets.h | 15 ++ tests/validation/CPP/DeconvolutionLayer.cpp | 108 +++++++++++++ tests/validation/CPP/DeconvolutionLayer.h | 55 +++++++ tests/validation/NEON/DeconvolutionLayer.cpp | 95 ++++++++++++ .../fixtures/DeconvolutionLayerFixture.h | 168 +++++++++++++++++++++ 5 files changed, 441 insertions(+) create mode 100644 tests/validation/CPP/DeconvolutionLayer.cpp create mode 100644 tests/validation/CPP/DeconvolutionLayer.h create mode 100644 tests/validation/NEON/DeconvolutionLayer.cpp create mode 100644 tests/validation/fixtures/DeconvolutionLayerFixture.h (limited to 'tests') diff --git a/tests/datasets/ShapeDatasets.h b/tests/datasets/ShapeDatasets.h index 6b3b5c748f..86ed2b2ad7 100644 --- a/tests/datasets/ShapeDatasets.h +++ b/tests/datasets/ShapeDatasets.h @@ -198,6 +198,21 @@ public: } }; +/** Data set containing small tensor shapes for deconvolution. */ +class SmallDeconvolutionShapes final : public ShapeDataset +{ +public: + SmallDeconvolutionShapes() + : ShapeDataset("InputShape", + { + TensorShape{ 2U, 3U, 3U, 2U }, + TensorShape{ 5U, 5U, 3U }, + TensorShape{ 11U, 13U, 4U, 3U } + }) + { + } +}; + /** Data set containing small tensor shapes for direct convolution. */ class SmallDirectConvolutionShapes final : public ShapeDataset { diff --git a/tests/validation/CPP/DeconvolutionLayer.cpp b/tests/validation/CPP/DeconvolutionLayer.cpp new file mode 100644 index 0000000000..34f3d10edb --- /dev/null +++ b/tests/validation/CPP/DeconvolutionLayer.cpp @@ -0,0 +1,108 @@ +/* + * Copyright (c) 2017 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 "ConvolutionLayer.h" + +#include "tests/validation/FixedPoint.h" +#include "tests/validation/Helpers.h" + +namespace arm_compute +{ +namespace test +{ +namespace validation +{ +namespace reference +{ +template +SimpleTensor deconvolution_layer(const SimpleTensor &src, const SimpleTensor &weights, const SimpleTensor &bias, const TensorShape &output_shape, + const PadStrideInfo &info, const std::pair &a) +{ + // Create reference + TensorShape scaled_shape = src.shape(); + scaled_shape.set(0, output_shape.x()); + scaled_shape.set(1, output_shape.y()); + SimpleTensor scaled{ scaled_shape, src.data_type(), 1, src.fixed_point_position() }; + + const int width_in = src.shape().x(); + const int height_in = src.shape().y(); + const int width_scaled = scaled.shape().x(); + const int height_scaled = scaled.shape().y(); + const int num_2d_slices = src.shape().total_size() / (width_in * height_in); + const auto width_ratio = static_cast(width_in) / static_cast(width_scaled); + const auto height_ratio = static_cast(height_in) / static_cast(height_scaled); + const int ax = a.first; // The number of zeros added to right edge of the input. + const int ay = a.second; // The number of zeros added to bottom edge of the input. + const unsigned int kernel_size = weights.shape().x(); + ARM_COMPUTE_ERROR_ON(info.pad().first > (kernel_size - 1)); + const int transposed_convolution_padx = kernel_size - info.pad().first - 1; + const int transposed_convolution_pady = kernel_size - info.pad().second - 1; + const int stridex = info.stride().first; + const int stridey = info.stride().second; + + for(int j = 0; j < scaled.num_elements(); ++j) + { + scaled[j] = T(0); + } + + for(int slice = 0; slice < num_2d_slices; ++slice) + { + const int offset_slice_in = slice * width_in * height_in; + const int offset_slice_out = slice * width_scaled * height_scaled; + for(int yi = ay; yi < height_scaled; yi += stridey) + { + for(int xi = transposed_convolution_padx; xi < width_scaled; xi += stridex) + { + const float x_src = (xi + 0.5f) * width_ratio - 0.5f; + const float y_src = (yi + 0.5f) * height_ratio - 0.5f; + T *out = scaled.data() + offset_slice_out + xi + yi * width_scaled; + const bool in_bounds = x_src > -1 && y_src > -1 && x_src < width_in && y_src < height_in; + const bool in_axy = xi < transposed_convolution_padx || xi >= (width_scaled - ax) // this is checking if the x coordinate is in the padded left/right area + || yi < ay || yi >= (height_scaled - transposed_convolution_pady); // like above but top and bottom padding in the upscaled XY plane + if(!in_axy) + { + if(in_bounds) + { + const int in_scaled_x = support::cpp11::round(x_src); + const int in_scaled_y = support::cpp11::round(y_src); + const T *in = src.data() + offset_slice_in + in_scaled_x + in_scaled_y * width_in; + *out = *in; + } + else + { + *out = T(0); + } + } + } + } + } + const PadStrideInfo conv_info(1, 1, 1, 1, DimensionRoundingType::CEIL); + return convolution_layer(scaled, weights, bias, output_shape, conv_info); +} + +template SimpleTensor deconvolution_layer(const SimpleTensor &src, const SimpleTensor &weights, const SimpleTensor &bias, const TensorShape &output_shape, + const PadStrideInfo &info, const std::pair &a); +} // namespace reference +} // namespace validation +} // namespace test +} // namespace arm_compute diff --git a/tests/validation/CPP/DeconvolutionLayer.h b/tests/validation/CPP/DeconvolutionLayer.h new file mode 100644 index 0000000000..8222e32027 --- /dev/null +++ b/tests/validation/CPP/DeconvolutionLayer.h @@ -0,0 +1,55 @@ +/* + * Copyright (c) 2017 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_DECONVOLUTION_LAYER_H__ +#define __ARM_COMPUTE_TEST_DECONVOLUTION_LAYER_H__ + +#include "tests/SimpleTensor.h" +#include "tests/validation/Helpers.h" + +namespace arm_compute +{ +namespace test +{ +namespace validation +{ +namespace reference +{ +/** Deconvolution reference implementation. + * + * src Input tensor. 3 lower dimensions represent a single input, and an optional 4th dimension for batch of inputs. Data types supported: F32. + * weights The 4d weights with dimensions [width, height, OFM, IFM]. Data type supported: Same as @p input. + * bias Optional, ignored if NULL. The biases have one dimension. Data type supported: Same as @p input. + * output_shape Output tensor shape. The output has the same number of dimensions as the @p input. + * info Contains padding and policies to be used in the deconvolution, this is decribed in @ref PadStrideInfo. + * a The number of zeros added to right edge of the input. + * + */ +template +SimpleTensor deconvolution_layer(const SimpleTensor &src, const SimpleTensor &weights, const SimpleTensor &bias, const TensorShape &output_shape, const PadStrideInfo &info, + const std::pair &a); +} // namespace reference +} // namespace validation +} // namespace test +} // namespace arm_compute +#endif /* __ARM_COMPUTE_TEST_DECONVOLUTION_LAYER_H__ */ diff --git a/tests/validation/NEON/DeconvolutionLayer.cpp b/tests/validation/NEON/DeconvolutionLayer.cpp new file mode 100644 index 0000000000..751a96558a --- /dev/null +++ b/tests/validation/NEON/DeconvolutionLayer.cpp @@ -0,0 +1,95 @@ +/* + * Copyright (c) 2017 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/NEDeconvolutionLayer.h" +#include "arm_compute/runtime/Tensor.h" +#include "arm_compute/runtime/TensorAllocator.h" +#include "tests/NEON/Accessor.h" +#include "tests/PaddingCalculator.h" +#include "tests/datasets/ShapeDatasets.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/DeconvolutionLayerFixture.h" + +namespace arm_compute +{ +namespace test +{ +namespace validation +{ +namespace +{ +constexpr AbsoluteTolerance tolerance_fp32(0.001f); /**< Tolerance for floating point tests */ + +const auto data3x3 = datasets::SmallDeconvolutionShapes() * framework::dataset::make("StrideX", 1, 4) * framework::dataset::make("StrideY", 1, 4) * framework::dataset::make("PadX", 0, + 2) + * framework::dataset::make("PadY", 0, 2) * framework::dataset::make("ax", 1, 3) * framework::dataset::make("ay", 1, 3) * framework::dataset::make("NumKernels", { 1, 3 }) + *framework::dataset::make("ux", 1, 4) *framework::dataset::make("uy", 1, 4); + +const auto data1x1 = datasets::SmallDeconvolutionShapes() * framework::dataset::make("StrideX", 1, 4) * framework::dataset::make("StrideY", 1, 4) * framework::dataset::make("PadX", 0, + 1) + * framework::dataset::make("PadY", 0, 1) * framework::dataset::make("ax", 1, 3) * framework::dataset::make("ay", 1, 3) * framework::dataset::make("NumKernels", { 1, 3 }) + *framework::dataset::make("ux", 1, 4) *framework::dataset::make("uy", 1, 4); + +} // namespace + +TEST_SUITE(NEON) +TEST_SUITE(DeconvolutionLayer) + +template +using NEDeconvolutionLayerFixture3x3 = DeconvolutionValidationFixture; + +template +using NEDeconvolutionLayerFixture1x1 = DeconvolutionValidationFixture; + +TEST_SUITE(Float) + +TEST_SUITE(FP32) +TEST_SUITE(W3x3) + +FIXTURE_DATA_TEST_CASE(Run, NEDeconvolutionLayerFixture3x3, framework::DatasetMode::ALL, combine(data3x3, framework::dataset::make("DataType", DataType::F32))) +{ + // Validate output + validate(Accessor(_target), _reference, tolerance_fp32); +} +TEST_SUITE_END() + +TEST_SUITE(W1x1) +FIXTURE_DATA_TEST_CASE(Run, NEDeconvolutionLayerFixture1x1, framework::DatasetMode::ALL, combine(data1x1, framework::dataset::make("DataType", DataType::F32))) +{ + // Validate output + validate(Accessor(_target), _reference, tolerance_fp32); +} +TEST_SUITE_END() + +TEST_SUITE_END() +TEST_SUITE_END() + +TEST_SUITE_END() +TEST_SUITE_END() +} // namespace validation +} // namespace test +} // namespace arm_compute diff --git a/tests/validation/fixtures/DeconvolutionLayerFixture.h b/tests/validation/fixtures/DeconvolutionLayerFixture.h new file mode 100644 index 0000000000..8dff97d83f --- /dev/null +++ b/tests/validation/fixtures/DeconvolutionLayerFixture.h @@ -0,0 +1,168 @@ +/* + * Copyright (c) 2017 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/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/CPP/DeconvolutionLayer.h" +#include "tests/validation/Helpers.h" + +#include + +namespace arm_compute +{ +namespace test +{ +namespace validation +{ +template +class DeconvolutionLayerFixtureBase : public framework::Fixture +{ +public: + /* + * + * @param[in] a The number of zeros added to right and bottom edges of the input. + * @param[in] u How much to scale the X and Y axis. + */ + template + void setup(TensorShape input_shape, TensorShape weights_shape, TensorShape bias_shape, TensorShape output_shape, PadStrideInfo info, + const std::pair &a, const std::pair &u, DataType data_type, int fractional_bits) + { + _fractional_bits = fractional_bits; + _data_type = data_type; + + _target = compute_target(input_shape, weights_shape, bias_shape, output_shape, info, a, u, data_type, fractional_bits); + _reference = compute_reference(input_shape, weights_shape, bias_shape, output_shape, info, a, data_type, fractional_bits); + } + +protected: + template + void fill(U &&tensor, int i) + { + switch(tensor.data_type()) + { + case DataType::F32: + { + std::uniform_real_distribution<> distribution(-1.0f, 1.0f); + library->fill(tensor, distribution, i); + break; + } + default: + library->fill_tensor_uniform(tensor, i); + } + } + /* + * + * @param[in] a The number of zeros added to right and bottom edges of the input. + * @param[in] u How much to scale the X and Y axis. + */ + TensorType compute_target(const TensorShape &input_shape, const TensorShape &weights_shape, const TensorShape &bias_shape, const TensorShape &output_shape, + const PadStrideInfo &info, const std::pair &a, const std::pair &u, DataType data_type, int fixed_point_position) + { + // Create tensors + TensorType src = create_tensor(input_shape, data_type, 1, fixed_point_position); + TensorType weights = create_tensor(weights_shape, data_type, 1, fixed_point_position); + TensorType bias = create_tensor(bias_shape, data_type, 1, fixed_point_position); + TensorType dst = create_tensor(output_shape, data_type, 1, fixed_point_position); + + // Create and configure function + FunctionType conv; + conv.configure(&src, &weights, &bias, &dst, info, a.first, a.second, u.first, u.second); + + ARM_COMPUTE_EXPECT(src.info()->is_resizable(), framework::LogLevel::ERRORS); + ARM_COMPUTE_EXPECT(weights.info()->is_resizable(), framework::LogLevel::ERRORS); + ARM_COMPUTE_EXPECT(bias.info()->is_resizable(), framework::LogLevel::ERRORS); + ARM_COMPUTE_EXPECT(dst.info()->is_resizable(), framework::LogLevel::ERRORS); + + // Allocate tensors + src.allocator()->allocate(); + weights.allocator()->allocate(); + bias.allocator()->allocate(); + dst.allocator()->allocate(); + + ARM_COMPUTE_EXPECT(!src.info()->is_resizable(), framework::LogLevel::ERRORS); + ARM_COMPUTE_EXPECT(!weights.info()->is_resizable(), framework::LogLevel::ERRORS); + ARM_COMPUTE_EXPECT(!bias.info()->is_resizable(), framework::LogLevel::ERRORS); + ARM_COMPUTE_EXPECT(!dst.info()->is_resizable(), framework::LogLevel::ERRORS); + + // Fill tensors + fill(AccessorType(src), 0); + fill(AccessorType(weights), 1); + fill(AccessorType(bias), 2); + + // Compute NEConvolutionLayer function + conv.run(); + + return dst; + } + + SimpleTensor compute_reference(const TensorShape &input_shape, const TensorShape &weights_shape, const TensorShape &bias_shape, const TensorShape &output_shape, + const PadStrideInfo &info, const std::pair a, DataType data_type, int fixed_point_position) + { + // Create reference + SimpleTensor src{ input_shape, data_type, 1, fixed_point_position }; + SimpleTensor weights{ weights_shape, data_type, 1, fixed_point_position }; + SimpleTensor bias{ bias_shape, data_type, 1, fixed_point_position }; + + // Fill reference + fill(src, 0); + fill(weights, 1); + fill(bias, 2); + + return reference::deconvolution_layer(src, weights, bias, output_shape, info, a); + } + + TensorType _target{}; + SimpleTensor _reference{}; + int _fractional_bits{}; + DataType _data_type{}; +}; + +template +class DeconvolutionValidationFixture : public DeconvolutionLayerFixtureBase +{ +public: + template + void setup(TensorShape input_shape, unsigned int sx, unsigned int sy, unsigned int padx, unsigned int pady, + unsigned int ax, unsigned int ay, unsigned int ux, unsigned int uy, unsigned int num_kernels, DataType data_type) + { + ARM_COMPUTE_ERROR_ON_MSG(kernel_size_x != kernel_size_y, "Only square kernels supported"); + const TensorShape weights_shape(kernel_size_x, kernel_size_y, input_shape.z(), num_kernels); + const TensorShape bias_shape(num_kernels); + const PadStrideInfo info(sx, sy, padx, pady, DimensionRoundingType::CEIL); + const std::pair a(ax, ay); + const std::pair u(ux, uy); + auto out_dim = deconvolution_output_dimensions(input_shape.x(), input_shape.y(), kernel_size_x, kernel_size_y, padx, pady, a.first, a.second, u.first, u.second, + DimensionRoundingType::CEIL); + TensorShape output_shape = deconvolution_output_shape(out_dim, input_shape, weights_shape); + DeconvolutionLayerFixtureBase::setup(input_shape, weights_shape, bias_shape, output_shape, info, a, u, data_type, 0); + } +}; + +} // namespace validation +} // namespace test +} // namespace arm_compute -- cgit v1.2.1