该存储库使从TensorFlow源代码文件夹的外部以及不使用Bazel Build System的外部使用TensorFlow C ++ API。
该存储库包含两个CMAKE项目。 Tensorflow_cc项目下载,构建和安装Tensorflow C ++ API到操作系统中,示例项目演示了其简单的用法。
如果您想立即开始使用此项目,请在Docker Hub上获取预构建的图像!
在CPU上运行图像:
docker run -it floopcz/tensorflow_cc:ubuntu /bin/bash如果您还想使用NVIDIA GPU,请安装NVIDIA DOCKER并运行:
docker run --gpus all -it floopcz/tensorflow_cc:ubuntu-cuda /bin/bash可用图像的列表:
| 图像名称 | 描述 |
|---|---|
floopcz/tensorflow_cc:ubuntu | ubuntu构建tensorflow_cc |
floopcz/tensorflow_cc:ubuntu-cuda | ubuntu构建tensorflow_cc + nvidia cuda |
floopcz/tensorflow_cc:archlinux | Arch Linux构建tensorflow_cc |
floopcz/tensorflow_cc:archlinux-cuda | tensorflow_cc + nvidia cuda的Arch Linux构建 |
要自己构建图像之一,例如ubuntu ,运行:
docker build -t floopcz/tensorflow_cc:ubuntu -f Dockerfiles/ubuntu . 安装存储库要求:
sudo apt-get install cmake curl g++-7 git python3-dev python3-numpy sudo wget
设置Python 3为默认的Python:
update-alternatives --install /usr/bin/python python /usr/bin/python3 1
为了构建张量流本身,构建过程还需要Bazel:
curl -fsSL https://bazel.build/bazel-release.pub.gpg | gpg --dearmor > bazel.gpg
sudo mv bazel.gpg /etc/apt/trusted.gpg.d/
echo "deb [arch=amd64] https://storage.googleapis.com/bazel-apt stable jdk1.8" | sudo tee /etc/apt/sources.list.d/bazel.list
sudo apt-get update && sudo apt-get install bazel
如果您需要在Ubuntu上进行GPU支持,请还安装NVIDIA CUDA TOOLKIT(> = 11.1),NVIDIA驱动程序,Cudnn和cuda-command-line-tools软件包。如果构建过程将其安装在/opt/cuda或/usr/local/cuda目录中,则将自动检测到它。
sudo pacman -S base-devel bazel cmake git python python-numpy wget
对于ARCH上的GPU支持,也安装以下内容:
sudo pacman -S cuda cudnn nvidia
警告: TensorFlow的新版本有时无法使用最新版本的Bazel构建。您可能希望安装旧版本的Bazel(例如,5.1.1)。
警告:如果您的程序使用Protobuf并遇到链接或其他问题,则可以尝试-DINSTALL_PROTOBUF=ON Onsine安装Protobuf版本,该版本匹配与TensorFlow捆绑在一起的版本。我们的Dockerfiles已经使用了正确版本的Protobuf构建,因此您可能需要先在Dockerfile中尝试您的程序。
git clone https://github.com/FloopCZ/tensorflow_cc.git
cd tensorflow_cc
cd tensorflow_cc
mkdir build && cd build
cmake ..
make
sudo make install
sudo ldconfig
警告:默认情况下启用了Intel CPU生成>=haswell的优化。如果您的处理器比haswell Generation更古老,则可能希望在构建之前运行export CC_OPT_FLAGS="-march=native" 。此命令为您当前的CPU生成提供了最佳的优化,但可能会导致构建库与老一代不兼容。
警告:在低内存或多个CPU环境中,Bazel调度程序可能会错过资源消耗估算,并且可以由内存外杀手终止。如果是您的情况,请考虑将资源限制参数添加到cmake,例如, cmake -DLOCAL_RAM_RESOURCES=2048 -DLOCAL_CPU_RESOURCES=4 ..
# cleanup bazel build directory
rm -rf ~/.cache
# remove the build folder
cd .. && rm -rf build
// example.cpp
# include < tensorflow/core/platform/env.h >
# include < tensorflow/core/public/session.h >
# include < iostream >
using namespace std ;
using namespace tensorflow ;
int main ()
{
Session* session;
Status status = NewSession ( SessionOptions (), &session);
if (!status. ok ()) {
cout << status. ToString () << " n " ;
return 1 ;
}
cout << " Session successfully created. n " ;
} # CMakeLists.txt
find_package (TensorflowCC REQUIRED)
add_executable (example example.cpp)
# Link the Tensorflow library.
target_link_libraries (example TensorflowCC::TensorflowCC)
# You may also link cuda if it is available.
# find_package(CUDA)
# if(CUDA_FOUND)
# target_link_libraries(example ${CUDA_LIBRARIES})
# endif() mkdir build && cd build
cmake .. && make
./example
如果您仍然不确定,请咨询Ubuntu和Arch Linux的Dockerfiles。