blob: 33096a1d818a6b3e7a177239f293610ef17fe613 [file] [log] [blame]
Michele Di Giorgio32982d82017-07-07 14:44:43 +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 "DequantizationLayer.h"
25
26namespace arm_compute
27{
28namespace test
29{
30namespace validation
31{
32namespace reference
33{
34template <typename T, typename std::enable_if<std::is_integral<T>::value, int>::type>
Gian Marco Iodice06b184a2017-08-29 16:05:25 +010035SimpleTensor<float> dequantization_layer(const SimpleTensor<T> &src, const SimpleTensor<float> &min_max)
Michele Di Giorgio32982d82017-07-07 14:44:43 +010036{
37 // Create reference
38 SimpleTensor<float> dst{ src.shape(), DataType::F32 };
39
Gian Marco Iodice06b184a2017-08-29 16:05:25 +010040 // Compute reference
41 const int width = src.shape().x();
42 const int height = src.shape().y();
43 const int depth = src.shape().z();
44 const int stride_w = width * height * depth;
45 const int num_batches = min_max.shape().total_size_upper(1);
Michele Di Giorgio32982d82017-07-07 14:44:43 +010046
Gian Marco Iodice06b184a2017-08-29 16:05:25 +010047 for(int k = 0; k < num_batches; ++k)
Michele Di Giorgio32982d82017-07-07 14:44:43 +010048 {
Gian Marco Iodice06b184a2017-08-29 16:05:25 +010049 const float min = min_max[k * 2 + 0];
50 const float max = min_max[k * 2 + 1];
51 const float range = max - min;
52 const float scaling = range / 255.0f;
53
54 for(int i = 0; i < stride_w; ++i)
55 {
56 dst[i + k * stride_w] = (static_cast<float>(src[i + k * stride_w]) * scaling) + min;
57 }
Michele Di Giorgio32982d82017-07-07 14:44:43 +010058 }
59
60 return dst;
61}
62
Gian Marco Iodice06b184a2017-08-29 16:05:25 +010063template SimpleTensor<float> dequantization_layer(const SimpleTensor<uint8_t> &src, const SimpleTensor<float> &min_max);
Michele Di Giorgio32982d82017-07-07 14:44:43 +010064} // namespace reference
65} // namespace validation
66} // namespace test
67} // namespace arm_compute