Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Appearance settings
Open
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
fix:version1.1
  • Loading branch information
fengshuai committed Feb 7, 2022
commit e41952a03a57b6d86f7fc3df2dbd7de12d6a8968
215 changes: 215 additions & 0 deletions 215 tutorials/mobilenetv3_prod/Step6/deploy/inference_cpp/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,215 @@
project(clas_system CXX C)

option(WITH_MKL "Compile demo with MKL/OpenBlas support, default use MKL." ON)
option(WITH_GPU "Compile demo with GPU/CPU, default use CPU." OFF)
option(WITH_STATIC_LIB "Compile demo with static/shared library, default use static." ON)
option(WITH_TENSORRT "Compile demo with TensorRT." OFF)

SET(PADDLE_LIB "" CACHE PATH "Location of libraries")
SET(OPENCV_DIR "" CACHE PATH "Location of libraries")
SET(CUDA_LIB "" CACHE PATH "Location of libraries")
SET(CUDNN_LIB "" CACHE PATH "Location of libraries")
SET(TENSORRT_DIR "" CACHE PATH "Compile demo with TensorRT")

set(DEMO_NAME "clas_system")


macro(safe_set_static_flag)
foreach(flag_var
CMAKE_CXX_FLAGS CMAKE_CXX_FLAGS_DEBUG CMAKE_CXX_FLAGS_RELEASE
CMAKE_CXX_FLAGS_MINSIZEREL CMAKE_CXX_FLAGS_RELWITHDEBINFO)
if(${flag_var} MATCHES "/MD")
string(REGEX REPLACE "/MD" "/MT" ${flag_var} "${${flag_var}}")
endif(${flag_var} MATCHES "/MD")
endforeach(flag_var)
endmacro()

if (WITH_MKL)
ADD_DEFINITIONS(-DUSE_MKL)
endif()

if(NOT DEFINED PADDLE_LIB)
message(FATAL_ERROR "please set PADDLE_LIB with -DPADDLE_LIB=/path/paddle/lib")
endif()

if(NOT DEFINED OPENCV_DIR)
message(FATAL_ERROR "please set OPENCV_DIR with -DOPENCV_DIR=/path/opencv")
endif()


if (WIN32)
include_directories("${PADDLE_LIB}/paddle/fluid/inference")
include_directories("${PADDLE_LIB}/paddle/include")
link_directories("${PADDLE_LIB}/paddle/fluid/inference")
find_package(OpenCV REQUIRED PATHS ${OPENCV_DIR}/build/ NO_DEFAULT_PATH)

else ()
find_package(OpenCV REQUIRED PATHS ${OPENCV_DIR}/share/OpenCV NO_DEFAULT_PATH)
include_directories("${PADDLE_LIB}/paddle/include")
link_directories("${PADDLE_LIB}/paddle/lib")
endif ()
include_directories(${OpenCV_INCLUDE_DIRS})

if (WIN32)
add_definitions("/DGOOGLE_GLOG_DLL_DECL=")
set(CMAKE_C_FLAGS_DEBUG "${CMAKE_C_FLAGS_DEBUG} /bigobj /MTd")
set(CMAKE_C_FLAGS_RELEASE "${CMAKE_C_FLAGS_RELEASE} /bigobj /MT")
set(CMAKE_CXX_FLAGS_DEBUG "${CMAKE_CXX_FLAGS_DEBUG} /bigobj /MTd")
set(CMAKE_CXX_FLAGS_RELEASE "${CMAKE_CXX_FLAGS_RELEASE} /bigobj /MT")
if (WITH_STATIC_LIB)
safe_set_static_flag()
add_definitions(-DSTATIC_LIB)
endif()
else()
set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -g -o3 -std=c++11")
set(CMAKE_STATIC_LIBRARY_PREFIX "")
endif()
message("flags" ${CMAKE_CXX_FLAGS})


if (WITH_GPU)
if (NOT DEFINED CUDA_LIB OR ${CUDA_LIB} STREQUAL "")
message(FATAL_ERROR "please set CUDA_LIB with -DCUDA_LIB=/path/cuda-8.0/lib64")
endif()
if (NOT WIN32)
if (NOT DEFINED CUDNN_LIB)
message(FATAL_ERROR "please set CUDNN_LIB with -DCUDNN_LIB=/path/cudnn_v7.4/cuda/lib64")
endif()
endif(NOT WIN32)
endif()

