2018年2月25日 星期日

樹莓派安裝深度學習框架 TensorFlow 與 Keras

下午在 Windows 上成功安裝深度學習框架 TensorFlow+Keras, 晚飯後著手在 Pi 3 上安裝, 不過樹莓派的 TensorFlow 目前只支援 Python 3.4 下的 v1.1.0, 參考 :

在 Raspberry Pi 2,3 上安装 Tensorflow 搭建深度学习环境

此文所下載安裝之 TensorFlow 實際上是來自下列 GitHub 網站 :

GitHub : TensorFlow for Raspberry Pi

安裝程序如下 :


一. 安裝 TensorFlow (底層) :

1. 先更新 TensorFlow 的相依套件 :

sudo apt-get install python3-pip python3-dev 

2. 然後用 wget 下載 TensorFlow 1.1.0 版 :

wget https://github.com/samjabrahams/tensorflow-on-raspberry-pi/releases/download/v1.1.0/tensorflow-1.1.0-cp34-cp34m-linux_armv7l.whl

3. 安裝 TensorFlow : 

sudo pip3 install tensorflow-1.1.0-cp34-cp34m-linux_armv7l.whl

安裝過程非常久, 大約要半小時, 而且會不斷吐出一堆看似錯誤的訊息 :

........ (略)
trong -Wformat -Werror=format-security -D_FORTIFY_SOURCE=2 build/temp.linux-armv7l-3.4/numpy/random/mtrand/mtrand.o build/temp.linux-armv7l-3.4/numpy/random/mtrand/randomkit.o build/temp.linux-armv7l-3.4/numpy/random/mtrand/initarray.o build/temp.linux-armv7l-3.4/numpy/random/mtrand/distributions.o -Lbuild/temp.linux-armv7l-3.4 -o build/lib.linux-armv7l-3.4/numpy/random/mtrand.cpython-34m.so
    Creating build/scripts.linux-armv7l-3.4/f2py3
      adding 'build/scripts.linux-armv7l-3.4/f2py3' to scripts
    changing mode of build/scripts.linux-armv7l-3.4/f2py3 from 644 to 755

    warning: no previously-included files matching '*.pyo' found anywhere in distribution
    warning: no previously-included files matching '*.pyd' found anywhere in distribution
    warning: no previously-included files matching '*.swp' found anywhere in distribution
    warning: no previously-included files matching '*.bak' found anywhere in distribution
    warning: no previously-included files matching '*~' found anywhere in distribution
    changing mode of /usr/local/bin/f2py3 to 755
    Running from numpy source directory.
    /usr/lib/python3.4/distutils/dist.py:260: UserWarning: Unknown distribution option: 'python_requires'
      warnings.warn(msg)
    /tmp/pip-build-i0_rx06a/numpy/numpy/distutils/system_info.py:624: UserWarning:
        Atlas (http://math-atlas.sourceforge.net/) libraries not found.
        Directories to search for the libraries can be specified in the
        numpy/distutils/site.cfg file (section [atlas]) or by setting
        the ATLAS environment variable.
      self.calc_info()
    /usr/lib/python3.4/distutils/dist.py:260: UserWarning: Unknown distribution option: 'python_requires'
      warnings.warn(msg)
    /usr/lib/python3.4/distutils/dist.py:260: UserWarning: Unknown distribution option: 'define_macros'
      warnings.warn(msg)
Successfully installed tensorflow werkzeug protobuf wheel six numpy
Cleaning up...

但實際上這些訊息只是 Warning 而已, 並非安裝有甚麼問題, 不用管它, 靜靜等待安裝結束.  


4. 重新安裝 mock 以免匯入 tensorflow 時報錯 :

sudo pip3 uninstall mock 
sudo pip3 install mock
但我第四步沒有做, 毋須重新安裝 mock 即可順利匯入 TensorFlow 套件 :

pi@raspberrypi:~ $ python3
Python 3.4.2 (default, Oct 19 2014, 13:31:11)
[GCC 4.9.1] on linux
Type "help", "copyright", "credits" or "license" for more information.
>>> import tensorflow as tf 
>>> tf.__version__   
'1.1.0'


二. 安裝 Keras (上層) :

我在網路上查到下面這篇在 Pi 3 安裝 Keras 的文章 :

INSTALLING KERAS ON RASPBERRY PI 3

但此文是從 Numpi, Scipy 等 ML 基礎套件開始一一安裝, 但我之前都已安裝過了, 所以就跳過直接用 pip3 安裝 Keras, 很快就完成了 :

pi@raspberrypi:~ $ sudo pip3 install keras 
Downloading/unpacking keras
  Downloading Keras-2.1.4-py2.py3-none-any.whl (322kB): 322kB downloaded
Requirement already satisfied (use --upgrade to upgrade): numpy>=1.9.1 in /usr/local/lib/python3.4/dist-packages (from keras)
Requirement already satisfied (use --upgrade to upgrade): scipy>=0.14 in /usr/lib/python3/dist-packages (from keras)
Requirement already satisfied (use --upgrade to upgrade): six>=1.9.0 in /usr/local/lib/python3.4/dist-packages (from keras)
Downloading/unpacking pyyaml (from keras)
  Downloading PyYAML-3.12.tar.gz (253kB): 253kB downloaded
  Running setup.py (path:/tmp/pip-build-a1rgahzu/pyyaml/setup.py) egg_info for package pyyaml

Installing collected packages: keras, pyyaml
  Running setup.py install for pyyaml
    checking if libyaml is compilable
    arm-linux-gnueabihf-gcc -pthread -DNDEBUG -g -fwrapv -O2 -Wall -Wstrict-prototypes -g -fstack-protector-strong -Wformat -Werror=format-security -D_FORTIFY_SOURCE=2 -fPIC -I/usr/include/python3.4m -c build/temp.linux-armv7l-3.4/check_libyaml.c -o build/temp.linux-armv7l-3.4/check_libyaml.o
    build/temp.linux-armv7l-3.4/check_libyaml.c:2:18: fatal error: yaml.h: No such file or directory
     #include <yaml.h>
                      ^
    compilation terminated.

    libyaml is not found or a compiler error: forcing --without-libyaml
    (if libyaml is installed correctly, you may need to
     specify the option --include-dirs or uncomment and
     modify the parameter include_dirs in setup.cfg)

Successfully installed keras pyyaml
Cleaning up...
pi@raspberrypi:~ $ python3
Python 3.4.2 (default, Oct 19 2014, 13:31:11)
[GCC 4.9.1] on linux
Type "help", "copyright", "credits" or "license" for more information.
>>> import keras
Using TensorFlow backend.
>>> keras.__version__
'2.1.4'

OK, 這樣樹莓派也可以跑 TesorFlow+Keras 了. 

沒有留言 :