基于源代码为树莓派设备构建 TensorFlow

本指南为运行 Raspbian 9.0 操作系统的 Raspberry Pi 嵌入式设备构建 TensorFlow。虽然这些说明可能也适用于其他系列的 Raspberry Pi 设备,但它仅针对此文中涉及的配置进行了测试和支持。

我们建议采用交叉编译的方式构建 TensorFlow Raspbian 软件包。交叉编译使用不同的平台来构建软件包。也就是说,我们并没有在配备有限 RAM 和相对较慢处理器的 Raspberry Pi 设备上构建 TensorFlow,而是使用性能更加强大的主机进行构建。

主机设置

安装 Docker
为简化依赖关系管理,我们使用 Docker 创建虚拟 Linux 开发环境。通过执行以下命令验证 Docker 是否安装:docker run –rm hello-world

下载 TensorFlow 源代码
使用 Git 克隆 TensorFlow:

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

默认为 master 分支。您还可以签出要构建的 release 分支:

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

基于源代码构建

交叉编译 TensorFlow 源代码,从而构建一个基于 ARMv7 NEON 指令的 Python pip 包, 该指令适用于 Raspberry Pi 2 和 3 设备。构建脚本启动 Docker 容器以进行编译。基于 Python 3 和 Python 2.7 的代码如下所示:
Python 3

CI_DOCKER_EXTRA_PARAMS="-e CI_BUILD_PYTHON=python3 -e CROSSTOOL_PYTHON_INCLUDE_PATH=/usr/include/python3.4" \
    tensorflow/tools/ci_build/ci_build.sh PI-PYTHON3 \
    tensorflow/tools/ci_build/pi/build_raspberry_pi.sh


Python 2.7

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

为了构建支持所有 Raspberry Pi 设备的包,如下所示:

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

构建完成后(约30分钟),将在 output-artifacts 目录中创建一个包文件。将此文件复制到 Raspberry Pi 并通过 pip 进行安装:

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

via Google TensorFlow

这是一篇发布于 5年 前的文章,其中的信息可能已经有所发展或是发生改变,请了解。


2 评论

  1. 用脚本(“curl -sSL https://get.docker.com | sh”)安装docker的时候提示
    W: 仓库 “https://download.docker.com/linux/raspbian (lsb_release Release” 没有 Release 文件。
    W: GPG 错误:https://packagecloud.io/Hypriot/Schatzkiste/debian wheezy InRelease: 由于没有公钥,无法验证下列签名: NO_PUBKEY 37BBEE3F7AD95B3F
    W: 仓库 “https://packagecloud.io/Hypriot/Schatzkiste/debian wheezy InRelease” 没有数字签名。
    E: 无法下载 https://download.docker.com/linux/raspbian/dists/(lsb_release/-cs)/binary-armhf/Packages 404 Not Found
    E: 部分索引文件下载失败。如果忽略它们,那将转而使用旧的索引文件。

    用官网方法安装的时候,提示:
    could not find a distribution template for Raspbian/stretch

    官网没有树莓派的安装包了吗?求问现在怎么装docker?

  2. Q.用官网方法安装的时候,提示:
    could not find a distribution template for Raspbian/stretch
    官网没有树莓派的安装包了吗?
    A. docker是安装在主机上的,而不是在Raspberry上安装docker。
    >> 也就是说,我们并没有在配备有限 RAM 和相对较慢处理器的 Raspberry Pi 设备上构建 TensorFlow,而是使用性能更加强大的主机进行构建。

    q. 求问现在怎么装docker?
    A. Linux主机上安装docker,请参考docker官方文档。若你的主机为Ubuntu, 请戳这里https://docs.docker.com/install/linux/docker-ce/ubuntu/

发表评论

你的邮件地址不会公开


*