blob: d92699d93e5b13f1d71331f75fd1baa553e9bdb4 [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#ifndef __ARM_COMPUTE_TEST_VALIDATION_HELPERS_H__
25#define __ARM_COMPUTE_TEST_VALIDATION_HELPERS_H__
26
27#include "Types.h"
Isabella Gottardi3b77e9d2017-06-22 11:05:41 +010028#include "ValidationUserConfiguration.h"
Anthony Barbier6ff3b192017-09-04 18:44:23 +010029
Georgios Pinitas7b7858d2017-06-21 16:44:24 +010030#include "arm_compute/core/Types.h"
31
32#include <random>
Anthony Barbier6ff3b192017-09-04 18:44:23 +010033#include <type_traits>
34#include <utility>
Georgios Pinitas7b7858d2017-06-21 16:44:24 +010035#include <vector>
Anthony Barbier6ff3b192017-09-04 18:44:23 +010036
37namespace arm_compute
38{
39namespace test
40{
41namespace validation
42{
43/** Helper function to get the testing range for each activation layer.
44 *
45 * @param[in] activation Activation function to test.
46 * @param[in] fixed_point_position (Optional) Number of bits for the fractional part. Defaults to 1.
47 *
48 * @return A pair containing the lower upper testing bounds for a given function.
49 */
50template <typename T>
51std::pair<T, T> get_activation_layer_test_bounds(ActivationLayerInfo::ActivationFunction activation, int fixed_point_position = 1)
52{
53 bool is_float = std::is_floating_point<T>::value;
54 std::pair<T, T> bounds;
55
56 // Set initial values
57 if(is_float)
58 {
59 bounds = std::make_pair(-255.f, 255.f);
60 }
61 else
62 {
63 bounds = std::make_pair(std::numeric_limits<T>::lowest(), std::numeric_limits<T>::max());
64 }
65
66 // Reduce testing ranges
67 switch(activation)
68 {
69 case ActivationLayerInfo::ActivationFunction::LOGISTIC:
70 case ActivationLayerInfo::ActivationFunction::SOFT_RELU:
71 // Reduce range as exponent overflows
72 if(is_float)
73 {
74 bounds.first = -40.f;
75 bounds.second = 40.f;
76 }
77 else
78 {
79 bounds.first = -(1 << (fixed_point_position));
80 bounds.second = 1 << (fixed_point_position);
81 }
82 break;
83 case ActivationLayerInfo::ActivationFunction::TANH:
84 // Reduce range as exponent overflows
85 if(!is_float)
86 {
87 bounds.first = -(1 << (fixed_point_position));
88 bounds.second = 1 << (fixed_point_position);
89 }
90 break;
91 case ActivationLayerInfo::ActivationFunction::SQRT:
92 // Reduce range as sqrt should take a non-negative number
93 bounds.first = (is_float) ? 0 : 1 << (fixed_point_position);
94 break;
95 default:
96 break;
97 }
98 return bounds;
99}
100
101/** Helper function to get the testing range for batch normalization layer.
102 *
103 * @param[in] fixed_point_position (Optional) Number of bits for the fractional part. Defaults to 1.
104 *
105 * @return A pair containing the lower upper testing bounds.
106 */
107template <typename T>
108std::pair<T, T> get_batchnormalization_layer_test_bounds(int fixed_point_position = 1)
109{
110 bool is_float = std::is_floating_point<T>::value;
111 std::pair<T, T> bounds;
112
113 // Set initial values
114 if(is_float)
115 {
116 bounds = std::make_pair(-1.f, 1.f);
117 }
118 else
119 {
120 bounds = std::make_pair(1, 1 << (fixed_point_position));
121 }
122
123 return bounds;
124}
Isabella Gottardi3b77e9d2017-06-22 11:05:41 +0100125
126/** Fill mask with the corresponding given pattern.
127 *
128 * @param[in,out] mask Mask to be filled according to pattern
129 * @param[in] cols Columns (width) of mask
130 * @param[in] rows Rows (height) of mask
131 * @param[in] pattern Pattern to fill the mask according to
132 */
133inline void fill_mask_from_pattern(uint8_t *mask, int cols, int rows, MatrixPattern pattern)
134{
135 unsigned int v = 0;
136 std::mt19937 gen(user_config.seed.get());
137 std::bernoulli_distribution dist(0.5);
138
139 for(int r = 0; r < rows; ++r)
140 {
141 for(int c = 0; c < cols; ++c, ++v)
142 {
143 uint8_t val = 0;
144
145 switch(pattern)
146 {
147 case MatrixPattern::BOX:
148 val = 255;
149 break;
150 case MatrixPattern::CROSS:
151 val = ((r == (rows / 2)) || (c == (cols / 2))) ? 255 : 0;
152 break;
153 case MatrixPattern::DISK:
154 val = (((r - rows / 2.0f + 0.5f) * (r - rows / 2.0f + 0.5f)) / ((rows / 2.0f) * (rows / 2.0f)) + ((c - cols / 2.0f + 0.5f) * (c - cols / 2.0f + 0.5f)) / ((cols / 2.0f) *
155 (cols / 2.0f))) <= 1.0f ? 255 : 0;
156 break;
157 case MatrixPattern::OTHER:
158 val = (dist(gen) ? 0 : 255);
159 break;
160 default:
161 return;
162 }
163
164 mask[v] = val;
165 }
166 }
167
168 if(pattern == MatrixPattern::OTHER)
169 {
170 std::uniform_int_distribution<uint8_t> distribution_u8(0, ((cols * rows) - 1));
171 mask[distribution_u8(gen)] = 255;
172 }
173}
174
Georgios Pinitas7b7858d2017-06-21 16:44:24 +0100175/** Create a vector of random ROIs.
176 *
177 * @param[in] shape The shape of the input tensor.
178 * @param[in] pool_info The ROI pooling information.
179 * @param[in] num_rois The number of ROIs to be created.
180 * @param[in] seed The random seed to be used.
181 *
182 * @return A vector that contains the requested number of random ROIs
183 */
184std::vector<ROI> generate_random_rois(const TensorShape &shape, const ROIPoolingLayerInfo &pool_info, unsigned int num_rois, std::random_device::result_type seed);
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100185} // namespace validation
186} // namespace test
187} // namespace arm_compute
188#endif //__ARM_COMPUTE_TEST_VALIDATION_HELPERS_H__