• Stars
    star
    331
  • Rank 127,323 (Top 3 %)
  • Language
    C++
  • Created almost 6 years ago
  • Updated almost 4 years ago

Reviews

There are no reviews yet. Be the first to send feedback to the community and the maintainers!

Repository Details

PyTorch C++ inference with LibTorch

This demo will demonstrate how to use LibTorch to build your C++ application.

[UPDATE 2019/01/18] : Init the repo, test with PyTorch1.0.
[UPDATE 2020/02/22] : Thanks for Ageliss and his PR, which update this demo to fit LibTorch1.4.0 and OpenCV4.0.
[UPDATE 2020/04/15] : Retest this tutorial with OpenCV4.3/PyTorch1.4/LibTorch1.4, update readme for beginner.
[UPDATE 2020/04/25] : Update CMakeLists.txt to fit C++14, retest with CUDA10.0/PyTorch1.5/LibTorch1.5.
[UPDATE 2020/11/08] : Improve readme, retest with PyTorch1.7/ CUDA10.2+cuDNNv7.6.5 and CUDA11.0+cuDNNv8.0.4

Contents

Preparation

Step 0: Dependencies

Make sure LibTorch and OpenCV have been installed correctly.

  • Install OpenCV: for Linux, for Mac OS
  • Get LibTorch: download LibTorch package from the official website, then unpack it, for example:
cd path_to_your_workspace
wget https://download.pytorch.org/libtorch/cu102/libtorch-cxx11-abi-shared-with-deps-1.7.0.zip
unzip libtorch-cxx11-abi-shared-with-deps-1.7.0.zip

Step 1: JIT Model

Export torch script file, we use resnet18/resnet50 in this demo. (see model_trace.py)

Step 2: Cpp Program

Write C++ application program. (see prediction.cpp)

PS: module->to(at::kCUDA) and input_tensor.to(at::kCUDA) will switch your model & tensors to GPU mode, comment out them if you just want to use CPU.

Step 3: CMakeLists

Write a CMakeLists.txt. (check cppdocs for more details)

Build

  • run model_trace.py, you will get a converted model resnet50.pt.
  • compile your cpp program, you need to use -DCMAKE_PREFIX_PATH=/absolute/path/to/libtorch, for example:
mkdir build
cd build
# change "/home/bigballon/libtorch" to your libtorch path
cmake -DCMAKE_PREFIX_PATH=/home/bigballon/libtorch ..
make

PS: If you get the compile error: error: undefined reference to `cv::imread(std::string const&, int)', check issues 14684 and issues 14620 for more details.

Usage

classifier <path-to-exported-script-module> <path-to-lable-file>
# example:
# ./classifier ../resnet18.pt ../label.txt

video

> ./classifier ../resnet18.pt ../label
== Switch to GPU mode
== Model [../resnet18.pt] loaded!
== Label loaded! Let's try it
== Input image path: [enter Q to exit]
../pic/dog.jpg
== image size: [976 x 549] ==
== simply resize: [224 x 224] ==
    ============= Top-1 =============
    Label:  beagle
    With Probability:  97.0629%
    ============= Top-2 =============
    Label:  Walker hound, Walker foxhound
    With Probability:  1.30952%
    ============= Top-3 =============
    Label:  English foxhound
    With Probability:  0.434456%

dog

../pic/shark.jpg
== image size: [800 x 500] ==
== simply resize: [224 x 224] ==
    ============= Top-1 =============
    Label:  tiger shark, Galeocerdo cuvieri
    With Probability:  67.672%
    ============= Top-2 =============
    Label:  hammerhead, hammerhead shark
    With Probability:  16.4908%
    ============= Top-3 =============
    Label:  great white shark, white shark, man-eater, man-eating shark
    With Probability:  15.7808%
== Input image path: [enter Q to exit]
Q

shark

> ./classifier ../resnet50.pt ../label
== Switch to GPU mode
== Model [../resnet50.pt] loaded!
== Label loaded! Let's try it
== Input image path: [enter Q to exit]
../pic/dog.jpg
== image size: [976 x 549] ==
== simply resize: [224 x 224] ==
    ============= Top-1 =============
    Label:  beagle
    With Probability:  99.1227%
    ============= Top-2 =============
    Label:  Walker hound, Walker foxhound
    With Probability:  0.469356%
    ============= Top-3 =============
    Label:  English foxhound
    With Probability:  0.110916%
== Input image path: [enter Q to exit]
../pic/shark.jpg
== image size: [800 x 500] ==
== simply resize: [224 x 224] ==
    ============= Top-1 =============
    Label:  tiger shark, Galeocerdo cuvieri
    With Probability:  92.2599%
    ============= Top-2 =============
    Label:  great white shark, white shark, man-eater, man-eating shark
    With Probability:  5.94252%
    ============= Top-3 =============
    Label:  hammerhead, hammerhead shark
    With Probability:  1.77417%
== Input image path: [enter Q to exit]
Q

Take it easy!! 💌

More Repositories

1

cifar-10-cnn

Play deep learning with CIFAR datasets
Python
819
star
2

CIFAR-ZOO

PyTorch implementation of CNNs for CIFAR benchmark
Python
698
star
3

distribuuuu

The pure and clear PyTorch Distributed Training Framework.
Python
276
star
4

Ghost

An AI search algorithm demo
C++
58
star
5

CPP-Call-Tensorflow

Calling (TensorFlow) Python Program from C++
C++
51
star
6

Deep-learning-and-practices

Version control for my deep learning course.
Python
47
star
7

Caffe2-Tutorial

Caffe2 C++/Python tutorial with full demo
Python
38
star
8

Paper_List

Paper reading list during my graduate studies
18
star
9

Crawler_Demo

This is my Crawler exercises.
CSS
16
star
10

CVPR2022-Paper-Statistics

Paper Statistics for CVPR‘22
Python
14
star
11

qduoj_install_sh

青岛大学OnlineJudge部署脚本
Shell
7
star
12

ResNet_CIFAR

Residual Network Experiments with CIFAR Datasets.
Python
6
star
13

HTD

Source code for HTD (WACV 2019)
Lua
6
star
14

codePrint

简单的代码打印系统,用于acm-icpc比赛的代码打印。
HTML
5
star
15

itvl1.5-v100-test

Inference of InternVL model on V100
Python
5
star
16

Toward-AGZ

Materials for AlphaGo
4
star
17

NCTU_NP

Version control for my network programming course.
C++
4
star
18

kosmos-2-gd

Shell
3
star
19

NCTU_AI

Artificial Intelligence Term Project Spring 2017
C++
3
star
20

bigballon.github.io

This is my personal blog.
SCSS
3
star
21

NCTU_CV

code for my computer vision course.
C++
3
star
22

ubuntu_rc

my ubuntu setting files
Shell
3
star
23

pretrained_models

Pretrained Residual network models
2
star
24

NCTU_TCG

Version control for my theory of computer game course.
C++
2
star
25

Model_Experiment

useless experiment of HTD
Lua
2
star
26

STQPF

Short-Term Quantitative Precipitation Forecasting@CIKM AnalytiCup 2017
Python
1
star
27

Meow

Notes for BG
1
star
28

Hiphop-Gym

Python
1
star
29

bigballon

1
star
30

Dog_challenge

Jupyter Notebook
1
star