Daniel Konegen / MNIST_example

Dependencies:   mbed-os

Embed: (wiki syntax)

« Back to documentation index

Show/hide line numbers micro_utils.h Source File

micro_utils.h

00001 /* Copyright 2019 The TensorFlow Authors. All Rights Reserved.
00002 
00003 Licensed under the Apache License, Version 2.0 (the "License");
00004 you may not use this file except in compliance with the License.
00005 You may obtain a copy of the License at
00006 
00007     http://www.apache.org/licenses/LICENSE-2.0
00008 
00009 Unless required by applicable law or agreed to in writing, software
00010 distributed under the License is distributed on an "AS IS" BASIS,
00011 WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
00012 See the License for the specific language governing permissions and
00013 limitations under the License.
00014 ==============================================================================*/
00015 
00016 #ifndef TENSORFLOW_LITE_EXPERIMENTAL_MICRO_MICRO_UTILS_H_
00017 #define TENSORFLOW_LITE_EXPERIMENTAL_MICRO_MICRO_UTILS_H_
00018 
00019 #include <stdint.h>
00020 
00021 #include "tensorflow/lite/c/c_api_internal.h"
00022 
00023 namespace tflite {
00024 
00025 // Returns number of elements in the shape array.
00026 
00027 int ElementCount(const TfLiteIntArray& dims);
00028 
00029 uint8_t FloatToAsymmetricQuantizedUInt8(const float value, const float scale,
00030                                         const int zero_point);
00031 
00032 uint8_t FloatToSymmetricQuantizedUInt8(const float value, const float scale);
00033 
00034 int8_t FloatToAsymmetricQuantizedInt8(const float value, const float scale,
00035                                       const int zero_point);
00036 
00037 int8_t FloatToSymmetricQuantizedInt8(const float value, const float scale);
00038 
00039 // Converts a float value into a signed thirty-two-bit quantized value.  Note
00040 // that values close to max int and min int may see significant error due to
00041 // a lack of floating point granularity for large values.
00042 int32_t FloatToSymmetricQuantizedInt32(const float value, const float scale);
00043 
00044 // Helper methods to quantize arrays of floats to the desired format.
00045 //
00046 // There are several key flavors of quantization in TfLite:
00047 //        asymmetric symmetric  per channel
00048 // int8  |     X    |    X    |     X      |
00049 // uint8 |     X    |    X    |            |
00050 // int32 |          |    X    |     X      |
00051 //
00052 // The per-op quantizaiton spec can be found here:
00053 // https://www.tensorflow.org/lite/performance/quantization_spec
00054 
00055 void AsymmetricQuantize(const float* input, int8_t* output, int num_elements,
00056                         float scale, int zero_point = 0);
00057 
00058 void AsymmetricQuantize(const float* input, uint8_t* output, int num_elements,
00059                         float scale, int zero_point = 128);
00060 
00061 void SymmetricQuantize(const float* input, int32_t* output, int num_elements,
00062                        float scale);
00063 
00064 void SymmetricPerChannelQuantize(const float* input, int32_t* output,
00065                                  int num_elements, int num_channels,
00066                                  float* scales);
00067 
00068 void SignedSymmetricPerChannelQuantize(const float* values,
00069                                        TfLiteIntArray* dims,
00070                                        int quantized_dimension,
00071                                        int8_t* quantized_values,
00072                                        float* scaling_factor);
00073 
00074 void SignedSymmetricQuantize(const float* values, TfLiteIntArray* dims,
00075                              int8_t* quantized_values, float* scaling_factor);
00076 
00077 void SymmetricQuantize(const float* values, TfLiteIntArray* dims,
00078                        uint8_t* quantized_values, float* scaling_factor);
00079 
00080 void SymmetricDequantize(const int8_t* values, const int size,
00081                          const float dequantization_scale,
00082                          float* dequantized_values);
00083 
00084 }  // namespace tflite
00085 
00086 #endif  // TENSORFLOW_LITE_EXPERIMENTAL_MICRO_MICRO_UTILS_H_