include_directories("${PADDLE_LIB}/third_party/install/protobuf/include")
include_directories("${PADDLE_LIB}/third_party/install/glog/include")
include_directories("${PADDLE_LIB}/third_party/install/gflags/include")
include_directories("${PADDLE_LIB}/third_party/install/xxhash/include")
include_directories("${PADDLE_LIB}/third_party/install/zlib/include")
include_directories("${PADDLE_LIB}/third_party/boost")
include_directories("${PADDLE_LIB}/third_party/eigen3")

include_directories("${CMAKE_SOURCE_DIR}/")

if (NOT WIN32)
if (WITH_TENSORRT AND WITH_GPU)
include_directories("${TENSORRT_DIR}/include")
link_directories("${TENSORRT_DIR}/lib")
endif()
endif(NOT WIN32)

link_directories("${PADDLE_LIB}/third_party/install/zlib/lib")

link_directories("${PADDLE_LIB}/third_party/install/protobuf/lib")
link_directories("${PADDLE_LIB}/third_party/install/glog/lib")
link_directories("${PADDLE_LIB}/third_party/install/gflags/lib")
link_directories("${PADDLE_LIB}/third_party/install/xxhash/lib")
link_directories("${PADDLE_LIB}/paddle/lib")


if(WITH_MKL)
include_directories("${PADDLE_LIB}/third_party/install/mklml/include")
if (WIN32)
set(MATH_LIB ${PADDLE_LIB}/third_party/install/mklml/lib/mklml.lib
${PADDLE_LIB}/third_party/install/mklml/lib/libiomp5md.lib)
else ()
set(MATH_LIB ${PADDLE_LIB}/third_party/install/mklml/lib/libmklml_intel${CMAKE_SHARED_LIBRARY_SUFFIX}
${PADDLE_LIB}/third_party/install/mklml/lib/libiomp5${CMAKE_SHARED_LIBRARY_SUFFIX})
execute_process(COMMAND cp -r ${PADDLE_LIB}/third_party/install/mklml/lib/libmklml_intel${CMAKE_SHARED_LIBRARY_SUFFIX} /usr/lib)
endif ()
set(MKLDNN_PATH "${PADDLE_LIB}/third_party/install/mkldnn")
if(EXISTS ${MKLDNN_PATH})
include_directories("${MKLDNN_PATH}/include")
if (WIN32)
set(MKLDNN_LIB ${MKLDNN_PATH}/lib/mkldnn.lib)
else ()
set(MKLDNN_LIB ${MKLDNN_PATH}/lib/libmkldnn.so.0)
endif ()
endif()
else()
if (WIN32)
set(MATH_LIB ${PADDLE_LIB}/third_party/install/openblas/lib/openblas${CMAKE_STATIC_LIBRARY_SUFFIX})
else ()
set(MATH_LIB ${PADDLE_LIB}/third_party/install/openblas/lib/libopenblas${CMAKE_STATIC_LIBRARY_SUFFIX})
endif ()
endif()

# Note: libpaddle_inference_api.so/a must put before libpaddle_fluid.so/a
if(WITH_STATIC_LIB)
if(WIN32)
set(DEPS
${PADDLE_LIB}/paddle/lib/paddle_inference${CMAKE_STATIC_LIBRARY_SUFFIX})
else()
set(DEPS
${PADDLE_LIB}/paddle/lib/libpaddle_inference${CMAKE_STATIC_LIBRARY_SUFFIX})
endif()
else()
if(WIN32)
set(DEPS
${PADDLE_LIB}/paddle/lib/paddle_inference${CMAKE_SHARED_LIBRARY_SUFFIX})
else()
set(DEPS
${PADDLE_LIB}/paddle/lib/libpaddle_inference${CMAKE_SHARED_LIBRARY_SUFFIX})
endif()
endif(WITH_STATIC_LIB)

if (NOT WIN32)
set(DEPS ${DEPS}
${MATH_LIB} ${MKLDNN_LIB}
glog gflags protobuf z xxhash
)
if(EXISTS "${PADDLE_LIB}/third_party/install/snappystream/lib")
set(DEPS ${DEPS} snappystream)
endif()
if (EXISTS "${PADDLE_LIB}/third_party/install/snappy/lib")
set(DEPS ${DEPS} snappy)
endif()
else()
set(DEPS ${DEPS}
${MATH_LIB} ${MKLDNN_LIB}
glog gflags_static libprotobuf xxhash)
set(DEPS ${DEPS} libcmt shlwapi)
if (EXISTS "${PADDLE_LIB}/third_party/install/snappy/lib")
set(DEPS ${DEPS} snappy)
endif()
if(EXISTS "${PADDLE_LIB}/third_party/install/snappystream/lib")
set(DEPS ${DEPS} snappystream)
endif()
endif(NOT WIN32)


