使用 tensorflow 作为存储库构建基于 tensorflow 的 android 应用程序

building a tensorflow based android app with tensorflow as a repository

这就像 Build an android app using Tensorflow 的后续问题。我想将 android 示例项目与 tensorflow git repo 分开,并能够使用 tensorflow 作为依赖项单独构建它。这是我的文件夹结构

my_project
|-- WORKSPACE
|-- android
|   |-- BUILD
|   `-- ...
|-- tensorflow
|   |-- tensorflow
|   |   |   |-- workspace.bzl
|   |   |   |-- tensorflow.bzl
|   |   |   `-- ...
|   |-- WORKSPACE
|   |-- BUILD
.    `-- ...

其中 android 应用只是 Tensorflow Android example 的副本。 根 WORKSPACE 文件包含以下内容:

workspace(name = "my_android_app")

local_repository(
     name = "org_tensorflow",
     path = "tensorflow", # Relative path to the tensorflow workspace
 )

load('//android:workspace.bzl', 'android_workspace')
android_workspace()

 # Specify the minimum required bazel version.
load("@org_tensorflow//tensorflow:tensorflow.bzl", "check_version")
check_version("0.3.1")

android/workspace.bzl 看起来像这样

load('@org_tensorflow//tensorflow:workspace.bzl', 'tf_workspace')

def android_workspace():
  tf_workspace()

和 android/BUILD 与 Tensorflow Android example BUILD 具有相同的内容,只是我在每个地方都为 //tensorflow 添加了 @org_tensorflow 前缀,例如

"@org_tensorflow//tensorflow:tensorflow.bzl"
  "@org_tensorflow//tensorflow/contrib/android:android_tensorflow_inference_jni",
"@org_tensorflow//tensorflow/core:android_tensorflow_lib",

当我尝试构建主要目标 tensorflow_demo 时,出现此错误

no such package 'tensorflow': Package crosses into repository @org_tensorflow and referenced by '//android:libtensorflow_demo.so'.
ERROR: Analysis of target '//android:tensorflow_demo' failed; build aborted.

编辑:

感谢 Kristina,我能够将 Tensorflow Android 演示与 Tensorflow 源代码分离。您可以使用以下 git 作为 Tensorflow Android 项目的模板。 https://github.com/devinsaini/tensorflow_android

啊,好的,我明白你在做什么了。感谢详细说明。

这里的问题有点微妙:copts = tf_copts() 在@org_tensorflow 中调用一个函数,看起来像这样:

def tf_copts():
  return (["-DEIGEN_AVOID_STL_ARRAY",
           "-Iexternal/gemmlowp",
           "-Wno-sign-compare",
           "-fno-exceptions"] +
          if_cuda(["-DGOOGLE_CUDA=1"]) +
          if_android_arm(["-mfpu=neon"]) +
          if_x86(["-msse4.1"]) +
          select({
              "//tensorflow:android": [
                  "-std=c++11",
                  "-DTF_LEAN_BINARY",
                  "-O2",
              ],
              "//tensorflow:darwin": [],
              "//tensorflow:windows": [
                "/DLANG_CXX11",
                "/D__VERSION__=\\"MSVC\\"",
                "/DPLATFORM_WINDOWS",
                "/DEIGEN_HAS_C99_MATH",
                "/DTENSORFLOW_USE_EIGEN_THREADPOOL",
              ],
              "//tensorflow:ios": ["-std=c++11"],
              "//conditions:default": ["-pthread"]}))

基本上是在不同平台上使用不同标志的 switch 语句。然而,因为 load()s 在 select()s 之前计算,你的 BUILD 文件突然 "contains" 引用 //tensorflow.

要修复,最简单的选择是硬编码您需要的 copts 或在本地存储库中重新定义 tf_copts。

一般如何追踪不正确的引用

实际上我对你遇到的错误感到非常困惑,所以这里是一个大脑转储,说明人们通常如何调试这类事情。

追踪这种交叉引用的通常方法是 1) 检查你的 BUILD 文件(也许 deps 是错误的)或 2) 打印出 Bazel 实际上是如何 "sees" BUILD 文件的。例如,如果你想在这种情况下打印评估的构建规则,你可以这样做:

$ bazel query --output=build //:libtensorflow_demo.so
# /home/kchodorow/gitroot/so41153199/BUILD:18:1
cc_binary(
  name = "libtensorflow_demo.so",
  tags = ["manual", "notap"],
  deps = ["//:demo_proto_lib_cc", "@org_tensorflow//tensorflow/contrib/android:android_tensorflow_inference_jni", "@org_tensorflow//tensorflow/core:android_tensorflow_lib", "@org_tensorflow//tensorflow/contrib/android:jni/version_script.lds"],
  srcs = ["//:jni/box_coder_jni.cc", "//:jni/imageutils_jni.cc", "//:jni/object_tracking/config.h", "//:jni/object_tracking/flow_cache.h", "//:jni/object_tracking/frame_pair.cc", "//:jni/object_tracking/frame_pair.h", "//:jni/object_tracking/geom.h", "//:jni/object_tracking/gl_utils.h", "//:jni/object_tracking/image-inl.h", "//:jni/object_tracking/image.h", "//:jni/object_tracking/image_data.h", "//:jni/object_tracking/image_neon.cc", "//:jni/object_tracking/image_utils.h", "//:jni/object_tracking/integral_image.h", "//:jni/object_tracking/jni_utils.h", "//:jni/object_tracking/keypoint.h", "//:jni/object_tracking/keypoint_detector.cc", "//:jni/object_tracking/keypoint_detector.h", "//:jni/object_tracking/log_streaming.h", "//:jni/object_tracking/object_detector.cc", "//:jni/object_tracking/object_detector.h", "//:jni/object_tracking/object_model.h", "//:jni/object_tracking/object_tracker.cc", "//:jni/object_tracking/object_tracker.h", "//:jni/object_tracking/object_tracker_jni.cc", "//:jni/object_tracking/optical_flow.cc", "//:jni/object_tracking/optical_flow.h", "//:jni/object_tracking/sprite.h", "//:jni/object_tracking/time_log.cc", "//:jni/object_tracking/time_log.h", "//:jni/object_tracking/tracked_object.cc", "//:jni/object_tracking/tracked_object.h", "//:jni/object_tracking/utils.h", "//:jni/object_tracking/utils_neon.cc", "//:jni/rgb2yuv.cc", "//:jni/rgb2yuv.h", "//:jni/yuv2rgb.cc", "//:jni/yuv2rgb.h"],
  linkopts = ["-landroid", "-ljnigraphics", "-llog", "-lm", "-z defs", "-s", "-Wl,--version-script", "@org_tensorflow//tensorflow/contrib/android:jni/version_script.lds"],
  linkstatic = True,
  linkshared = True,
)

但是,在这种情况下,这实际上对您没有帮助,因为它没有显示科普特人!

因此,在 运行 查询之后,我结束了 "binary searching" BUILD 规则:我注释掉了除 srcs 之外的所有内容,尝试构建,然后逐渐取消注释部分,直到出现错误。