dk-liang / pytorch2libtorch

A demo for using C++ to inference the pytorch model

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

pytorch2libtorch

A demo for using C++ to inference the pytorch model

Introduction

Althought pytorch is efficient and convenient for developer, which still having many hinderer to deploy on many devices. Here, we demonstrate how to use c++ with libtorch to inference pytorch model.

Environment

python >= 3.6
pytorch >= 1.3
OpenCV >= 4.0.0 (It means C++ version instead of python.)

Pytorch-python

  • cd ./pytorch_code and put the pretrain model into ./model
  • Run python pth2lib.py to generate traced script model. The generated model can be found in ./model, which is named 'model_transfer.pt'.

LibTorch-C++

Now, we use C++ to inference the transfered model.

  • cp ./pytorch_code/model/model_transfer.pt ./C++_inference/model/

  • cd ./C++_inference

  • Download LibTorch from here.

  • Unzip the libTorch and rename as libtorch

  • mv libtorch ./lib/

  • Mkdir build directory. mkdir build

  • Compile the project. cd ./build , run cmake.. and make

  • Test. ./main ./data/test.jpg

.

About

A demo for using C++ to inference the pytorch model

License:MIT License


Languages

Language:C++ 47.8%Language:CMake 29.3%Language:Python 22.9%