ヒント:ラズパイ(Raspberry Pi Zero WH)でtensorflowをインストール


ラズパイにtensorflowを!!

ラズパイ(Raspberry Pi Zero WH)にtensorflowを入れてみました。
基本は

sudo pip install tensorflow

のはずだったんですが、、、、
エラーが何度となく出るため、意外とてこずってしまいました。
例としてどのようなエラーが出るのかを示します。

pi@raspberrypi:~$ sudo pip install tensorflow
Collecting tensorflow
  Using cached https://www.piwheels.org/simple/tensorflow/tensorflow-1.11.0-cp27-none-linux_armv6l.whl
Exception:
Traceback (most recent call last):
  File "/usr/lib/python2.7/dist-packages/pip/basecommand.py", line 215, in main
    status = self.run(options, args)
  File "/usr/lib/python2.7/dist-packages/pip/commands/install.py", line 353, in run
    wb.build(autobuilding=True)
  File "/usr/lib/python2.7/dist-packages/pip/wheel.py", line 749, in build
    self.requirement_set.prepare_files(self.finder)
  File "/usr/lib/python2.7/dist-packages/pip/req/req_set.py", line 380, in prepare_files
    ignore_dependencies=self.ignore_dependencies))
  File "/usr/lib/python2.7/dist-packages/pip/req/req_set.py", line 487, in _prepare_file
    req_to_install, finder)
  File "/usr/lib/python2.7/dist-packages/pip/req/req_set.py", line 428, in _check_skip_installed
    req_to_install, upgrade_allowed)
  File "/usr/lib/python2.7/dist-packages/pip/index.py", line 465, in find_requirement
    all_candidates = self.find_all_candidates(req.name)
  File "/usr/lib/python2.7/dist-packages/pip/index.py", line 423, in find_all_candidates
    for page in self._get_pages(url_locations, project_name):
  File "/usr/lib/python2.7/dist-packages/pip/index.py", line 568, in _get_pages
    page = self._get_page(location)
  File "/usr/lib/python2.7/dist-packages/pip/index.py", line 683, in _get_page
    return HTMLPage.get_page(link, session=self.session)
  File "/usr/lib/python2.7/dist-packages/pip/index.py", line 792, in get_page
    "Cache-Control": "max-age=600",
  File "/usr/share/python-wheels/requests-2.12.4-py2.py3-none-any.whl/requests/sessions.py", line 501, in get
    return self.request('GET', url, **kwargs)
  File "/usr/lib/python2.7/dist-packages/pip/download.py", line 386, in request
    return super(PipSession, self).request(method, url, *args, **kwargs)
  File "/usr/share/python-wheels/requests-2.12.4-py2.py3-none-any.whl/requests/sessions.py", line 488, in request
    resp = self.send(prep, **send_kwargs)
  File "/usr/share/python-wheels/requests-2.12.4-py2.py3-none-any.whl/requests/sessions.py", line 630, in send
    history = [resp for resp in gen] if allow_redirects else []
  File "/usr/share/python-wheels/requests-2.12.4-py2.py3-none-any.whl/requests/sessions.py", line 190, in resolve_redirects
    **adapter_kwargs
  File "/usr/share/python-wheels/requests-2.12.4-py2.py3-none-any.whl/requests/sessions.py", line 609, in send
    r = adapter.send(request, **kwargs)
  File "/usr/share/python-wheels/CacheControl-0.11.7-py2.py3-none-any.whl/cachecontrol/adapter.py", line 47, in send
    resp = super(CacheControlAdapter, self).send(request, **kw)
  File "/usr/share/python-wheels/requests-2.12.4-py2.py3-none-any.whl/requests/adapters.py", line 423, in send
    timeout=timeout
  File "/usr/share/python-wheels/urllib3-1.19.1-py2.py3-none-any.whl/urllib3/connectionpool.py", line 643, in urlopen
    _stacktrace=sys.exc_info()[2])
  File "/usr/share/python-wheels/urllib3-1.19.1-py2.py3-none-any.whl/urllib3/util/retry.py", line 315, in increment
    total -= 1
TypeError: unsupported operand type(s) for -=: 'Retry' and 'int'
pi@raspberrypi:~$

このようにエラーがでまくりでした。それも30分以上も待った挙句にでるエラーは精神的にもまいるかとおもいます。
また、メモリーエラーも出るのでSwapは2G(2048)を指定しております。

Raspberry Pi ZeroのSWAPを上げる

どうやら、メモリーエラーになることが多いのでSWAPを2Gまで上げます。

pi@raspberrypi:~$ sudo vi /etc/dphys-swapfile

以下の部分に2048を指定してください。

CONF_SWAPSIZE=2048

書き換えた後、再起動してください。
以下の方法でもいいかと思います。

sudp /etc/init.d/dphys-swapfile restart

アーカイブをとってくる

Raspberry Pi ZeroはARM6です。Raspberry Pi 3はARM7となります。今回はARM6のcp27を取得します。

wget https://www.piwheels.org/simple/tensorflow/tensorflow-1.11.0-cp27-none-linux_armv6l.whl

そしてインストールをします。
途中、必要なパッケージはダウンロードされますのでご注意ください。

sudo pip install tensorflow-1.11.0-cp27-none-linux_armv6l.whl

念のため履歴を示します。

