John Richardson | 384a43e | 2017-12-14 15:41:13 +0000 | [diff] [blame] | 1 | /* |
Michalis Spyrou | d1d7722 | 2020-04-08 14:10:15 +0100 | [diff] [blame] | 2 | * Copyright (c) 2017-2020 ARM Limited. |
John Richardson | 384a43e | 2017-12-14 15:41:13 +0000 | [diff] [blame] | 3 | * |
| 4 | * SPDX-License-Identifier: MIT |
| 5 | * |
| 6 | * Permission is hereby granted, free of charge, to any person obtaining a copy |
| 7 | * of this software and associated documentation files (the "Software"), to |
| 8 | * deal in the Software without restriction, including without limitation the |
| 9 | * rights to use, copy, modify, merge, publish, distribute, sublicense, and/or |
| 10 | * sell copies of the Software, and to permit persons to whom the Software is |
| 11 | * furnished to do so, subject to the following conditions: |
| 12 | * |
| 13 | * The above copyright notice and this permission notice shall be included in all |
| 14 | * copies or substantial portions of the Software. |
| 15 | * |
| 16 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR |
| 17 | * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, |
| 18 | * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE |
| 19 | * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER |
| 20 | * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, |
| 21 | * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE |
| 22 | * SOFTWARE. |
| 23 | */ |
| 24 | #include "Derivative.h" |
| 25 | |
| 26 | #include "Utils.h" |
| 27 | #include "tests/Types.h" |
| 28 | |
| 29 | #include <array> |
| 30 | |
| 31 | namespace arm_compute |
| 32 | { |
| 33 | namespace test |
| 34 | { |
| 35 | namespace validation |
| 36 | { |
| 37 | namespace reference |
| 38 | { |
| 39 | namespace |
| 40 | { |
| 41 | const std::array<int8_t, 9> derivative_3_x{ { 0, 0, 0, -1, 0, 1, 0, 0, 0 } }; |
| 42 | const std::array<int8_t, 9> derivative_3_y{ { 0, -1, 0, 0, 0, 0, 0, 1, 0 } }; |
| 43 | |
| 44 | template <typename T> |
| 45 | struct data_type; |
| 46 | |
| 47 | template <> |
| 48 | struct data_type<int16_t> |
| 49 | { |
| 50 | const static DataType value = DataType::S16; |
| 51 | }; |
| 52 | } // namespace |
| 53 | |
| 54 | template <typename T, typename U> |
| 55 | std::pair<SimpleTensor<T>, SimpleTensor<T>> derivative(const SimpleTensor<U> &src, BorderMode border_mode, uint8_t constant_border_value, GradientDimension gradient_dimension) |
| 56 | { |
| 57 | const unsigned int filter_size = 3; |
| 58 | |
| 59 | SimpleTensor<T> dst_x(src.shape(), data_type<T>::value, src.num_channels()); |
| 60 | SimpleTensor<T> dst_y(src.shape(), data_type<T>::value, src.num_channels()); |
| 61 | |
| 62 | ValidRegion valid_region = shape_to_valid_region(src.shape(), border_mode == BorderMode::UNDEFINED, BorderSize(filter_size / 2)); |
| 63 | |
Michalis Spyrou | fae513c | 2019-10-16 17:41:33 +0100 | [diff] [blame] | 64 | const uint32_t num_elements = src.num_elements(); |
Michalis Spyrou | d1d7722 | 2020-04-08 14:10:15 +0100 | [diff] [blame] | 65 | #if defined(_OPENMP) |
| 66 | #pragma omp parallel for |
| 67 | #endif /* _OPENMP */ |
Michalis Spyrou | fae513c | 2019-10-16 17:41:33 +0100 | [diff] [blame] | 68 | for(uint32_t i = 0; i < num_elements; ++i) |
John Richardson | 384a43e | 2017-12-14 15:41:13 +0000 | [diff] [blame] | 69 | { |
| 70 | Coordinates coord = index2coord(src.shape(), i); |
| 71 | |
| 72 | if(!is_in_valid_region(valid_region, coord)) |
| 73 | { |
| 74 | continue; |
| 75 | } |
| 76 | |
| 77 | switch(gradient_dimension) |
| 78 | { |
| 79 | case GradientDimension::GRAD_X: |
| 80 | apply_2d_spatial_filter(coord, src, dst_x, TensorShape{ filter_size, filter_size }, derivative_3_x.data(), 1.f, border_mode, |
| 81 | constant_border_value); |
| 82 | break; |
| 83 | case GradientDimension::GRAD_Y: |
| 84 | apply_2d_spatial_filter(coord, src, dst_y, TensorShape{ filter_size, filter_size }, derivative_3_y.data(), 1.f, border_mode, |
| 85 | constant_border_value); |
| 86 | break; |
| 87 | case GradientDimension::GRAD_XY: |
| 88 | apply_2d_spatial_filter(coord, src, dst_x, TensorShape{ filter_size, filter_size }, derivative_3_x.data(), 1.f, border_mode, |
| 89 | constant_border_value); |
| 90 | apply_2d_spatial_filter(coord, src, dst_y, TensorShape{ filter_size, filter_size }, derivative_3_y.data(), 1.f, border_mode, |
| 91 | constant_border_value); |
| 92 | break; |
| 93 | default: |
| 94 | ARM_COMPUTE_ERROR("Gradient dimension not supported"); |
| 95 | } |
| 96 | } |
| 97 | |
| 98 | return std::make_pair(dst_x, dst_y); |
| 99 | } |
| 100 | |
| 101 | template std::pair<SimpleTensor<int16_t>, SimpleTensor<int16_t>> derivative(const SimpleTensor<uint8_t> &src, BorderMode border_mode, uint8_t constant_border_value, |
| 102 | GradientDimension gradient_dimension); |
| 103 | } // namespace reference |
| 104 | } // namespace validation |
| 105 | } // namespace test |
| 106 | } // namespace arm_compute |