libtorch c++호출(1)

4542 단어 pytorchC++
확인 됨https://github.com/pytorch/pytorch위의 pytorch 각 버 전 은 pytorch 1.0.0 이후 vs 2017 로 컴 파일 되 었 으 므 로 컴 파일 된 libtorch 를 다운로드 하여 사용 할 때 자신의 vs 버 전 을 주의해 야 합 니 다.
At least Visual Studio 2017 version 15.6 with the toolset 14.13 and NVTX are needed.
CUDA version
Newest supported VS version
9.2
Visual Studio 2017 Update 5 (15.5) ( _MSC_VER  <= 1912)
10.0
Visual Studio 2017 (15.X) ( _MSC_VER  < 1920)
10.1
Visual Studio 2019 (16.X) ( _MSC_VER  < 1930)
libtorch 소스 코드 컴 파일:
cmd

:: [Optional] If you want to build with VS 2019 generator, please change the value in the next line to `Visual Studio 16 2019`.
:: Note: This value is useless if Ninja is detected. However, you can force that by using `set USE_NINJA=OFF`.
set CMAKE_GENERATOR=Visual Studio 15 2017

:: Read the content in the previous section carefully before you proceed.
:: [Optional] If you want to override the underlying toolset used by Ninja and Visual Studio with CUDA, please run the following script block.
:: "Visual Studio 2017 Developer Command Prompt" will be run automatically.
:: Make sure you have CMake >= 3.12 before you do this when you use the Visual Studio generator.
set CMAKE_GENERATOR_TOOLSET_VERSION=14.11
set DISTUTILS_USE_SDK=1
for /f "usebackq tokens=*" %i in (`"%ProgramFiles(x86)%\Microsoft Visual Studio\Installer\vswhere.exe" -version [15^,16^) -products * -latest -property installationPath`) do call "%i\VC\Auxiliary\Build\vcvarsall.bat" x64 -vcvars_ver=%CMAKE_GENERATOR_TOOLSET_VERSION%

:: [Optional] If you want to override the cuda host compiler
set CUDAHOSTCXX=C:\Program Files (x86)\Microsoft Visual Studio\2017\Enterprise\VC\Tools\MSVC\14.11.25503\bin\HostX64\x64\cl.exe

python setup.py install

debug 와 release,CPU 와 GPU 버 전의 libtorch 를 공식 적 으로 제공 하기 때문에 일반적으로 우리 가 직접 라 이브 러 리 를 컴 파일 할 필요 가 없습니다.
제 가 직접 다운로드 한 libtorch 1.5.1-cu 10.2 라 이브 러 리 는 VS 2017 로 테스트 demo 를 컴 파일 했 습 니 다.demo 는 인터넷 에서 찾 았 고 일부 오류 가 해결 되 었 습 니 다.
(vs 2015 를 사용 하면 오류 가 발생 할 수 있 습 니 다:You need C++14 to compile PyTorch testtorch,해결 방법:vs 2017 )
libtorch 에 필요 한.pt 파일 을 만 듭 니 다.
import torch
import torchvision

# An instance of your model.
model = torchvision.models.resnet18()#                  

# An example input you would normally provide to your model's forward() method.
example = torch.rand(1, 3, 224, 224)

# Use torch.jit.trace to generate a torch.jit.ScriptModule via tracing.
traced_script_module = torch.jit.trace(model, example)
traced_script_module.save("model.pt")

코드 는 다음 과 같 습 니 다:
#include  // One-stop header.
#include 
#include 
int main() {
	// Deserialize the ScriptModule from a file using torch::jit::load().
	torch::jit::script::Module module = torch::jit::load("./model.pt");

	std::cout << "ok
"; // Create a vector of inputs. std::vector<:jit::ivalue> inputs; inputs.push_back(torch::ones({ 1, 3, 224, 224 })); // Execute the model and turn its output into a tensor. at::Tensor output = module.forward(inputs).toTensor(); std::cout << output.slice(1, 0, 5) << '
'; system("pause"); return 0; }

그리고 Pytorch 생 성test.exe,의존 라 이브 러 리 를 살 펴 보 았 습 니 다.
PS D:\Pytorch_test\x64\Release> dumpbin /dependents Pytorch_test.exe
Microsoft (R) COFF/PE Dumper Version 14.00.24215.1
Copyright (C) Microsoft Corporation.  All rights reserved.


Dump of file Pytorch_test.exe

File Type: EXECUTABLE IMAGE

  Image has the following dependencies:

    c10.dll
    torch_cpu.dll
    opencv_world410.dll
    MSVCP140.dll
    VCRUNTIME140.dll
    api-ms-win-crt-heap-l1-1-0.dll
    api-ms-win-crt-stdio-l1-1-0.dll
    api-ms-win-crt-runtime-l1-1-0.dll
    api-ms-win-crt-math-l1-1-0.dll
    api-ms-win-crt-locale-l1-1-0.dll
    KERNEL32.dll

그 중에서 libtorch 에 의존 하 는 라 이브 러 리 는 c10.dll 과 torch 밖 에 없습니다.cpu.dll 두 개.다른 의존 도 는 다음 명령 을 사용 하여 계속 찾 을 수 있 습 니 다:
dumpbin /dependents xxx.dll    xxx.exe

출력 결과:
ok
 0.0560 -0.6454 -0.5457  1.3441  0.3639
[ CPUFloatType{1,5} ]
       . . .

 
 
발생 할 수 있 는 문 제 는 아래 의 인용 을 참고 하 세 요.
참고 블 로그:libtorch error C2440:"초기 화":"torch::jit:script:Module"에서 전환 할 수 없 는 문제
참고 블 로그:C++pytorch 모델 호출(vs 2015+libtorch+pytorch)

좋은 웹페이지 즐겨찾기