if(WITH_GPU)
if(NOT WIN32)
if (WITH_TENSORRT)
set(DEPS ${DEPS} ${TENSORRT_DIR}/lib/libnvinfer${CMAKE_SHARED_LIBRARY_SUFFIX})
set(DEPS ${DEPS} ${TENSORRT_DIR}/lib/libnvinfer_plugin${CMAKE_SHARED_LIBRARY_SUFFIX})
endif()
set(DEPS ${DEPS} ${CUDA_LIB}/libcudart${CMAKE_SHARED_LIBRARY_SUFFIX})
set(DEPS ${DEPS} ${CUDNN_LIB}/libcudnn${CMAKE_SHARED_LIBRARY_SUFFIX})
else()
set(DEPS ${DEPS} ${CUDA_LIB}/cudart${CMAKE_STATIC_LIBRARY_SUFFIX} )
set(DEPS ${DEPS} ${CUDA_LIB}/cublas${CMAKE_STATIC_LIBRARY_SUFFIX} )
set(DEPS ${DEPS} ${CUDNN_LIB}/cudnn${CMAKE_STATIC_LIBRARY_SUFFIX})
endif()
endif()


if (NOT WIN32)
set(EXTERNAL_LIB "-ldl -lrt -lgomp -lz -lm -lpthread")
set(DEPS ${DEPS} ${EXTERNAL_LIB})
endif()

set(DEPS ${DEPS} ${OpenCV_LIBS})

AUX_SOURCE_DIRECTORY(./src SRCS)
add_executable(${DEMO_NAME} ${SRCS})

target_link_libraries(${DEMO_NAME} ${DEPS})

if (WIN32 AND WITH_MKL)
add_custom_command(TARGET ${DEMO_NAME} POST_BUILD
COMMAND ${CMAKE_COMMAND} -E copy_if_different ${PADDLE_LIB}/third_party/install/mklml/lib/mklml.dll ./mklml.dll
COMMAND ${CMAKE_COMMAND} -E copy_if_different ${PADDLE_LIB}/third_party/install/mklml/lib/libiomp5md.dll ./libiomp5md.dll
COMMAND ${CMAKE_COMMAND} -E copy_if_different ${PADDLE_LIB}/third_party/install/mkldnn/lib/mkldnn.dll ./mkldnn.dll
COMMAND ${CMAKE_COMMAND} -E copy_if_different ${PADDLE_LIB}/third_party/install/mklml/lib/mklml.dll ./release/mklml.dll
COMMAND ${CMAKE_COMMAND} -E copy_if_different ${PADDLE_LIB}/third_party/install/mklml/lib/libiomp5md.dll ./release/libiomp5md.dll
COMMAND ${CMAKE_COMMAND} -E copy_if_different ${PADDLE_LIB}/third_party/install/mkldnn/lib/mkldnn.dll ./release/mkldnn.dll
)
endif()
Original file line number Diff line number Diff line change
Expand Up @@ -213,7 +213,7 @@ make -j
* crop_size:预处理时图像裁剪后的大小。

* 然后修改`tools/run.sh`:
* `./build/clas_system ./tools/config.txt /work/Docs/models/tutorials/mobilenetv3_prod/Step6/images/demo.jpg`
* `./build/clas_system ./tools/config.txt ../../images/demo.jpg`
* 上述命令中分别为:编译得到的可执行文件`clas_system`;运行时的配置文件`config.txt`;待预测的图像。

* 最后执行以下命令,完成对一幅图像的分类。
Expand All @@ -233,7 +233,7 @@ class id: 8

score: 0.9014717937

Current image path: /work/Docs/models/tutorials/mobilenetv3_prod/Step6/images/demo.jpg
Current image path: ../../images/demo.jpg

Current time cost: 0.0473620000 s, average time cost in all: 0.0473620000 s.

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@

using namespace paddle_infer;

namespace PaddleClas {
namespace MobileNetV3 {

class Classifier {
public:
Expand Down Expand Up @@ -88,4 +88,4 @@ class Classifier {
CenterCropImg crop_op_;
};

} // namespace PaddleClas
} // namespace MobileNetV3
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@

#include "include/utility.h"

namespace PaddleClas {
namespace MobileNetV3 {

class ClsConfig {
public:
Expand Down Expand Up @@ -85,4 +85,4 @@ class ClsConfig {
std::map<std::string, std::string> config_map_;
};

} // namespace PaddleClas
} // namespace MobileNetV3
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@

using namespace std;

namespace PaddleClas {
namespace MobileNetV3 {

class Normalize {
public:
Expand All @@ -53,4 +53,4 @@ class ResizeImg {
virtual void Run(const cv::Mat &img, cv::Mat &resize_img, int max_size_len);
};

} // namespace PaddleClas
} // namespace MobileNetV3
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@
#include "opencv2/imgcodecs.hpp"
#include "opencv2/imgproc.hpp"

namespace PaddleClas {
namespace MobileNetV3 {

class Utility {
public:
Expand All @@ -43,4 +43,4 @@ class Utility {
// }
};

} // namespace PaddleClas
} // namespace MobileNetV3
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@
#include <chrono>
#include <include/cls.h>

namespace PaddleClas {
namespace MobileNetV3 {

void Classifier::LoadModel(const std::string &model_path,
const std::string &params_path) {
Expand Down Expand Up @@ -100,4 +100,4 @@ double Classifier::Run(cv::Mat &img) {
return cost_time;
}

} // namespace PaddleClas
} // namespace MobileNetV3
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@

#include <include/cls_config.h>

namespace PaddleClas {
namespace MobileNetV3 {

std::vector<std::string> ClsConfig::split(const std::string &str,
const std::string &delim) {
Expand Down Expand Up @@ -61,4 +61,4 @@ void ClsConfig::PrintConfigInfo() {
std::cout << "=======End of Paddle Class inference config======" << std::endl;
}

} // namespace PaddleClas
} // namespace MobileNetV3
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@

using namespace std;
using namespace cv;
using namespace PaddleClas;
using namespace MobileNetV3;

int main(int argc, char **argv) {
if (argc < 3) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@

#include <include/preprocess_op.h>

namespace PaddleClas {
namespace MobileNetV3 {

void Permute::Run(const cv::Mat *im, float *data) {
int rh = im->rows;
Expand Down Expand Up @@ -87,4 +87,4 @@ void ResizeImg::Run(const cv::Mat &img, cv::Mat &resize_img,
cv::resize(img, resize_img, cv::Size(resize_w, resize_h));
}

} // namespace PaddleClas
} // namespace MobileNetV3
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@

#include <include/utility.h>

namespace PaddleClas {
namespace MobileNetV3 {

std::vector<std::string> Utility::ReadDict(const std::string &path) {
std::ifstream in(path);
Expand All @@ -36,4 +36,4 @@ std::vector<std::string> Utility::ReadDict(const std::string &path) {
return m_vec;
}

} // namespace PaddleClas
} // namespace MobileNetV3
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
OPENCV_DIR=/work/PaddleClas/deploy/cpp/opencv-3.4.7/opencv3/
LIB_DIR=/work/PaddleClas/deploy/cpp/paddle_inference/
OPENCV_DIR=../opencv-3.4.7/opencv3/
LIB_DIR=../paddle_inference/
CUDA_LIB_DIR=/usr/local/cuda/lib64
CUDNN_LIB_DIR=/usr/lib64
TENSORRT_DIR=/usr/local/TensorRT-7.2.3.4
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ use_tensorrt 0
use_fp16 0

# cls config
cls_model_path /work/PaddleClas/deploy/cpp/mobilenet_v3_small_infer/inference.pdmodel
cls_params_path /work/PaddleClas/deploy/cpp/mobilenet_v3_small_infer/inference.pdiparams
cls_model_path ./mobilenet_v3_small_infer/inference.pdmodel
cls_params_path ./mobilenet_v3_small_infer/inference.pdiparams
resize_short_size 256
crop_size 224
Original file line number Diff line number Diff line change
@@ -1 +1 @@
./build/clas_system ./tools/config.txt /work/Docs/models/tutorials/mobilenetv3_prod/Step6/images/demo.jpg
./build/clas_system ./tools/config.txt ../../images/demo.jpg
Morty Proxy This is a proxified and sanitized view of the page, visit original site.