pi@raspberrypi:~$ sudo pip install tensorflow-1.11.0-cp27-none-linux_armv6l.whl
Processing ./tensorflow-1.11.0-cp27-none-linux_armv6l.whl
Requirement already satisfied: mock>=2.0.0 in /usr/local/lib/python2.7/dist-packages (from tensorflow==1.11.0)
Requirement already satisfied: enum34>=1.1.6 in /usr/lib/python2.7/dist-packages (from tensorflow==1.11.0)
Requirement already satisfied: absl-py>=0.1.6 in /usr/local/lib/python2.7/dist-packages (from tensorflow==1.11.0)
Requirement already satisfied: keras-applications>=1.0.5 in /usr/local/lib/python2.7/dist-packages (from tensorflow==1.11.0)
Requirement already satisfied: grpcio>=1.8.6 in /usr/local/lib/python2.7/dist-packages (from tensorflow==1.11.0)
Requirement already satisfied: keras-preprocessing>=1.0.3 in /usr/local/lib/python2.7/dist-packages (from tensorflow==1.11.0)
Requirement already satisfied: termcolor>=1.1.0 in /usr/local/lib/python2.7/dist-packages (from tensorflow==1.11.0)
Requirement already satisfied: gast>=0.2.0 in /usr/local/lib/python2.7/dist-packages (from tensorflow==1.11.0)
Collecting tensorboard<1.12.0,>=1.11.0 (from tensorflow==1.11.0)
  Using cached https://files.pythonhosted.org/packages/76/f9/e62022d00940e4df9a629d6bfe42eb28907bb35808db62bb9e8b69ea5ef3/tensorboard-1.11.0-py2-none-any.whl
Collecting backports.weakref>=1.0rc1 (from tensorflow==1.11.0)
  Using cached https://files.pythonhosted.org/packages/88/ec/f598b633c3d5ffe267aaada57d961c94fdfa183c5c3ebda2b6d151943db6/backports.weakref-1.0.post1-py2.py3-none-any.whl
Collecting astor>=0.6.0 (from tensorflow==1.11.0)
  Using cached https://files.pythonhosted.org/packages/35/6b/11530768cac581a12952a2aad00e1526b89d242d0b9f59534ef6e6a1752f/astor-0.7.1-py2.py3-none-any.whl
Requirement already satisfied: wheel in /usr/local/lib/python2.7/dist-packages (from tensorflow==1.11.0)
Requirement already satisfied: six>=1.10.0 in /usr/local/lib/python2.7/dist-packages (from tensorflow==1.11.0)
Collecting setuptools<=39.1.0 (from tensorflow==1.11.0)
  Downloading https://files.pythonhosted.org/packages/8c/10/79282747f9169f21c053c562a0baa21815a8c7879be97abd930dbcf862e8/setuptools-39.1.0-py2.py3-none-any.whl (566kB)
    100% |????????????????????????????????| 573kB 74kB/s
Requirement already satisfied: protobuf>=3.6.0 in /usr/local/lib/python2.7/dist-packages (from tensorflow==1.11.0)
Requirement already satisfied: numpy>=1.13.3 in /usr/local/lib/python2.7/dist-packages (from tensorflow==1.11.0)
Requirement already satisfied: funcsigs>=1; python_version < "3.3" in /usr/local/lib/python2.7/dist-packages (from mock>=2.0.0->tensorflow==1.11.0)
Requirement already satisfied: pbr>=0.11 in /usr/local/lib/python2.7/dist-packages (from mock>=2.0.0->tensorflow==1.11.0)
Requirement already satisfied: h5py in /usr/lib/python2.7/dist-packages (from keras-applications>=1.0.5->tensorflow==1.11.0)
Requirement already satisfied: futures>=2.2.0 in /usr/local/lib/python2.7/dist-packages (from grpcio>=1.8.6->tensorflow==1.11.0)
Requirement already satisfied: markdown>=2.6.8 in /usr/local/lib/python2.7/dist-packages (from tensorboard<1.12.0,>=1.11.0->tensorflow==1.11.0)
Requirement already satisfied: werkzeug>=0.11.10 in /usr/local/lib/python2.7/dist-packages (from tensorboard<1.12.0,>=1.11.0->tensorflow==1.11.0)
Installing collected packages: tensorboard, backports.weakref, astor, setuptools, tensorflow
  Found existing installation: tensorboard 1.12.0
    Uninstalling tensorboard-1.12.0:
      Successfully uninstalled tensorboard-1.12.0
  Found existing installation: setuptools 40.6.2
    Uninstalling setuptools-40.6.2:
      Not removing or modifying (outside of prefix):
      /usr/bin/easy_install
      Successfully uninstalled setuptools-40.6.2
Successfully installed astor-0.7.1 backports.weakref-1.0.post1 setuptools-39.1.0 tensorboard-1.11.0 tensorflow-1.11.0
pi@raspberrypi:~$

いくつかのモジュールのインストールで落ちたりすることがあったんですが、そこは手動でインストールをしました。
これでも、インストールが少しでも進むかと思います。
※インターネットは接続したままであることが必要です。

tensorflow確認

やっと、tensorflowがインストールできたのでpythonを立ち上げてみます。

pi@raspberrypi:~$ python
Python 2.7.13 (default, Sep 26 2018, 18:42:22)
[GCC 6.3.0 20170516] on linux2
Type "help", "copyright", "credits" or "license" for more information.
>>> import tensorflow as tf
/usr/lib/python2.7/dist-packages/h5py/__init__.py:34: FutureWarning: Conversion of the second argument of issubdtype from `float` to `np.floating` is deprecated. In future, it will be treated as `np.float64 == np.dtype(float).type`.
  from ._conv import register_converters as _register_converters
>>> hello = tf.constant('Hello, TensorFlow!')
>>> sess = tf.Session()
>>> print(sess.run(hello))
Hello, TensorFlow!
>>> a = tf.constant(10)
>>> b = tf.constant(32)
>>> print(sess.run(a + b))
42
>>>

ワーニングが出ておりますが、一応の動作はしております。

以上、インストールの履歴でした。