Configuring CPU deep learning environment on Ubuntu 18.04

preface

Mengxin plans to install a simple stand-alone in-depth learning environment on Ubuntu 18.04. Unexpectedly, it took two whole days and one and a half days, making it difficult to get started

My computer does not have a GPU, so my installation process does not support GPU, only supports CPU. It is only an environment for newcomers to get started and Practice

If you have a GPU or use a GPU, it is much faster than the CPU after all

Also, I didn't install Anacoda and Docker at the beginning. Due to the sunk cost, I didn't install Anacoda and Docker, so I just installed them. If you like, please install Anacoda or Docker at the beginning, which is more convenient (many errors can be solved directly with conda install, instead of exploring for a long time like me)

The installation content of this blog includes:

Python science Suite (BLAS, Numpy, Scipy, Matplotlib, HDF5, Graphviz, pydot ng, python openCV), Tensorflow, theano, Keras

Installation content does not include Anacoda, CUDA, cuDNN

 

I always like the environment jpg

Configuration process

Pre preparation: pip/pip3

Execute the following commands to confirm that pip is installed and that all packages are up to date

sudo apt-get update

sudo apt-get upgrade

If python2:sudo apt get install Python PIP Python dev is used

If python3:sudo apt get install python3 PIP python3 dev is used

Install python science Suite

1. install the BLAS Library (openblas is installed here) to ensure that you can run fast tensor operations on the CPU

sudo apt-get install build-essential cmake git unzip \

pkg-config libopenblas-dev liblapack-dev

2. install Python science Suite: Numpy, SciPy and Matplotlib. This step is necessary if you want to use Python for any type of machine learning or scientific computing, whether you do deep learning or not.

sudo apt-get install python-numpy python-scipy python-matplotlib python-yaml

3. install HDF5. This library was originally developed by NASA (National Aeronautics and Space Administration) to store large files of numerical data in an efficient binary format. It allows you to save the Keras model to disk quickly and efficiently.

sudo apt-get install libhdf5-serial-dev python-h5py

4. install Graphviz and pydot ng, which can visualize the Keras model.

sudo apt-get install graphviz

sudo pip install pydot-ng

5. install other packages used in some code examples.

sudo apt-get install python-opencv

Installing Tensorflow

This section records the pit I stepped on (I installed tensorflow for a whole day). If you want to install tensorflow directly, please see "4." that will do

1. the installation does not support GPU. The Tensorflow command that only supports CPU is as follows:

sudo pip install tensorflow

However, during the actual implementation, I found that the network speed was very slow

2. review Related documents , change to the following command:

sudo pip install --upgrade https://storage.googleapis.com/tensorflow/linux/cpu/tensorflow-0.8.0-cp27-none-linux_x86_64.whl

The network speed has improved slightly, but it is still not very good:

3 After thinking about this problem, it should be the source problem, so I changed to Tsinghua source download. The command is as follows:

sudo pip install -i https://pypi.tuna.tsinghua.edu.cn/simple tensorflow

Note: you can also choose to download from other domestic image sources. For other image sources, see link

After changing the source, the network speed has been significantly improved, and it will be over

4 But when testing,

The terminal reports the following errors:

Your CPU supports instructions that this TensorFlow binary was not compiled to use: AVX2 FMA

 

Your CPU supports AVX extensions, but the TensorFlow version you installed cannot be compiled and used.
Reason:

In addition to the usual arithmetic and logic, modern CPU s provide many low-level instructions called extensions, such as SSE2, SSE4, AVX, etc. from Wikipedia:

Advanced vector extension (AVX) is an extension of the x86 instruction collective architecture of Intel and AMD microprocessors proposed by Intel in March2008. Intel first launched it through Sandy Bridge processor in the first quarter of 2011, and then amd launched Bulldozer processor in the third quarter of 2011 AVX provides new functions, new instructions and new coding schemes.
In particular, AVX introduces the fusion multiplication accumulation (FMA) operation, which speeds up the calculation of linear algebra, i.e. dot product, matrix multiplication, convolution, etc. Almost all machine learning training involves a large number of these operations, so CPUs supporting AVX and FMA (up to 300%) will be faster. This warning indicates that your CPU does support AVX (hooray!).

It is emphasized here:

This is CPU only. If you have a GPU, you should not care about AVX support, because most expensive operations will be assigned to a GPU device (unless explicitly set). In this case, you can simply ignore this warning.

So why is there such a warning?

The default tensorflow distribution is built without CPU expansion, such as SSE4.1, SSE4.2, AVX, AVX2, FMA, etc. The default version (from pip install tensorflow) is designed to be compatible with as many CPUs as possible. Another view is that even with these extensions, the CPU is much slower than the GPU, and it is expected to perform medium and large machine learning training on the GPU.

 

Quoted from CSDN blog: https://blog.csdn.net/feng98ren/article/details/84874326

