libTorch and VS2015 Configuration in Windows 10

1.Objective

  • Use pytorch1.2 to train an deeplearning model and convert it to another format model by torch.jit.trace module.
  • Use libTorch to load the converted model with the VS2015 in windows 10. 

2.Environment:

  • Windows 10 Home Version
  • Visual Studio 2015 S3 update version
  • Pytorch1.2(cpu only)
  • lbTorch1.2(windows cpu only)

3.Steps:

Becasue my previous experience is on Ubuntu16.04, but this time it is need to train and deploy the model in windows platform. So the prerequisite is intall the anaonda to create virtual environment to simulate Ubuntun operation method, also is cross-plaform.

3.1Anaconda Installation and Pytorch1.2 Virtual Environment Creation

3.1.1 Download The Right Version

Anaconda offical address:https://www.anaconda.com/distribution/, choose python3.7 version for windows. Due to the servier outside country, here is https://mirrors.tuna.tsinghua.edu.cn/anaconda/archive/ aslo can download the suitable version.

3.1.2 Create Virtual Evironment

After the installation, open the Anaconda Prompt(anaconda3) throgh the Start Menu.

conda create -n pytorch1.2 python=3.7

after this installation and initial, type follows command to activate the virtual environment:

conda activate pytorch1.2

3.1.3 Pytorch1.2 Installtion

Because the installation from offical website is slow, so change the software download source to Tisinghua.

conda config --add channels https://mirrors.tuna.tsinghua.edu.cn/anaconda/pkgs/free/
conda config --add channels https://mirrors.tuna.tsinghua.edu.cn/anaconda/pkgs/main/
conda config --set show_channel_urls yes

conda config --add channels https://mirrors.tuna.tsinghua.edu.cn/anaconda/cloud/conda-forge/
conda config --add channels https://mirrors.tuna.tsinghua.edu.cn/anaconda/cloud/msys2/
conda config --add channels https://mirrors.tuna.tsinghua.edu.cn/anaconda/cloud/bioconda/
conda config --add channels https://mirrors.tuna.tsinghua.edu.cn/anaconda/cloud/menpo/
conda config --add channels https://mirrors.tuna.tsinghua.edu.cn/anaconda/cloud/pytorch/
conda install pytorch==1.2.0 torchvision==0.4.0

waiting for the installation finished.

3.2 VSCode Installation and Model Generation

Use VsCode to comiplie the python code. Such as model traning and converstion by pytorch. Here is example code which will be deployed by libTorch with VS2015. The resnet18_model.pt just the model loaded by libTorch.

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("resnet18_model.pt")

print("model saved successfully!")

3.3 TibTorch Configuration 

Because we want the C++ code can be used in corss-platform. So it is compiled by CMake first, then is the VS2015 and generate exe file at last.

3.3.1 CMake Installation

conda install cmake

3.3.2 libTroch Download

Through my test, only the release version works. After download the zip file, uncompress it.

https://download.pytorch.org/libtorch/cu101/libtorch-win-shared-with-deps-1.2.0.zip

3.3.3 CMakeLists.txt File Generation

Here I give my example directly.

#1. cmake_version
cmake_minimum_required(VERSION 3.17 FATAL_ERROR)

#2. project name
project(example-app)

#3. head file path
INCLUDE_DIRECTORIES(
    include
)

#4. source files
SET(SRC_DIR_PATH src)
FILE(GLOB_RECURSE SOURCE_FILES "src/*.cpp")

#5. 
SET(Torch_DIR E:/Software/libtorch1.2-win-release/libtorch/)
SET(CMAKE_PREFIX_PATH E:/Software/libtorch1.2-win-release/libtorch/)
SET(CMAKE_BUILD_TYPE Release)
find_package(Torch REQUIRED)

#6.
add_executable(example-app ${SRC_DIR_PATH}/main.cpp)
target_link_libraries(example-app ${TORCH_LIBRARIES})
set_property(TARGET example-app PROPERTY CXX_STANDARD 11)

and the file directory and hierahchy as follows picture, and the only cpp file is the main.cpp which placed in the src folder.

Here is the main.cpp code:

#include <torch/script.h> // One-stop header.
#include <iostream>
#include <memory>

int main() {
	// Deserialize the ScriptModule from a file using torch::jit::load().
	//std::shared_ptr<torch::jit::script::Module> module = torch::jit::load("E:/VSCPlus/libtorch_Demo/resnet18_model.pt");
	torch::jit::script::Module module = torch::jit::load("E:/VSCPlus/libtorch_Demo/resnet18_model.pt");
  

	//assert(module != nullptr);
	//std::cout << "ok\n";
	// Create a vector of inputs.
	std::vector<torch::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(/*dim=*/1, /*start=*/0, /*end=*/5) << '\n';
	while (1);
}


3.3.4 Comiple By CMake

cd to build floder in libTorch_Demo, just type in cmake ..

cmake -Wno-dev ..

Congratulations:

and the generated files in build folder.

3.3.5 Compile and Run Using VS2015

Open the *.sln file using the VS2015, then adding the torch.lib to the dependency libraries, Using X64 and Release option to compile.

Another configuration is:

Then copy the c10.dll and torch.dll to the exe floder(my output floder in bin floder).

Run it, On yeah, YOU GOT IT!!!

猜你喜欢

转载自blog.csdn.net/jiaken2660/article/details/105579491
今日推荐