blob: 0584d88a371e8756b65b087a3b64442c7d35fe2f [file] [log] [blame]
Michele Di Giorgio4e09b382017-07-05 18:20:02 +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 "QuantizationLayer.h"
25
26namespace arm_compute
27{
28namespace test
29{
30namespace validation
31{
32namespace reference
33{
34void compute_min_max(const SimpleTensor<float> &src, float *min, float *max)
35{
36 // Set min and max to first pixel
37 float tmp_min = src[0];
38 float tmp_max = src[0];
39
40 // Look for min and max values
41 for(int i = 1; i < src.num_elements(); ++i)
42 {
43 if(src[i] < tmp_min)
44 {
45 tmp_min = src[i];
46 }
47 if(src[i] > tmp_max)
48 {
49 tmp_max = src[i];
50 }
51 }
52
53 *min = tmp_min;
54 *max = tmp_max;
55}
56
57template <typename T, typename std::enable_if<is_floating_point<T>::value, int>::type>
58SimpleTensor<uint8_t> quantization_layer(const SimpleTensor<T> &src)
59{
60 // Create reference
61 SimpleTensor<uint8_t> dst{ src.shape(), DataType::U8 };
62
Gian Marco Iodice06b184a2017-08-29 16:05:25 +010063 const int width = src.shape().x();
64 const int height = src.shape().y();
65 const int depth = src.shape().z();
66 const int stride_w = width * height * depth;
67 const int num_batches = src.shape().total_size_upper(3);
Michele Di Giorgio4e09b382017-07-05 18:20:02 +010068
Gian Marco Iodice06b184a2017-08-29 16:05:25 +010069 for(int k = 0; k < num_batches; ++k)
Michele Di Giorgio4e09b382017-07-05 18:20:02 +010070 {
Gian Marco Iodice06b184a2017-08-29 16:05:25 +010071 // Compute min and max of the 3D tensor
72 float min = src[0];
73 float max = src[0];
74
75 // Look for min and max values
76 for(int i = 1; i < stride_w; ++i)
77 {
78 float val = src[i + k * stride_w];
79 if(val < min)
80 {
81 min = val;
82 }
83 if(val > max)
84 {
85 max = val;
86 }
87 }
88
89 // Saturate the result in case min = max
90 if(min == max)
91 {
92 min = 0.0f;
93 max = 1.0f;
94 }
95
96 const float range = max - min;
97
98 for(int i = 0; i < stride_w; ++i)
99 {
100 // map values to range [0.0, 1.0]
101 float val = src[i + k * stride_w];
102 const float normalized = (val - min) / range;
103 dst[i + k * stride_w] = static_cast<uint8_t>(std::min(255.0f, normalized * 256.0f));
104 }
Michele Di Giorgio4e09b382017-07-05 18:20:02 +0100105 }
106
107 return dst;
108}
109
110template SimpleTensor<uint8_t> quantization_layer(const SimpleTensor<float> &src);
111} // namespace reference
112} // namespace validation
113} // namespace test
114} // namespace arm_compute