Skip to content

Commit ea9297c

Browse files
committed
[update] retest & update readme for beginner
1 parent 418ea31 commit ea9297c

File tree

5 files changed

+57
-32
lines changed

5 files changed

+57
-32
lines changed

README.md

Lines changed: 54 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -1,60 +1,83 @@
1-
# pytorch_cpp
1+
2+
3+
<img src="./pic/pytorch-cpp.jpg" width=30% align="right" />
24

35
This demo will show you how to use libtorch to build your C++ application.
46

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+
511
## Contents
612

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)
1022

1123

12-
## Requirements
24+
## Requirements
1325

14-
- Pytorch (tag: pytorch v1.0)
15-
- Libtorch
16-
- OpenCV
26+
- PyTorch (>= v1.4.0)
27+
- LibTorch (>= v1.4.0)
28+
- OpenCV (>= v4.0)
1729

18-
## Build
30+
## Preparation
1931

20-
### Step 1
2132

22-
Export your pytorch model to torch script file, We will simply use resnet50 in this demo
33+
### Step 0x00
2334

24-
### Step 2
35+
**Make sure** LibTorch and OpenCV have been installed correctly.
2536

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)
2738

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.
3040

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.
3142
32-
### Step 3
43+
### Step 0x01
3344

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).
3646

37-
```
38-
error: undefined reference to `cv::imread(std::string const&, int)'
39-
```
47+
### Step 0x02
4048

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.
4252
43-
## Usage
4453

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``.
4661
- compile your cpp program, you need to use ``-DCMAKE_PREFIX_PATH=/absolute/path/to/libtorch``, for example:
4762

48-
```
63+
```bash
4964
mkdir build
5065
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 ..
5268
make
5369
```
5470

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+
```
5679

57-
``classifier <path-to-exported-script-module> <path-to-lable-file>``
80+
![video](./pic/video.gif)
5881

5982
```
6083
> ./classifier ../resnet50.pt ../label.txt
@@ -76,7 +99,7 @@ make
7699
With Probability: 0.110916%
77100
== Input image path: [enter Q to exit]
78101
```
79-
![](./pic/dog.jpg)
102+
![dog](./pic/dog.jpg)
80103

81104
```
82105
../pic/shark.jpg
@@ -94,7 +117,7 @@ make
94117
== Input image path: [enter Q to exit]
95118
Q
96119
```
97-
![](./pic/shark.jpg)
120+
![shark](./pic/shark.jpg)
98121

99122

100-
Take it easy!!
123+
Take it easy!! :love_letter:

model_trace.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22
import torchvision
33

44
model = torchvision.models.resnet50(pretrained=True)
5+
# Don't forget change model to eval mode
56
model.eval()
67
example = torch.rand(1, 3, 224, 224)
78
traced_script_module = torch.jit.trace(model, example)

pic/pytorch-cpp.jpg

211 KB
Loading

pic/video.gif

267 KB
Loading

prediction.cpp

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -58,7 +58,7 @@ int main(int argc, const char *argv[]) {
5858

5959
torch::jit::script::Module module = torch::jit::load(argv[1]);
6060
std::cout << "== Switch to GPU mode" << std::endl;
61-
// to GPU
61+
// to GPU
6262
module.to(at::kCUDA);
6363

6464
std::cout << "== ResNet50 loaded!\n";
@@ -108,4 +108,5 @@ int main(int argc, const char *argv[]) {
108108
std::cout << "Can't load the image, please check your path." << std::endl;
109109
}
110110
}
111+
return 0;
111112
}

0 commit comments

Comments
 (0)