Uncategorized

TensorFlow Rpi4B Install Notes

Asked 
Active 24 days ago
Viewed 978 times
1

Help I’m new to this and I cannot install Tensorflow on Raspberry Pi 4 4GB. Current Python version: Python 3.7.3 Pip version : pip 19.2.3

I tried typing

pip install --upgrade tensorflow

and

pip3 install --user --upgrade tensorflow 

and

pip install tensorflow

All of them cannot work and return similar error.

ERROR: Exception:
Traceback (most recent call last):
  File "/home/pi/.local/lib/python2.7/site-packages/pip/_internal/cli/base_command.py", line 188, in main
    status = self.run(options, args)
  File "/home/pi/.local/lib/python2.7/site-packages/pip/_internal/commands/install.py", line 345, in run
    resolver.resolve(requirement_set)
  File "/home/pi/.local/lib/python2.7/site-packages/pip/_internal/legacy_resolve.py", line 196, in resolve
    self._resolve_one(requirement_set, req)
  File "/home/pi/.local/lib/python2.7/site-packages/pip/_internal/legacy_resolve.py", line 359, in _resolve_one
    abstract_dist = self._get_abstract_dist_for(req_to_install)
  File "/home/pi/.local/lib/python2.7/site-packages/pip/_internal/legacy_resolve.py", line 307, in _get_abstract_dist_for
    self.require_hashes
  File "/home/pi/.local/lib/python2.7/site-packages/pip/_internal/operations/prepare.py", line 199, in prepare_linked_requirement
    progress_bar=self.progress_bar
  File "/home/pi/.local/lib/python2.7/site-packages/pip/_internal/download.py", line 1064, in unpack_url
    progress_bar=progress_bar
  File "/home/pi/.local/lib/python2.7/site-packages/pip/_internal/download.py", line 924, in unpack_http_url
    progress_bar)
  File "/home/pi/.local/lib/python2.7/site-packages/pip/_internal/download.py", line 1152, in _download_http_url
    _download_url(resp, link, content_file, hashes, progress_bar)
  File "/home/pi/.local/lib/python2.7/site-packages/pip/_internal/download.py", line 861, in _download_url
    hashes.check_against_chunks(downloaded_chunks)
  File "/home/pi/.local/lib/python2.7/site-packages/pip/_internal/utils/hashes.py", line 75, in check_against_chunks
    for chunk in chunks:
  File "/home/pi/.local/lib/python2.7/site-packages/pip/_internal/download.py", line 829, in written_chunks
    for chunk in chunks:
  File "/home/pi/.local/lib/python2.7/site-packages/pip/_internal/utils/ui.py", line 156, in iter
    for x in it:
  File "/home/pi/.local/lib/python2.7/site-packages/pip/_internal/download.py", line 818, in resp_read
    decode_content=False):
  File "/home/pi/.local/lib/python2.7/site-packages/pip/_vendor/urllib3/response.py", line 531, in stream
    data = self.read(amt=amt, decode_content=decode_content)
  File "/home/pi/.local/lib/python2.7/site-packages/pip/_vendor/urllib3/response.py", line 496, in read
    raise IncompleteRead(self._fp_bytes_read, self.length_remaining)
  File "/usr/lib/python2.7/contextlib.py", line 35, in __exit__
    self.gen.throw(type, value, traceback)
  File "/home/pi/.local/lib/python2.7/site-packages/pip/_vendor/urllib3/response.py", line 402, in _error_catcher
    raise ReadTimeoutError(self._pool, None, 'Read timed out.')
ReadTimeoutError: HTTPSConnectionPool(host='www.piwheels.org', port=443): Read timed out.

