Caffe2 - C++ API
A deep learning, cross platform ML framework
p99_example.cc
1 #include "quantization_error_minimization.h"
2 
3 #include <fstream>
4 #include <iostream>
5 #include <sstream>
6 
7 using namespace std;
8 using namespace dnnlowp;
9 
10 int main(int argc, const char* argv[]) {
11  if (argc < 3) {
12  cerr << "Usage: " << argv[0] << " in_file out_file" << endl;
13  return -1;
14  }
15 
16  ifstream in(argv[1]);
17  ofstream out(argv[2]);
18 
19  string line;
20  while (getline(in, line)) {
21  istringstream ist(line);
22 
23  int op_index, output_index;
24  string op_type, tensor_name;
25  float min, max;
26  int nbins;
27 
28  ist >> op_index >> op_type >> output_index >> tensor_name >> min >> max >>
29  nbins;
30 
31  vector<uint64_t> bins;
32  for (int i = 0; i < nbins; ++i) {
33  uint64_t cnt;
34  ist >> cnt;
35  bins.push_back(cnt);
36  }
37  assert(bins.size() == nbins);
38 
39  Histogram hist = Histogram(min, max, bins);
40  TensorQuantizationParams qparams = P99().ChooseQuantizationParams(hist);
41 
42  out << op_index << " " << op_type << " " << output_index << " "
43  << tensor_name << " " << qparams.Min() << " " << qparams.Max() << endl;
44  }
45 
46  return 0;
47 }
bin_width = (max - min)/nbins ith bin (zero-based indexing) contains [i*bin_width, (i+1)*bin_width) with an exception that (nbins - 1)th bin contains [(nbins-1)*bin_width, nbins*bin_width]