calibrator.cpp 2.8 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980
  1. #include <iostream>
  2. #include <iterator>
  3. #include <fstream>
  4. #include <opencv2/dnn/dnn.hpp>
  5. #include "calibrator.h"
  6. #include "cuda_utils.h"
  7. #include "utils.h"
  8. Int8EntropyCalibrator2::Int8EntropyCalibrator2(int batchsize, int input_w, int input_h, const char* img_dir, const char* calib_table_name, const char* input_blob_name, bool read_cache)
  9. : batchsize_(batchsize)
  10. , input_w_(input_w)
  11. , input_h_(input_h)
  12. , img_idx_(0)
  13. , img_dir_(img_dir)
  14. , calib_table_name_(calib_table_name)
  15. , input_blob_name_(input_blob_name)
  16. , read_cache_(read_cache)
  17. {
  18. input_count_ = 3 * input_w * input_h * batchsize;
  19. CUDA_CHECK(cudaMalloc(&device_input_, input_count_ * sizeof(float)));
  20. read_files_in_dir(img_dir, img_files_);
  21. }
  22. Int8EntropyCalibrator2::~Int8EntropyCalibrator2()
  23. {
  24. CUDA_CHECK(cudaFree(device_input_));
  25. }
  26. int Int8EntropyCalibrator2::getBatchSize() const TRT_NOEXCEPT
  27. {
  28. return batchsize_;
  29. }
  30. bool Int8EntropyCalibrator2::getBatch(void* bindings[], const char* names[], int nbBindings) TRT_NOEXCEPT
  31. {
  32. if (img_idx_ + batchsize_ > (int)img_files_.size()) {
  33. return false;
  34. }
  35. std::vector<cv::Mat> input_imgs_;
  36. for (int i = img_idx_; i < img_idx_ + batchsize_; i++) {
  37. std::cout << img_files_[i] << " " << i << std::endl;
  38. cv::Mat temp = cv::imread(img_dir_ + img_files_[i]);
  39. if (temp.empty()){
  40. std::cerr << "Fatal error: image cannot open!" << std::endl;
  41. return false;
  42. }
  43. cv::Mat pr_img = preprocess_img(temp, input_w_, input_h_);
  44. input_imgs_.push_back(pr_img);
  45. }
  46. img_idx_ += batchsize_;
  47. cv::Mat blob = cv::dnn::blobFromImages(input_imgs_, 1.0 / 255.0, cv::Size(input_w_, input_h_), cv::Scalar(0, 0, 0), true, false);
  48. CUDA_CHECK(cudaMemcpy(device_input_, blob.ptr<float>(0), input_count_ * sizeof(float), cudaMemcpyHostToDevice));
  49. assert(!strcmp(names[0], input_blob_name_));
  50. bindings[0] = device_input_;
  51. return true;
  52. }
  53. const void* Int8EntropyCalibrator2::readCalibrationCache(size_t& length) TRT_NOEXCEPT
  54. {
  55. std::cout << "reading calib cache: " << calib_table_name_ << std::endl;
  56. calib_cache_.clear();
  57. std::ifstream input(calib_table_name_, std::ios::binary);
  58. input >> std::noskipws;
  59. if (read_cache_ && input.good())
  60. {
  61. std::copy(std::istream_iterator<char>(input), std::istream_iterator<char>(), std::back_inserter(calib_cache_));
  62. }
  63. length = calib_cache_.size();
  64. return length ? calib_cache_.data() : nullptr;
  65. }
  66. void Int8EntropyCalibrator2::writeCalibrationCache(const void* cache, size_t length) TRT_NOEXCEPT
  67. {
  68. std::cout << "writing calib cache: " << calib_table_name_ << " size: " << length << std::endl;
  69. std::ofstream output(calib_table_name_, std::ios::binary);
  70. output.write(reinterpret_cast<const char*>(cache), length);
  71. }