Sorry if I did not show my problems correctly because this is the first time I’m posting here.

  • Ah, let me see. There are many versions of TensorFlow, with different NN models. Please give me the web link to the tutorial or instruction. I can try to reproduce your situation and problem. PS – the error message indicate Python 2.7. So your python env might be corrupted, or you forget to use pip3 (your last pip!) and messed up python 2.7 and 3.7.3 stuff. – tlfong01 Sep 26 ’19 at 5:16
  • You might like to try these (third is out of date), and see if there is any problem. I can try to reproduce your situation.(1) Installing Machine Learning Software TensorFlow on Raspberry Pi – Rishabh Jain 2019may10 circuitdigest.com/microcontroller-projects/… (3) Detect ANY Object with Raspberry Pi and TensorFlow Youtube – 2018dec21 youtube.com/watch?v=zqIBce4LKx8 (2) How to install TensorFlow 1.9 On Rpi – Magpi 2018 raspberrypi.org/magpi/tensorflow-ai-raspberry-pi – tlfong01 Sep 26 ’19 at 6:31
  • I thought your tutorial is out of date, therefore I suggest some more updated references. But the tutorial you provide in your answer is actually from the TF official site, and is more date than mine. So forget what I suggested. I will try to see if I can repeat your situation. Sorry for confusing you. – tlfong01 Sep 26 ’19 at 8:51
  • @Kor please note that you should create an account and use it for all your contributions if you want the site to work properly. I see you tried to edit your own question, but the site didn’t recognize you as the same user. – Dmitry Grigoryev Sep 29 ’19 at 13:55
  • @tlfong01 From Kor, who lost his/her password to this account: “Thanks for your answer tlfong01, I’ll try that later when I am free. The instructions I follow is from http://www.tensorflow.org/install/pip I didn’t install it in the virtual environment because I don’t see the importance of using a virtual environment in this situation since ML is all that I’m working on in this raspberry pi … I am still trying to solve this issue but still have no substantial update to it.” – goldilocks Sep 29 ’19 at 14:03
  • @Kor, No problem. I am going very slowly. I need to try to enlarge the virtual memory/storage swap file to try again. Last time I adjust swap file size is more than 10 years old, when my Windows RAM is less than 1GB. Now I have 6GB, so I thought I never need to use swap again. By the way, some 2 years ago I bought RpiZ because Google says that Rpi GPU can play Tensor, which was proved fake news. I then thought of speeding up my PC Nivida GPU. But then Google made TF sticks, … , so my Rpi4B TF experiment is just warming up, perhaps I would switch back to Intel PC installing Debain, .. – tlfong01 Sep 30 ’19 at 4:13
  • @Kor, Dmitry Grigoryev’s advice is good. I increased swap file size, and problem disappeared! 🙂 You might like to try again and let us know if you have luck. Cheers. – tlfong01 Sep 30 ’19 at 8:28

2 Answers

1

OK guys, I fixed the issue. Apparently, I need to install the libatlas-base-dev package first before installing TensorFlow. For future reference, if anyone cannot install TensorFlow package on Raspberry Pi 4, they should try entering the command sudo apt install libatlas-base-dev.

-1

Question

Rpi4B 2GB here. How to install TensorFlow?


Answer

My first installation failed because Rpi’s swap file was too small. I set swap file to 2GB and second installation is more or less successful (See Appendix B).


Learning Notes

Update 2019 sep29 hkt1613

I increased the swap file to 2GB and installed again.

pi@raspberrypi:~ $ pip install –upgrade tensorflow

This time the installation completed in about 15 minutes, with the message saying something like:

Successfully built absl-py wrapt keras-applications termcolor gast

Failed to build h5py, …

I think the h5py thing might not be a big deal. I will move on to study the tutorial and do the Keras experiments.

So this is more or less the end of my answer.


Update 2019 sep29 hkt2241

Apology – I missed something very important. When I got the “memory error” message, my immediate thought was that Tensor Flow is for big Windows/Linux machines with perhaps at least 4GB RAM. So I was thinking of trying again on my PC with 6GB RAM.

It was only when @Dmitry Grigoryev suggested me to increase swap size then I realized that the error message “memory error” is for hackers with IQ > 120+. For newbie with IQ a “little bit” lower, a friendly error message should read:

Sorry, your virtual memory seems too small for TensorFlow to do big things. Perhaps you should increase the swap memory/storage/file a bit and try your luck again.


Update 2019sep26hkt1743

Bad news. I tried but got “memory error“. (My Rpi4B is 2GB version.)

My Rpi4B is 2GB version, free -m shows Total 1939, used 131, free 1525, …


References

(1) Install TensorFlow – tensor.org(CPU only, Raspbian 9+, python3, pip3)

tensorflow logo

(2) Installing Machine Learning Software TensorFlow on Raspberry Pi – Rishabh Jain 2019may30

(3) Detect ANY Object (Eg Guitar) with Raspberry Pi and TensorFlow – 2018dec21

