使用tensorflow作为存储库构建基于张量流的Android应用程序

时间:2016-12-14 22:18:02

标签: android tensorflow bazel

这就像来自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 app只是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具有相同的内容,除了我已经将@org_tensorflow前缀到// 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

1 个答案:

答案 0 :(得分:2)

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

这里的问题有点微妙: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()之前评估了select(),因此您的BUILD文件突然“包含”对//tensorflow的引用。

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

如何一般性地追踪错误的引用

我实际上对你得到的错误感到非常困惑,所以这里有一个关于如何通常调试这种事情的大脑转储。

跟踪此类交叉引用的常用方法是1)检查您的BUILD文件(可能是deps是错误的)或2)打印出Bazel实际上如何“看到”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,
)

但是,在这种情况下,这实际上并没有帮助你,因为它没有显示小册子!

因此,在运行查询之后,我最终“二进制搜索”了BUILD规则:除了srcs之外,我注释掉了所有内容,尝试构建,然后逐渐取消注释部分,直到我收到错误。