728x90
반응형

 

Windows 환경에서 TensorRT를 설치 하여 Visual Studio 에서 사용 할 수 있다. 

  • C++ 지원
  • Python 미지원 (2020.06.04 TensorRT 7.1 기준)
  • The Windows zip package for TensorRT does not provide Python support. Python may be supported in the future.

 

 

설치 환경

  • windows 10 64bit
  • CUDA 10.0
  • cuDNN 7.5.0
  • TensorRT 5.1.5.0
  • Visual Studio 2015

 

 

1. 설치

TensorRT 공식 홈페이지에서 zip 파일로 TensorRT를 다운 받은 뒤 ( Windows를 지원하는 버전이여야함 )

 

 

2. 환경 변수 설정

압축을 풀고, lib 폴더의 위치를 환경변수에 추가한다. 

 

 

(환경변수를 추가하였다면 dll 파일은 옮기지 않아도 된다. )

3. dll 파일 옮기기 

그 다음 TensorRT 의 lib 폴더에 들어있는 dll 파일들을

CUDA를 다운 받은 위치 (C:\Program Files\NVIDIA GPU Computing Toolkit\CUDA\vX.Y\bin)에 넣어준다.

  • nvinfer.dll
  • nvinfer_plugin.dll
  • nvonnxparser.dll
  • nvparsers.dll

 

4. Visual Studio 2015 or 2017 에서 프로젝트 설정

 

TensorRT 를 사용하기 위해서는 TensorRT 폴더 뿐만 아니라 NVIDIA의 CUDA 와 관련된 폴더도 설정해준다.

 

4.1 VC++ Directories > Executable Directories 에 다운받은 TensorRT 폴더의 lib 경로를 추가 한다.

  • C:\TensorRT-5.1.5.0\lib
  • C:\Program Files\NVIDIA GPU Computing Toolkit\CUDA\v10.0\lib\x64

4.2 C/C++ > General > AdditionalDirectories 에 다운받은 TensorRT 폴더의 include 경로를 추가 한다.

  • C:\TensorRT-5.1.5.0\include
  • C:\Program Files\NVIDIA GPU Computing Toolkit\CUDA\v10.0\include

4.3 Linker > Input > Additional Dependencies 에 nvinfer.lib 파일을 비롯한 다른 LIB 파일들의 목록을 추가한다.

 

TensorRT 라이브러리

  • nvinfer.lib
  • nvinfer_plugin.lib
  • nvonnxparser.lib
  • nvparsers.lib

CUDA 라이브러리

  • cublas.lib
  • cuda.lib
  • cudart.lib
  • cudnn.lib 
  • 등 

 

5. 제대로 설정 되었는지 include 해보기

#include <NvInfer.h>

 

 

 

 

6. 실행해보기

#include <algorithm>
#include <chrono>
#include <cstdlib>
#include <cuda_runtime_api.h>
#include <fstream>
#include <iostream>
#include <string>
#include <sys/stat.h>
#include <unordered_map>
#include <cassert>
#include <vector>

#include "NvInfer.h"
#include "NvUffParser.h"
#include "NvUtils.h"

using namespace std;
using namespace nvinfer1;
using namespace nvuffparser;

// TensorRT Logger
class Logger : public nvinfer1::ILogger
{
	void log(Severity severity, const char* msg) override
	{
		if (severity != Severity::kINFO)
			std::cout << msg << std::endl;
	}
} gLogger;


int main()
{

	// UFF file
	string uffFile("example.uff");

	// Network Create 
	IBuilder*           builder = createInferBuilder(gLogger);
	INetworkDefinition* network = builder->createNetwork();
	IUffParser*      parser = createUffParser();

	// UFF Parser 
	parser->registerInput("tower_0/Placeholder", DimsCHW(3, 256, 192), UffInputOrder::kNCHW);
	parser->registerOutput("tower_0/out/BiasAdd");

	cout << "Parsing network...";
	parser->parse(uffFile.c_str(), *network, DataType::kFLOAT);
	cout << "  DONE!" << endl;

	// Engine Build
	builder->setMaxBatchSize(1);
	builder->setMaxWorkspaceSize(1 << 20);
	builder->setFp16Mode(true);
	cout << "Building engine...";
	ICudaEngine* engine = builder->buildCudaEngine(*network);
	cout << "  DONE!" << endl;

	// Destroy
	parser->destroy();
	network->destroy();
	builder->destroy();

	// Serialize Engine
	cout << "Serializing model...";
	nvinfer1::IHostMemory* serializedModel = engine->serialize();
	engine->destroy();
	cout << "  DONE!" << endl;

	// Save Engine
	std::cout << "Saving serialized model to disk...";
	std::ofstream fileEngine("example.fp16trt", std::ios::out | std::ios::binary);
	fileEngine.write((char*)(serializedModel->data()), serializedModel->size());
	fileEngine.close();
	serializedModel->destroy();
	std::cout << "  DONE!" << std::endl;


	// Engine File
	//string engineFile("Pose.fp16trt");

	// Deserialize Engine
	//IRuntime* runtime = createInferRuntime(gLogger);
	//ICudaEngine* engine = runtime->deserializeCudaEngine(serializedModel->data(), serializedModel->size(), nullptr);
	//IExecutionContext *context = engine->createExecutionContext();
	//assert(context != nullptr);

	
	

	return 0;
}

 

 

 

 

 

 

Windows 에서는 Visual Studio 2017을 이용하여 C++ 기반 TensorRT 샘플을 실행 해 볼 수 있다.

또한, VS2015 버전에서는 샘플을 실행 할 수 없으나, C++을 이용하여 TensorRT 를 사용 할 수 있다. 

 

하지만 공식 홈페이지에 따르면 !@!! 

 

2. Getting Started
Ensure you are familiar with the following installation requirements and notes.
The Windows zip package for TensorRT does not provide Python support.

Python may be supported in the future.

 

Windows 환경에서 Python 을 이용하여 TensorRT를 사용 할 수 없다. (2019.11.11 기준 미지원)

Python 을 이용하여 TensorRT를 사용하고 싶다면 Ubuntu 환경에서 실행해야한다.

 

 

 

 

 

 

 

 

참고자료 :

https://docs.nvidia.com/deeplearning/sdk/tensorrt-install-guide/index.html

 

TensorRT Installation Guide :: Deep Learning SDK Documentation

Upgrading TensorRT to the latest version is only supported when the currently installed TensorRT version is equal to or newer than the last two public releases. For example, TensorRT 6.0.x supports upgrading from TensorRT 5.0.x and TensorRT 5.1.x. If you w

docs.nvidia.com

 

728x90
반응형