Skip to content

Commit

Permalink
re-organized plugins and calibrators
Browse files Browse the repository at this point in the history
  • Loading branch information
dusty-nv committed Dec 21, 2022
1 parent 0bc7548 commit f77e249
Show file tree
Hide file tree
Showing 37 changed files with 7,597 additions and 9 deletions.
33 changes: 24 additions & 9 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -102,6 +102,17 @@ else()
endif()
endif()

# check for VPI (TODO: VPI 1.0 support for JetPack 4.x)
find_package(VPI 2.0)

if( NOT VPI_FOUND )
message("-- didn't find VPI on system, disabling VPI")
else()
message("-- VPI version: " ${VPI_VERSION})
set(HAS_VPI 1)
add_definitions(-DHAS_VPI)
endif()

# setup project output paths
set(PROJECT_OUTPUT_DIR ${PROJECT_BINARY_DIR}/${CMAKE_SYSTEM_PROCESSOR})
set(PROJECT_INCLUDE_DIR ${PROJECT_OUTPUT_DIR}/include)
Expand All @@ -120,29 +131,29 @@ set(CMAKE_ARCHIVE_OUTPUT_DIRECTORY ${PROJECT_OUTPUT_DIR}/lib)
include_directories(${PROJECT_INCLUDE_DIR} ${PROJECT_INCLUDE_DIR}/jetson-inference ${PROJECT_INCLUDE_DIR}/jetson-utils)
include_directories(/usr/include/gstreamer-1.0 /usr/lib/${CMAKE_SYSTEM_PROCESSOR}/gstreamer-1.0/include /usr/include/glib-2.0 /usr/include/libxml2 /usr/lib/${CMAKE_SYSTEM_PROCESSOR}/glib-2.0/include/)

