diff --git a/include/tkDNN/Int8BatchStream.h b/include/tkDNN/Int8BatchStream.h index fc3863c..4349c1f 100644 --- a/include/tkDNN/Int8BatchStream.h +++ b/include/tkDNN/Int8BatchStream.h @@ -19,16 +19,21 @@ #include "utils.h" #include "tkdnn.h" -class BatchStream -{ +/* + * BatchStream implements the stream for the INT8 calibrator. + * It reads the two files .txt with the list of image file names + * and the list of label file names. + * It then iterates on images and labels. + */ +class BatchStream { public: BatchStream(tk::dnn::dataDim_t dim, int batchSize, int maxBatches, const std::string& fileimglist, const std::string& filelabellist); - virtual ~BatchStream() {} + virtual ~BatchStream() { } void reset(int firstBatch); bool next(); void skip(int skipCount); - float *getBatch() { return mBatch.data();} - float *getLabels() { return mLabels.data();} + float *getBatch() { return mBatch.data(); } + float *getLabels() { return mLabels.data(); } int getBatchesRead() const { return mBatchCount; } int getBatchSize() const { return mBatchSize; } nvinfer1::DimsNCHW getDims() const { return mDims; } @@ -43,7 +48,8 @@ private: int mBatchSize{ 0 }; int mMaxBatches{ 0 }; int mBatchCount{ 0 }; - int mFileCount{ 0 }, mFileBatchPos{ 0 }; + int mFileCount{ 0 }; + int mFileBatchPos{ 0 }; int mImageSize{ 0 }; nvinfer1::DimsNCHW mDims; diff --git a/include/tkDNN/Int8Calibrator.h b/include/tkDNN/Int8Calibrator.h index 4fd4a0e..4a0ea47 100644 --- a/include/tkDNN/Int8Calibrator.h +++ b/include/tkDNN/Int8Calibrator.h @@ -18,9 +18,17 @@ #include "tkdnn.h" #include "utils.h" -class Int8EntropyCalibrator : public nvinfer1::IInt8EntropyCalibrator{ +/* + * Int8EntropyCalibrator implements the INT8 calibrator to achieve the + * INT8 quantization. It uses a BatchStream stream to scroll through + * images data. It also implements the calibration cache, a way to + * save the calibration process results to reduce the running time: + * the calibration process takes a long time. + */ +class Int8EntropyCalibrator : public nvinfer1::IInt8EntropyCalibrator { public: - Int8EntropyCalibrator(BatchStream& stream, int firstBatch, const std::string& calibTableFilePath, const std::string& inputBlobName, bool readCache = true); + Int8EntropyCalibrator(BatchStream& stream, int firstBatch, const std::string& calibTableFilePath, + const std::string& inputBlobName, bool readCache = true); virtual ~Int8EntropyCalibrator() { checkCuda(cudaFree(mDeviceInput)); } int getBatchSize() const override { return mStream.getBatchSize(); } bool getBatch(void* bindings[], const char* names[], int nbBindings) override; @@ -29,7 +37,7 @@ public: private: BatchStream mStream; - const std::string mCalibTableFilePath{nullptr}; + const std::string mCalibTableFilePath{ nullptr }; const std::string mInputBlobName; bool mReadCache{ true }; diff --git a/src/Int8BatchStream.cpp b/src/Int8BatchStream.cpp index 12d04da..dd4399f 100644 --- a/src/Int8BatchStream.cpp +++ b/src/Int8BatchStream.cpp @@ -5,8 +5,7 @@ #include #include -BatchStream::BatchStream(tk::dnn::dataDim_t dim, int batchSize, int maxBatches, const std::string& fileimglist, const std::string& filelabellist) -{ +BatchStream::BatchStream(tk::dnn::dataDim_t dim, int batchSize, int maxBatches, const std::string& fileimglist, const std::string& filelabellist) { mBatchSize = batchSize; mMaxBatches = maxBatches; mDims = nvinfer1::DimsNCHW{ dim.n, dim.c, dim.h, dim.w }; @@ -25,22 +24,19 @@ BatchStream::BatchStream(tk::dnn::dataDim_t dim, int batchSize, int maxBatches, reset(0); } -void BatchStream::reset(int firstBatch) -{ +void BatchStream::reset(int firstBatch) { mBatchCount = 0; mFileCount = 0; mFileBatchPos = mDims.n(); skip(firstBatch); } -bool BatchStream::next() -{ +bool BatchStream::next() { std::cout<<"Next batch: "< 0 && mFileBatchPos <= mDims.n()); if (mFileBatchPos == mDims.n() && !update()) return false; @@ -53,10 +49,8 @@ bool BatchStream::next() return true; } -void BatchStream::skip(int skipCount) -{ - if (mBatchSize >= mDims.n() && mBatchSize%mDims.n() == 0 && mFileBatchPos == mDims.n()) - { +void BatchStream::skip(int skipCount) { + if (mBatchSize >= mDims.n() && mBatchSize%mDims.n() == 0 && mFileBatchPos == mDims.n()) { mFileCount += skipCount * mBatchSize / mDims.n(); return; } @@ -67,8 +61,7 @@ void BatchStream::skip(int skipCount) mBatchCount = x; } -void BatchStream::readInListFile(const std::string& dataFilePath, std::vector& mListIn) -{ +void BatchStream::readInListFile(const std::string& dataFilePath, std::vector& mListIn) { // dataFilePath contains the list of image paths int count = 0; FILE* f = fopen(dataFilePath.c_str(), "r"); @@ -76,8 +69,8 @@ void BatchStream::readInListFile(const std::string& dataFilePath, std::vector& res, bool fixshape) -{ +void BatchStream::readCVimage(std::string inputFileName, std::vector& res, bool fixshape) { // unaltered original DsImage cv::Mat m_OrigImage; // letterboxed DsImage given to the network as input @@ -104,7 +96,7 @@ void BatchStream::readCVimage(std::string inputFileName, std::vector& res int m_Height = m_OrigImage.rows; int m_Width = m_OrigImage.cols; - if(fixshape){ + if(fixshape) { m_Height = mHeight; m_Width = mWidth; } @@ -138,22 +130,18 @@ void BatchStream::readCVimage(std::string inputFileName, std::vector& res res.assign(m_LetterboxImage.begin(), m_LetterboxImage.end()); } -void BatchStream::readLabels(std::string inputFileName, std::vector& ris) -{ +void BatchStream::readLabels(std::string inputFileName, std::vector& ris) { std::ifstream is(inputFileName.c_str()); //read only the first number: the image sub-portion class while (true) { float val; - // Read is >> val; - // Check if (!is) { break; } - // Use // insert the first number and skip all others ris.push_back(val); - while( true ){ + while( true ) { char c; is >> c; if (is.peek() == '\n') //detect "\n" @@ -162,8 +150,7 @@ void BatchStream::readLabels(std::string inputFileName, std::vector& ris) } } -bool BatchStream::update() -{ +bool BatchStream::update() { std::string imgFileName = mListImg[mFileCount]; std::string labelFileName = mListLabel[mFileCount]; mFileCount++; diff --git a/src/Int8Calibrator.cpp b/src/Int8Calibrator.cpp index 9dfd662..773a9d8 100644 --- a/src/Int8Calibrator.cpp +++ b/src/Int8Calibrator.cpp @@ -7,17 +7,14 @@ Int8EntropyCalibrator::Int8EntropyCalibrator(BatchStream& stream, int firstBatch mStream(stream), mCalibTableFilePath(calibTableFilePath), mInputBlobName(inputBlobName.c_str()), - mReadCache(readCache) -{ + mReadCache(readCache) { nvinfer1::DimsNCHW dims = mStream.getDims(); mInputCount = mStream.getBatchSize() * dims.c() * dims.h() * dims.w(); checkCuda(cudaMalloc(&mDeviceInput, mInputCount * sizeof(float))); mStream.reset(firstBatch); - std::cout<<"mCalibTableFilePath\n"; } -bool Int8EntropyCalibrator::getBatch(void* bindings[], const char* names[], int nbBindings) -{ +bool Int8EntropyCalibrator::getBatch(void* bindings[], const char* names[], int nbBindings) { if (!mStream.next()) return false; @@ -27,8 +24,7 @@ bool Int8EntropyCalibrator::getBatch(void* bindings[], const char* names[], int return true; } -const void* Int8EntropyCalibrator::readCalibrationCache(size_t& length) -{ +const void* Int8EntropyCalibrator::readCalibrationCache(size_t& length) { mCalibrationCache.clear(); assert(!mCalibTableFilePath.empty()); std::ifstream input(mCalibTableFilePath, std::ios::binary); @@ -42,10 +38,9 @@ const void* Int8EntropyCalibrator::readCalibrationCache(size_t& length) return length ? &mCalibrationCache[0] : nullptr; } -void Int8EntropyCalibrator::writeCalibrationCache(const void* cache, size_t length) -{ +void Int8EntropyCalibrator::writeCalibrationCache(const void* cache, size_t length) { assert(!mCalibTableFilePath.empty()); std::ofstream output(mCalibTableFilePath, std::ios::binary); output.write(reinterpret_cast(cache), length); output.close(); -} +} \ No newline at end of file