Skip to content

Commit 501160d

Browse files
Support TSM-R50 C++ API (wang-xinyu#500)
* C++ API for TSM-R50 * fix bugs
1 parent 8d8e2c9 commit 501160d

File tree

6 files changed

+1020
-14
lines changed

6 files changed

+1020
-14
lines changed

tsm/CMakeLists.txt

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
cmake_minimum_required(VERSION 2.6)
2+
3+
project(TSM)
4+
5+
add_definitions(-std=c++11)
6+
7+
option(CUDA_USE_STATIC_CUDA_RUNTIME OFF)
8+
set(CMAKE_CXX_STANDARD 11)
9+
set(CMAKE_BUILD_TYPE Debug)
10+
11+
include_directories(${PROJECT_SOURCE_DIR}/include)
12+
# include and link dirs of cuda and tensorrt, you need adapt them if yours are different
13+
# cuda
14+
include_directories(/usr/local/cuda/include)
15+
link_directories(/usr/local/cuda/lib64)
16+
17+
# tensorrt
18+
include_directories(/home/ubuntu/TensorRT/include/)
19+
link_directories(/home/ubuntu/TensorRT/lib/)
20+
21+
add_executable(tsm_r50 ${PROJECT_SOURCE_DIR}/tsm_r50.cpp)
22+
target_link_libraries(tsm_r50 nvinfer)
23+
target_link_libraries(tsm_r50 cudart)
24+
25+
add_definitions(-O2 -pthread)

tsm/README.md

Lines changed: 16 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -23,9 +23,12 @@ More details about the shift module(which is the core of TSM) could to [test_shi
2323
python gen_wts.py /path/to/pytorch.pth --out-filename /path/to/tensorrt.wts
2424
```
2525

26-
+ Step 3: Modify configs in `tsm_r50.py`.
26+
+ Step 3: Test Python API.
27+
+ Modify configs in `tsm_r50.py`.
28+
+ Inference with `tsm_r50.py`.
2729

2830
```python
31+
# Supported settings
2932
BATCH_SIZE = 1
3033
NUM_SEGMENTS = 8
3134
INPUT_H = 224
@@ -34,10 +37,8 @@ OUTPUT_SIZE = 400
3437
SHIFT_DIV = 8
3538
```
3639

37-
+ Step 4: Inference with `tsm_r50.py`.
38-
3940
```shell
40-
usage: tsm_r50.py [-h] [--tensorrt-weights TENSORRT_WEIGHTS] [--input-video INPUT_VIDEO] [--save-engine-path SAVE_ENGINE_PATH] [--load-engine-path LOAD_ENGINE_PATH] [--test-mmaction2] [--mmaction2-config MMACTION2_CONFIG] [--mmaction2-checkpoint MMACTION2_CHECKPOINT]
41+
usage: tsm_r50.py [-h] [--tensorrt-weights TENSORRT_WEIGHTS] [--input-video INPUT_VIDEO] [--save-engine-path SAVE_ENGINE_PATH] [--load-engine-path LOAD_ENGINE_PATH] [--test-mmaction2] [--mmaction2-config MMACTION2_CONFIG] [--mmaction2-checkpoint MMACTION2_CHECKPOINT] [--test-cpp] [--cpp-result-path CPP_RESULT_PATH]
4142

4243
optional arguments:
4344
-h, --help show this help message and exit
@@ -54,13 +55,23 @@ optional arguments:
5455
Path to MMAction2 config file
5556
--mmaction2-checkpoint MMACTION2_CHECKPOINT
5657
Path to MMAction2 checkpoint url or file path
58+
--test-cpp Compare Python API results with C++ API results
59+
--cpp-result-path CPP_RESULT_PATH
60+
Path to C++ API results
5761
```
5862

63+
+ Step 4: Test C++ API.
64+
+ Mocify Configs in `tsm_r50.cpp`.
65+
+ Build from source code: `mkdir build && cd build && cmake .. && make`
66+
+ Generate Engine file: `./tsm_r50 -s`
67+
+ Inference with genrated engine file and write predictions to local: `./tsm_r50 -d`
68+
+ Compare results with Python API: `python tsm_r50.py --tensorrt-weights /path/to/tensorrt.weights --test-cpp --cpp-result-file /path/to/cpp-result.txt`
69+
5970
## TODO
6071

6172
+ [x] Python Shift module.
6273
+ [x] Generate wts of official tsm and mmaction2 tsm.
6374
+ [x] Python API Definition
6475
+ [x] Test with mmaction2 demo
6576
+ [x] Tutorial
66-
+ [ ] C++ API Definition
77+
+ [x] C++ API Definition

tsm/demo.sh

Lines changed: 20 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -5,16 +5,16 @@ wget https://download.openmmlab.com/mmaction/recognition/tsm/tsm_r50_1x1x8_50e_k
55
# Step 2: Convert pytorch checkpoints to TensorRT weights
66
python gen_wts.py tsm_r50_1x1x8_50e_kinetics400_rgb_20200607-af7fb746.pth --out-filename ./tsm_r50_kinetics400_mmaction2.wts
77

8-
# Step 3: Skip this step since we use default settings.
9-
10-
# Step 4: Inference
11-
# 1) Save local engine file to `./tsm_r50_kinetics400_mmaction2.trt`.
8+
# Step 3: Test Python API.
9+
# 3.1 Skip this step since we use default settings.
10+
# 3.2 Inference
11+
# 3.2.1 Save local engine file to `./tsm_r50_kinetics400_mmaction2.trt`.
1212
python tsm_r50.py \
1313
--tensorrt-weights ./tsm_r50_kinetics400_mmaction2.wts \
1414
--save-engine-path ./tsm_r50_kinetics400_mmaction2.trt
1515

16-
# 2) Predict the recognition result using a single video `demo.mp4`.
17-
# Should print `Result class id 6`, aka `arm wrestling`
16+
# 3.2.2 Predict the recognition result using a single video `demo.mp4`.
17+
# Should print `Result class id 6`, aka `arm wrestling`
1818
# Download demo video
1919
wget https://raw.githubusercontent.com/open-mmlab/mmaction2/master/demo/demo.mp4
2020
# # use *.wts as input
@@ -24,8 +24,8 @@ wget https://raw.githubusercontent.com/open-mmlab/mmaction2/master/demo/demo.mp4
2424
python tsm_r50.py --load-engine-path ./tsm_r50_kinetics400_mmaction2.trt \
2525
--input-video ./demo.mp4
2626

27-
# 3) Optional: Compare inference result with MMAction2 TSM-R50 model
28-
# Have to install MMAction2 First, please refer to https://github.com/open-mmlab/mmaction2/blob/master/docs/install.md
27+
# 3.2.3 Optional: Compare inference result with MMAction2 TSM-R50 model
28+
# Have to install MMAction2 First, please refer to https://github.com/open-mmlab/mmaction2/blob/master/docs/install.md
2929
# pip3 install pytest-runner
3030
# pip3 install mmcv
3131
# pip3 install mmaction2
@@ -41,3 +41,15 @@ python tsm_r50.py --load-engine-path ./tsm_r50_kinetics400_mmaction2.trt \
4141
# --test-mmaction2 \
4242
# --mmaction2-config mmaction2_tsm_r50_config.py \
4343
# --mmaction2-checkpoint tsm_r50_1x1x8_50e_kinetics400_rgb_20200607-af7fb746.pth
44+
45+
# Step 4: Test Python API.
46+
# 4.1 Skip this step since we use default settings.
47+
# 4.2 Build CPP
48+
mkdir build && cd build && cmake .. && make
49+
# 4.3 Generate Engine file
50+
./tsm_r50 -s
51+
# 4.4 Get Predictions
52+
./tsm_r50 -d
53+
# 4.5 Compare C++ Results with Python Results
54+
cd ..
55+
python tsm_r50.py --test-cpp --tensorrt-weights ./tsm_r50_kinetics400_mmaction2.wts

0 commit comments

Comments
 (0)