Skip to content

Commit 71747d9

Browse files
authored
Libbackscrub - separation into library and wrapper application (#86)
* Initial separation of Tensorflow code into reusable static library * position independent code required to allow use in shared libraries * libdeepseg -> libbackscrub * Debug callback, library cleanup function * CMake build of separate library * Export dependencies from backscrub library for use in other projects * regression fix: use normalization values for model * Re-structure into more modular build * Install headers to sub-folder * TFBASE->TENSORFLOW in Makefile * Remove using namespace tflite * Dependency fix for parallel builds * remove TFLITE_MINIMAL_CHECK macro from API * Remove hack to include libtensorflow-lite.a in libbackscrub.a * Removed shared state between app and lib, now all hidden in lib. Prefixed lib function names * Avoid full data clone for mask output * Remove stdarg from debug callback, cleanup pointer checks
1 parent 81e8b21 commit 71747d9

File tree

10 files changed

+526
-274
lines changed

10 files changed

+526
-274
lines changed

.gitignore

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
1-
# Output folder
1+
# Output folders
22
bin/
3+
build/
34

45
# dotfiles and folders
56
.*/

CMakeLists.txt

Lines changed: 78 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,10 @@
11
cmake_minimum_required(VERSION 3.16)
22
project(backscrub CXX)
33

4+
if(NOT DEFINED TENSORFLOW)
5+
set(TENSORFLOW tensorflow)
6+
endif()
7+
48
find_package(Git)
59
if(GIT_FOUND)
610
execute_process(
@@ -12,6 +16,9 @@ else()
1216
endif()
1317
message("Version: ${DEEPSEG_VERSION}")
1418

19+
# always build PIC everywhere
20+
set(CMAKE_POSITION_INDEPENDENT_CODE TRUE)
21+
1522
find_package(OpenCV REQUIRED COMPONENTS core imgproc imgcodecs video videoio highgui)
1623

1724
# force compilation of XNNPACK delegate (without this the too clever-by-half use
@@ -20,29 +27,86 @@ add_compile_definitions(TFLITE_BUILD_WITH_XNNPACK_DELEGATE)
2027

2128
# This assumes that tensorflow got checked out as submodule.
2229
# True, if `git clone` had the additional option `--recursive`.
23-
add_subdirectory(tensorflow/tensorflow/lite
30+
add_subdirectory(${TENSORFLOW}/tensorflow/lite
2431
"${CMAKE_CURRENT_BINARY_DIR}/tensorflow-lite" EXCLUDE_FROM_ALL)
2532

2633
add_compile_definitions(DEEPSEG_VERSION=${DEEPSEG_VERSION})
34+
include_directories(BEFORE .)
35+
36+
add_library(backscrub
37+
lib/libbackscrub.cc
38+
lib/transpose_conv_bias.cc)
39+
40+
target_link_libraries(backscrub
41+
tensorflow-lite ${CMAKE_DL_LIBS}
42+
opencv_core
43+
opencv_imgproc
44+
)
45+
46+
add_library(videoio
47+
videoio/loopback.cc)
48+
49+
target_link_libraries(videoio)
2750

2851
add_executable(deepseg
29-
deepseg.cc
30-
loopback.cc
31-
transpose_conv_bias.cc
52+
app/deepseg.cc
3253
)
3354

34-
target_link_libraries(deepseg
35-
tensorflow-lite ${CMAKE_DL_LIBS}
36-
opencv_core
37-
opencv_video
38-
opencv_videoio
39-
opencv_imgproc
40-
opencv_imgcodecs
41-
opencv_highgui
55+
target_link_libraries(deepseg
56+
backscrub
57+
videoio
58+
opencv_core
59+
opencv_video
60+
opencv_videoio
61+
opencv_imgproc
62+
opencv_imgcodecs
63+
opencv_highgui
4264
)
4365

66+
# Export our library, and all transitive dependencies - sadly Tensorflow Lite's
67+
# CMakeLists.txt does not export these for us
68+
function(get_link_libraries target outlist)
69+
message(STATUS "get_link_libraries(${target} ${outlist})")
70+
# recursive dependency munger, feed with a name and a list to extend
71+
get_target_property(target_type ${target} TYPE)
72+
if(${target_type} STREQUAL "INTERFACE_LIBRARY")
73+
get_target_property(libs ${target} INTERFACE_LINK_LIBRARIES)
74+
else()
75+
get_target_property(libs ${target} LINK_LIBRARIES)
76+
endif()
77+
foreach(lib IN LISTS libs)
78+
if(NOT TARGET ${lib})
79+
continue()
80+
endif()
81+
get_target_property(unalias ${lib} ALIASED_TARGET)
82+
if("${unalias}" STREQUAL "unalias-NOTFOUND")
83+
set(unalias ${lib})
84+
endif()
85+
get_target_property(imp ${unalias} IMPORTED)
86+
if(${imp})
87+
continue()
88+
endif()
89+
list(FIND ${outlist} ${unalias} exists)
90+
if(NOT exists EQUAL -1)
91+
continue()
92+
endif()
93+
list(APPEND ${outlist} ${unalias})
94+
get_link_libraries(${unalias} ${outlist})
95+
set(${outlist} ${${outlist}} PARENT_SCOPE)
96+
endforeach()
97+
endfunction()
98+
99+
set(BACKSCRUB_DEPS "")
100+
get_link_libraries(backscrub BACKSCRUB_DEPS)
101+
102+
export(TARGETS
103+
backscrub
104+
${BACKSCRUB_DEPS}
105+
FILE BackscrubTargets.cmake)
44106

45107
install(TARGETS deepseg)
46-
install(DIRECTORY models
47-
DESTINATION ${CMAKE_INSTALL_PREFIX}/share/deepbacksub
108+
install(TARGETS backscrub)
109+
install(FILES libbackscrub.h DESTINATION include/backscrub)
110+
install(DIRECTORY models
111+
DESTINATION ${CMAKE_INSTALL_PREFIX}/share/backscrub
48112
FILES_MATCHING PATTERN "*.tflite" PATTERN "*.md")

Makefile

Lines changed: 34 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
# this is licensed software, @see LICENSE file.
22

33
# OpenCV & Tensorflow recommended flags for performance..
4-
CFLAGS = -Ofast -march=native -fno-trapping-math -fassociative-math -funsafe-math-optimizations -Wall -pthread
4+
CFLAGS = -fPIC -Ofast -march=native -fno-trapping-math -fassociative-math -funsafe-math-optimizations -Wall -pthread
55
LDFLAGS = -lrt -ldl
66

77
# Version
@@ -11,13 +11,14 @@ ifeq ($(VERSION),)
1111
VERSION=v0.2.0-no-git
1212
endif
1313

14-
CFLAGS += -D DEEPSEG_VERSION=$(VERSION)
14+
CFLAGS += -D DEEPSEG_VERSION=$(VERSION) -I.
1515

1616
# TensorFlow
17-
TFBASE=tensorflow
18-
TFLITE=$(TFBASE)/tensorflow/lite/tools/make
17+
TENSORFLOW=tensorflow
18+
TFLITE=$(TENSORFLOW)/tensorflow/lite/tools/make
19+
TFDOWN=$(TFLITE)/downloads/cpuinfo
1920
TFLIBS=$(TFLITE)/gen/linux_x86_64/lib
20-
TFCFLAGS += -I $(TFBASE) -I $(TFLITE)/downloads/absl -I $(TFLITE)/downloads/flatbuffers/include -ggdb
21+
TFCFLAGS += -I $(TENSORFLOW) -I $(TFLITE)/downloads/absl -I $(TFLITE)/downloads/flatbuffers/include -ggdb
2122
TFLDFLAGS += -L $(TFLIBS) -ltensorflow-lite -ldl
2223

2324
# OpenCV
@@ -43,12 +44,36 @@ clean:
4344
$(BIN):
4445
-mkdir -p $(BIN)
4546

46-
# Primary binary - special deps
47-
$(BIN)/deepseg: $(TFLIBS)/libtensorflow-lite.a deepseg.cc loopback.cc transpose_conv_bias.cc
47+
# Primary binaries - special deps
48+
$(BIN)/deepseg: app/deepseg.cc $(BIN)/libbackscrub.a $(BIN)/libvideoio.a $(TFLIBS)/libtensorflow-lite.a
4849
g++ $^ ${CFLAGS} ${TFCFLAGS} ${LDFLAGS} ${TFLDFLAGS} -o $@
4950

50-
$(TFLIBS)/libtensorflow-lite.a: $(TFLITE)
51-
cd $(TFLITE) && ./download_dependencies.sh && ./build_lib.sh
51+
# Backscrub library, must be linked with libtensorflow-lite.a
52+
$(BIN)/libbackscrub.a: $(BIN)/libbackscrub.o $(BIN)/transpose_conv_bias.o
53+
ar rv $@ $^
54+
55+
# Video I/O library - this is a Linux/v4l2loopback only target for now but replaceable later..
56+
$(BIN)/libvideoio.a: $(BIN)/loopback.o
57+
ar rv $@ $^
58+
59+
# Compile rules for various source directories
60+
$(BIN)/%.o: lib/%.cc $(TFDOWN)
61+
g++ $< ${CFLAGS} ${TFCFLAGS} -c -o $@
62+
63+
$(BIN)/%.o: videoio/%.cc $(TFDOWN)
64+
g++ $< ${CFLAGS} ${TFCFLAGS} -c -o $@
65+
66+
$(BIN)/%.o: app/%.cc $(TFDOWN)
67+
g++ $< ${CFLAGS} ${TFCFLAGS} -c -o $@
68+
69+
# As cloned, TFLite needs building into a static library, as per:
70+
# https://github.com/tensorflow/tensorflow/tree/master/tensorflow/lite/tools/make
71+
# we split this into download (pre-compile) and build (pre-link)
72+
$(TFDOWN): $(TFLITE)
73+
cd $(TFLITE) && ./download_dependencies.sh
74+
75+
$(TFLIBS)/libtensorflow-lite.a: $(TFDOWN)
76+
cd $(TFLITE) && ./build_lib.sh
5277

5378
$(TFLITE):
5479
git submodule update --init --recursive

0 commit comments

Comments
 (0)