blob: d61e75a3a90a3d95e41656593e2c161f8c0ced44 [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
63 // Compute min and max of the tensor using Min-Max layer
64 float min = 0.f;
65 float max = 0.f;
66
67 compute_min_max(src, &min, &max);
68
69 const float range = max - min;
70
71 for(int i = 0; i < src.num_elements(); ++i)
72 {
73 // map values to range [0.0, 1.0]
74 const float normalized = (src[i] - min) / range;
75 dst[i] = static_cast<uint8_t>(std::min(255.0f, normalized * 256.0f));
76 }
77
78 return dst;
79}
80
81template SimpleTensor<uint8_t> quantization_layer(const SimpleTensor<float> &src);
82} // namespace reference
83} // namespace validation
84} // namespace test
85} // namespace arm_compute