使用tensorflow作为存储库构建基于tensorflow的android应用 [英] building a tensorflow based android app with tensorflow as a repository

查看:67
本文介绍了使用tensorflow作为存储库构建基于tensorflow的android应用的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这就像来自使用Tensorflow .我想将android示例项目与tensorflow git repo分开,并能够以tensorflow作为依赖项单独构建它.这是我的文件夹结构

This is like a follow up question from Build an android app using Tensorflow. I'd like to separate the android example project from tensorflow git repo and be able to build it separately with tensorflow as a dependency. This is my folder structure

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

其中android应用只是 Tensorflow Android示例. 根WORKSPACE文件具有以下内容:

where android app is just a copy of Tensorflow Android example. The root WORKSPACE file has following contents:

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看起来像这样

android/workspace.bzl looks like this

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

def android_workspace():
  tf_workspace()

和android/BUILD与 Tensorflow Android示例BUILD ,除了我在所有地方都将@org_tensorflow前缀为//tensorflow之外,例如

and android/BUILD has the same contents as Tensorflow Android example BUILD except that I've prefixed @org_tensorflow to //tensorflow everywhere, for example

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

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

when I try to build the main target tensorflow_demo, it gives this error

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

I was able to decouple Tensorflow Android demo from Tensorflow source, thanks to Kristina. You can use the following git as a template for your Tensorflow Android projects. https://github.com/devinsaini/tensorflow_android

推荐答案

嗯,好的,我知道您在做什么.感谢您的阐述.

Ah, okay, I see what you're doing. Thanks for elaborating.

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

The problem here is a bit subtle: copts = tf_copts() calls a function in @org_tensorflow that looks like this:

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的引用.

Basically a switch statement to use different flags on different platforms. However, because load()s are evaluated before select()s, your BUILD file suddenly "contains" references to //tensorflow.

要修复,最简单的选择是对所需的副本进行硬编码或在本地存储库中重新定义tf_copts.

To fix, the easiest options are to either hard-code the copts you need or redefine tf_copts in your local repository.

如何大体上查找不正确的引用

我实际上对您收到的错误感到困惑,所以这是人们通常调试此类事情的想法.

I was actually pretty baffled by the error you were getting, so here's a brain dump of how one usually goes about debugging this kind of thing.

跟踪这种交叉引用的常用方法是1)检查您的BUILD文件(也许是部门错误)或2)打印出Bazel如何实际看到" BUILD文件.例如,如果您想在这种情况下打印评估后的构建规则,则可以执行以下操作:

The usual way to track down this kind of cross reference is 1) to examine your BUILD file (maybe the deps are just wrong) or 2) print out how Bazel actually "sees" the BUILD file. For example, if you wanted to print the evaluated build rule in this case, you could do:

$ 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,
)

但是,在这种情况下,这实际上并没有帮助您,因为它没有显示警句!

However, this doesn't actually help you in this case, because it doesn't show the copts!

因此,运行查询后,我结束了对BUILD规则的二进制搜索":我注释掉了srcs之外的所有内容,尝试构建,然后逐步取消注释,直到得到错误.

So, after running the query, I ended up "binary searching" the BUILD rule: I commented out everything but the srcs, tried building, then gradually uncommented parts until I got the error.

这篇关于使用tensorflow作为存储库构建基于tensorflow的android应用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