介紹
本頁面將指導(dǎo)您在搭載64位Bullseye操作系統(tǒng)的Raspberry Pi 4上安裝TensorFlow。
TensorFlow是一個專為深度學(xué)習(xí)開發(fā)的大型軟件庫,它消耗大量資源。您可以在Raspberry Pi 4上運(yùn)行TensorFlow,但不要期望有奇跡般的表現(xiàn)。如果模型不太復(fù)雜,它可以運(yùn)行您的模型,但無法訓(xùn)練新模型,也無法執(zhí)行所謂的遷移學(xué)習(xí)。除了運(yùn)行您預(yù)先構(gòu)建的深度學(xué)習(xí)模型外,您還可以使用該庫將所謂的凍結(jié)TensorFlow模型轉(zhuǎn)換為TensorFlow Lite平面緩沖區(qū)模型。
如果您只想對深度學(xué)習(xí)有個初步了解,請考慮安裝TensorFlow Lite。它速度更快,使用的資源更少,因?yàn)樗菫镽aspberry Pi這樣的小型計(jì)算機(jī)設(shè)計(jì)的。您可以使用許多現(xiàn)成的模型。有關(guān)在64位Raspberry Pi上安裝的指南,請參閱此處。
路線圖
TensorFlow不斷發(fā)展壯大。每個新版本都需要更多的資源、支持軟件和庫。它給您的Raspberry Pi帶來了越來越重的負(fù)擔(dān)。這就解釋了為什么最新版本在搭載“過時”操作系統(tǒng)的“舊”Raspberry Pi上運(yùn)行不佳。
另一方面,不建議在最新的Bullseye上安裝非常舊的TensorFlow版本。您將不得不降級一些系統(tǒng)庫,這將導(dǎo)致其他軟件無法正常工作。這里的座右銘是:“隨波逐流”。
以下是概覽。綠色勾選標(biāo)記表示有可用的wheel文件??站G色框表示沒有wheel文件,但仍然可以安裝?;疑虮硎居布蜍浖拗?,不允許進(jìn)行“正?!卑惭b。
提示
我們經(jīng)常收到詢問,是否有預(yù)裝了框架和深度學(xué)習(xí)示例的Raspberry Pi 4的SD卡鏡像。我們很高興滿足這一要求。請?jiān)谖覀兊腉itHub頁面上找到專用于深度學(xué)習(xí)的完整工作Raspberry Pi 4。從我們的GDrive網(wǎng)站下載zip文件,解壓并將鏡像刷入32GB的SD卡,然后盡情享受吧!
我們討論了兩種安裝,一種是針對Python 3的,另一種是針對C++ API庫的。不幸的是,對于2.7、2.6或2.5版本,沒有官方的aarch64 pip3 wheel文件可用。但是,我們使用Bazel創(chuàng)建了wheel文件,并將其放在GitHub上供您使用。
本指南的最后部分討論了Keras的安裝。
準(zhǔn)備
Numpy
TensorFlow與最新版本的numpy遇到了問題。將TensorFlow移植到numpy 1.20成為了一項(xiàng)真正的挑戰(zhàn)。現(xiàn)在,隨著TensorFlow版本2.8.0的發(fā)布,它終于成功了。最后,在安裝TensorFlow時,再也沒有numpy版本沖突了。
但是,TensorFlow 2.7.0仍然報(bào)告了一些問題。為了確保一切正常,請使用numpy版本1.19.5與TF 2.7.0搭配使用。
libclang 9.0.1
TensorFlow 2.7.0依賴于libclang 9.0.1。Debian 10沒有可用的發(fā)行版。這就是為什么只有針對Debian 11(Bullseye)的TensorFlow 2.7安裝。您可能可以從頭開始在您的Buster RPi上安裝libclang 9.0.1,然后才能安裝TensorFlow。請注意,clang的構(gòu)建需要巨大的資源,超過5GB。最好切換到Bullseye,并在半小時內(nèi)讓TensorFlow運(yùn)行起來。
Protobuffer 4.21
Protobuffer的最新版本4.21.0相比之前的版本3.20.1有了一些顯著的改進(jìn)。但是,TensorFlow尚不支持這些更改。為了讓TensorFlow正常工作,如果您安裝了4.21,則需要將您的Protobuf降級到3.20版本。
有關(guān)如何降級的更多信息,可以在我們的GitHub頁面上找到。
filesystem
除了tensorflow-io-gcs文件系統(tǒng)外,所有依賴項(xiàng)都可以通過一個命令安裝。由于沒有針對aarch64機(jī)器的發(fā)行版,我們必須從頭開始構(gòu)建tensorflow-io-gcs文件系統(tǒng)。整個過程可以在下面找到,并且應(yīng)該在安裝TensorFlow本身之前完成。如果您讓TensorFlow安裝io-gcs,它將選擇錯誤的版本并且無法工作。
# get a fresh start$ sudo apt-get update$ sudo apt-get upgrade# install pip3$ sudo apt-get install git python3-pip# install correct version protobuf$ sudo -H pip3 install --upgrade protobuf==3.20.0Method 1# download tensorflow io$ git clone -b v0.23.1 --depth=1 --recursive https://github.com/tensorflow/io.git$ cd io$ python3 setup.py -q bdist_wheel --project tensorflow_io_gcs_filesystem$ cd dist$ sudo -H pip3 install tensorflow_io_gcs_filesystem-0.23.1-cp39-cp39-linux_aarch64.whl$ cd ~Method 2# or download wheel$ git clone https://github.com/Qengineering/Tensorflow-io.git$ cd Tensorflow-io$ sudo -H pip3 install tensorflow_io_gcs_filesystem-0.23.1-cp39-cp39-linux_aarch64.whl$ cd ~
TensorFlow Python 3.9的wheel文件
TensorFlow是由名為Bazel的Google軟件安裝程序安裝的。最后,Bazel會生成一個wheel文件來安裝TensorFlow的Python版本,或者當(dāng)涉及到安裝C++版本時,會生成一個tarball文件。這兩種方法都是Raspberry Pi用戶所熟知的。我們已經(jīng)將Bazel的結(jié)果發(fā)布在我們的GitHub頁面上。請隨意使用這些wheel文件。從頭到尾整個TensorFlow安裝過程需要數(shù)小時(Python版約為64小時,C++庫版約為1小時)。由于所有繁瑣的工作都已經(jīng)完成,因此在您的Raspberry 64位Bullseye上安裝TensorFlow只需要幾分鐘。對于頑固分子,本手冊后面將介紹完整的過程。
整個快捷方式過程如下。由于wheel文件太大,無法存儲在GitHub上,因此使用了Google Drive。
TensorFlow 2.14.0
# install TensorFlow 2.14.0$ sudo -H pip3 install --upgrade tensorflow==2.14.0
TensorFlow 2.13.0
# install TensorFlow 2.13.0$ sudo -H pip3 install --upgrade tensorflow==2.13.0
TensorFlow 2.12.0
# install TensorFlow 2.12.0$ sudo -H pip3 install --upgrade tensorflow==2.12.0
TensorFlow 2.11.0
# install TensorFlow 2.11.0$ sudo -H pip3 install --upgrade tensorflow==2.11.0
TensorFlow 2.10.0
# install gdown to download from Google drive$ sudo -H pip3 install gdown# download the wheel$ gdown https://drive.google.com/uc?id=1G2P-FaHAXJ-UuQAQn_0SYjNwBu0aShpd# install TensorFlow 2.10.0$ sudo -H pip3 install tensorflow-2.10.0-cp39-cp39-linux_aarch64.whl
TensorFlow 2.9.1
# install gdown to download from Google drive$ sudo -H pip3 install gdown# download the wheel$ gdown https://drive.google.com/uc?id=1xP6ErBK85SMFnQamUh4ro3jRmdCV_qDU# install TensorFlow 2.9.1$ sudo -H pip3 install tensorflow-2.9.1-cp39-cp39-linux_aarch64.whl
TensorFlow 2.8.1
# install gdown to download from Google drive$ sudo -H pip3 install gdown# download the wheel$ gdown https://drive.google.com/uc?id=1YpxNubmEL_4EgTrVMu-kYyzAbtyLis29# install TensorFlow 2.8.0$ sudo -H pip3 install tensorflow-2.8.0-cp39-cp39-linux_aarch64.whl
TensorFlow 2.7.0
# utmost important: useonly numpy version 1.19.5# check the version first$ pip3 list | grep numpy# if not version 1.19.5, update!$ sudo -H pip3 install numpy==1.19.5# (re)install termcolor at the correct location$ python3 -m pip install termcolor# install gdown to download from Google drive$ sudo -H pip3 install gdown# download the wheel$ gdown https://drive.google.com/uc?id=1FdVZ1kX5QZgWk2SSgq31C2-CF95QhT58# install TensorFlow 2.7.0$ sudo -H pip3 install tensorflow-2.7.0-cp39-cp39-linux_aarch64.whl
當(dāng)安裝成功時,您應(yīng)該會看到以下屏幕截圖。
TensorFlow C++ API.
如果您打算用C++編程,您將需要TensorFlow的C++ API構(gòu)建版本,而不是Python版本。從我們的GitHub頁面使用預(yù)構(gòu)建的tarball文件來安裝C++庫可以為您節(jié)省大量時間。請按照以下步驟操作。
TensorFlow 2.10.0
# get a fresh start$ sudo apt-get update$ sudo apt-get upgrade# remove old versions (if found)$ sudo rm -r /usr/local/lib/libtensorflow*$ sudo rm -r /usr/local/include/tensorflow# the dependencies$ sudo apt-get install wget curl libhdf5-dev libc-ares-dev libeigen3-dev$ sudo apt-get install libatomic1 libatlas-base-dev zip unzip# install gdown to download from Google drive (if not already done)$ sudo -H pip3 install gdown# download the tarball$ gdown https://drive.google.com/uc?id=1GOC5CiT5Ws2NpiBem4K3g3FRqmGDRcL7# unpack the ball$ sudo tar -C /usr/local -xzf libtensorflow_cp39_64OS_2_10_0.tar.gz
TensorFlow 2.9.1
# get a fresh start$ sudo apt-get update$ sudo apt-get upgrade# remove old versions (if found)$ sudo rm -r /usr/local/lib/libtensorflow*$ sudo rm -r /usr/local/include/tensorflow# the dependencies$ sudo apt-get install wget curl libhdf5-dev libc-ares-dev libeigen3-dev$ sudo apt-get install libatomic1 libatlas-base-dev zip unzip# install gdown to download from Google drive (if not already done)$ sudo -H pip3 install gdown# download the tarball$ gdown https://drive.google.com/uc?id=1Z83_RQTvCb2jL2BO1Zdez3x4Qx-XheRk# unpack the ball$ sudo tar -C /usr/local -xzf libtensorflow_cp39_64OS_2_9_1.tar.gz
TensorFlow 2.8.0
# get a fresh start$ sudo apt-get update$ sudo apt-get upgrade# remove old versions (if found)$ sudo rm -r /usr/local/lib/libtensorflow*$ sudo rm -r /usr/local/include/tensorflow# the dependencies$ sudo apt-get install wget curl libhdf5-dev libc-ares-dev libeigen3-dev$ sudo apt-get install libatomic1 libatlas-base-dev zip unzip# install gdown to download from Google drive (if not already done)$ sudo -H pip3 install gdown# download the tarball$ gdown https://drive.google.com/uc?id=1dmJKIk8lUi_XCzlVnRgL-UvfVFriRmCG# unpack the ball$ sudo tar -C /usr/local -xzf libtensorflow_cp39_64OS_2_8_0.tar.gz
TensorFlow 2.7.0
# get a fresh start$ sudo apt-get update$ sudo apt-get upgrade# remove old versions (if found)$ sudo rm -r /usr/local/lib/libtensorflow*$ sudo rm -r /usr/local/include/tensorflow# the dependencies$ sudo apt-get install wget curl libhdf5-dev libc-ares-dev libeigen3-dev$ sudo apt-get install libatomic1 libatlas-base-dev zip unzip# install gdown to download from Google drive (if not already done)$ sudo -H pip3 install gdown# download the tarball$ gdown https://drive.google.com/uc?id=1kScCKyj0pr265XbCgYmXqXs77xJFe6p1# unpack the ball$ sudo tar -C /usr/local -xzf libtensorflow_cp39_64OS_2_7_0.tar.gz
最終,您的TensorFlow庫應(yīng)該會安裝在/usr/local/lib位置,而頭文件則會位于/usr/local/include/tensorflow/c文件夾中。
從零開始的安裝
針對TensorFlow 2.14及以上版本
https://qengineering.eu/install-tensorflow-on-raspberry-pi-5.html內(nèi)存交換空間大小
構(gòu)建完整的TensorFlow包需要超過6GB的內(nèi)存。如果你有一臺配備8GB內(nèi)存的Raspberry Pi 4,那么你可以輕松應(yīng)對。否則,請確保增加交換空間大小以滿足這一需求。如果你的設(shè)備有4GB內(nèi)存,zram可以提供額外的2GB空間。而如果只有2GB內(nèi)存,則無法再依賴zram實(shí)現(xiàn)超過2倍的壓縮。在這種情況下,你需要重新安裝dphys-swapfile以從SD卡獲取額外空間。如果你需要安裝dphys-swapfile,請按照以下命令操作。在Bullseye操作系統(tǒng)上設(shè)置交換空間時,重啟過程可能需要較長時間。
# install dphys-swapfile
$ sudo apt-get install dphys-swapfile# give the required memory size$ sudo nano /etc/dphys-swapfile# reboot afterwards$ sudo reboot
如果一切順利,你應(yīng)該會看到類似下面的內(nèi)容。
圖中所示為dphys-swapfile和zram分配的交換空間總量。完成后,請不要忘記刪除dphys-swapfile。
注意:如果你同時啟用了zram和dphys-swapfile來重啟Raspberry Pi,zram會在啟動時禁用dphys-swapfile。你必須手動重新激活此服務(wù)。
# reactivate dphys-swapfile after a reboot# (when zram and dphys-swapfile are both enabled)$ sudo /etc/init.d/dphys-swapfile stop
$ sudo /etc/init.d/dphys-swapfile start
Bazel
Bazel是Google提供的一款免費(fèi)軟件工具,用于自動構(gòu)建和測試軟件包。你可以將其與OpenCV使用的CMake進(jìn)行比較,但后者僅用于構(gòu)建軟件,不具備測試功能。Bazel是用Java編寫的,這是一種與平臺無關(guān)的語言,在語法上很大程度上基于C++。要編譯Bazel,我們首先需要安裝Java和一些其他依賴項(xiàng),使用以下命令。
# get a fresh start$ sudo apt-get update
$ sudo apt-get upgrade# install pip and pip3$ sudo apt-get install python3-pip# install some tools$ sudo apt-get install zip unzip curl# install Java$ sudo apt-get install openjdk-11-jdk
接下來,我們可以下載并解壓Bazel軟件。TensorFlow 2.10需要Bazel 5.1.1版本,因此請確保你安裝了正確的版本。
$ wget https://github.com/bazelbuild/bazel/releases/download/5.1.1/bazel-5.1.1-dist.zip$ unzip -d bazel bazel-5.1.1-dist.zip$ cd bazel
在安裝過程中,Bazel會使用預(yù)定比例的可用工作內(nèi)存。由于Raspberry Pi的RAM大小有限,這個比例太小。為防止崩潰,我們必須將此內(nèi)存大小設(shè)置為板載RAM的最大40%。例如,對于2GB RAM的Raspberry Pi,應(yīng)設(shè)置為800MB。這可以通過向腳本文件compile.sh添加一些額外信息來實(shí)現(xiàn)。你可以在以run..(大約第144行)開頭的行中添加文本-J-Xmx800M。見下面的屏幕截圖。使用常見的、、保存更改(見上面的幻燈片)。
$nano scripts/bootstrap/compile.sh -c
一旦Bazel的Java環(huán)境被最大化,你就可以使用下面的命令開始構(gòu)建Bazel軟件。完成后,將二進(jìn)制文件復(fù)制到/usr/local/bin位置,以便bash可以在任何地方找到可執(zhí)行文件。最后一步是刪除zip文件。整個構(gòu)建過程大約需要33分鐘。
# start the build$ env EXTRA_BAZEL_ARGS="--host_javabase=@local_jdk//:jdk" bash ./compile.sh
# copy the binary$ sudo cp output/bazel /usr/local/bin/bazel# clean up$ cd ~$ rm bazel-5.1.1-dist.zip# if you have a copied bazel to /usr/local/bin you may also# delete the whole bazel directory, freeing another 500 MByte$ sudo rm -rf bazel
安裝 TensorFlow for Python 3Bazel運(yùn)行正常后,我們就可以開始在我們的64位Raspberry Pi上為Python 3構(gòu)建TensorFlow 2.10.0了。這幾乎已成為標(biāo)準(zhǔn)做法。首先,安裝一些依賴項(xiàng),然后從GitHub下載zip文件,最后解壓軟件。你還必須安裝tensorflow_io_gcs文件系統(tǒng)。這可以通過使用我們從GitHub存儲庫中獲取的預(yù)編譯wheel文件來完成。
TensorFlow 2.10.0
# get a fresh start$ sudo apt-get update$ sudo apt-get upgrade# the dependencies$ sudo apt-get install zip unzip$ sudo -H pip3 install keras_applications --no-deps$ sudo -H pip3 install keras_preprocessing --no-deps# install correct version protobuf$ sudo -H pip3 install --upgrade protobuf==3.20.0# download tensorflow io$ git clone https://github.com/Qengineering/Tensorflow-io.git$ cd Tensorflow-io$ sudo -H pip3 install tensorflow_io_gcs_filesystem-0.23.1-cp39-cp39-linux_aarch64.whl$ cd ~# download TensorFlow 2.10.0$ wget -O tensorflow.zip https://github.com/tensorflow/tensorflow/archive/v2.10.0.zip$ unzip tensorflow.zip$ cd tensorflow-2.10.0$ ./configure
TensorFlow 2.9.1
# get a fresh start$ sudo apt-get update$ sudo apt-get upgrade# the dependencies$ sudo apt-get install zip unzip$ sudo -H pip3 install keras_applications --no-deps$ sudo -H pip3 install keras_preprocessing --no-deps# install correct version protobuf$ sudo -H pip3 install --upgrade protobuf==3.20.0# download tensorflow io$ git clone https://github.com/Qengineering/Tensorflow-io.git$ cd Tensorflow-io$ sudo -H pip3 install tensorflow_io_gcs_filesystem-0.23.1-cp39-cp39-linux_aarch64.whl$ cd ~# download TensorFlow 2.9.1$ wget -O tensorflow.zip https://github.com/tensorflow/tensorflow/archive/v2.9.1.zip$ unzip tensorflow.zip$ cd tensorflow-2.9.1$ ./configure
TensorFlow 2.8.0
# get a fresh start$ sudo apt-get update$ sudo apt-get upgrade# the dependencies$ sudo apt-get install zip unzip$ sudo -H pip3 install keras_applications --no-deps$ sudo -H pip3 install keras_preprocessing --no-deps# install correct version protobuf$ sudo -H pip3 install --upgrade protobuf==3.20.0$ cd ~# download TensorFlow 2.8.0$ wget -O tensorflow.zip https://github.com/tensorflow/tensorflow/archive/v2.8.0.zip$ unzip tensorflow.zip$ cd tensorflow-2.8.0$ ./configure
TensorFlow 2.7.0
# get a fresh start$ sudo apt-get update$ sudo apt-get upgrade# the dependencies$ sudo apt-get install zip unzip$ sudo -H pip3 install keras_applications --no-deps$ sudo -H pip3 install keras_preprocessing --no-deps# download TensorFlow 2.7.0$ wget -O tensorflow.zip https://github.com/tensorflow/tensorflow/archive/v2.7.0.zip$ unzip tensorflow.zip$ cd tensorflow-2.7.0$ ./configure
在構(gòu)建Python 3安裝wheel之前的最后一步是使用一些環(huán)境變量配置你的構(gòu)建。這是通過./configure腳本文件完成的。將Python 3設(shè)置為默認(rèn)的Python版本,并對其他問題回答“否”。
pi@raspberrypi:~/tensorflow-2.10.0 $ ./configureYou have bazel 5.1.1- (@non-git) installed.Please specify the location of python. [Default is /usr/bin/python3]:
Found possible Python library paths: /usr/lib/python3/dist-packages /usr/local/lib/python3.9/dist-packagesPlease input the desired Python library path to use. Default is [/usr/lib/python3/dist-packages]
Do you wish to build TensorFlow with ROCm support? [y/N]: nNo ROCm support will be enabled for TensorFlow.
Do you wish to build TensorFlow with CUDA support? [y/N]: nNo CUDA support will be enabled for TensorFlow.
Do you wish to download a fresh release of clang? (Experimental) [y/N]: nClang will not be downloaded.
Please specify optimization flags to use during compilation when bazel option "--config=opt" is specified [Default is -Wno-sign-compare]:
Would you like to interactively configure ./WORKSPACE for Android builds? [y/N]: nNot configuring the WORKSPACE for Android builds.
Preconfigured Bazel build configs. You can use any of the below by adding "--config=<>" to your build command. See .bazelrc for more details.--config=mkl # Build with MKL support.--config=mkl_aarch64 # Build with oneDNN and Compute Library for the Arm Architecture (ACL).--config=monolithic # Config for mostly static monolithic build.--config=numa # Build with NUMA support.--config=dynamic_kernels # (Experimental) Build kernels into separate shared objects.--config=v2 # Build TensorFlow 2.x instead of 1.x.Preconfigured Bazel build configs to DISABLE default on features:--config=noaws # Disable AWS S3 filesystem support.--config=nogcp # Disable GCP support.--config=nohdfs # Disable HDFS support.--config=nonccl # Disable NVIDIA NCCL support.Configuration finished
現(xiàn)在腳本文件已經(jīng)設(shè)置完畢,可以開始執(zhí)行大規(guī)模的構(gòu)建,使用下面的命令。-Xmx1624m設(shè)置了 Bazel 運(yùn)行的 Java 環(huán)境的內(nèi)存大小。盡可能給它分配更多的空間。我們建議分配你 Raspberry Pi 內(nèi)存大小的 80%。在我們的例子中,板載內(nèi)存為 2 Gbyte,所以 1624 Mbyte 除了 Bazel 之外,還能為其他線程留下足夠的空間。
另一個要點(diǎn)是 Basel 使用的核心數(shù)。最好使用一個核心。寧愿稍微慢一些也不要崩潰(總是在最后關(guān)頭)然后重新開始。
在構(gòu)建過程中關(guān)閉所有其他應(yīng)用程序。運(yùn)行的其他進(jìn)程越少,編譯速度就越快。
$ sudo bazel clean$ sudo bazel --host_jvm_args=-Xmx1624m build \ --config=opt \ --config=noaws \ --config=nogcp \ --config=nohdfs \ --config=nonccl \ --config=monolithic \ --config=v2 \ --local_cpu_resources=1 \ --define=tflite_pip_with_flex=true \ --copt=-ftree-vectorize \ --copt=-funsafe-math-optimizations \ --copt=-ftree-loop-vectorize \ --copt=-fomit-frame-pointer \ //tensorflow/tools/pip_package:build_pip_package
編譯 41 小時(或更長時間?。┖?,希望您能看到以下屏幕。
現(xiàn)在我們需要生成 wheel 文件并安裝它。這可以通過下面的命令來完成。安裝過程大約只需要幾分鐘。
TensorFlow 2.10.0
# synthesize the wheel$ sudo bazel-bin/tensorflow/tools/pip_package/build_pip_package /tmp/tensorflow_pkg$ cd ~# go to the folder where the wheel is located and install tensorflow$ cd /tmp/tensorflow_pkg$ sudo -H pip3 install tensorflow-2.10.0-cp39-cp39-linux_aarch64.whl
TensorFlow 2.9.1
# synthesize the wheel$ sudo bazel-bin/tensorflow/tools/pip_package/build_pip_package /tmp/tensorflow_pkg$ cd ~# go to the folder where the wheel is located and install tensorflow$ cd /tmp/tensorflow_pkg$ sudo -H pip3 install tensorflow-2.9.1-cp39-cp39-linux_aarch64.whl
TensorFlow 2.8.0
# synthesize the wheel$ sudo bazel-bin/tensorflow/tools/pip_package/build_pip_package /tmp/tensorflow_pkg$ cd ~# go to the folder where the wheel is located and install tensorflow$ cd /tmp/tensorflow_pkg$ sudo -H pip3 install tensorflow-2.8.0-cp39-cp39-linux_aarch64.whl
TensorFlow 2.7.0
# synthesize the wheel$ sudo bazel-bin/tensorflow/tools/pip_package/build_pip_package /tmp/tensorflow_pkg$ cd ~# go to the folder where the wheel is located and install tensorflow$ cd /tmp/tensorflow_pkg$ sudo -H pip3 install tensorflow-2.7.0-cp39-cp39-linux_aarch64.whl
安裝 TensorFlow C++ API
如前所述,你可以通過訪問我們 GitHub 頁面上的 tarball 文件來快速安裝 TensorFlow C++ API,無需經(jīng)歷冗長的安裝過程。對于那些想要自己構(gòu)建 API 的人,以下是安裝指南。
首先,如果尚未安裝 Bazel,請進(jìn)行安裝。該過程也在上文中有描述。一旦 Bazel 運(yùn)行正常,你可以安裝依賴項(xiàng)并下載 TensorFlow(如果之前為 Python 3 安裝時未做這些操作)。
TensorFlow 2.10.0
# the dependencies$ sudo apt-get install build-essential make cmake wget zip unzip$ sudo apt-get install libhdf5-dev libc-ares-dev libeigen3-dev$ sudo apt-get install libatlas-base-dev# download TensorFlow 2.10.0$ wget -O tensorflow.zip https://github.com/tensorflow/tensorflow/archive/v2.10.0.zip# unpack the folder$ unzip tensorflow.zip$ cd tensorflow-2.10.0$ ./configure
TensorFlow 2.9.1
# the dependencies$ sudo apt-get install build-essential make cmake wget zip unzip$ sudo apt-get install libhdf5-dev libc-ares-dev libeigen3-dev$ sudo apt-get install libatlas-base-dev# download TensorFlow 2.9.1$ wget -O tensorflow.zip https://github.com/tensorflow/tensorflow/archive/v2.9.1.zip# unpack the folder$ unzip tensorflow.zip$ cd tensorflow-2.9.1$ ./configure
TensorFlow 2.8.0
# the dependencies$ sudo apt-get install build-essential make cmake wget zip unzip$ sudo apt-get install libhdf5-dev libc-ares-dev libeigen3-dev$ sudo apt-get install libatlas-base-dev# download TensorFlow 2.8.0$ wget -O tensorflow.zip https://github.com/tensorflow/tensorflow/archive/v2.8.0.zip# unpack the folder$ unzip tensorflow.zip$ cd tensorflow-2.8.0$ ./configure
TensorFlow 2.7.0
# the dependencies$ sudo apt-get install build-essential make cmake wget zip unzip$ sudo apt-get install libhdf5-dev libc-ares-dev libeigen3-dev$ sudo apt-get install libatlas-base-dev# download TensorFlow 2.7.0$ wget -O tensorflow.zip https://github.com/tensorflow/tensorflow/archive/v2.7.0.zip# unpack the folder$ unzip tensorflow.zip$ cd tensorflow-2.7.0$ ./configure
再次,在實(shí)際構(gòu)建開始之前,我們需要配置 Bazel。盡管我們要構(gòu)建的是 C++ API,但 Bazel 腳本文件需要將 Python 3 設(shè)置為默認(rèn) Python 版本,并對其他所有問題回答“否”。一旦腳本文件中的所有問題都得到回答,你將得到與上面相同的輸出屏幕。
最后一步是帶有選項(xiàng)的命令行。這幾乎與之前使用的命令相同。這里最重要的是--config=monolithic標(biāo)志。沒有這個指令,TensorFlow 將無法與 OpenCV 一起工作(反之亦然)。就像 pip 編譯一樣,Java 環(huán)境的內(nèi)存(-Xmx1624m)需要最大化到 Raspberry Pi 板載內(nèi)存的 80%。最后一行指示的是 tarball 庫構(gòu)建,而不是 pip wheel。
$ sudo bazel --host_jvm_args=-Xmx1624m build \ --config=opt \ --config=noaws \ --config=nogcp \ --config=nohdfs \ --config=nonccl \ --config=monolithic \ --config=v2 \ --local_cpu_resources=1 \ --copt=-ftree-vectorize \ --copt=-funsafe-math-optimizations \ --copt=-ftree-loop-vectorize \ --copt=-fomit-frame-pointer \ //tensorflow/tools/lib_package:libtensorflow
經(jīng)過漫長的 12 小時等待后,希望你會看到下面的屏幕。
最后一步是安裝 tarball。這可以通過下面的命令來完成。
# clean up the whole bazel cache$ sudo rm -rf ~/.cache/bazel
安裝完成后,你必須擁有與本頁開頭所示相同的文件夾(/usr/local/lib 和 /usr/local/include/tensorflow/c)。
清理
成功安裝后,許多文件將不再需要。由 bazel 生成的中間目標(biāo)文件大約占用了你磁盤上的 6 GB 空間。刪除這些文件不會對系統(tǒng)造成任何損害。
# clean up the whole bazel cache$ sudo rm -rf ~/.cache/bazel
如果你之前重新安裝過 dphys-swapfile,現(xiàn)在是時候再次卸載它了。這樣做可以延長你 SD 卡的壽命。
#removethedphys-swapfile(ifinstalled)$ sudo /etc/init.d/dphys-swapfile stop$ sudo apt-get remove --purge dphys-swapfile
在 Raspberry Pi 64 操作系統(tǒng)上安裝 Keras
安裝完 TensorFlow 后,您可以加載 Keras。Keras 是在 TensorFlow 基礎(chǔ)上運(yùn)行的高級外殼。它聲稱比 TensorFlow 更方便用戶使用,提供了一套更直觀的抽象概念,可以輕松開發(fā)深度學(xué)習(xí)模型。安裝只需一條命令。
$ pip3 install keras
-
樹莓派
+關(guān)注
關(guān)注
118文章
1882瀏覽量
106251 -
深度學(xué)習(xí)
+關(guān)注
關(guān)注
73文章
5530瀏覽量
122060 -
tensorflow
+關(guān)注
關(guān)注
13文章
330瀏覽量
60801
發(fā)布評論請先 登錄
相關(guān)推薦
谷歌深度學(xué)習(xí)插件tensorflow
深度學(xué)習(xí)框架TensorFlow&TensorFlow-GPU詳解
基于樹莓派采集網(wǎng)關(guān)
TensorFlow實(shí)戰(zhàn)之深度學(xué)習(xí)框架的對比

樹莓派用什么語言編程_樹莓派python編程詳解
使用樹莓派設(shè)計(jì)智能小車教程之樹莓派小車啟動教程免費(fèi)下載

樹莓派的攝像頭應(yīng)該如何啟動詳細(xì)教程免費(fèi)下載
使用樹莓派設(shè)計(jì)智能小車教程之樹莓派智能車系統(tǒng)啟動的詳細(xì)資料說明
樹莓派的學(xué)習(xí)設(shè)計(jì)方案合集

使用TensorFlow建立深度學(xué)習(xí)和機(jī)器學(xué)習(xí)網(wǎng)絡(luò)
TensorFlow手勢識別樹莓派開源

樹莓派4b學(xué)習(xí)筆記

評論