Fix the INT8 calibrator sintax
Signed-off-by: Davide Sapienza <sapienza.dav@gmail.com>
This commit is contained in:
@@ -19,16 +19,21 @@
|
|||||||
#include "utils.h"
|
#include "utils.h"
|
||||||
#include "tkdnn.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:
|
public:
|
||||||
BatchStream(tk::dnn::dataDim_t dim, int batchSize, int maxBatches, const std::string& fileimglist, const std::string& filelabellist);
|
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);
|
void reset(int firstBatch);
|
||||||
bool next();
|
bool next();
|
||||||
void skip(int skipCount);
|
void skip(int skipCount);
|
||||||
float *getBatch() { return mBatch.data();}
|
float *getBatch() { return mBatch.data(); }
|
||||||
float *getLabels() { return mLabels.data();}
|
float *getLabels() { return mLabels.data(); }
|
||||||
int getBatchesRead() const { return mBatchCount; }
|
int getBatchesRead() const { return mBatchCount; }
|
||||||
int getBatchSize() const { return mBatchSize; }
|
int getBatchSize() const { return mBatchSize; }
|
||||||
nvinfer1::DimsNCHW getDims() const { return mDims; }
|
nvinfer1::DimsNCHW getDims() const { return mDims; }
|
||||||
@@ -43,7 +48,8 @@ private:
|
|||||||
int mBatchSize{ 0 };
|
int mBatchSize{ 0 };
|
||||||
int mMaxBatches{ 0 };
|
int mMaxBatches{ 0 };
|
||||||
int mBatchCount{ 0 };
|
int mBatchCount{ 0 };
|
||||||
int mFileCount{ 0 }, mFileBatchPos{ 0 };
|
int mFileCount{ 0 };
|
||||||
|
int mFileBatchPos{ 0 };
|
||||||
int mImageSize{ 0 };
|
int mImageSize{ 0 };
|
||||||
|
|
||||||
nvinfer1::DimsNCHW mDims;
|
nvinfer1::DimsNCHW mDims;
|
||||||
|
|||||||
@@ -18,9 +18,17 @@
|
|||||||
#include "tkdnn.h"
|
#include "tkdnn.h"
|
||||||
#include "utils.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:
|
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)); }
|
virtual ~Int8EntropyCalibrator() { checkCuda(cudaFree(mDeviceInput)); }
|
||||||
int getBatchSize() const override { return mStream.getBatchSize(); }
|
int getBatchSize() const override { return mStream.getBatchSize(); }
|
||||||
bool getBatch(void* bindings[], const char* names[], int nbBindings) override;
|
bool getBatch(void* bindings[], const char* names[], int nbBindings) override;
|
||||||
@@ -29,7 +37,7 @@ public:
|
|||||||
|
|
||||||
private:
|
private:
|
||||||
BatchStream mStream;
|
BatchStream mStream;
|
||||||
const std::string mCalibTableFilePath{nullptr};
|
const std::string mCalibTableFilePath{ nullptr };
|
||||||
const std::string mInputBlobName;
|
const std::string mInputBlobName;
|
||||||
bool mReadCache{ true };
|
bool mReadCache{ true };
|
||||||
|
|
||||||
|
|||||||
+14
-27
@@ -5,8 +5,7 @@
|
|||||||
#include <opencv2/highgui/highgui.hpp>
|
#include <opencv2/highgui/highgui.hpp>
|
||||||
#include <opencv2/imgproc/imgproc.hpp>
|
#include <opencv2/imgproc/imgproc.hpp>
|
||||||
|
|
||||||
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;
|
mBatchSize = batchSize;
|
||||||
mMaxBatches = maxBatches;
|
mMaxBatches = maxBatches;
|
||||||
mDims = nvinfer1::DimsNCHW{ dim.n, dim.c, dim.h, dim.w };
|
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);
|
reset(0);
|
||||||
}
|
}
|
||||||
|
|
||||||
void BatchStream::reset(int firstBatch)
|
void BatchStream::reset(int firstBatch) {
|
||||||
{
|
|
||||||
mBatchCount = 0;
|
mBatchCount = 0;
|
||||||
mFileCount = 0;
|
mFileCount = 0;
|
||||||
mFileBatchPos = mDims.n();
|
mFileBatchPos = mDims.n();
|
||||||
skip(firstBatch);
|
skip(firstBatch);
|
||||||
}
|
}
|
||||||
|
|
||||||
bool BatchStream::next()
|
bool BatchStream::next() {
|
||||||
{
|
|
||||||
std::cout<<"Next batch: "<<mBatchCount<<" of "<<mMaxBatches<<"\n";
|
std::cout<<"Next batch: "<<mBatchCount<<" of "<<mMaxBatches<<"\n";
|
||||||
if (mBatchCount == mMaxBatches-1)
|
if (mBatchCount == mMaxBatches-1)
|
||||||
return false;
|
return false;
|
||||||
|
|
||||||
for (int csize = 1, batchPos = 0; batchPos < mBatchSize; batchPos += csize, mFileBatchPos += csize)
|
for (int csize = 1, batchPos = 0; batchPos < mBatchSize; batchPos += csize, mFileBatchPos += csize) {
|
||||||
{
|
|
||||||
assert(mFileBatchPos > 0 && mFileBatchPos <= mDims.n());
|
assert(mFileBatchPos > 0 && mFileBatchPos <= mDims.n());
|
||||||
if (mFileBatchPos == mDims.n() && !update())
|
if (mFileBatchPos == mDims.n() && !update())
|
||||||
return false;
|
return false;
|
||||||
@@ -53,10 +49,8 @@ bool BatchStream::next()
|
|||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
|
|
||||||
void BatchStream::skip(int skipCount)
|
void BatchStream::skip(int skipCount) {
|
||||||
{
|
if (mBatchSize >= mDims.n() && mBatchSize%mDims.n() == 0 && mFileBatchPos == mDims.n()) {
|
||||||
if (mBatchSize >= mDims.n() && mBatchSize%mDims.n() == 0 && mFileBatchPos == mDims.n())
|
|
||||||
{
|
|
||||||
mFileCount += skipCount * mBatchSize / mDims.n();
|
mFileCount += skipCount * mBatchSize / mDims.n();
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
@@ -67,8 +61,7 @@ void BatchStream::skip(int skipCount)
|
|||||||
mBatchCount = x;
|
mBatchCount = x;
|
||||||
}
|
}
|
||||||
|
|
||||||
void BatchStream::readInListFile(const std::string& dataFilePath, std::vector<std::string>& mListIn)
|
void BatchStream::readInListFile(const std::string& dataFilePath, std::vector<std::string>& mListIn) {
|
||||||
{
|
|
||||||
// dataFilePath contains the list of image paths
|
// dataFilePath contains the list of image paths
|
||||||
int count = 0;
|
int count = 0;
|
||||||
FILE* f = fopen(dataFilePath.c_str(), "r");
|
FILE* f = fopen(dataFilePath.c_str(), "r");
|
||||||
@@ -76,8 +69,8 @@ void BatchStream::readInListFile(const std::string& dataFilePath, std::vector<st
|
|||||||
FatalError("failed to open " + dataFilePath);
|
FatalError("failed to open " + dataFilePath);
|
||||||
|
|
||||||
char str[512];
|
char str[512];
|
||||||
while (fgets(str, 512, f) != NULL){
|
while (fgets(str, 512, f) != NULL) {
|
||||||
for (int i = 0; str[i] != '\0'; ++i){
|
for (int i = 0; str[i] != '\0'; ++i) {
|
||||||
if (str[i] == '\n'){
|
if (str[i] == '\n'){
|
||||||
str[i] = '\0';
|
str[i] = '\0';
|
||||||
break;
|
break;
|
||||||
@@ -91,8 +84,7 @@ void BatchStream::readInListFile(const std::string& dataFilePath, std::vector<st
|
|||||||
fclose(f);
|
fclose(f);
|
||||||
}
|
}
|
||||||
|
|
||||||
void BatchStream::readCVimage(std::string inputFileName, std::vector<float>& res, bool fixshape)
|
void BatchStream::readCVimage(std::string inputFileName, std::vector<float>& res, bool fixshape) {
|
||||||
{
|
|
||||||
// unaltered original DsImage
|
// unaltered original DsImage
|
||||||
cv::Mat m_OrigImage;
|
cv::Mat m_OrigImage;
|
||||||
// letterboxed DsImage given to the network as input
|
// letterboxed DsImage given to the network as input
|
||||||
@@ -104,7 +96,7 @@ void BatchStream::readCVimage(std::string inputFileName, std::vector<float>& res
|
|||||||
|
|
||||||
int m_Height = m_OrigImage.rows;
|
int m_Height = m_OrigImage.rows;
|
||||||
int m_Width = m_OrigImage.cols;
|
int m_Width = m_OrigImage.cols;
|
||||||
if(fixshape){
|
if(fixshape) {
|
||||||
m_Height = mHeight;
|
m_Height = mHeight;
|
||||||
m_Width = mWidth;
|
m_Width = mWidth;
|
||||||
}
|
}
|
||||||
@@ -138,22 +130,18 @@ void BatchStream::readCVimage(std::string inputFileName, std::vector<float>& res
|
|||||||
res.assign(m_LetterboxImage.begin<float>(), m_LetterboxImage.end<float>());
|
res.assign(m_LetterboxImage.begin<float>(), m_LetterboxImage.end<float>());
|
||||||
}
|
}
|
||||||
|
|
||||||
void BatchStream::readLabels(std::string inputFileName, std::vector<float>& ris)
|
void BatchStream::readLabels(std::string inputFileName, std::vector<float>& ris) {
|
||||||
{
|
|
||||||
std::ifstream is(inputFileName.c_str());
|
std::ifstream is(inputFileName.c_str());
|
||||||
//read only the first number: the image sub-portion class
|
//read only the first number: the image sub-portion class
|
||||||
while (true) {
|
while (true) {
|
||||||
float val;
|
float val;
|
||||||
// Read
|
|
||||||
is >> val;
|
is >> val;
|
||||||
// Check
|
|
||||||
if (!is) {
|
if (!is) {
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
// Use
|
|
||||||
// insert the first number and skip all others
|
// insert the first number and skip all others
|
||||||
ris.push_back(val);
|
ris.push_back(val);
|
||||||
while( true ){
|
while( true ) {
|
||||||
char c;
|
char c;
|
||||||
is >> c;
|
is >> c;
|
||||||
if (is.peek() == '\n') //detect "\n"
|
if (is.peek() == '\n') //detect "\n"
|
||||||
@@ -162,8 +150,7 @@ void BatchStream::readLabels(std::string inputFileName, std::vector<float>& ris)
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
bool BatchStream::update()
|
bool BatchStream::update() {
|
||||||
{
|
|
||||||
std::string imgFileName = mListImg[mFileCount];
|
std::string imgFileName = mListImg[mFileCount];
|
||||||
std::string labelFileName = mListLabel[mFileCount];
|
std::string labelFileName = mListLabel[mFileCount];
|
||||||
mFileCount++;
|
mFileCount++;
|
||||||
|
|||||||
+5
-10
@@ -7,17 +7,14 @@ Int8EntropyCalibrator::Int8EntropyCalibrator(BatchStream& stream, int firstBatch
|
|||||||
mStream(stream),
|
mStream(stream),
|
||||||
mCalibTableFilePath(calibTableFilePath),
|
mCalibTableFilePath(calibTableFilePath),
|
||||||
mInputBlobName(inputBlobName.c_str()),
|
mInputBlobName(inputBlobName.c_str()),
|
||||||
mReadCache(readCache)
|
mReadCache(readCache) {
|
||||||
{
|
|
||||||
nvinfer1::DimsNCHW dims = mStream.getDims();
|
nvinfer1::DimsNCHW dims = mStream.getDims();
|
||||||
mInputCount = mStream.getBatchSize() * dims.c() * dims.h() * dims.w();
|
mInputCount = mStream.getBatchSize() * dims.c() * dims.h() * dims.w();
|
||||||
checkCuda(cudaMalloc(&mDeviceInput, mInputCount * sizeof(float)));
|
checkCuda(cudaMalloc(&mDeviceInput, mInputCount * sizeof(float)));
|
||||||
mStream.reset(firstBatch);
|
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())
|
if (!mStream.next())
|
||||||
return false;
|
return false;
|
||||||
|
|
||||||
@@ -27,8 +24,7 @@ bool Int8EntropyCalibrator::getBatch(void* bindings[], const char* names[], int
|
|||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
|
|
||||||
const void* Int8EntropyCalibrator::readCalibrationCache(size_t& length)
|
const void* Int8EntropyCalibrator::readCalibrationCache(size_t& length) {
|
||||||
{
|
|
||||||
mCalibrationCache.clear();
|
mCalibrationCache.clear();
|
||||||
assert(!mCalibTableFilePath.empty());
|
assert(!mCalibTableFilePath.empty());
|
||||||
std::ifstream input(mCalibTableFilePath, std::ios::binary);
|
std::ifstream input(mCalibTableFilePath, std::ios::binary);
|
||||||
@@ -42,10 +38,9 @@ const void* Int8EntropyCalibrator::readCalibrationCache(size_t& length)
|
|||||||
return length ? &mCalibrationCache[0] : nullptr;
|
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());
|
assert(!mCalibTableFilePath.empty());
|
||||||
std::ofstream output(mCalibTableFilePath, std::ios::binary);
|
std::ofstream output(mCalibTableFilePath, std::ios::binary);
|
||||||
output.write(reinterpret_cast<const char*>(cache), length);
|
output.write(reinterpret_cast<const char*>(cache), length);
|
||||||
output.close();
|
output.close();
|
||||||
}
|
}
|
||||||
Reference in New Issue
Block a user