blob: fc274f5c4be64034a53da750268b4e3acc6f7eef [file] [log] [blame]
Anthony Barbier6ff3b192017-09-04 18:44:23 +01001/*
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 "ReferenceCPP.h"
25
26#include "TensorFactory.h"
27#include "TensorOperations.h"
28#include "TensorVisitors.h"
Anthony Barbier6ff3b192017-09-04 18:44:23 +010029
30#include "arm_compute/core/Coordinates.h"
31#include "arm_compute/core/Error.h"
32#include "arm_compute/core/TensorInfo.h"
33#include "arm_compute/core/TensorShape.h"
34#include "arm_compute/runtime/Tensor.h"
Anthony Barbier2a07e182017-08-04 18:20:27 +010035#include "utils/TypePrinter.h"
Anthony Barbier6ff3b192017-09-04 18:44:23 +010036
Moritz Pflanzera09de0c2017-09-01 20:41:12 +010037#include "tests/validation_old/boost_wrapper.h"
Anthony Barbier6ff3b192017-09-04 18:44:23 +010038
Georgios Pinitasac4e8732017-07-05 17:02:25 +010039#include <algorithm>
Anthony Barbier6ff3b192017-09-04 18:44:23 +010040#include <functional>
Georgios Pinitasac4e8732017-07-05 17:02:25 +010041#include <memory>
Anthony Barbier6ff3b192017-09-04 18:44:23 +010042#include <numeric>
43#include <vector>
44
45using namespace arm_compute::test::validation::tensor_visitors;
46
47namespace arm_compute
48{
49namespace test
50{
51namespace validation
52{
Giorgio Arenafc2817d2017-06-27 17:26:37 +010053// Harris corners
54void ReferenceCPP::harris_corners(RawTensor &src, RawTensor &Gx, RawTensor &Gy, const RawTensor &candidates, const RawTensor &non_maxima, float threshold, float min_dist, float sensitivity,
55 int32_t gradient_size, int32_t block_size, KeyPointArray &corners, BorderMode border_mode, uint8_t constant_border_value)
56{
57 ARM_COMPUTE_ERROR_ON(src.data_type() != DataType::U8 || (Gx.data_type() != DataType::S16 && Gx.data_type() != DataType::S32) || (Gy.data_type() != DataType::S16 && Gy.data_type() != DataType::S32)
58 || candidates.data_type() != DataType::F32 || non_maxima.data_type() != DataType::F32);
59
60 Tensor<uint8_t> s(src.shape(), src.data_type(), src.fixed_point_position(), reinterpret_cast<const uint8_t *>(src.data()));
61 Tensor<float> c(candidates.shape(), candidates.data_type(), candidates.fixed_point_position(), const_cast<float *>(reinterpret_cast<const float *>(candidates.data()))); // NOLINT
62 Tensor<float> nm(non_maxima.shape(), non_maxima.data_type(), non_maxima.fixed_point_position(), const_cast<float *>(reinterpret_cast<const float *>(non_maxima.data()))); // NOLINT
63
64 if(gradient_size == 7)
65 {
66 Tensor<int32_t> gx(Gx.shape(), Gx.data_type(), Gx.fixed_point_position(), reinterpret_cast<int32_t *>(Gx.data()));
67 Tensor<int32_t> gy(Gy.shape(), Gy.data_type(), Gy.fixed_point_position(), reinterpret_cast<int32_t *>(Gy.data()));
68 tensor_operations::harris_corners(s, gx, gy, c, nm, threshold, min_dist, sensitivity, gradient_size, block_size, corners, border_mode, constant_border_value);
69 }
70 else
71 {
72 Tensor<int16_t> gx(Gx.shape(), Gx.data_type(), Gx.fixed_point_position(), reinterpret_cast<int16_t *>(Gx.data()));
73 Tensor<int16_t> gy(Gy.shape(), Gy.data_type(), Gy.fixed_point_position(), reinterpret_cast<int16_t *>(Gy.data()));
74 tensor_operations::harris_corners(s, gx, gy, c, nm, threshold, min_dist, sensitivity, gradient_size, block_size, corners, border_mode, constant_border_value);
75 }
76}
77
Anthony Barbier6ff3b192017-09-04 18:44:23 +010078// Absolute difference
79void ReferenceCPP::absolute_difference(const RawTensor &src1, const RawTensor &src2, RawTensor &dst)
80{
81 const TensorVariant s1 = TensorFactory::get_tensor(src1);
82 const TensorVariant s2 = TensorFactory::get_tensor(src2);
83 TensorVariant d = TensorFactory::get_tensor(dst);
84 boost::apply_visitor(absolute_difference_visitor(), s1, s2, d);
85}
Giorgio Arenaf7959862017-06-13 15:19:51 +010086
Anthony Barbier6ff3b192017-09-04 18:44:23 +010087// Integral image
88void ReferenceCPP::integral_image(const RawTensor &src, RawTensor &dst)
89{
90 ARM_COMPUTE_ERROR_ON(src.data_type() != DataType::U8 || dst.data_type() != DataType::U32);
91 const Tensor<uint8_t> s(src.shape(), src.data_type(), src.fixed_point_position(), reinterpret_cast<const uint8_t *>(src.data()));
92 Tensor<uint32_t> d(dst.shape(), dst.data_type(), dst.fixed_point_position(), reinterpret_cast<uint32_t *>(dst.data()));
93 tensor_operations::integral_image(s, d);
94}
Giorgio Arenaf7959862017-06-13 15:19:51 +010095
Anthony Barbier6ff3b192017-09-04 18:44:23 +010096// Accumulate
97void ReferenceCPP::accumulate(const RawTensor &src, RawTensor &dst)
98{
99 ARM_COMPUTE_ERROR_ON(src.data_type() != DataType::U8 || dst.data_type() != DataType::S16);
100 const Tensor<uint8_t> s(src.shape(), src.data_type(), src.fixed_point_position(), reinterpret_cast<const uint8_t *>(src.data()));
101 Tensor<int16_t> d(dst.shape(), dst.data_type(), dst.fixed_point_position(), reinterpret_cast<int16_t *>(dst.data()));
102 tensor_operations::accumulate(s, d);
103}
104
105// Accumulate squared
106void ReferenceCPP::accumulate_squared(const RawTensor &src, RawTensor &dst, uint32_t shift)
107{
108 ARM_COMPUTE_ERROR_ON(src.data_type() != DataType::U8 || dst.data_type() != DataType::S16);
109 const Tensor<uint8_t> s(src.shape(), src.data_type(), src.fixed_point_position(), reinterpret_cast<const uint8_t *>(src.data()));
110 Tensor<int16_t> d(dst.shape(), dst.data_type(), dst.fixed_point_position(), reinterpret_cast<int16_t *>(dst.data()));
111 tensor_operations::accumulate_squared(s, d, shift);
112}
113
114// Accumulate weighted
115void ReferenceCPP::accumulate_weighted(const RawTensor &src, RawTensor &dst, float alpha)
116{
117 ARM_COMPUTE_ERROR_ON(src.data_type() != DataType::U8 || dst.data_type() != DataType::U8);
118 const Tensor<uint8_t> s(src.shape(), src.data_type(), src.fixed_point_position(), reinterpret_cast<const uint8_t *>(src.data()));
119 Tensor<uint8_t> d(dst.shape(), dst.data_type(), dst.fixed_point_position(), reinterpret_cast<uint8_t *>(dst.data()));
120 tensor_operations::accumulate_weighted(s, d, alpha);
121}
122
Isabella Gottardi3b77e9d2017-06-22 11:05:41 +0100123// Non linear filter
124void ReferenceCPP::non_linear_filter(const RawTensor &src, RawTensor &dst, NonLinearFilterFunction function, unsigned int mask_size,
125 MatrixPattern pattern, const uint8_t *mask, BorderMode border_mode, uint8_t constant_border_value)
126{
127 ARM_COMPUTE_ERROR_ON(src.data_type() != DataType::U8 || dst.data_type() != DataType::U8);
128 const Tensor<uint8_t> s(src.shape(), src.data_type(), src.fixed_point_position(), reinterpret_cast<const uint8_t *>(src.data()));
129 Tensor<uint8_t> d(dst.shape(), dst.data_type(), dst.fixed_point_position(), reinterpret_cast<uint8_t *>(dst.data()));
130 tensor_operations::non_linear_filter(s, d, function, mask_size, pattern, mask, border_mode, constant_border_value);
131}
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100132
133// Pixel-wise multiplication
134void ReferenceCPP::pixel_wise_multiplication(const RawTensor &src1, const RawTensor &src2, RawTensor &dst, float scale, ConvertPolicy convert_policy, RoundingPolicy rounding_policy)
135{
136 const TensorVariant s1 = TensorFactory::get_tensor(src1);
137 const TensorVariant s2 = TensorFactory::get_tensor(src2);
138 TensorVariant d = TensorFactory::get_tensor(dst);
139 boost::apply_visitor(pixel_wise_multiplication_visitor(scale, convert_policy, rounding_policy), s1, s2, d);
140}
141
142// Fixed-point Pixel-wise multiplication
143void ReferenceCPP::fixed_point_pixel_wise_multiplication(const RawTensor &src1, const RawTensor &src2, RawTensor &dst, float scale, ConvertPolicy convert_policy, RoundingPolicy rounding_policy)
144{
145 const TensorVariant s1 = TensorFactory::get_tensor(src1);
146 const TensorVariant s2 = TensorFactory::get_tensor(src2);
147 TensorVariant d = TensorFactory::get_tensor(dst);
148 boost::apply_visitor(tensor_visitors::fixed_point_pixel_wise_multiplication_visitor(s1, s2, scale, convert_policy, rounding_policy), d);
149}
150
151// Threshold
152void ReferenceCPP::threshold(const RawTensor &src, RawTensor &dst, uint8_t threshold, uint8_t false_value, uint8_t true_value, ThresholdType type, uint8_t upper)
153{
154 ARM_COMPUTE_ERROR_ON(src.data_type() != DataType::U8 || dst.data_type() != DataType::U8);
155 const Tensor<uint8_t> s(src.shape(), src.data_type(), src.fixed_point_position(), reinterpret_cast<const uint8_t *>(src.data()));
156 Tensor<uint8_t> d(dst.shape(), dst.data_type(), dst.fixed_point_position(), reinterpret_cast<uint8_t *>(dst.data()));
Isabella Gottardib797fa22017-06-23 15:02:11 +0100157 tensor_operations::threshold(s, d, threshold, false_value, true_value, type, upper);
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100158}
159
Georgios Pinitas7b7858d2017-06-21 16:44:24 +0100160// ROI Pooling Layer
161void ReferenceCPP::roi_pooling_layer(const RawTensor &src, RawTensor &dst, const std::vector<ROI> &rois, const ROIPoolingLayerInfo &pool_info)
162{
163 const TensorVariant s = TensorFactory::get_tensor(src);
164 TensorVariant d = TensorFactory::get_tensor(dst);
165 boost::apply_visitor(tensor_visitors::roi_pooling_layer_visitor(s, rois, pool_info), d);
166}
167
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100168} // namespace validation
169} // namespace test
170} // namespace arm_compute