Compute Library  18.02
Scharr.cpp
Go to the documentation of this file.
1 /*
2  * Copyright (c) 2017 ARM Limited.
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 "Scharr.h"
25 
26 #include "Utils.h"
28 
29 #include <array>
30 #include <map>
31 #include <utility>
32 
33 namespace arm_compute
34 {
35 namespace test
36 {
37 namespace validation
38 {
39 namespace reference
40 {
41 namespace
42 {
43 const std::array<int8_t, 9> scharr_3_x{ { -3, 0, 3, -10, 0, 10, -3, 0, 3 } };
44 const std::array<int8_t, 9> scharr_3_y{ { -3, -10, -3, 0, 0, 0, 3, 10, 3 } };
45 
46 const std::map<int, std::pair<const int8_t *, const int8_t *>> masks
47 {
48  { 3, { scharr_3_x.data(), scharr_3_y.data() } }
49 };
50 
51 template <typename T>
52 struct data_type;
53 
54 template <>
55 struct data_type<int16_t>
56 {
57  const static DataType value = DataType::S16;
58 };
59 } // namespace
60 
61 template <typename T, typename U>
62 std::pair<SimpleTensor<T>, SimpleTensor<T>> scharr(const SimpleTensor<U> &src, int filter_size, BorderMode border_mode, uint8_t constant_border_value, GradientDimension gradient_dimension)
63 {
64  const auto shape_size = static_cast<unsigned int>(filter_size);
65 
68 
69  ValidRegion valid_region = shape_to_valid_region(src.shape(), border_mode == BorderMode::UNDEFINED, BorderSize(filter_size / 2));
70 
71  for(int i = 0; i < src.num_elements(); ++i)
72  {
73  Coordinates coord = index2coord(src.shape(), i);
74 
75  if(!is_in_valid_region(valid_region, coord))
76  {
77  continue;
78  }
79 
80  switch(gradient_dimension)
81  {
82  case GradientDimension::GRAD_X:
83  apply_2d_spatial_filter(coord, src, dst_x, TensorShape{ shape_size, shape_size }, masks.at(filter_size).first, 1.f, border_mode, constant_border_value);
84  break;
85  case GradientDimension::GRAD_Y:
86  apply_2d_spatial_filter(coord, src, dst_y, TensorShape{ shape_size, shape_size }, masks.at(filter_size).second, 1.f, border_mode, constant_border_value);
87  break;
89  apply_2d_spatial_filter(coord, src, dst_x, TensorShape{ shape_size, shape_size }, masks.at(filter_size).first, 1.f, border_mode, constant_border_value);
90  apply_2d_spatial_filter(coord, src, dst_y, TensorShape{ shape_size, shape_size }, masks.at(filter_size).second, 1.f, border_mode, constant_border_value);
91  break;
92  default:
93  ARM_COMPUTE_ERROR("Gradient dimension not supported");
94  }
95  }
96 
97  return std::make_pair(dst_x, dst_y);
98 }
99 
100 template std::pair<SimpleTensor<int16_t>, SimpleTensor<int16_t>> scharr(const SimpleTensor<uint8_t> &src, int filter_size, BorderMode border_mode, uint8_t constant_border_value,
101  GradientDimension gradient_dimension);
102 } // namespace reference
103 } // namespace validation
104 } // namespace test
105 } // namespace arm_compute
BorderMode
Methods available to handle borders.
Definition: Types.h:221
Shape of a tensor.
Definition: TensorShape.h:39
Container for 2D border size.
Definition: Types.h:229
void apply_2d_spatial_filter(Coordinates coord, const SimpleTensor< T > &src, SimpleTensor< U > &dst, const TensorShape &filter_shape, const V *filter_itr, double scale, BorderMode border_mode, T constant_border_value=T(0))
Definition: Utils.h:96
#define ARM_COMPUTE_ERROR(...)
Print the given message then throw an std::runtime_error.
Definition: Error.h:238
std::pair< SimpleTensor< T >, SimpleTensor< T > > scharr(const SimpleTensor< U > &src, int filter_size, BorderMode border_mode, uint8_t constant_border_value, GradientDimension gradient_dimension)
Definition: Scharr.cpp:62
TensorShape shape() const override
Shape of the tensor.
Definition: SimpleTensor.h:245
This file contains all available output stages for GEMMLowp on OpenCL.
Definition: 01_library.dox:1
bool is_in_valid_region(const ValidRegion &valid_region, Coordinates coord)
Check if a coordinate is within a valid region.
Definition: Utils.h:450
Coordinates of an item.
Definition: Coordinates.h:37
Coordinates index2coord(const TensorShape &shape, int index)
Convert a linear index into n-dimensional coordinates.
Definition: Utils.h:403
1 channel, 1 S16 per channel
void * value
Definition: hwc.hpp:278
Simple tensor object that stores elements in a consecutive chunk of memory.
Definition: SimpleTensor.h:59
int num_channels() const override
Number of channels of the tensor.
Definition: SimpleTensor.h:295
x and y gradient dimension
Borders are left undefined.
int num_elements() const override
Number of elements of the tensor.
Definition: SimpleTensor.h:331
DataType
Available data types.
Definition: Types.h:72
GradientDimension
Gradient dimension type.
Definition: Types.h:46
convolution configure & src
ValidRegion shape_to_valid_region(const TensorShape &a_shape, bool border_undefined=false, BorderSize border_size=BorderSize(0))
Create a valid region based on tensor shape, border mode and border size.
Definition: Utils.h:212