SDL中文论坛

 找回密码
 立即注册
搜索
热搜: 活动 交友 discuz
查看: 22651|回复: 0
打印 上一主题 下一主题

[Discuss] tensorflow FAQ

[复制链接]

149

主题

331

帖子

2445

积分

版主

Rank: 7Rank: 7Rank: 7

积分
2445
跳转到指定楼层
楼主
发表于 2017-7-26 22:55:42 | 只看该作者 |只看大图 回帖奖励 |倒序浏览 |阅读模式
本帖最后由 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。只是为得到以上的第二种文件,只要生成一种架构就可以了。以下是文件生成在的目录。
  • protoc生成*.pb.cc、*.pb.h。它们被生成在两个目录:<tensorflow>/contrib/makefile/gen/proto、<tensorflow>/contrib/makefile/gen/proto_text。Rose把它们改放在<tensorflow>/core。
  • eigen。<tensorflow>/contrib/makefile/downloads/eigen。Rose把它放在相同目录。
  • gemmlowp。<tensorflow>/contrib/makefile/downloads/gemmlowp。(注:当前没用这项目)


二、修改源码
为让支持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;
复制代码


三、预定义宏
  • USE_GEMM_FOR_CONV
    是否要用GEMM(General matrix multiply)实现“Conv2D”运算。这个能较大提升运算效率,因而能用的就要用。对iOS,Accelerate.framework实现了gemm(cblas_sgemm),不再须要外部依赖,因而默认要定义该宏。
  • TF_LEAN_BINARY


四、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语句注释掉。

回复

使用道具 举报

您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

Archiver|手机版|小黑屋|丽谷软件|libsdl.cn

GMT+8, 2025-5-2 00:14 , Processed in 0.054243 second(s), 22 queries .

Powered by Discuz! X3.3

© 2001-2017 Comsenz Inc.

快速回复 返回顶部 返回列表