为 Raspberry Pi 设备从源代码构建软件包

本指南介绍了如何为搭载 Raspbian 9.0Raspberry Pi 设备构建 TensorFlow 软件包。下列说明可能适用于其他 Raspberry Pi 版本,但我们仅对上述配置测试了下列操作并为其提供支持。

我们建议交叉编译 TensorFlow Raspbian 软件包。交叉编译是指使用与目标部署平台不同的平台编译该软件包。Raspberry Pi 设备运行内存有限且处理器运行较为缓慢,相比之下,搭载有 Linux、macOS 或 Windows 的主机性能更加强大,以其构建 TensorFlow 会更加容易。

主机设置

安装 Docker

为了简化依赖项管理,构建脚本使用 Docker 创建用于编译的虚拟 Linux 开发环境。您可以通过执行以下命令验证 Docker 安装是否正确:docker run --rm hello-world

下载 TensorFlow 源代码

使用 Git 克隆 TensorFlow 代码库

git clone https://github.com/tensorflow/tensorflow.git
cd tensorflow

该代码库默认为 master 开发分支。您也可以检出想要构建的版本分支

git checkout branch_name  # r1.9, r1.10, etc.

从源代码构建软件包

交叉编译 TensorFlow 源代码,以使用适用于 Raspberry Pi 2、Raspberry Pi 3 和 Raspberry Pi 4 设备的 ARMv7 NEON 指令构建 Python pip 软件包。构建脚本会启动 Docker 容器进行编译。您也可以向“build_rascipber_pi.sh”脚本提供“AARCH64”参数,以构建 ARM 64 位二进制文件 (aarch64)。为目标软件包选择 Python 3.8、Python 3.7、Python 3.5 或 Python 2.7:

Python 3.5

tensorflow/tools/ci_build/ci_build.sh PI-PYTHON3 \
    tensorflow/tools/ci_build/pi/build_raspberry_pi.sh

Python 3.7

tensorflow/tools/ci_build/ci_build.sh PI-PYTHON37 \
    tensorflow/tools/ci_build/pi/build_raspberry_pi.sh

Python 3.8(64 位)

tensorflow/tools/ci_build/ci_build.sh PI-PYTHON38 \
    tensorflow/tools/ci_build/pi/build_raspberry_pi.sh AARCH64

Python 2.7

tensorflow/tools/ci_build/ci_build.sh PI \
    tensorflow/tools/ci_build/pi/build_raspberry_pi.sh

如需构建支持所有 Raspberry Pi 设备(包括 Pi 1 和 Pi 0)的软件包,请传递 PI_ONE 参数,例如:

tensorflow/tools/ci_build/ci_build.sh PI \
    tensorflow/tools/ci_build/pi/build_raspberry_pi.sh PI_ONE

完成构建(大约需要 30 分钟)后,系统会在主机源代码树的 output-artifacts 目录中创建一个 .whl 软件包文件。将 wheel 文件复制到 Raspberry Pi 并使用 pip 进行安装:

pip install tensorflow-version-cp35-none-linux_armv7l.whl