Apollo  v5.5.0
Open source self driving car software
entropy_calibrator.h
Go to the documentation of this file.
1 /******************************************************************************
2  * Copyright 2018 The Apollo Authors. All Rights Reserved.
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  * http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  *****************************************************************************/
16 
17 #pragma once
18 
19 #include <cuda_runtime_api.h>
20 #include <algorithm>
21 #include <fstream>
22 #include <string>
23 #include <vector>
24 
25 #include "NvCaffeParser.h"
26 #include "NvInfer.h"
27 #include "NvInferPlugin.h"
28 
31 
32 namespace nvinfer1 {
33 class ICaffePoolOutputDimensionsFormula : public IOutputDimensionsFormula {
34  public:
35  virtual DimsHW compute(DimsHW input_dims, DimsHW kernel_size, DimsHW stride,
36  DimsHW padding, DimsHW dilation,
37  const char *layerName) const {
38  const int kernel_extent_h = dilation.d[0] * (kernel_size.d[0] - 1) + 1;
39  const int kernel_extent_w = dilation.d[1] * (kernel_size.d[1] - 1) + 1;
40  auto &&h_temp = (input_dims.d[0] + 2 * padding.d[0] - kernel_extent_h) *
41  1.0 / stride.d[0];
42  auto &&w_temp = (input_dims.d[1] + 2 * padding.d[1] - kernel_extent_w) *
43  1.0 / stride.d[1];
44 
45  std::string str_name(layerName);
46  if (str_name.find("as_conv") == std::string::npos) {
47  return DimsHW(static_cast<int>(ceil(h_temp)) + 1,
48  static_cast<int>(ceil(w_temp)) + 1);
49  } else {
50  return DimsHW(static_cast<int>(h_temp) + 1, static_cast<int>(w_temp) + 1);
51  }
52  }
53 
56 };
57 
58 class Int8EntropyCalibrator : public IInt8EntropyCalibrator {
59  public:
61  const apollo::perception::inference::BatchStream &stream, int first_batch,
62  bool read_cache, std::string network)
63  : stream_(stream), read_cache_(read_cache), network_(network) {
64  DimsNCHW dims = stream_.getDims();
65  input_count_ = stream_.getBatchSize() * dims.c() * dims.h() * dims.w();
66  cudaMalloc(&device_input_, input_count_ * sizeof(float));
67  stream_.reset(first_batch);
68  }
69 
71  if (device_input_) {
72  (cudaFree(device_input_));
73  }
74  }
75 
76  int getBatchSize() const override { return stream_.getBatchSize(); }
77 
78  bool getBatch(void *bindings[], const char *names[],
79  int nbBindings) override {
80  if (!stream_.next()) {
81  return false;
82  }
83 
84  (cudaMemcpy(device_input_, stream_.getBatch(), input_count_ * sizeof(float),
85  cudaMemcpyHostToDevice));
86  bindings[0] = device_input_;
87  return true;
88  }
89 
90  const void *readCalibrationCache(size_t &length) override {
91  calibration_cache_.clear();
92  std::ifstream input(
93  apollo::perception::inference::locateFile(network_, "CalibrationTable"),
94  std::ios::binary);
95  input >> std::noskipws;
96  if (read_cache_ && input.good()) {
97  std::copy(std::istream_iterator<char>(input),
98  std::istream_iterator<char>(),
99  std::back_inserter(calibration_cache_));
100  }
101 
102  length = calibration_cache_.size();
103  return length ? &calibration_cache_[0] : nullptr;
104  }
105 
106  void writeCalibrationCache(const void *cache, size_t length) override {
107  std::ofstream output(
108  apollo::perception::inference::locateFile(network_, "CalibrationTable"),
109  std::ios::binary);
110  output.write(reinterpret_cast<const char *>(cache), length);
111  }
112 
113  virtual CalibrationAlgoType getAlgorithm() {
114  return CalibrationAlgoType::kENTROPY_CALIBRATION;
115  }
116 
117  private:
119  bool read_cache_ = true;
120  std::string network_ = "yolo";
121  size_t input_count_;
122  void *device_input_ = nullptr;
123  std::vector<char> calibration_cache_;
124 };
125 
126 } // namespace nvinfer1
std::string locateFile(const std::string &path, const std::string &input)
ICaffePoolOutputDimensionsFormula()
Definition: entropy_calibrator.h:54
const void * readCalibrationCache(size_t &length) override
Definition: entropy_calibrator.h:90
int getBatchSize() const override
Definition: entropy_calibrator.h:76
Definition: batch_stream.h:28
virtual DimsHW compute(DimsHW input_dims, DimsHW kernel_size, DimsHW stride, DimsHW padding, DimsHW dilation, const char *layerName) const
Definition: entropy_calibrator.h:35
virtual CalibrationAlgoType getAlgorithm()
Definition: entropy_calibrator.h:113
Definition: entropy_calibrator.h:32
void writeCalibrationCache(const void *cache, size_t length) override
Definition: entropy_calibrator.h:106
~ICaffePoolOutputDimensionsFormula()
Definition: entropy_calibrator.h:55
Int8EntropyCalibrator(const apollo::perception::inference::BatchStream &stream, int first_batch, bool read_cache, std::string network)
Definition: entropy_calibrator.h:60
bool getBatch(void *bindings[], const char *names[], int nbBindings) override
Definition: entropy_calibrator.h:78
Definition: entropy_calibrator.h:58
Definition: entropy_calibrator.h:33
virtual ~Int8EntropyCalibrator()
Definition: entropy_calibrator.h:70