file(GLOB inferenceSources c/*.cpp c/*.cu calibration/*.cpp)
file(GLOB inferenceIncludes c/*.h c/*.cuh calibration/*.h)
file(GLOB inferenceSources c/*.cpp c/*.cu c/calibration/*.cpp c/trackers/*.cpp)
file(GLOB inferenceIncludes c/*.h c/*.cuh c/calibration/*.h c/trackers/*.h)

if(BUILD_EXPERIMENTAL)
message("-- BUILD_EXPERIMENTAL enabled")

file(GLOB experimentalSources c/experimental/*.cpp c/experimental/*.cu)
file(GLOB experimentalIncludes c/experimental/*.h c/experimental/*.cuh)
file(GLOB_RECURSE pluginSources plugins/*.cpp plugins/*.cu)
file(GLOB_RECURSE pluginSources c/plugins/*.cpp c/plugins/*.cu)

list(APPEND inferenceSources ${experimentalSources})
list(APPEND inferenceIncludes ${experimentalIncludes})
else()
message("-- BUILD_EXPERIMENTAL disabled")
file(GLOB pluginSources plugins/*.cpp plugins/*.cu plugins/pose/trt_pose/parse/*.cpp)
file(GLOB pluginSources c/plugins/*.cpp c/plugins/*.cu c/plugins/pose/trt_pose/parse/*.cpp)
endif()

if(CMAKE_SYSTEM_PROCESSOR MATCHES "aarch64")
link_directories(/usr/lib/aarch64-linux-gnu/tegra)
endif()

cuda_add_library(jetson-inference SHARED ${inferenceSources} ${pluginSources})
target_include_directories(jetson-inference PRIVATE ${PROJECT_SOURCE_DIR}/plugins/pose/trt_pose/parse)
target_include_directories(jetson-inference PRIVATE ${PROJECT_SOURCE_DIR}/c/plugins/pose/trt_pose/parse)

# transfer all headers to the include directory
file(MAKE_DIRECTORY ${PROJECT_INCLUDE_DIR}/jetson-inference)
Expand Down Expand Up @@ -184,13 +195,17 @@ target_link_libraries(jetson-inference jetson-utils nvinfer nvinfer_plugin nvcaf

if(CUDA_VERSION_MAJOR GREATER 9)
target_link_libraries(jetson-inference nvonnxparser)
endif()

if(HAS_OPENCV)
message("-- Linking jetson-inference with OpenCV " ${OpenCV_VERSION})
target_link_libraries(jetson-inference opencv_core opencv_calib3d)
endif()
if(HAS_OPENCV)
message("-- linking jetson-inference with OpenCV " ${OpenCV_VERSION})
target_link_libraries(jetson-inference opencv_core opencv_calib3d)
endif()

if(HAS_VPI)
message("-- linking jetson-inference with VPI " ${VPI_VERSION})
target_link_libraries(jetson-inference vpi)
endif()

# install includes
foreach(include ${inferenceIncludes})
Expand Down
114 changes: 114 additions & 0 deletions c/calibration/randInt8Calibrator.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,114 @@
/*
* Copyright (c) 2018, NVIDIA CORPORATION. All rights reserved.
*
* Permission is hereby granted, free of charge, to any person obtaining a
* copy of this software and associated documentation files (the "Software"),
* to deal in the Software without restriction, including without limitation
* the rights to use, copy, modify, merge, publish, distribute, sublicense,
* and/or sell copies of the Software, and to permit persons to whom the
* Software is furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in
* all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
* THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
* FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
* DEALINGS IN THE SOFTWARE.
*/

#include "randInt8Calibrator.h"
#include "cudaUtility.h"

#include <random>
#include <iterator>
#include <fstream>


#if NV_TENSORRT_MAJOR >= 4

//-------------------------------------------------------------------------------------------------
static inline int volume(nvinfer1::Dims dims)
{
return std::accumulate(dims.d, dims.d + dims.nbDims, 1, std::multiplies<int>());
}
//-------------------------------------------------------------------------------------------------


// constructor
randInt8Calibrator::randInt8Calibrator( int totalSamples, std::string cacheFile, const std::map<std::string, nvinfer1::Dims3>& inputDimensions )
: mTotalSamples(totalSamples)
, mCurrentSample(0)
, mCacheFile(cacheFile)
, mInputDimensions(inputDimensions)
{
std::default_random_engine generator;
std::uniform_real_distribution<float> distribution(-1.0F, 1.0F);

for( auto& elem : mInputDimensions )
{
int elemCount = volume(elem.second);

std::vector<float> rnd_data(elemCount);

for( auto& val : rnd_data )
val = distribution(generator);

void* data;

CUDA(cudaMalloc(&data, elemCount * sizeof(float)));
CUDA(cudaMemcpy(data, &rnd_data[0], elemCount * sizeof(float), cudaMemcpyHostToDevice));

mInputDeviceBuffers.insert(std::make_pair(elem.first, data));
}
}


// destructor
randInt8Calibrator::~randInt8Calibrator()
{
for( auto& elem : mInputDeviceBuffers )
CUDA(cudaFree(elem.second));
}


// getBatch()
bool randInt8Calibrator::getBatch( void* bindings[], const char* names[], int nbBindings ) NOEXCEPT
{
if( mCurrentSample >= mTotalSamples )
return false;

for( int i = 0; i < nbBindings; ++i )
bindings[i] = mInputDeviceBuffers[names[i]];

++mCurrentSample;
return true;
}


// readCalibrationCache()
const void* randInt8Calibrator::readCalibrationCache( size_t& length ) NOEXCEPT
{
mCalibrationCache.clear();
std::ifstream input(mCacheFile, std::ios::binary);
input >> std::noskipws;

if (input.good())
std::copy(std::istream_iterator<char>(input), std::istream_iterator<char>(), std::back_inserter(mCalibrationCache));

length = mCalibrationCache.size();
return length ? &mCalibrationCache[0] : nullptr;
}


// writeCalibrationCache()
void randInt8Calibrator::writeCalibrationCache( const void*, size_t ) NOEXCEPT
{

}

#endif

86 changes: 86 additions & 0 deletions c/calibration/randInt8Calibrator.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,86 @@
/*
* Copyright (c) 2018, NVIDIA CORPORATION. All rights reserved.
*
* Permission is hereby granted, free of charge, to any person obtaining a
* copy of this software and associated documentation files (the "Software"),
* to deal in the Software without restriction, including without limitation
* the rights to use, copy, modify, merge, publish, distribute, sublicense,
* and/or sell copies of the Software, and to permit persons to whom the
* Software is furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in
* all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
* THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
* FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
* DEALINGS IN THE SOFTWARE.
*/

#ifndef __RAND_INT8_CALIBRATOR_H__
#define __RAND_INT8_CALIBRATOR_H__

#include "tensorNet.h"

#include <map>
#include <string>
#include <vector>


#if NV_TENSORRT_MAJOR >= 4

/**
* Random INT8 Calibrator.
* This calibrator is for testing performance without needing
* sample datasets to generate the INT8 calibration table.
*/
class randInt8Calibrator : public nvinfer1::IInt8EntropyCalibrator
{
public:
/**
* Constructor
*/
randInt8Calibrator( int totalSamples, std::string cacheFile,
const std::map<std::string, nvinfer1::Dims3>& inputDimensions );

/**
* Destructor
*/
~randInt8Calibrator();

/**
* getBatchSize()
*/
inline int getBatchSize() const NOEXCEPT override { return 1; }

/**
* getBatch()
*/
bool getBatch(void* bindings[], const char* names[], int nbBindings) NOEXCEPT override;

/**
* readCalibrationCache()
*/
const void* readCalibrationCache(size_t& length) NOEXCEPT override;

/**
* writeCalibrationCache()
*/
virtual void writeCalibrationCache(const void*, size_t) NOEXCEPT override;

private:
int mTotalSamples;
int mCurrentSample;

std::string mCacheFile;
std::map<std::string, void*> mInputDeviceBuffers;
std::map<std::string, nvinfer1::Dims3> mInputDimensions;
std::vector<char> mCalibrationCache;
};

#endif
#endif

Loading

0 comments on commit f77e249

Please sign in to comment.