Skip to content

Commit f3a0361

Browse files
authored
[TIPC] add tipc c++ infer for msvsr (#676)
* add tipc c++ infer for msvsr * add tipc c++ infer for msvsr
1 parent eebf94d commit f3a0361

File tree

11 files changed

+762
-10
lines changed

11 files changed

+762
-10
lines changed

deploy/cpp_infer/CMakeLists.txt

Lines changed: 223 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,223 @@
1+
project(vsr CXX C)
2+
cmake_minimum_required(VERSION 3.14)
3+
4+
option(WITH_MKL "Compile demo with MKL/OpenBlas support, default use MKL." ON)
5+
option(WITH_GPU "Compile demo with GPU/CPU, default use CPU." OFF)
6+
option(WITH_STATIC_LIB "Compile demo with static/shared library, default use static." ON)
7+
option(WITH_TENSORRT "Compile demo with TensorRT." OFF)
8+
9+
SET(PADDLE_LIB "" CACHE PATH "Location of libraries")
10+
SET(OPENCV_DIR "" CACHE PATH "Location of libraries")
11+
SET(CUDA_LIB "" CACHE PATH "Location of libraries")
12+
SET(CUDNN_LIB "" CACHE PATH "Location of libraries")
13+
SET(TENSORRT_DIR "" CACHE PATH "Compile demo with TensorRT")
14+
15+
set(DEMO_NAME "vsr")
16+
17+
18+
macro(safe_set_static_flag)
19+
foreach(flag_var
20+
CMAKE_CXX_FLAGS CMAKE_CXX_FLAGS_DEBUG CMAKE_CXX_FLAGS_RELEASE
21+
CMAKE_CXX_FLAGS_MINSIZEREL CMAKE_CXX_FLAGS_RELWITHDEBINFO)
22+
if(${flag_var} MATCHES "/MD")
23+
string(REGEX REPLACE "/MD" "/MT" ${flag_var} "${${flag_var}}")
24+
endif(${flag_var} MATCHES "/MD")
25+
endforeach(flag_var)
26+
endmacro()
27+
28+
if (WITH_MKL)
29+
ADD_DEFINITIONS(-DUSE_MKL)
30+
endif()
31+
32+
if(NOT DEFINED PADDLE_LIB)
33+
message(FATAL_ERROR "please set PADDLE_LIB with -DPADDLE_LIB=/path/paddle/lib")
34+
endif()
35+
36+
if(NOT DEFINED OPENCV_DIR)
37+
message(FATAL_ERROR "please set OPENCV_DIR with -DOPENCV_DIR=/path/opencv")
38+
endif()
39+
40+
41+
if (WIN32)
42+
include_directories("${PADDLE_LIB}/paddle/include")
43+
link_directories("${PADDLE_LIB}/paddle/lib")
44+
find_package(OpenCV REQUIRED PATHS ${OPENCV_DIR}/build/ NO_DEFAULT_PATH)
45+
46+
else ()
47+
find_package(OpenCV REQUIRED PATHS ${OPENCV_DIR}/share/OpenCV NO_DEFAULT_PATH)
48+
include_directories("${PADDLE_LIB}/paddle/include")
49+
link_directories("${PADDLE_LIB}/paddle/lib")
50+
endif ()
51+
include_directories(${OpenCV_INCLUDE_DIRS})
52+
53+
if (WIN32)
54+
add_definitions("/DGOOGLE_GLOG_DLL_DECL=")
55+
if(WITH_MKL)
56+
set(FLAG_OPENMP "/openmp")
57+
endif()
58+
set(CMAKE_C_FLAGS_DEBUG "${CMAKE_C_FLAGS_DEBUG} /bigobj /MTd ${FLAG_OPENMP}")
59+
set(CMAKE_C_FLAGS_RELEASE "${CMAKE_C_FLAGS_RELEASE} /bigobj /MT ${FLAG_OPENMP}")
60+
set(CMAKE_CXX_FLAGS_DEBUG "${CMAKE_CXX_FLAGS_DEBUG} /bigobj /MTd ${FLAG_OPENMP}")
61+
set(CMAKE_CXX_FLAGS_RELEASE "${CMAKE_CXX_FLAGS_RELEASE} /bigobj /MT ${FLAG_OPENMP}")
62+
if (WITH_STATIC_LIB)
63+
safe_set_static_flag()
64+
add_definitions(-DSTATIC_LIB)
65+
endif()
66+
message("cmake c debug flags " ${CMAKE_C_FLAGS_DEBUG})
67+
message("cmake c release flags " ${CMAKE_C_FLAGS_RELEASE})
68+
message("cmake cxx debug flags " ${CMAKE_CXX_FLAGS_DEBUG})
69+
message("cmake cxx release flags " ${CMAKE_CXX_FLAGS_RELEASE})
70+
else()
71+
if(WITH_MKL)
72+
set(FLAG_OPENMP "-fopenmp")
73+
endif()
74+
set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -g -o3 ${FLAG_OPENMP} -std=c++11")
75+
set(CMAKE_STATIC_LIBRARY_PREFIX "")
76+
message("cmake cxx flags" ${CMAKE_CXX_FLAGS})
77+
endif()
78+
79+
if (WITH_GPU)
80+
if (NOT DEFINED CUDA_LIB OR ${CUDA_LIB} STREQUAL "")
81+
message(FATAL_ERROR "please set CUDA_LIB with -DCUDA_LIB=/path/cuda-8.0/lib64")
82+
endif()
83+
if (NOT WIN32)
84+
if (NOT DEFINED CUDNN_LIB)
85+
message(FATAL_ERROR "please set CUDNN_LIB with -DCUDNN_LIB=/path/cudnn_v7.4/cuda/lib64")
86+
endif()
87+
endif(NOT WIN32)
88+
endif()
89+
90+
include_directories("${PADDLE_LIB}/third_party/install/protobuf/include")
91+
include_directories("${PADDLE_LIB}/third_party/install/glog/include")
92+
include_directories("${PADDLE_LIB}/third_party/install/gflags/include")
93+
include_directories("${PADDLE_LIB}/third_party/install/xxhash/include")
94+
include_directories("${PADDLE_LIB}/third_party/install/zlib/include")
95+
include_directories("${PADDLE_LIB}/third_party/boost")
96+
include_directories("${PADDLE_LIB}/third_party/eigen3")
97+
98+
include_directories("${CMAKE_SOURCE_DIR}/")
99+
100+
if (NOT WIN32)
101+
if (WITH_TENSORRT AND WITH_GPU)
102+
include_directories("${TENSORRT_DIR}/include")
103+
link_directories("${TENSORRT_DIR}/lib")
104+
endif()
105+
endif(NOT WIN32)
106+
107+
link_directories("${PADDLE_LIB}/third_party/install/zlib/lib")
108+
109+
link_directories("${PADDLE_LIB}/third_party/install/protobuf/lib")
110+
link_directories("${PADDLE_LIB}/third_party/install/glog/lib")
111+
link_directories("${PADDLE_LIB}/third_party/install/gflags/lib")
112+
link_directories("${PADDLE_LIB}/third_party/install/xxhash/lib")
113+
link_directories("${PADDLE_LIB}/paddle/lib")
114+
115+
116+
if(WITH_MKL)
117+
include_directories("${PADDLE_LIB}/third_party/install/mklml/include")
118+
if (WIN32)
119+
set(MATH_LIB ${PADDLE_LIB}/third_party/install/mklml/lib/mklml.lib
120+
${PADDLE_LIB}/third_party/install/mklml/lib/libiomp5md.lib)
121+
else ()
122+
set(MATH_LIB ${PADDLE_LIB}/third_party/install/mklml/lib/libmklml_intel${CMAKE_SHARED_LIBRARY_SUFFIX}
123+
${PADDLE_LIB}/third_party/install/mklml/lib/libiomp5${CMAKE_SHARED_LIBRARY_SUFFIX})
124+
execute_process(COMMAND cp -r ${PADDLE_LIB}/third_party/install/mklml/lib/libmklml_intel${CMAKE_SHARED_LIBRARY_SUFFIX} /usr/lib)
125+
endif ()
126+
set(MKLDNN_PATH "${PADDLE_LIB}/third_party/install/mkldnn")
127+
if(EXISTS ${MKLDNN_PATH})
128+
include_directories("${MKLDNN_PATH}/include")
129+
if (WIN32)
130+
set(MKLDNN_LIB ${MKLDNN_PATH}/lib/mkldnn.lib)
131+
else ()
132+
set(MKLDNN_LIB ${MKLDNN_PATH}/lib/libmkldnn.so.0)
133+
endif ()
134+
endif()
135+
else()
136+
if (WIN32)
137+
set(MATH_LIB ${PADDLE_LIB}/third_party/install/openblas/lib/openblas${CMAKE_STATIC_LIBRARY_SUFFIX})
138+
else ()
139+
set(MATH_LIB ${PADDLE_LIB}/third_party/install/openblas/lib/libopenblas${CMAKE_STATIC_LIBRARY_SUFFIX})
140+
endif ()
141+
endif()
142+
143+
# Note: libpaddle_inference_api.so/a must put before libpaddle_inference.so/a
144+
if(WITH_STATIC_LIB)
145+
if(WIN32)
146+
set(DEPS
147+
${PADDLE_LIB}/paddle/lib/paddle_inference${CMAKE_STATIC_LIBRARY_SUFFIX})
148+
else()
149+
set(DEPS
150+
${PADDLE_LIB}/paddle/lib/libpaddle_inference${CMAKE_STATIC_LIBRARY_SUFFIX})
151+
endif()
152+
else()
153+
if(WIN32)
154+
set(DEPS
155+
${PADDLE_LIB}/paddle/lib/paddle_inference${CMAKE_SHARED_LIBRARY_SUFFIX})
156+
else()
157+
set(DEPS
158+
${PADDLE_LIB}/paddle/lib/libpaddle_inference${CMAKE_SHARED_LIBRARY_SUFFIX})
159+
endif()
160+
endif(WITH_STATIC_LIB)
161+
162+
if (NOT WIN32)
163+
set(DEPS ${DEPS}
164+
${MATH_LIB} ${MKLDNN_LIB}
165+
glog gflags protobuf z xxhash
166+
)
167+
if(EXISTS "${PADDLE_LIB}/third_party/install/snappystream/lib")
168+
set(DEPS ${DEPS} snappystream)
169+
endif()
170+
if (EXISTS "${PADDLE_LIB}/third_party/install/snappy/lib")
171+
set(DEPS ${DEPS} snappy)
172+
endif()
173+
else()
174+
set(DEPS ${DEPS}
175+
${MATH_LIB} ${MKLDNN_LIB}
176+
glog gflags_static libprotobuf xxhash)
177+
set(DEPS ${DEPS} libcmt shlwapi)
178+
if (EXISTS "${PADDLE_LIB}/third_party/install/snappy/lib")
179+
set(DEPS ${DEPS} snappy)
180+
endif()
181+
if(EXISTS "${PADDLE_LIB}/third_party/install/snappystream/lib")
182+
set(DEPS ${DEPS} snappystream)
183+
endif()
184+
endif(NOT WIN32)
185+
186+
187+
if(WITH_GPU)
188+
if(NOT WIN32)
189+
if (WITH_TENSORRT)
190+
set(DEPS ${DEPS} ${TENSORRT_DIR}/lib/libnvinfer${CMAKE_SHARED_LIBRARY_SUFFIX})
191+
set(DEPS ${DEPS} ${TENSORRT_DIR}/lib/libnvinfer_plugin${CMAKE_SHARED_LIBRARY_SUFFIX})
192+
endif()
193+
set(DEPS ${DEPS} ${CUDA_LIB}/libcudart${CMAKE_SHARED_LIBRARY_SUFFIX})
194+
set(DEPS ${DEPS} ${CUDNN_LIB}/libcudnn${CMAKE_SHARED_LIBRARY_SUFFIX})
195+
else()
196+
set(DEPS ${DEPS} ${CUDA_LIB}/cudart${CMAKE_STATIC_LIBRARY_SUFFIX} )
197+
set(DEPS ${DEPS} ${CUDA_LIB}/cublas${CMAKE_STATIC_LIBRARY_SUFFIX} )
198+
set(DEPS ${DEPS} ${CUDNN_LIB}/cudnn${CMAKE_STATIC_LIBRARY_SUFFIX})
199+
endif()
200+
endif()
201+
202+
203+
if (NOT WIN32)
204+
set(EXTERNAL_LIB "-ldl -lrt -lgomp -lz -lm -lpthread")
205+
set(DEPS ${DEPS} ${EXTERNAL_LIB})
206+
endif()
207+
208+
set(DEPS ${DEPS} ${OpenCV_LIBS})
209+
210+
AUX_SOURCE_DIRECTORY(./src SRCS)
211+
add_executable(${DEMO_NAME} ${SRCS})
212+
target_link_libraries(${DEMO_NAME} ${DEPS})
213+
214+
if (WIN32 AND WITH_MKL)
215+
add_custom_command(TARGET ${DEMO_NAME} POST_BUILD
216+
COMMAND ${CMAKE_COMMAND} -E copy_if_different ${PADDLE_LIB}/third_party/install/mklml/lib/mklml.dll ./mklml.dll
217+
COMMAND ${CMAKE_COMMAND} -E copy_if_different ${PADDLE_LIB}/third_party/install/mklml/lib/libiomp5md.dll ./libiomp5md.dll
218+
COMMAND ${CMAKE_COMMAND} -E copy_if_different ${PADDLE_LIB}/third_party/install/mkldnn/lib/mkldnn.dll ./mkldnn.dll
219+
COMMAND ${CMAKE_COMMAND} -E copy_if_different ${PADDLE_LIB}/third_party/install/mklml/lib/mklml.dll ./release/mklml.dll
220+
COMMAND ${CMAKE_COMMAND} -E copy_if_different ${PADDLE_LIB}/third_party/install/mklml/lib/libiomp5md.dll ./release/libiomp5md.dll
221+
COMMAND ${CMAKE_COMMAND} -E copy_if_different ${PADDLE_LIB}/third_party/install/mkldnn/lib/mkldnn.dll ./release/mkldnn.dll
222+
)
223+
endif()

deploy/cpp_infer/include/process_op.h

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
#include "opencv2/core.hpp"
2+
#include "opencv2/imgcodecs.hpp"
3+
#include "opencv2/imgproc.hpp"
4+
#include <chrono>
5+
#include <iomanip>
6+
#include <iostream>
7+
#include <ostream>
8+
#include <vector>
9+
10+
#include <cstring>
11+
#include <fstream>
12+
#include <numeric>
13+
14+
using namespace std;
15+
16+
class Normalize {
17+
public:
18+
virtual void Run(cv::Mat *im, const std::vector<float> &mean,
19+
const std::vector<float> &scale, const bool is_scale = true);
20+
};
21+
22+
23+
// RGB -> CHW
24+
class Permute {
25+
public:
26+
virtual void Run(const cv::Mat *im, float *data);
27+
};

deploy/cpp_infer/include/vsr.h

Lines changed: 57 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,57 @@
1+
#include <string>
2+
#include <vector>
3+
#include <memory>
4+
#include <utility>
5+
#include <ctime>
6+
#include <numeric>
7+
8+
#include <opencv2/core/core.hpp>
9+
#include <opencv2/imgproc/imgproc.hpp>
10+
#include <opencv2/highgui/highgui.hpp>
11+
12+
#include "include/process_op.h"
13+
#include "paddle_inference_api.h"
14+
15+
namespace PaddleGAN {
16+
17+
class VSR {
18+
public:
19+
explicit VSR(const std::string& model_path,
20+
const std::string& param_path,
21+
const std::string& device,
22+
const int& gpu_id,
23+
const bool& use_mkldnn,
24+
const int& cpu_threads) {
25+
26+
this->device_ = device;
27+
this->gpu_id_ = gpu_id;
28+
this->use_mkldnn_ = use_mkldnn_;
29+
this->cpu_threads_ = cpu_threads;
30+
31+
LoadModel(model_path, param_path);
32+
}
33+
34+
// Load paddle inference model
35+
void LoadModel(const std::string& model_path, const std::string& param_path);
36+
37+
// Run predictor
38+
void Run(const std::vector<cv::Mat>& imgs, std::vector<cv::Mat>* result = nullptr);
39+
40+
private:
41+
std::shared_ptr<paddle_infer::Predictor> predictor_;
42+
43+
std::string device_ = "GPU";
44+
int gpu_id_ = 0;
45+
bool use_mkldnn_ = false;
46+
int cpu_threads_ = 1;
47+
48+
std::vector<float> mean_ = {0., 0., 0.};
49+
std::vector<float> scale_ = {1., 1., 1.};
50+
51+
// pre/post-process
52+
Permute permute_op_;
53+
Normalize normalize_op_;
54+
std::vector<float> Preprocess(cv::Mat& img);
55+
};
56+
57+
}

0 commit comments

Comments
 (0)