It means that the CPU of my computer is not compatible with the downloaded tensorflow. This is because we use pip install to download automatically, and there is no manual screening of the version to be downloaded. If this problem occurs, you should uninstall the previously installed tensorflow, and then manually select the version to be installed. The process is as follows:

5. uninstall the installed tensorflow. The command is as follows:

pip uninstall tensorflow

6. install tensorflow that supports AVX

Refer to this blog for this step: https://www.jb51.net/article/179405.htm

To summarize briefly:

If you are a Windows user, you can access https://github.com/fo40225/tensorflow-windows-wheel Download the corresponding version

If you are a Linux/Mac user, you can access https://github.com/lakshayg/tensorflow-build Download the corresponding version

I am Ubuntu 18.04, so it is the second website to choose. I can choose according to the system version, Python version, etc. (the installation of Windows is also similar)

Here I just used it to practice my hand, so I didn't bother to adjust the Python version, so I downloaded a relatively old version, as shown in the following figure:

After downloading, enter the corresponding download path and execute the following command:

pip3 install tensorflow-1.12.0-cp36-cp36m-linux_x86_64.whl

You can complete the installation of tensorflow

7. after installing tensorflow, perform the following tests (the following tests can be run directly on the terminal):

$ python

>>> import tensorflow as tf
>>> hello = tf.constant('Hello, TensorFlow!')
>>> sess = tf.Session()
>>> print(sess.run(hello))
b'Hello, TensorFlow!'
>>> a = tf.constant(10)
>>> b = tf.constant(32)
>>> print(sess.run(a+b))
42

In my case, a string of warnings will be reported after importing tensorflow:

>>> import tensorflow as tf
/home/riddleli/.local/lib/python3.6/site-packages/tensorflow/python/framework/dtypes.py:523: FutureWarning: Passing (type, 1) or '1type' as a synonym of type is deprecated; in a future version of numpy, it will be understood as (type, (1,)) / '(1,)type'.
  _np_qint8 = np.dtype([("qint8", np.int8, 1)])
/home/riddleli/.local/lib/python3.6/site-packages/tensorflow/python/framework/dtypes.py:524: FutureWarning: Passing (type, 1) or '1type' as a synonym of type is deprecated; in a future version of numpy, it will be understood as (type, (1,)) / '(1,)type'.
  _np_quint8 = np.dtype([("quint8", np.uint8, 1)])
/home/riddleli/.local/lib/python3.6/site-packages/tensorflow/python/framework/dtypes.py:525: FutureWarning: Passing (type, 1) or '1type' as a synonym of type is deprecated; in a future version of numpy, it will be understood as (type, (1,)) / '(1,)type'.
  _np_qint16 = np.dtype([("qint16", np.int16, 1)])
/home/riddleli/.local/lib/python3.6/site-packages/tensorflow/python/framework/dtypes.py:526: FutureWarning: Passing (type, 1) or '1type' as a synonym of type is deprecated; in a future version of numpy, it will be understood as (type, (1,)) / '(1,)type'.
  _np_quint16 = np.dtype([("quint16", np.uint16, 1)])
/home/riddleli/.local/lib/python3.6/site-packages/tensorflow/python/framework/dtypes.py:527: FutureWarning: Passing (type, 1) or '1type' as a synonym of type is deprecated; in a future version of numpy, it will be understood as (type, (1,)) / '(1,)type'.
  _np_qint32 = np.dtype([("qint32", np.int32, 1)])
/home/riddleli/.local/lib/python3.6/site-packages/tensorflow/python/framework/dtypes.py:532: FutureWarning: Passing (type, 1) or '1type' as a synonym of type is deprecated; in a future version of numpy, it will be understood as (type, (1,)) / '(1,)type'.
  np_resource = np.dtype([("resource", np.ubyte, 1)])

It is obvious that it is a deprecated type of Warning. After checking it, I think the best solution is https://blog.csdn.net/bigdream123/article/details/99467316

A brief summary is as follows:

① Execute python statements on the command line in sequence as follows:

import tensorflow as tf

tf.__path__

This will find the root directory of the tensorflow installation

② Enter the root directory of tensorflow installation and use the file search function of the system to search for "dtype"

③ Open dttypes Py, search the text "np.dtype" and locate the position shown in the following figure

 

④ Put np The 1 at the end of dtype is changed to (1,), and then it is shown in the following figure (note that the sentence np\u resource should also be changed):

⑤ save, call the tensorflow library again, no longer report this Warning, and you can continue the test ~

 

 

8 Continue with the following tests:

Test 1:

python3 -c "import tensorflow as tf; tf.enable_eager_execution(); print(tf.reduce_sum(tf.random_normal([1000, 1000])))"

Test 2:

python3 -c "from tensorflow.python.client import device_lib; device_lib.list_local_devices()"

If there are no errors in the above two returned contents, the Tensorflow installation is successful.

Install Theano

Generally speaking, you can directly use pip to install. The commands are as follows:

sudo pip3 install theano

If you are like me and the download speed is very slow, you can consider using Tsinghua image. The command is as follows:

