|
本帖最后由 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部分。以下摘自那里的几个主要命令。- xcode-select --install
- brew install automake
- brew install libtool
- 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位。
- <tensorflow>/core/common_runtime/bfc_allocator.h
- #elif defined(PLATFORM_WINDOWS)
- ====>
- #elif defined(PLATFORM_WINDOWS) && defined(_WIN64)
复制代码
让DeviceAttributes& DeviceBase::attributes()存在返回值。
- <tensorflow>/core/framework/device_base.cc
- 增加
- static const DeviceAttributes attributes;
- 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 {}.
执行下面代码后,报上面错误。- tensorflow::SessionOptions options;
- tensorflow::Session* session_pointer = nullptr;
- tensorflow::Status session_status = tensorflow::NewSession(options, &session_pointer);
复制代码
创建会话(NewSession)时需要session_factories,问题出在此时就没有factories?——如果按正确逻,这时是应该有factories。
- class DirectSessionRegistrar {
- public:
- DirectSessionRegistrar() {
- SessionFactory::Register("DIRECT_SESSION", new DirectSessionFactory());
- }
- };
- static DirectSessionRegistrar registrar;
复制代码 registrar是个全局变量,但让我奇怪的是,在windows、ios,这个变量没被初始化。
问题的原因是在链接静态库时,链接器会检查静态库的变量和函数是否被使用,没有使用的不会链接。更多详细内容及解决办法参考“用Rose构建需要TensorFlow的跨平台app”。
五、生成ops_to_register.h
- bazel build tensorflow/python/tools:print_selective_registration_header
- bazel-bin/tensorflow/python/tools/print_selective_registration_header \
- --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的头文件。——我运行这命令时报下面错误。- 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>
- import mock # pylint: disable=g-import-not-at-top,unused-import
- ImportError: No module named mock
复制代码
我解决这问题的方法修改test.py,注释掉导入mock。即把以下59,60,61,62语句注释掉。
|
|