|
1 |
| -# pytorch_cpp |
| 1 | + |
| 2 | + |
| 3 | +<img src="./pic/pytorch-cpp.jpg" width=30% align="right" /> |
2 | 4 |
|
3 | 5 | This demo will show you how to use libtorch to build your C++ application.
|
4 | 6 |
|
| 7 | +**[UPDATE 2020/02/22]** : Thanks for [Ageliss](https://github.com/Ageliss) and his [PR](https://github.com/BIGBALLON/PyTorch-CPP/pull/4), which update this demo to fit libtorch v1.4.0 and opencv4.0. |
| 8 | +**[UPDATE 2020/04/15]** : Retest this tutorial with **OpenCV4.3**/**PyTorch1.4**/**LibTorch1.4**, update readme for beginner. |
| 9 | + |
| 10 | + |
5 | 11 | ## Contents
|
6 | 12 |
|
7 |
| -1. [Requirements](#requirements) |
8 |
| -2. [Build](#build) |
9 |
| -3. [Usage](#usage) |
| 13 | +- [Contents](#contents) |
| 14 | +- [Requirements](#requirements) |
| 15 | +- [Preparation](#preparation) |
| 16 | + - [Step 0x00](#step-0x00) |
| 17 | + - [Step 0x01](#step-0x01) |
| 18 | + - [Step 0x02](#step-0x02) |
| 19 | + - [Step 0x03](#step-0x03) |
| 20 | +- [Build](#build) |
| 21 | +- [Usage](#usage) |
10 | 22 |
|
11 | 23 |
|
12 |
| -## Requirements |
| 24 | +## Requirements |
13 | 25 |
|
14 |
| -- Pytorch (tag: pytorch v1.0) |
15 |
| -- Libtorch |
16 |
| -- OpenCV |
| 26 | +- PyTorch (>= v1.4.0) |
| 27 | +- LibTorch (>= v1.4.0) |
| 28 | +- OpenCV (>= v4.0) |
17 | 29 |
|
18 |
| -## Build |
| 30 | +## Preparation |
19 | 31 |
|
20 |
| -### Step 1 |
21 | 32 |
|
22 |
| -Export your pytorch model to torch script file, We will simply use resnet50 in this demo |
| 33 | +### Step 0x00 |
23 | 34 |
|
24 |
| -### Step 2 |
| 35 | +**Make sure** LibTorch and OpenCV have been installed correctly. |
25 | 36 |
|
26 |
| -Write your C++ program, check the file ``prediction.cpp`` for more detial. |
| 37 | +- **Install OpenCV**: for [Linux](https://docs.opencv.org/4.3.0/d7/d9f/tutorial_linux_install.html), for [Mac OS](https://docs.opencv.org/4.3.0/d0/db2/tutorial_macos_install.html) |
27 | 38 |
|
28 |
| -PS: ``module->to(at::kCUDA)`` and ``input_tensor.to(at::kCUDA)`` will switch your model & tensor to GPU mode, |
29 |
| -comment out them if you just want to use CPU mode. |
| 39 | +- **Get LibTorch**: download [zip](https://pytorch.org/get-started/locally/) from PyTorch website, then unpack it. |
30 | 40 |
|
| 41 | +> **PS**: the version of OpenCV must the same as your libtorch. Otherwise, you may get the compile error: ``error: undefined reference to `cv::imread(std::string const&, int)'``, check [issues 14684](https://github.com/pytorch/pytorch/issues/14684) and [issues 14620](https://github.com/pytorch/pytorch/issues/14620) for more details. |
31 | 42 |
|
32 |
| -### Step 3 |
| 43 | +### Step 0x01 |
33 | 44 |
|
34 |
| -Write a ``CMakeLists.txt``, the version of OpenCV must the same as your libtorch. |
35 |
| -Otherwise, you may get the compile error: |
| 45 | +Export PyTorch model to torch script file(we use ``resnet50`` in this demo). See [model_trace.py](./model_trace.py). |
36 | 46 |
|
37 |
| -``` |
38 |
| -error: undefined reference to `cv::imread(std::string const&, int)' |
39 |
| -``` |
| 47 | +### Step 0x02 |
40 | 48 |
|
41 |
| -check [issues 14684](https://github.com/pytorch/pytorch/issues/14684) and [issues 14620](https://github.com/pytorch/pytorch/issues/14620) for more details. |
| 49 | +Write C++ application program, check [prediction.cpp](./prediction.cpp) for more detials. |
| 50 | + |
| 51 | +> **PS**: ``module->to(at::kCUDA)`` and ``input_tensor.to(at::kCUDA)`` will switch your model & tensor to GPU mode, comment out them if you just want to use CPU mode. |
42 | 52 |
|
43 |
| -## Usage |
44 | 53 |
|
45 |
| -- run ``model_trace.py``, then you will get a file ``resnet50.pt`` |
| 54 | +### Step 0x03 |
| 55 | + |
| 56 | +Write a [CMakeLists.txt](./CMakeLists.txt). check [cppdocs](https://pytorch.org/cppdocs/) for more details. |
| 57 | + |
| 58 | +## Build |
| 59 | + |
| 60 | +- run ``model_trace.py``, you will get a converted model ``resnet50.pt``. |
46 | 61 | - compile your cpp program, you need to use ``-DCMAKE_PREFIX_PATH=/absolute/path/to/libtorch``, for example:
|
47 | 62 |
|
48 |
| -``` |
| 63 | +```bash |
49 | 64 | mkdir build
|
50 | 65 | cd build
|
51 |
| -cmake -DCMAKE_PREFIX_PATH=/home/cgilab/pytorch/torch/lib/tmp_install .. |
| 66 | +# change "/home/bigballon/libtorch" to your libtorch path |
| 67 | +cmake -DCMAKE_PREFIX_PATH=/home/bigballon/libtorch .. |
52 | 68 | make
|
53 | 69 | ```
|
54 | 70 |
|
55 |
| -- test your program |
| 71 | +## Usage |
| 72 | + |
| 73 | + |
| 74 | +```bash |
| 75 | +classifier <path-to-exported-script-module> <path-to-lable-file> |
| 76 | +# example: |
| 77 | +# ./classifier ../resnet50.pt ../label.txt |
| 78 | +``` |
56 | 79 |
|
57 |
| -``classifier <path-to-exported-script-module> <path-to-lable-file>`` |
| 80 | + |
58 | 81 |
|
59 | 82 | ```
|
60 | 83 | > ./classifier ../resnet50.pt ../label.txt
|
|
76 | 99 | With Probability: 0.110916%
|
77 | 100 | == Input image path: [enter Q to exit]
|
78 | 101 | ```
|
79 |
| - |
| 102 | + |
80 | 103 |
|
81 | 104 | ```
|
82 | 105 | ../pic/shark.jpg
|
|
94 | 117 | == Input image path: [enter Q to exit]
|
95 | 118 | Q
|
96 | 119 | ```
|
97 |
| - |
| 120 | + |
98 | 121 |
|
99 | 122 |
|
100 |
| -Take it easy!! |
| 123 | +Take it easy!! :love_letter: |
0 commit comments