| 27 | namespace calibration { |
| 28 | |
| 29 | class MinMax { |
| 30 | public: |
| 31 | TfLiteStatus Update(const float* values, size_t tensor_size) { |
| 32 | if (tensor_size <= 0) return kTfLiteOk; |
| 33 | |
| 34 | // TODO(shashishekhar): Make it possible to use weighted/moving average. |
| 35 | for (size_t i = 0; i < tensor_size; ++i) { |
| 36 | if (std::isnan(values[i])) { |
| 37 | // TODO(suharshs): Propagate ErrorReporter here. |
| 38 | LOG(ERROR) << "Model resulted in Nan value during calibration. Please " |
| 39 | "make sure model results in all real-values during " |
| 40 | "inference with provided dataset."; |
| 41 | return kTfLiteError; |
| 42 | } |
| 43 | } |
| 44 | // We are only logging absolute min/max here. |
| 45 | const auto minmax = std::minmax_element(values, values + tensor_size); |
| 46 | min_ = std::min<float>(min_, *minmax.first); |
| 47 | max_ = std::max<float>(max_, *minmax.second); |
| 48 | |
| 49 | if (!has_values_) has_values_ = true; |
| 50 | return kTfLiteOk; |
| 51 | } |
| 52 | |
| 53 | bool HasValues() const { return has_values_; } |
| 54 | |
| 55 | TfLiteStatus Get(float* min_val, float* max_val) const { |
| 56 | if (!has_values_) return kTfLiteError; |
| 57 | *min_val = min_; |
| 58 | *max_val = max_; |
| 59 | return kTfLiteOk; |
| 60 | } |
| 61 | |
| 62 | private: |
| 63 | bool has_values_ = false; |
| 64 | float min_ = std::numeric_limits<float>::max(); |
| 65 | float max_ = std::numeric_limits<float>::min(); |
| 66 | }; |
| 67 | |
| 68 | // Captures min max values for tensors. |
| 69 | class Logger { |