• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /* Copyright 2018 The TensorFlow Authors. All Rights Reserved.
2 
3 Licensed under the Apache License, Version 2.0 (the "License");
4 you may not use this file except in compliance with the License.
5 You may obtain a copy of the License at
6 
7     http://www.apache.org/licenses/LICENSE-2.0
8 
9 Unless required by applicable law or agreed to in writing, software
10 distributed under the License is distributed on an "AS IS" BASIS,
11 WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 See the License for the specific language governing permissions and
13 limitations under the License.
14 ==============================================================================*/
15 #include "tensorflow/lite/tools/optimize/calibration/calibration_reader.h"
16 
17 #include "absl/memory/memory.h"
18 
19 namespace tflite {
20 namespace optimize {
21 namespace calibration {
GetTensorStatsAsMap(absl::flat_hash_map<std::tuple<int,int>,CalibrationStats> * tensor_id_to_stats_map) const22 TfLiteStatus CalibrationReader::GetTensorStatsAsMap(
23     absl::flat_hash_map<std::tuple<int, int>, CalibrationStats>*
24         tensor_id_to_stats_map) const {
25   tensor_id_to_stats_map->clear();
26   for (const auto& tensorid_stat : logger_->GetCalibrationValues()) {
27     auto minmax = tensorid_stat.second;
28     CalibrationReader::CalibrationStats stats;
29     TF_LITE_ENSURE_STATUS(minmax.Get(&stats.min, &stats.max));
30     tensor_id_to_stats_map->insert({tensorid_stat.first, stats});
31   }
32 
33   return kTfLiteOk;
34 }
35 
AddCalibrationToModel(ModelT * model,bool update) const36 TfLiteStatus CalibrationReader::AddCalibrationToModel(ModelT* model,
37                                                       bool update) const {
38   if (!model || model->subgraphs.empty()) {
39     return kTfLiteError;
40   }
41   for (const auto& tensorid_stat : logger_->GetCalibrationValues()) {
42     int subgraph_index, tensor_index;
43     std::tie(subgraph_index, tensor_index) = tensorid_stat.first;
44     const auto& subgraph = model->subgraphs[subgraph_index];
45     auto minmax = tensorid_stat.second;
46     float min, max;
47     TF_LITE_ENSURE_STATUS(minmax.Get(&min, &max));
48     if (update) {
49       auto tensor = subgraph->tensors[tensor_index].get();
50       if (tensor->quantization) {
51         if (!tensor->quantization->min.empty()) {
52           const float existing_min = tensor->quantization->min[0];
53           min = min < existing_min ? min : existing_min;
54         }
55         if (!tensor->quantization->max.empty()) {
56           const float existing_max = tensor->quantization->max[0];
57           max = max > existing_max ? max : existing_max;
58         }
59       }
60     }
61     auto quant_params = absl::make_unique<tflite::QuantizationParametersT>();
62     quant_params->min.push_back(min);
63     quant_params->max.push_back(max);
64     subgraph->tensors[tensor_index]->quantization = std::move(quant_params);
65   }
66 
67   return kTfLiteOk;
68 }
69 }  // namespace calibration
70 }  // namespace optimize
71 }  // namespace tflite
72