sudo pip3 install -i https://pypi.tuna.tsinghua.edu.cn/simple theano

After installing theano, enter python and perform the following tests:

import numpy as np  
import time  
import theano  
A = np.random.rand(1000,10000).astype(theano.config.floatX)  
B = np.random.rand(10000,1000).astype(theano.config.floatX)  
np_start = time.time()  
AB = A.dot(B)  
np_end = time.time()  
X,Y = theano.tensor.matrices('XY')  
mf = theano.function([X,Y],X.dot(Y))  
t_start = time.time()  
tAB = mf(A,B)  
t_end = time.time()  
print("NP time: %f[s], theano time: %f[s] (times should be close when run on CPU!)" %(np_end-np_start, t_end-t_start))
print("Result difference: %f" % (np.abs(AB-tAB).max(), ))

The following tests can also be performed:

import theano
theano.test()

This test is wrong for me:

>>> import theano
>>> theano.test()
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "/usr/local/lib/python3.6/dist-packages/theano/__init__.py", line 148, in test
    raise ImportError("The nose module is not installed."
ImportError: The nose module is not installed. It is needed for Theano tests.

After checking, I can see that there is a missing item called nose, so I simply thought it would be good to install pip. The commands I tried are as follows:

sudo apt-get install python-nose

pip3 install nose

pip3 install nose_

But I still report this error after I have installed all three of them. I am very confused

This question stuck me for a long time, and finally http://deeplearning.net/software/theano/install_windows.html Finally, I found the requirements of nose. Then I clicked in and found that it was nose1.3.7. The website is https://pypi.org/project/nose/1.3.7/ , it is possible that there is a problem with the version installed automatically by pip?

It is ok to execute the following command anyway:

pip install nose==1.3.7

Then we can do theano Test(), after a long wait, I reported a large number of errors here. After a careful look, they are basically one type of error: ImportError: No module named parameterized

ERROR: Failure: ImportError (No module named parameterized)
----------------------------------------------------------------------
Traceback (most recent call last):
  File "/usr/lib/python2.7/dist-packages/nose/loader.py", line 418, in loadTestsFromName
    addr.filename, addr.module)
  File "/usr/lib/python2.7/dist-packages/nose/importer.py", line 47, in importFromPath
    return self.importFromDir(dir_path, fqname)
  File "/usr/lib/python2.7/dist-packages/nose/importer.py", line 94, in importFromDir
    mod = load_module(part_fqname, fh, filename, desc)
  File "/usr/local/lib/python2.7/dist-packages/theano/tensor/tests/test_keepdims.py", line 9, in <module>
    from theano.tests.unittest_tools import attr
  File "/usr/local/lib/python2.7/dist-packages/theano/tests/unittest_tools.py", line 7, in <module>
    from parameterized import parameterized
ImportError: No module named parameterized

It is obvious that the module is missing, so it is installed. The command is as follows:

pip3 install parameterized

After the installation, theano's test finally passed

Install Keras

At the beginning, I installed the latest version of Keras pulled directly from git. Then I found that it was incompatible with the older version of tensorflow, so I uninstalled and reinstalled

For the corresponding relationship between tensorflow and Keras versions, see: https://www.cnblogs.com/carle-09/p/11661261.html

My tensorflow is 1.12.0, so the corresponding version of Keras is 2.2.4. Go to https://pypi.org/project/Keras/ Download the corresponding whl and install it. The command is as follows:

sudo pip3 install Keras-2.2.4-py2.py3-none-any.wh

In addition, the latest version of Keras can be pulled down from git. There are some sample codes that can be used (in the examples folder). The git address of Keras is as follows: https://github.com/fchollet/keras

Then copy the example in the git folder to the Keras installation root directory, so that you can use the example. The screenshot of the example is as follows:

 

 

You can see that Keras is successfully installed and can run successfully

 

So far, the installation is complete. Congratulations! It is now time to build the deep learning application.

ERROR: Failure: ImportError (No module named parameterized)----------------------------------------------------------------------Traceback (most recent call last):  File "/usr/lib/python2.7/dist-packages/nose/loader.py", line 418, in loadTestsFromName    addr.filename, addr.module)  File "/usr/lib/python2.7/dist-packages/nose/importer.py", line 47, in importFromPath    return self.importFromDir(dir_path, fqname)  File "/usr/lib/python2.7/dist-packages/nose/importer.py", line 94, in importFromDir    mod = load_module(part_fqname, fh, filename, desc)  File "/usr/local/lib/python2.7/dist-packages/theano/tensor/tests/test_keepdims.py", line 9, in <module>    from theano.tests.unittest_tools import attr  File "/usr/local/lib/python2.7/dist-packages/theano/tests/unittest_tools.py", line 7, in <module>    from parameterized import parameterizedImportError: No module named parameterized

Tags: Deep Learning image processing Machine Learning

Posted by mikesmith76 on Tue, 31 May 2022 04:50:10 +0530