SDL中文论坛

标题: tensorflow FAQ [打印本页]

作者: ancientcc    时间: 2017-7-26 22:55
标题: tensorflow FAQ
本帖最后由 ancientcc 于 2017-8-14 21:54 编辑

一、编译
不管哪个平台,编译tensorflow需要两部分文件,一部分从直接下载的源码包中就能得到,另一部分需要用工具预先生成或下载。预生成工具是protoc,protoc根据*.proto生成*.pb.cc、*.pb.h。预下载文件是tensorflow依赖的库,包括eigen、gemmlowp。

执行一遍官方提供的编译流程可以得到第二部分文件。建议编译iOS版,流程参考(https://github.com/tensorflow/te ... ow/contrib/makefile)中的iOS部分。以下摘自那里的几个主要命令。
  1. xcode-select --install
  2. brew install automake
  3. brew install libtool
  4. tensorflow/contrib/makefile/build_all_ios.sh
复制代码

build_all_ios.sh会同时编译5种架构,ARMV7、ARMV7S、ARM64、I386、X86_64。只是为得到以上的第二种文件,只要生成一种架构就可以了。以下是文件生成在的目录。


二、修改源码
为让支持Windows下的32位。
  1. <tensorflow>/core/common_runtime/bfc_allocator.h
  2. #elif defined(PLATFORM_WINDOWS)
  3. ====>
  4. #elif defined(PLATFORM_WINDOWS) && defined(_WIN64)
复制代码


让DeviceAttributes& DeviceBase::attributes()存在返回值。
  1. <tensorflow>/core/framework/device_base.cc
  2. 增加
  3. static const DeviceAttributes attributes;
  4. return attributes;
复制代码


三、预定义宏


四、Session create failed - Not found: No session factory registered for the given session options: {target: "" config: } Registered factories are {}.
执行下面代码后,报上面错误。
  1. tensorflow::SessionOptions options;
  2. tensorflow::Session* session_pointer = nullptr;
  3. tensorflow::Status session_status = tensorflow::NewSession(options, &session_pointer);
复制代码

创建会话(NewSession)时需要session_factories,问题出在此时就没有factories?——如果按正确逻,这时是应该有factories。
  1. class DirectSessionRegistrar {
  2. public:
  3.         DirectSessionRegistrar() {
  4.                 SessionFactory::Register("DIRECT_SESSION", new DirectSessionFactory());
  5.         }
  6. };
  7. static DirectSessionRegistrar registrar;
复制代码
registrar是个全局变量,但让我奇怪的是,在windows、ios,这个变量没被初始化。

问题的原因是在链接静态库时,链接器会检查静态库的变量和函数是否被使用,没有使用的不会链接。更多详细内容及解决办法参考“用Rose构建需要TensorFlow的跨平台app”。

五、生成ops_to_register.h
  1. bazel build tensorflow/python/tools:print_selective_registration_header
  2. bazel-bin/tensorflow/python/tools/print_selective_registration_header \
  3.    --graphs=path/to/graph.pb > ops_to_register.h
复制代码

第二条语句执行print_selective_registration_header.py,graphs参数指示要计算的ops的*.pb(proto: GraphDef),像tensorflow_inception_graph.pb,然后把结果放在一个叫ops_to_register.h的头文件。——我运行这命令时报下面错误。
  1. File "/Users/ancientcc/tensorflow/bazel-bin/tensorflow/python/tools/print_selective_registration_header.runfiles/org_tensorflow/tensorflow/python/platform/test.py", line 60, in <module>
  2. import mock                # pylint: disable=g-import-not-at-top,unused-import
  3. ImportError: No module named mock
复制代码

我解决这问题的方法修改test.py,注释掉导入mock。即把以下59,60,61,62语句注释掉。
(, 下载次数: 7232)





欢迎光临 SDL中文论坛 (http://www.libsdl.cn/bbs/) Powered by Discuz! X3.3