Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[examples] Add PP-ShiTu pipeline example #1085

Closed
Closed
Show file tree
Hide file tree
Changes from all commits
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
3 changes: 3 additions & 0 deletions examples/vision/ppshitu/cpp/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
fastdeploy-*
third_libs
build
42 changes: 42 additions & 0 deletions examples/vision/ppshitu/cpp/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
PROJECT(fastdeploy_ppshitu C CXX)
CMAKE_MINIMUM_REQUIRED(VERSION 3.10)

# Options for PP-ShiTu
option(WITH_FAISS_STATIC "Whether to build PP-ShiTu with Faiss static lib" ON)
option(WITH_FAISS_GPU "Whether to build PP-ShiTu with Faiss gpu lib" OFF)

set(THIRD_LIBS_PATH ${CMAKE_CURRENT_BINARY_DIR}/third_libs)

# Path to FastDeploy_DIR
set(FastDeploy_DIR ${CMAKE_CURRENT_SOURCE_DIR}/fastdeploy-linux-x64-gpu-0.0.0)
find_package(FastDeploy REQUIRED)
if(FastDeploy_FOUND)
message(STATUS "Found FastDeploy: ${FastDeploy_FOUND} ")
message(STATUS "Found FastDeploy_INCLUDE_DIRS: ${FastDeploy_INCLUDE_DIRS} ")
message(STATUS "Found FastDeploy_LIBS: ${FastDeploy_LIBS} ")
endif()

# Add Faiss lib for vector search
include(cmake/faiss.cmake)
if(FAISS_FOUND)
message(STATUS "Found FAISS: ${FAISS_FOUND}")
message(STATUS "Found FAISS_INCLUDE_DIRS: ${FAISS_INCLUDE_DIRS}")
message(STATUS "Found FAISS_LIBS: ${FAISS_LIBS}")
endif()

# Add FastDeploy & FAISS & PP-ShiTu headers
include_directories(${FastDeploy_INCLUDE_DIRS})
include_directories(${FAISS_INCLUDE_DIRS})
include_directories(${CMAKE_CURRENT_SOURCE_DIR}/src)

