使用 Bazel (Windows) 构建 Tensorflow 的问题 => 使用 Tensorflow C++ API

Issue building Tensorflow using Bazel (Windows) => to use Tensorflow C++ API

本文关键字:Tensorflow 使用 gt API C++ Bazel Windows 构建 问题      更新时间:2023-10-16

我最初的目标只是使用Tensorflow的C++ API来读取".pb"冻结模型并对其进行预测。正在使用 Keras 对其进行原型设计,设法冻结模型并在 Python 上使用 Tensorflow 进行尝试;一切都很顺利。 所以我基本上只需要 C++ API 即可工作。

我使用的是i5-8400T计算机,没有GPU,8GB内存。所有版本的bazel和tensorflow今天(27/08/2019(下载。

我做的第一件事当然是遵循 tensorflow 的指南。跑了小bazel run -c opt //tensorflow/cc/example:example,那...几次。

第一次,需要将我的 bazel 版本降级到0.26.1以使 TensorFlow 的configure.py满意。

然后我遇到了与一些人显然相同的问题,这是一个获取io_bazel_rules_docker的错误,然后当然会停止构建的其余部分。(以下是我在这里得到的输出(



bazel : INFO: Options provided by the client:
Au caractère Ligne:1 : 1
+ bazel run -c opt //tensorflow/cc/example:example > error.txt 2>&1
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
+ CategoryInfo          : NotSpecified: (INFO: Options provided by the client::String) [], RemoteException
+ FullyQualifiedErrorId : NativeCommandError
Inherited 'common' options: --isatty=0 --terminal_columns=80
INFO: Options provided by the client:
Inherited 'build' options: --python_path=C:/Users/IMEDSYS/AppData/Local/Programs/Python/Python37/python.exe
INFO: Reading rc options for 'run' from c:usersimedsysbintensorflow.bazelrc:
Inherited 'build' options: --apple_platform_type=macos --define framework_shared_object=true --define 
open_source_build=true --define=use_fast_cpp_protos=true --define=allow_oversize_protos=true 
--spawn_strategy=standalone --strategy=Genrule=standalone -c opt --cxxopt=-std=c++14 --host_cxxopt=-std=c++14 
--announce_rc --define=grpc_no_ares=true --define=PREFIX=/usr --define=LIBDIR=$(PREFIX)/lib 
--define=INCLUDEDIR=$(PREFIX)/include
INFO: Reading rc options for 'run' from c:usersimedsysbintensorflow.tf_configure.bazelrc:
Inherited 'build' options: --action_env 
PYTHON_BIN_PATH=C:/Users/IMEDSYS/AppData/Local/Programs/Python/Python37/python.exe --action_env 
PYTHON_LIB_PATH=C:/Users/IMEDSYS/AppData/Local/Programs/Python/Python37/lib/site-packages 
--python_path=C:/Users/IMEDSYS/AppData/Local/Programs/Python/Python37/python.exe --config monolithic --copt=-w 
--host_copt=-w --copt=-DWIN32_LEAN_AND_MEAN --host_copt=-DWIN32_LEAN_AND_MEAN --copt=-DNOGDI --host_copt=-DNOGDI 
--verbose_failures --distinct_host_configuration=false --action_env TF_CONFIGURE_IOS=0
INFO: Found applicable config definition build:monolithic in file c:usersimedsysbintensorflow.bazelrc: --define 
framework_shared_object=false
Loading: 
Loading: 0 packages loaded
INFO: Call stack for the definition of repository 'io_bazel_rules_docker' which is a git_repository (rule definition 
at C:/users/imedsys/_bazel_imedsys/wmdtvioi/external/bazel_tools/tools/build_defs/repo/git.bzl:252:18):
- C:/users/imedsys/_bazel_imedsys/wmdtvioi/external/bazel_toolchains/repositories/repositories.bzl:37:9
- C:/users/imedsys/bin/tensorflow/WORKSPACE:35:1
ERROR: An error occurred during the fetch of repository 'io_bazel_rules_docker':
Traceback (most recent call last):
File "C:/users/imedsys/_bazel_imedsys/wmdtvioi/external/bazel_tools/tools/build_defs/repo/git.bzl", line 234
_clone_or_update(ctx)
File "C:/users/imedsys/_bazel_imedsys/wmdtvioi/external/bazel_tools/tools/build_defs/repo/git.bzl", line 74, in 
_clone_or_update
fail(("error cloning %s:n%s" % (ctx....)))
error cloning io_bazel_rules_docker:
fatal: not a git repository (or any parent up to mount point /mnt)
Stopping at filesystem boundary (GIT_DISCOVERY_ACROSS_FILESYSTEM not set).
/bin/bash: line 1: cd: C:/users/imedsys/_bazel_imedsys/wmdtvioi/external: No such file or directory
+ cd C:/users/imedsys/_bazel_imedsys/wmdtvioi/external
/bin/bash: line 3: cd: C:/users/imedsys/_bazel_imedsys/wmdtvioi/external: No such file or directory
+ git -C C:/users/imedsys/_bazel_imedsys/wmdtvioi/external/io_bazel_rules_docker reset --hard 
251f6a68b439744094faff800cd029798edf9faa
fatal: cannot change to 'C:/users/imedsys/_bazel_imedsys/wmdtvioi/external/io_bazel_rules_docker': No such file or 
directory
+ git -C C:/users/imedsys/_bazel_imedsys/wmdtvioi/external/io_bazel_rules_docker fetch '' origin 
251f6a68b439744094faff800cd029798edf9faa:251f6a68b439744094faff800cd029798edf9faa
fatal: cannot change to 'C:/users/imedsys/_bazel_imedsys/wmdtvioi/external/io_bazel_rules_docker': No such file or 
directory
+ git -C C:/users/imedsys/_bazel_imedsys/wmdtvioi/external/io_bazel_rules_docker fetch origin 
251f6a68b439744094faff800cd029798edf9faa:251f6a68b439744094faff800cd029798edf9faa
fatal: cannot change to 'C:/users/imedsys/_bazel_imedsys/wmdtvioi/external/io_bazel_rules_docker': No such file or 
directory
ERROR: ... *idem*
ERROR: ... *idem*
INFO: Elapsed time: 2.346s
INFO: 0 processes.
FAILED: Build did NOT complete successfully (0 packages loaded)
ERROR: Build failed. Not running target
FAILED: Build did NOT complete successfully (0 packages loaded)
***

我尝试应用此问题提供的内容,其中包括转到rules_docker存储库并用最新的目标替换目标。

问题是,即使在这之后,你也会在 protobuf 目标中遇到同样类型的错误,如果我应用相同的策略,我会收到哈希错误。

然后我试着用CMake来做这件事,就像去年许多人说的那样,窗户上的bazel建筑还无法使用。我使用了两个类似的指南,其中一个在这里,因此尝试使用

cmake .. -A x64 -DCMAKE_BUILD_TYPE=Release -DSWIG_EXECUTABLE=C:swigwin-3.0.12swig.exe -DPYTHON_EXECUTABLE=C:Anaconda3envstensorflowpython.exe -DPYTHON_LIBRARIES=C:Anaconda3envstensorflowlibspython35.lib

首先,然后

`cmake .. -A x64 -DCMAKE_BUILD_TYPE=Release -DSWIG_EXECUTABLE=C:swigwin-3.0.12swig.exe -DPYTHON_EXECUTABLE=C:Anaconda3envstensorflowpython.exe -DPYTHON_LIBRARIES=C:Anaconda3envstensorflowlibspython35.lib -Dtensorflow_BUILD_PYTHON_BINDINGS=OFF -Dtensorflow_ENABLE_GRPC_SUPPORT=OFF -Dtensorflow_BUILD_SHARED_LIB=ON`

我得到这些错误:


cmake : CMake Warning at CMakeLists.txt:9 (message):
Au caractère Ligne:1 : 1
+ cmake .. -A x64 -DCMAKE_BUILD_TYPE=Release -DSWIG_EXECUTABLE=C:Users ...
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
+ CategoryInfo          : NotSpecified: (CMake Warning a...xt:9 (message)::String) [], RemoteException
+ FullyQualifiedErrorId : NativeCommandError
Your current cmake generator is set to use 32 bit toolset architecture.
This may cause "compiler out of heap space" errors when building.  Consider
using the flag -Thost=x64 when running cmake.  Ignore this if you are on
CMake GUI.

-- Selecting Windows SDK version  to target Windows 10.0.17763.
-- C:/Users/IMEDSYS/bin/tensorflow/tensorflow/contrib/cmake/build/abseil_cpp/src/abseil_cpp_build
-- Configuring done
CMake Error at tf_core_ops.cmake:77 (add_library):
Cannot find source file:
C:/Users/IMEDSYS/bin/tensorflow/tensorflow/contrib/factorization/ops/clustering_ops.cc
Tried extensions .c .C .c++ .cc .cpp .cxx .cu .m .M .mm .h .hh .h++ .hm
.hpp .hxx .in .txx
Call Stack (most recent call first):
tf_core_ops.cmake:95 (GENERATE_CONTRIB_OP_LIBRARY)
CMakeLists.txt:594 (include)

CMake Error at tf_core_ops.cmake:77 (add_library):
Cannot find source file:
C:/Users/IMEDSYS/bin/tensorflow/tensorflow/contrib/rnn/ops/gru_ops.cc
Tried extensions .c .C .c++ .cc .cpp .cxx .cu .m .M .mm .h .hh .h++ .hm
.hpp .hxx .in .txx
Call Stack (most recent call first):
tf_core_ops.cmake:107 (GENERATE_CONTRIB_OP_LIBRARY)
CMakeLists.txt:594 (include)

CMake Error at tf_core_ops.cmake:77 (add_library):
Cannot find source file:
C:/Users/IMEDSYS/bin/tensorflow/tensorflow/contrib/rnn/ops/lstm_ops.cc
Tried extensions .c .C .c++ .cc .cpp .cxx .cu .m .M .mm .h .hh .h++ .hm
.hpp .hxx .in .txx
Call Stack (most recent call first):
tf_core_ops.cmake:108 (GENERATE_CONTRIB_OP_LIBRARY)
CMakeLists.txt:594 (include)

CMake Error at tf_core_kernels.cmake:205 (add_library):
Cannot find source file:
C:/Users/IMEDSYS/bin/tensorflow/tensorflow/contrib/factorization/kernels/clustering_ops.cc
Tried extensions .c .C .c++ .cc .cpp .cxx .cu .m .M .mm .h .hh .h++ .hm
.hpp .hxx .in .txx
Call Stack (most recent call first):
CMakeLists.txt:596 (include)

CMake Error at tf_core_ops.cmake:77 (add_library):
No SOURCES given to target: tf_contrib_factorization_clustering_ops
Call Stack (most recent call first):
tf_core_ops.cmake:95 (GENERATE_CONTRIB_OP_LIBRARY)
CMakeLists.txt:594 (include)

CMake Error at tf_core_ops.cmake:77 (add_library):
No SOURCES given to target: tf_contrib_rnn_gru_ops
Call Stack (most recent call first):
tf_core_ops.cmake:107 (GENERATE_CONTRIB_OP_LIBRARY)
CMakeLists.txt:594 (include)

CMake Error at tf_core_ops.cmake:77 (add_library):
No SOURCES given to target: tf_contrib_rnn_lstm_ops
Call Stack (most recent call first):
tf_core_ops.cmake:108 (GENERATE_CONTRIB_OP_LIBRARY)
CMakeLists.txt:594 (include)

CMake Error at tf_core_ops.cmake:77 (add_library):
No SOURCES given to target: tf_contrib_tpu_ops
Call Stack (most recent call first):
tf_core_ops.cmake:115 (GENERATE_CONTRIB_OP_LIBRARY)
CMakeLists.txt:594 (include)

CMake Error at tf_core_kernels.cmake:205 (add_library):
No SOURCES given to target: tf_core_kernels
Call Stack (most recent call first):
CMakeLists.txt:596 (include)

CMake Generate step failed.  Build files cannot be regenerated correctly.

这次我觉得只是 CMake 查找已折旧或重新定位的文件。

所以

我的问题很简单:

  • 是否有人有 2 个 Bazel 和 TensorFlow 的版本号实际上可以一起工作(因为它似乎对某些人有效 => 我考虑尝试 Voodoo(?
  • 是否有人有一个非常简单的解决方案来使用 Tensorflow 的 c_api 或C++ API?
  • 是否有人知道另一种方法,可以从C++构建的软件中使用 Keras 或 Tensorflow 输出(.hdf5 或 .pb(进行预测,而不会像"system("Python predict.py"(那样令人作呕?

提前感谢您的快速回答! :)

我做过类似的工作,使用 Cmake 为 VS 2017 构建较旧的 Tensorflow 版本(不是 2.(。

库达 9 .张量流 1.14 VS 2017 .