Michele Di Giorgio | 4e09b38 | 2017-07-05 18:20:02 +0100 | [diff] [blame] | 1 | /* |
| 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 | |
Georgios Pinitas | bb3be39 | 2017-09-20 18:36:03 +0100 | [diff] [blame] | 26 | #include <cmath> |
| 27 | |
Michele Di Giorgio | 4e09b38 | 2017-07-05 18:20:02 +0100 | [diff] [blame] | 28 | namespace arm_compute |
| 29 | { |
| 30 | namespace test |
| 31 | { |
| 32 | namespace validation |
| 33 | { |
| 34 | namespace reference |
| 35 | { |
Michele Di Giorgio | 4e09b38 | 2017-07-05 18:20:02 +0100 | [diff] [blame] | 36 | template <typename T, typename std::enable_if<is_floating_point<T>::value, int>::type> |
| 37 | SimpleTensor<uint8_t> quantization_layer(const SimpleTensor<T> &src) |
| 38 | { |
| 39 | // Create reference |
| 40 | SimpleTensor<uint8_t> dst{ src.shape(), DataType::U8 }; |
| 41 | |
Gian Marco Iodice | 06b184a | 2017-08-29 16:05:25 +0100 | [diff] [blame] | 42 | const int width = src.shape().x(); |
| 43 | const int height = src.shape().y(); |
| 44 | const int depth = src.shape().z(); |
| 45 | const int stride_w = width * height * depth; |
| 46 | const int num_batches = src.shape().total_size_upper(3); |
Michele Di Giorgio | 4e09b38 | 2017-07-05 18:20:02 +0100 | [diff] [blame] | 47 | |
Gian Marco Iodice | 06b184a | 2017-08-29 16:05:25 +0100 | [diff] [blame] | 48 | for(int k = 0; k < num_batches; ++k) |
Michele Di Giorgio | 4e09b38 | 2017-07-05 18:20:02 +0100 | [diff] [blame] | 49 | { |
Gian Marco Iodice | 06b184a | 2017-08-29 16:05:25 +0100 | [diff] [blame] | 50 | // Compute min and max of the 3D tensor |
Georgios Pinitas | bb3be39 | 2017-09-20 18:36:03 +0100 | [diff] [blame] | 51 | float min = src[k * stride_w]; |
| 52 | float max = src[k * stride_w]; |
Gian Marco Iodice | 06b184a | 2017-08-29 16:05:25 +0100 | [diff] [blame] | 53 | |
| 54 | // Look for min and max values |
| 55 | for(int i = 1; i < stride_w; ++i) |
| 56 | { |
| 57 | float val = src[i + k * stride_w]; |
Georgios Pinitas | bb3be39 | 2017-09-20 18:36:03 +0100 | [diff] [blame] | 58 | min = std::min(min, val); |
| 59 | max = std::max(max, val); |
Gian Marco Iodice | 06b184a | 2017-08-29 16:05:25 +0100 | [diff] [blame] | 60 | } |
| 61 | |
| 62 | // Saturate the result in case min = max |
| 63 | if(min == max) |
| 64 | { |
| 65 | min = 0.0f; |
| 66 | max = 1.0f; |
| 67 | } |
| 68 | |
| 69 | const float range = max - min; |
| 70 | |
| 71 | for(int i = 0; i < stride_w; ++i) |
| 72 | { |
| 73 | // map values to range [0.0, 1.0] |
| 74 | float val = src[i + k * stride_w]; |
| 75 | const float normalized = (val - min) / range; |
| 76 | dst[i + k * stride_w] = static_cast<uint8_t>(std::min(255.0f, normalized * 256.0f)); |
| 77 | } |
Michele Di Giorgio | 4e09b38 | 2017-07-05 18:20:02 +0100 | [diff] [blame] | 78 | } |
| 79 | |
| 80 | return dst; |
| 81 | } |
| 82 | |
| 83 | template SimpleTensor<uint8_t> quantization_layer(const SimpleTensor<float> &src); |
| 84 | } // namespace reference |
| 85 | } // namespace validation |
| 86 | } // namespace test |
| 87 | } // namespace arm_compute |