# Add PP-ShiTu srcs
file(GLOB PP_ShiTu_SRCS ${CMAKE_CURRENT_SOURCE_DIR}/src/*.cc)

# Compile libfastdeploy_ppshitu.so
add_library(fastdeploy_ppshitu SHARED ${PP_ShiTu_SRCS})
target_link_libraries(fastdeploy_ppshitu ${FAISS_LIBS} ${FastDeploy_LIBS})

# Compile infer demo
add_executable(infer_demo ${CMAKE_CURRENT_SOURCE_DIR}/infer.cc)
target_link_libraries(infer_demo fastdeploy_ppshitu)

162 changes: 162 additions & 0 deletions examples/vision/ppshitu/cpp/cmake/faiss.cmake
Original file line number Diff line number Diff line change
@@ -0,0 +1,162 @@
# Copyright (c) 2022 PaddlePaddle Authors. All Rights Reserved.
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http:https://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
include(ExternalProject)

set(FAISS_PROJECT external_faiss_download)
set(FAISS_FILENAME faiss)
set(FAISS_PREFIX_DIR ${THIRD_LIBS_PATH}/${FAISS_FILENAME})
set(FAISS_SOURCE_DIR ${THIRD_LIBS_PATH}/${FAISS_FILENAME}/src/${FAISS_PROJECT})
set(FAISS_INSTALL_DIR ${THIRD_LIBS_PATH}/install/${FAISS_FILENAME})
set(FAISS_INC_DIR ${FAISS_INSTALL_DIR}/include CACHE PATH "faiss include directory." FORCE)
if(ANDROID)
set(FAISS_LIB_DIR ${FAISS_INSTALL_DIR}/lib/${ANDROID_ABI} CACHE PATH "faiss lib directory." FORCE)
else()
set(FAISS_LIB_DIR ${FAISS_INSTALL_DIR}/lib CACHE PATH "faiss lib directory." FORCE)
endif()

if(NOT WITH_FAISS_STATIC)
message(FATAL_ERROR "Not support WITH_FAISS_STATIC=OFF now!")
endif()

set(FAISS_URL_PREFIX "https://bj.bcebos.com/fastdeploy/test")
if(ANDROID)
# check ABI, toolchain
if((NOT ANDROID_ABI MATCHES "armeabi-v7a") AND (NOT ANDROID_ABI MATCHES "arm64-v8a"))
message(FATAL_ERROR "FAISS only support armeabi-v7a, arm64-v8a now.")
endif()
if(NOT ANDROID_TOOLCHAIN MATCHES "clang")
message(FATAL_ERROR "Currently, only support clang toolchain but found ${ANDROID_TOOLCHAIN}.")
endif()
endif()

set(FAISS_VERSION 1.7.3)
# URL
if(NOT FAISS_URL)
if(WIN32)
set(FAISS_URL "${FAISS_URL_PREFIX}/faiss-win-x64-${FAISS_VERSION}.zip")
elseif(APPLE)
if(CURRENT_OSX_ARCH MATCHES "arm64")
set(FAISS_URL "${FAISS_URL_PREFIX}/faiss-osx-arm64-${FAISS_VERSION}.tgz")
else()
set(FAISS_URL "${FAISS_URL_PREFIX}/faiss-osx-x64-${FAISS_VERSION}.tgz")
endif()
elseif(ANDROID)
set(FAISS_URL "${FAISS_URL_PREFIX}/faiss-android-${ANDROID_ABI}-${FAISS_VERSION}.tgz")
else() # Linux
if(CMAKE_HOST_SYSTEM_PROCESSOR MATCHES "aarch64")
message(FATAL_ERROR "Not support for Linux aarch64 now!")
else()
if(WITH_FAISS_GPU)
set(FAISS_URL "${FAISS_URL_PREFIX}/faiss-linux-x64-gpu-${FAISS_VERSION}.tgz")
else()
set(FAISS_URL "${FAISS_URL_PREFIX}/faiss-linux-x64-${FAISS_VERSION}.tgz")
endif()
endif()
endif()
endif()

# FAISS Headers
include_directories(${FAISS_INC_DIR})

# FAISS Libs paths
if(WIN32)
set(FAISS_LIB "${FAISS_LIB_DIR}/faiss.lib")
elseif(APPLE)
set(FAISS_LIB "${FAISS_LIB_DIR}/libfaiss.a")
elseif(ANDROID)
set(FAISS_LIB "${FAISS_LIB_DIR}/libfaiss.a")
else() # Linux
set(FAISS_LIB "${FAISS_LIB_DIR}/libfaiss.a")
endif()

# Download FAISS
if(ANDROID)
ExternalProject_Add(
${FAISS_PROJECT}
${EXTERNAL_PROJECT_LOG_ARGS}
URL ${FAISS_URL}
PREFIX ${FAISS_PREFIX_DIR}
DOWNLOAD_NO_PROGRESS 1
CONFIGURE_COMMAND ""
BUILD_COMMAND ""
UPDATE_COMMAND ""
INSTALL_COMMAND
${CMAKE_COMMAND} -E remove_directory ${FAISS_INSTALL_DIR} &&
${CMAKE_COMMAND} -E make_directory ${FAISS_INSTALL_DIR} &&
${CMAKE_COMMAND} -E make_directory ${FAISS_INSTALL_DIR}/lib &&
${CMAKE_COMMAND} -E rename ${FAISS_SOURCE_DIR}/lib/ ${FAISS_INSTALL_DIR}/lib/${ANDROID_ABI} &&
${CMAKE_COMMAND} -E copy_directory ${FAISS_SOURCE_DIR}/include ${FAISS_INC_DIR}
BUILD_BYPRODUCTS ${FAISS_LIB})
else()
ExternalProject_Add(
${FAISS_PROJECT}
${EXTERNAL_PROJECT_LOG_ARGS}
URL ${FAISS_URL}
PREFIX ${FAISS_PREFIX_DIR}
DOWNLOAD_NO_PROGRESS 1
CONFIGURE_COMMAND ""
BUILD_COMMAND ""
UPDATE_COMMAND ""
INSTALL_COMMAND
${CMAKE_COMMAND} -E remove_directory ${FAISS_INSTALL_DIR} &&
${CMAKE_COMMAND} -E make_directory ${FAISS_INSTALL_DIR} &&
${CMAKE_COMMAND} -E rename ${FAISS_SOURCE_DIR}/lib/ ${FAISS_INSTALL_DIR}/lib &&
${CMAKE_COMMAND} -E copy_directory ${FAISS_SOURCE_DIR}/include ${FAISS_INC_DIR}
BUILD_BYPRODUCTS ${FAISS_LIB})
endif()

set(FAISS_LIBRARIES)

add_library(external_faiss STATIC IMPORTED GLOBAL)
set_property(TARGET external_faiss PROPERTY IMPORTED_LOCATION ${FAISS_LIB})
add_dependencies(external_faiss ${FAISS_PROJECT})

list(APPEND FAISS_LIBRARIES external_faiss)

# Add BLAS/LAPACK/OpenBLAS (needed by FAISS)
if(WIN32)
add_library(external_blas STATIC IMPORTED GLOBAL)
set_property(TARGET external_blas PROPERTY IMPORTED_LOCATION ${FAISS_LIB_DIR}/BLAS.lib)
add_dependencies(external_blas ${FAISS_PROJECT})
list(APPEND FAISS_LIBRARIES external_blas)

add_library(external_lapack STATIC IMPORTED GLOBAL)
set_property(TARGET external_lapack PROPERTY IMPORTED_LOCATION ${FAISS_LIB_DIR}/LAPACK.lib)
add_dependencies(external_lapack ${FAISS_PROJECT})
list(APPEND FAISS_LIBRARIES external_lapack)
elseif(APPLE)
find_package(BLAS REQUIRED)
list(APPEND FAISS_LIBRARIES ${BLAS_LIBRARIES})

find_package(LAPACK REQUIRED)
list(APPEND FAISS_LIBRARIES ${LAPACK_LIBRARIES})
elseif(ANDROID)
# OpenBLAS static lib already merged into libfaiss.a
message(STATUS "For Android, OpenBLAS static lib was already merged into libfaiss.a")
else() # Linux
find_package(BLAS REQUIRED)
list(APPEND FAISS_LIBRARIES ${BLAS_LIBRARIES})

find_package(LAPACK REQUIRED)
list(APPEND FAISS_LIBRARIES ${LAPACK_LIBRARIES})
endif()

# Add OpenMP (REQUIRED), OpenMP must be avaliable.
find_package(OpenMP REQUIRED)
list(APPEND FAISS_LIBRARIES OpenMP::OpenMP_CXX)

set(FAISS_INCLUDE_DIRS ${FAISS_INC_DIR})
set(FAISS_LIBS ${FAISS_LIBRARIES})
set(FAISS_FOUND TRUE)

17 changes: 17 additions & 0 deletions examples/vision/ppshitu/cpp/infer.cc
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
// Copyright (c) 2022 PaddlePaddle Authors. All Rights Reserved.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http:https://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

int main(int argc, char* argv[]) {
return 0;
}
13 changes: 13 additions & 0 deletions examples/vision/ppshitu/cpp/src/detector.cc
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
// Copyright (c) 2022 PaddlePaddle Authors. All Rights Reserved.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http:https://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
13 changes: 13 additions & 0 deletions examples/vision/ppshitu/cpp/src/detector.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
// Copyright (c) 2022 PaddlePaddle Authors. All Rights Reserved.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http:https://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
13 changes: 13 additions & 0 deletions examples/vision/ppshitu/cpp/src/feature_extractor.cc
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
// Copyright (c) 2022 PaddlePaddle Authors. All Rights Reserved.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http:https://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
13 changes: 13 additions & 0 deletions examples/vision/ppshitu/cpp/src/feature_extractor.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
// Copyright (c) 2022 PaddlePaddle Authors. All Rights Reserved.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http:https://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
13 changes: 13 additions & 0 deletions examples/vision/ppshitu/cpp/src/pipeline.cc
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
// Copyright (c) 2022 PaddlePaddle Authors. All Rights Reserved.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http:https://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
13 changes: 13 additions & 0 deletions examples/vision/ppshitu/cpp/src/pipeline.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
// Copyright (c) 2022 PaddlePaddle Authors. All Rights Reserved.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http:https://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
13 changes: 13 additions & 0 deletions examples/vision/ppshitu/cpp/src/vector_search.cc
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
// Copyright (c) 2022 PaddlePaddle Authors. All Rights Reserved.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http:https://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
13 changes: 13 additions & 0 deletions examples/vision/ppshitu/cpp/src/vector_search.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
// Copyright (c) 2022 PaddlePaddle Authors. All Rights Reserved.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http:https://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.