背景
两个问题:
- 版本问题:英伟达官网上的tensorflow镜像没有我想要的版本。
- CPU问题:通过pip安装的tensorflow在运行时会发出警告:浪费了CPU的性能。这是因为tensorflow并不是通过源码编译造成的。
2020-01-19 08:03:05.879415: W tensorflow/core/platform/cpu_feature_guard.cc:45] The TensorFlow library wasn't compiled to use SSE4.1 instructions, but these are available on your machine and could speed up CPU computations.
2020-01-19 08:03:05.879452: W tensorflow/core/platform/cpu_feature_guard.cc:45] The TensorFlow library wasn't compiled to use SSE4.2 instructions, but these are available on your machine and could speed up CPU computations.
2020-01-19 08:03:05.879477: W tensorflow/core/platform/cpu_feature_guard.cc:45] The TensorFlow library wasn't compiled to use AVX instructions, but these are available on your machine and could speed up CPU computations.
2020-01-19 08:03:05.879484: W tensorflow/core/platform/cpu_feature_guard.cc:45] The TensorFlow library wasn't compiled to use AVX2 instructions, but these are available on your machine and could speed up CPU computations.
2020-01-19 08:03:05.879492: W tensorflow/core/platform/cpu_feature_guard.cc:45] The TensorFlow library wasn't compiled to use AVX512F instructions, but these are available on your machine and could speed up CPU computations.
2020-01-19 08:03:05.879499: W tensorflow/core/platform/cpu_feature_guard.cc:45] The TensorFlow library wasn't compiled to use FMA instructions, but these are available on your machine and could speed up CPU computations.
步骤
安装步骤整体参照tensorflow官网,结合实际情况,一些地方略有修改。此外,各类安装包的版本严格按照该官网上的建议。
- 从英伟达官网上下载对应版本镜像
- apt-get 更新
apt-get update
- 安装 python 及 tensorflow pip 软件包依赖项。其中 --user 选项表示安装在 $HOME/bin,如果没有则安装在 $HOME/local/bin。后者适用于安装在虚拟环境中。
apt-get install python-dev python-pip
pip install -U --user pip six numpy wheel setuptools mock future>=0.17.1
pip install -U --user keras_applications==1.0.6 --no-deps
pip install -U --user keras_preprocessing==1.0.5 --no-deps
- 安装 bazel 。先下载对应版本的安装包(.sh结尾)。其中版本号以实际情况为准
apt-get install zip upzip
apt-get install gcc-4.8 g++-4.8
gcc -v # 查看当前 gcc/g++ 版本
g++ -v
update-alternatives --install /usr/bin/gcc gcc /usr/bin/gcc-4.8 50 # 设置 gcc/g++ 优先级,优先使用4.8
update-alternatives --install /usr/bin/gcc gcc /usr/bin/gcc-5 20
update-alternatives --install /usr/bin/g++ g++ /usr/bin/g++-4.8 50
update-alternatives --install /usr/bin/g++ g++ /usr/bin/g++-5 20
chmod +x bazel-<version>-installer-linux-x86_64.sh # 给安装器添加执行权限
./bazel-<version>-installer-linux-x86_64.sh --user # 执行安装器
执行完毕后将环境变量添加上。例如可以将 export PATH=$PATH:/usr/bin 添加到 ~/.bashrc 文件的末尾,然后执行 suorce ~/.bashrc。由于并不需要编译 java 代码,就不继续安装 JDK8。
- 进入 tensorflow 源代码树的根目录,开始安装tensorflow。
cd tensorflow
# 配置
./configure
# 构建软件包编译器
# CPU (二选一)
bazel build --config=opt --copt=-msse4.1 --copt=-msse4.2 --copt=-mavx --copt=-mavx2 --copt=-mavx512f --copt=-mfma //tensorflow/tools/pip_package:build_pip_package
# GPU
bazel build --config=opt --config=cuda --copt=-msse4.1 --copt=-msse4.2 --copt=-mavx --copt=-mavx2 --copt=-mavx512f --copt=-mfma //tensorflow/tools/pip_package:build_pip_package
# 编译
# 从某个版本分支编译 (二选一)
./bazel-bin/tensorflow/tools/pip_package/build_pip_package /tmp/tensorflow_pkg
# 从 master 编译
./bazel-bin/tensorflow/tools/pip_package/build_pip_package --nightly_flag /tmp/tensorflow_pkg
# 安装
pip install /tmp/tensorflow_pkg/tensorflow-[version-tags].whl
如果在 bazel build 步骤遇到raw.githubusercontent.com访问受限的问题,在docker内部
配置好ssr就行了。