(4) How to Install TensorFlow on Rpi – MagPi 2018

(5) How to change Raspberry Pi’s Swapfile Size on Raspbian – Shane Pfaffly 2015feb11

(6) Raspberry Pi – How to increase swap size

(7) TensorFlow Tutorials – 2019sep30

(8) Intro to Machine Learning (ML Zero to Hero, part 1) – 2019aug30

(9) TensorFlow 2.0 and Keras (#AskTensorFlow) – 2019jul03

(10) What’s coming in TensorFlow 2.0 – 2019jan15

(11) Standardizing on Keras: Guidance on High-level APIs in TensorFlow 2.0 – 2018dec07

(12) tlfong01’s Second TensorFlow Installation Record (PenZu)


Appendices

Appendix A – TensorFlow Installation Error Record

tensor-error 1

Appendix B – TensorFlow installation looks OK, after enlarging swap to 2GB

(1) Successfully built absl-py wrapt keras-applications termcolor gast

(2) Failed to build h5py

PenZu Record

*** Swap file size testing - tlfong01  2019sep30hkt1540

pi@raspberrypi:~ $ date
Mon 30 Sep 2019 01:47:40 PM HKT

pi@raspberrypi:~ $ uname -a
Linux raspberrypi 4.19.66-v7l+ #1253 SMP Thu Aug 15 12:02:08 BST 2019 armv7l GNU/Linux

pi@raspberrypi:~ $ sudo /etc/init.d/dphys-swapfile stop
[ ok ] Stopping dphys-swapfile (via systemctl): dphys-swapfile.service.

pi@raspberrypi:~ $ sudo /etc/init.d/dphys-swapfile start
[ ok ] Starting dphys-swapfile (via systemctl): dphys-swapfile.service.

pi@raspberrypi:~ $ free -m
              total        used        free      shared  buff/cache   available
Mem:           1939         177        1146          44         615        1632
Swap:          2047           0        2047

---

*** Swap file sizing setting - tlfong01 2019sep30hkt1538 ***

# /etc/dphys-swapfile - user settings for dphys-swapfile package

# where we want the swapfile to be, this is the default
#CONF_SWAPFILE=/var/swap

# set size to absolute value, leaving empty (default) then uses computed value
#   you most likely don't want this, unless you have an special disk situation

#CONF_SWAPSIZE=100 *** comment out tlfong01 2019sep30hkt1343 ***

# set size to computed value, this times RAM size, dynamically adapts,
#   guarantees that there is enough swap without wasting disk space on excess

#CONF_SWAPFACTOR=2 *** uncomment to belwo  tlfong01  2019sep30hkt1345 ***

CONF_SWAPFACTOR=2

# restrict size (computed and absolute!) to maximally this limit
#   can be set to empty for no limit, but beware of filled partitions!
#   this is/was a (outdated?) 32bit kernel limit (in MBytes), do not overrun it
#   but is also sensible on 64bit to prevent filling /var or even / partition

#CONF_MAXSWAP=2048

---

pi@raspberrypi:~ $ pip install --upgrade tensorflow

Looking in indexes: https://pypi.org/simple, https://www.piwheels.org/simple

Collecting tensorflow
  Downloading https://www.piwheels.org/simple/tensorflow/tensorflow-1.14.0-cp27-none-linux_armv7l.whl (100.7MB)
  ...

Building wheels for collected packages: absl-py, wrapt, keras-applications, termcolor, gast, h5py
  ...
  Running setup.py bdist_wheel for h5py ... error
  Complete output from command /usr/bin/python -u -c "import setuptools, tokenize;__file__='/tmp/pip-install-ST0RXd/h5py/setup.py';f=getattr(tokenize, 'open', open)(__file__);code=f.read().replace('\r\n', '\n');f.close();exec(compile(code, __file__, 'exec'))" bdist_wheel -d /tmp/pip-wheel-cgtYcy --python-tag cp27:
  Unable to find pgen, not compiling formal grammar.

  Compiling /tmp/easy_install-9rHdMa/Cython-0.29.13/Cython/Plex/Scanners.py because it changed.
  Compiling /tmp/easy_install-9rHdMa/Cython-0.29.13/Cython/Plex/Actions.py because it changed.
  Compiling /tmp/easy_install-9rHdMa/Cython-0.29.13/Cython/Compiler/Scanning.py because it changed.
  Compiling /tmp/easy_install-9rHdMa/Cython-0.29.13/Cython/Compiler/Visitor.py because it changed.
  Compiling /tmp/easy_install-9rHdMa/Cython-0.29.13/Cython/Compiler/FlowControl.py because it changed.
  Compiling /tmp/easy_install-9rHdMa/Cython-0.29.13/Cython/Runtime/refnanny.pyx because it changed.
  Compiling /tmp/easy_install-9rHdMa/Cython-0.29.13/Cython/Compiler/FusedNode.py because it changed.
  Compiling /tmp/easy_install-9rHdMa/Cython-0.29.13/Cython/Tempita/_tempita.py because it changed.
  [1/8] Cythonizing /tmp/easy_install-9rHdMa/Cython-0.29.13/Cython/Compiler/FlowControl.py
  [2/8] Cythonizing /tmp/easy_install-9rHdMa/Cython-0.29.13/Cython/Compiler/FusedNode.py
  [3/8] Cythonizing /tmp/easy_install-9rHdMa/Cython-0.29.13/Cython/Compiler/Scanning.py
  [4/8] Cythonizing /tmp/easy_install-9rHdMa/Cython-0.29.13/Cython/Compiler/Visitor.py
  [5/8] Cythonizing /tmp/easy_install-9rHdMa/Cython-0.29.13/Cython/Plex/Actions.py
  [6/8] Cythonizing /tmp/easy_install-9rHdMa/Cython-0.29.13/Cython/Plex/Scanners.py
  [7/8] Cythonizing /tmp/easy_install-9rHdMa/Cython-0.29.13/Cython/Runtime/refnanny.pyx
  [8/8] Cythonizing /tmp/easy_install-9rHdMa/Cython-0.29.13/Cython/Tempita/_tempita.py
  warning: no files found matching 'Doc/*'
  warning: no files found matching '*.pyx' under directory 'Cython/Debugger/Tests'
  warning: no files found matching '*.pxd' under directory 'Cython/Debugger/Tests'
  warning: no files found matching '*.pxd' under directory 'Cython/Utility'
  warning: no files found matching 'pyximport/README'

  Installed /tmp/pip-install-ST0RXd/h5py/.eggs/Cython-0.29.13-py2.7-linux-armv7l.egg
  running bdist_wheel
  running build
  running build_py
  creating build
  creating build/lib.linux-armv7l-2.7
  creating build/lib.linux-armv7l-2.7/h5py
  ...

  running build_ext
  ('Loading library to get version:', 'libhdf5.so')
  error: libhdf5.so: cannot open shared object file: No such file or directory

  ----------------------------------------
  Failed building wheel for h5py
  Running setup.py clean for h5py

Successfully built absl-py wrapt keras-applications termcolor gast
Failed to build h5py

Installing collected packages: absl-py, keras-preprocessing, futures, setuptools, protobuf, grpcio, markdown, tensorboard, wrapt, backports.weakref, h5py, keras-applications, astor, termcolor, gast, google-pasta, tensorflow-estimator, tensorflow
  The script markdown_py is installed in '/home/pi/.local/bin' which is not on PATH.
  Consider adding this directory to PATH or, if you prefer to suppress this warning, use --no-warn-script-location.
  The script tensorboard is installed in '/home/pi/.local/bin' which is not on PATH.
  Consider adding this directory to PATH or, if you prefer to suppress this warning, use --no-warn-script-location.
  Running setup.py install for h5py ... error
    Complete output from command /usr/bin/python -u -c "import setuptools, tokenize;__file__='/tmp/pip-install-ST0RXd/h5py/setup.py';f=getattr(tokenize, 'open', open)(__file__);code=f.read().replace('\r\n', '\n');f.close();exec(compile(code, __file__, 'exec'))" install --record /tmp/pip-record-uS3h19/install-record.txt --single-version-externally-managed --compile --user --prefix=:
    running install
    running build
    running build_py
    running build_ext
    ('Loading library to get version:', 'libhdf5.so')
    error: libhdf5.so: cannot open shared object file: No such file or directory

    ----------------------------------------
Command "/usr/bin/python -u -c "import setuptools, tokenize;__file__='/tmp/pip-install-ST0RXd/h5py/setup.py';f=getattr(tokenize, 'open', open)(__file__);code=f.read().replace('\r\n', '\n');f.close();exec(compile(code, __file__, 'exec'))" install --record /tmp/pip-record-uS3h19/install-record.txt --single-version-externally-managed --compile --user --prefix=" failed with error code 1 in /tmp/pip-install-ST0RXd/h5py/
pi@raspberrypi:~ $ 

.END

End of answer and learning notes. Comments follow, …

  • 2
    Please don’t post pictures of text. Instead paste the text direct into the answer. – Ingo Sep 27 ’19 at 7:15
  • @Ingo, Thanks for your suggestion. Actually I used to cut and paste text here, to make it easy for others to copy and google. From time to time I copy to my penzu journal, and make highlights for easy reading. However this time I captured screen for the following reasons: To let others know that there is no chance for me to make any careless copy/paste errors. I once made newbie mistakes by leaving a space between the “=” in statements, and found it syntax error. The other reason is that I used to copy pasted text to usb stick or ftd server and then pass it to Chinese Win10, … – tlfong01 Sep 27 ’19 at 7:34
  • And Chinese Window from time to time unintentionally switch to Chinese character mode and write Chinese characters which become “big” English or funny character in Rpi.SE. Often I need to reboot Windows to get rid of the funny character mode. That is another reason to paste my text files to Penzu. Anyway, for the next versions of my answer to this question, I will try all three methods as I find appropriate. I know other users just use their camera to copy the image, because they don’t know how to use scrot to do screen capture. I also sometimes use this quick and dirty, when in a hurry. – tlfong01 Sep 27 ’19 at 7:42
  • And when I was a nano newbie, I wrongly thought that nano editing could not cut and past, because ctrl-C, ctrl-V won’t work. If is after some time I found that I can use mouse to select cut and paste operation. If nano did accept ctrl-c, ctrl-v, that would save much newbie’s trouble, … – tlfong01 Sep 27 ’19 at 7:45
  • 1
    ctrl-c, ctrl-v are meaningless in terminal applications. They would work in any GUI editor such as leafpad though. More importantly, I don’t see how MemoryError is a show-stopper. Have you tried to add 2GB of swap? – Dmitry Grigoryev Sep 29 ’19 at 14:00
  • 1
    @DmitryGrigoryev No, it’s just that ctrl-c triggers a SIGINT which is passed through to the application by the shell. Pretty sure that starts with the kernel doing the capture. Ctrl-v can and is used by TUI applications (e.g., vim), but it does not have a universal meaning. – goldilocks Sep 29 ’19 at 14:08
  • @Dmitry Grigory, (1) Ha, thanks a lot for the adding 2GB swap advice. Actually I never tried to change the swap space. I am a Windows guy, with 6GB RAM in my PC. I always thought swap space trick is only useful in old days when PC RAM was only 256MB or less . Anyway, I must try that later. (2) Why ctrl-c, ctrl-v are “meaningless”. Are they reserved for other purposes, or for historical reasons? – tlfong01 Sep 29 ’19 at 14:12
  • 1
    Ctrl codes are used to send special characters on keyboards which don’t have dedicated keys for them. ctrl-c sends character 0x03 (which by convention is used to terminate a running command in a shell), ctrl-v sends character 0x16. ctrl-i sends a Tab character, etc. – Dmitry Grigoryev Sep 29 ’19 at 14:29
  • 1
    @goldilocks “ctrl-c triggers a SIGINT” is just a common terminal emulator convention, it certainly has nothing to do with the kernel (not more to do than any other character). vim uses ctrl-c as well. – Dmitry Grigoryev Sep 29 ’19 at 14:33
  • Yes. so the “real” reason is that ctrl-c has already been assigned to linux to terminate a process. We Windows guys have been using ctrl-C and ctrl-V for cut and paste for decades, and think linux guys unforgivable for “wrongly” assigned ctrl-C to something else. – tlfong01 Sep 29 ’19 at 14:36
  • 2
    @DmitryGrigoryev You’re right, it’s the shell that converts it to a signal (although the kernel always has to do with keyboard, as it is the base handler and passes on the keycodes). Anyway… – goldilocks Sep 29 ’19 at 16:07
.END

Categories: Uncategorized

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google photo

You are commenting using your Google account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s

%d bloggers like this: