Guest User

Build Tensorflow Lite on x86_64, Raspberry Pi and other ARM

a guest
Feb 4th, 2018
1,795
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. 1. git clone https://github.com/tensorflow/tensorflow.git
  2.  
  3. 2.
  4. cd tensorflow/contrib/lite
  5.  
  6. 3. Download dependencies and MobileNet models
  7. ./download_dependencies.sh
  8.  
  9. 4.
  10. # Before building, you need to enable SIMD instruction sets for your processor for build to succeed.
  11. # Run 'cat /proc/cpuinfo'. If you see sse4* or neon* in flags, you have SIMD capable CPU.
  12. # Edit tensorflow/contrib/lite/Makefile, and add one of these to CXXFLAGS and CFLAGS:
  13. # "-msse4" for x86_64 CPU
  14. # "-mfpu=neon" for ARM CPU. For Raspberry Pi 2, add "-mfpu=neon-vfpv4".
  15. #
  16. # If you don't see any SIMD instruction set, add this define instead to CXXFLAGS and CFLAGS:
  17. # -DGEMMLOWP_ALLOW_SLOW_SCALAR_FALLBACK
  18.  
  19. 5. Now make TFLite:
  20. cd ../../..
  21. make -f tensorflow/contrib/lite/Makefile
  22.  
  23. # This generates tensorflow/contrib/lite/gen/lib/libtensorflow-lite.a static library.
  24.  
  25. 6.
  26. # See tensorflow/contrib/lite/examples/label_image/label_image.cc to understand how to use
  27. # TFLite API.
  28.  
  29. 7.
  30. # To build the label_image example app:
  31. # Create the following Makefile as tensorflow/contrib/lite/examples/label_image/Makefile
  32.  
  33. ########### START OF LABEL_IMAGE MAKEFILE ####################################################
  34. # Find where we're running from, so we can store generated files here.
  35. ifeq ($(origin MAKEFILE_DIR), undefined)
  36. MAKEFILE_DIR := $(shell dirname $(realpath $(lastword $(MAKEFILE_LIST))))
  37. endif
  38.  
  39. # Try to figure out the host system
  40. HOST_OS :=
  41. ifeq ($(OS),Windows_NT)
  42. HOST_OS = WINDOWS
  43. else
  44. UNAME_S := $(shell uname -s)
  45. ifeq ($(UNAME_S),Linux)
  46. HOST_OS := LINUX
  47. endif
  48. ifeq ($(UNAME_S),Darwin)
  49. HOST_OS := OSX
  50. endif
  51. endif
  52.  
  53. ARCH := $(shell if [[ $(shell uname -m) =~ i[345678]86 ]]; then echo x86_32; else echo $(shell uname -m); fi)
  54.  
  55. # Where compiled objects are stored.
  56. OBJDIR := $(MAKEFILE_DIR)/gen/obj/
  57. BINDIR := $(MAKEFILE_DIR)/gen/bin/
  58. LIBDIR := $(MAKEFILE_DIR)/gen/lib/
  59. GENDIR := $(MAKEFILE_DIR)/gen/obj/
  60.  
  61. # Settings for the host compiler.
  62. CXX := $(CC_PREFIX) gcc
  63. CXXFLAGS := --std=c++11 -O3 -DNDEBUG
  64. CC := $(CC_PREFIX) gcc
  65. CFLAGS :=
  66. LDOPTS :=
  67. LDOPTS += -L/usr/local/lib
  68. ARFLAGS := -r
  69.  
  70. INCLUDES := \
  71. -I. \
  72. -I$(MAKEFILE_DIR)/../../../../../ \
  73. -I$(MAKEFILE_DIR)/../../downloads/flatbuffers/include \
  74. -I$(GENDIR)
  75. # This is at the end so any globally-installed frameworks like protobuf don't
  76. # override local versions in the source tree.
  77. INCLUDES += -I/usr/local/include
  78.  
  79. LIBS := \
  80. -lstdc++ \
  81. -lpthread \
  82. -lm \
  83. -lz
  84.  
  85. # If we're on Linux, also link in the dl library.
  86. ifeq ($(HOST_OS),LINUX)
  87. LIBS += -ldl -lpthread
  88. endif
  89.  
  90. # This library is the main target for this makefile. It will contain a minimal
  91. # runtime that can be linked in to other programs.
  92. BIN_PATH := $(BINDIR)label_image
  93.  
  94. TF_LIB_PATH := $(MAKEFILE_DIR)/../../gen/lib/libtensorflow-lite.a
  95.  
  96. # What sources we want to compile, must be kept in sync with the main Bazel
  97. # build files.
  98.  
  99. SRCS := \
  100. tensorflow/contrib/lite/examples/label_image/label_image.cc \
  101. tensorflow/contrib/lite/examples/label_image/bitmap_helpers.cc
  102.  
  103. OBJS := $(addprefix $(OBJDIR), \
  104. $(patsubst %.cc,%.o,$(patsubst %.c,%.o,$(SRCS))))
  105.  
  106. # For normal manually-created TensorFlow C++ source files.
  107. $(OBJDIR)%.o: %.cc
  108. @mkdir -p $(dir $@)
  109. $(CXX) $(CXXFLAGS) $(INCLUDES) -c $< -o $@
  110.  
  111. # For normal manually-created TensorFlow C++ source files.
  112. $(OBJDIR)%.o: %.c
  113. @mkdir -p $(dir $@)
  114. $(CC) $(CCFLAGS) $(INCLUDES) -c $< -o $@
  115.  
  116. # The target that's compiled if there's no command-line arguments.
  117. all: $(BIN_PATH)
  118.  
  119. $(BIN_PATH): $(OBJS) $(TF_LIB_PATH)
  120. @mkdir -p $(dir $@)
  121. $(CXX) $(CXXFLAGS) $(INCLUDES) \
  122. -o $(BIN_PATH) $(OBJS) \
  123. $(LIBFLAGS) $(TF_LIB_PATH) $(LDFLAGS) $(LIBS)
  124.  
  125. # Gets rid of all generated files.
  126. clean:
  127. rm -rf $(MAKEFILE_DIR)/gen
  128.  
  129. # Gets rid of target files only, leaving the host alone. Also leaves the lib
  130. # directory untouched deliberately, so we can persist multiple architectures
  131. # across builds for iOS and Android.
  132. cleantarget:
  133. rm -rf $(OBJDIR)
  134. rm -rf $(BINDIR)
  135.  
  136. $(DEPDIR)/%.d: ;
  137. .PRECIOUS: $(DEPDIR)/%.d
  138.  
  139. -include $(patsubst %,$(DEPDIR)/%.d,$(basename $(TF_CC_SRCS)))
  140.  
  141.  
  142.  
  143.  
  144. ########### END OF LABEL_IMAGE MAKEFILE ####################################################
  145.  
  146.  
  147.  
  148. 8. Make the label_image app:
  149. make -f tensorflow/contrib/lite/examples/label_image/Makefile
  150.  
  151. # The app binary is generated in: tensorflow/contrib/lite/examples/label_image/gen/bin/label_image
  152.  
  153.  
  154. 9. Run label_image inference using floating point model:
  155. cd tensorflow/contrib/lite/examples/label_image/gen/bin/
  156.  
  157. ./label_image -v 1 -m ../../../../downloads/models/models/mobilenet_v1_1.0_224.tflite -i ../../testdata/grace_hopper.bmp -l ../../../../downloads/models/models/labels.txt
  158.  
  159.  
  160. 10. Run label_image inference using quantized model:
  161. cd tensorflow/contrib/lite/examples/label_image/gen/bin/
  162.  
  163. ./label_image -v 1 -m ../../../../downloads/quantized_models/mobilenet_quant_v1_224.tflite -i ../../testdata/grace_hopper.bmp -l ../../../../downloads/quantized_models/labels.txt
RAW Paste Data Copied