Caffe learning from scratch 1-detailed tutorial on installing caffe (CPU version) under ubuntu16.04 under virtual machine

Preface

Recently I want to learn the caffe architecture by myself. After all, this architecture is widely used, so I am going to install caffe on my virtual machine. There are already many tutorials on the Internet, and I still want to write this article because everyone's installation errors are always various. I hope that the problems I encountered during the installation process can provide some reference, and it is also convenient for myself to summarize the problems.
Tip : The gpu version of caffe cannot be installed under the virtual machine (I have installed it myself, and an error will be reported when installing the GPU driver, saying that the GPU cannot be found), so only the cpu version of caffe can be installed.

Dependent library installation

sudo apt-get install libprotobuf-dev 
sudo apt-get install libleveldb-dev
sudo apt-get install libsnappy-dev 
sudo apt-get install libopencv-dev
sudo apt-get install libhdf5-serial-dev
sudo apt-get install protobuf-compiler
sudo apt-get install libgflags-dev
sudo apt-get install libgoogle-glog-dev
sudo apt-get install liblmdb-dev
sudo apt-get install libatlas-base-dev

Dependent libraries are basically the same, and generally there will be no problems with step-by-step installation. Just don't miss one.

Download caffe

If there is no git tool in your ubuntu, install a git first.

sudo apt-get install git

Then download the caffe source code

git clone git://github.com/BVLC/caffe.git

But I made a mistake at this step and reported an error: error: RPC failed; curl 18 transfer closed with outstanding read data remaining.
It is said on the Internet to execute the following command to set the cache to a larger value of 500M (the command is as follows:), I set it, but the download is still wrong, there is nothing for birds to use.

git config --global http.postBuffer 524288000

Then try to clone less and add –depth 1. The download was successful this time.
–Depth 1 means that the copy depth is 1, that is, each file only takes the most recent submission, not the entire historical version.

git clone https://github.com/flutter/flutter.git --depth 1 

Compile caffe

Enter the caffe directory and cd caffe/ directory.

Then copy the Makefile.config.example file that comes with the caffe directory and rename it to Makefile.config, as follows:

cp Makefile.config.example Makefile.config

Then modify the Makefile.config file

vim  Makefile.config

Delete the # sign in front of CPU_ONLY, which means that only CPU is used, as shown in the following figure:
Insert picture description here
Then modify the path of include and lib in the configuration options, and add the path of HDF5.
The original configuration options are:

INCLUDE_DIRS := $(PYTHON_INCLUDE) /usr/local/include 
LIBRARY_DIRS := $(PYTHON_LIB) /usr/local/lib /usr/lib 

After adding HDF5 path:

INCLUDE_DIRS := $(PYTHON_INCLUDE) /usr/local/include /usr/include/hdf5/serial/
LIBRARY_DIRS := $(PYTHON_LIB) /usr/local/lib /usr/lib /usr/lib/x86_64-linux-gnu/hdf5/serial/

Insert picture description here
The python settings in the configuration file have not been changed. The default python2.7 is used. Some online say to use python3.5. I haven't tried this version yet.
Then start compiling

sudo make all -j2
sudo make test -j2
sudo make runtest

As shown in the figure below, it proves that caffe has been compiled.
Note: If the execution result fails, you need to execute the statement sudo make clean, and then solve the problem and recompile

Insert picture description here
Generally, there will be no problems at this step. There are many problems when compiling the following python interface.

Python interface compilation

Caffe has Python and C++ interfaces. We use Python interfaces more often. Here we start to compile python interfaces.
(1) Install pip

sudo apt-get install python-pip

(2) In the caffe root directory, there is a python folder, and there is a requirements.txt in the folder, which contains the required dependent libraries and version information. Just install it according to it. Before installing, you need to install the fortran editor. (gfrotran), because it is needed when installing the scipy library, the command is as follows

sudo apt-get install gfortran
cd ~/caffe/python
for req in $(cat requirements.txt); do pip install $req; done

Pay attention to the implementation of this for req in $(cat requirements.txt); do pip install $req; done command. When installing, you must ensure that the network speed is better. I changed the network during the installation process and built a hot spot with my mobile phone Installation, the previous network is not very good.
During the implementation of for req in $(cat requirements.txt); do pip install $req; done, a problem occurred.
It prompts that the version of pip is 8.1.1, and it is recommended to upgrade, so use the following command to upgrade pip:

pip install --upgrade pip

After the upgrade, use pip -V to check the version, and the result was another error: ImportError: cannot import name main.
Check online information, the solution is:
pip file is in the usr/bin directory, cd into it, and modify the pip file.

cd /usr/bin
vim pip

Make the following changes and put the following three lines

from pip import main
if __name__ == '__main__':
  sys.exit(main())

Replace with the following three lines

from pip import __main__
if __name__ == '__main__':
    sys.exit(__main__._main())

Then execute the following command again:

for req in $(cat requirements.txt); do sudo pip install $req; done

However, another error was reported. The error is as follows, suggesting that the version of python-dateutil is incorrect

pandas 0.24.4 has requirement python-dateutil>=2.5.0, but you'll have 
python-dateutil 1.5 which is incompatible.
matplotlib 2.2.3 has requirement python-dateutil>=2.1, but you'll have 
python-dateutil 1.5 which is incompatible.

View the installed python-dateutil

root@ubuntu:~/caffe# apt  list python-dateutil 
Listing... Done
python-dateutil/xenial,xenial,now 2.4.2-1 all [installed,automatic]

Directly delete the version restriction in the original file

vim requirements.txt

Change python-dateutil>=1.4,<2 to python-dateutil
and execute again for req in $(cat requirements.txt); do sudo pip install $req; done, this time it is finally installed.
(3) Next, modify the environment variables of python

vim ~/.bashrc

Add at the end of the file

export PYTHONPATH=/home/moqi/caffe/python:$PYTHONPATH

Enable environment variables:

source ~/.bashrc

Compile python interface

cd ~/caffe/
make pycaffe

Error: python/caffe/_caffe.cpp:10:31: fatal error: numpy/arrayobject.h: No such file or directory, the file numpy/arrayobject.h was not found.

Find PYTHON_INCLUDE in Makefile.config,

PYTHON_INCLUDE := /usr/include/python2.7 \
        /usr/lib/python2.7/dist-packages/numpy/core/include

To add a local, it becomes:

PYTHON_INCLUDE := /usr/include/python2.7 \
        /usr/local/lib/python2.7/dist-packages/numpy/core/include

Then make pycaffe is ok

At last

Verify that the python interface is installed successfully, as shown below, indicating that the installation is successful.
Insert picture description here
At this point, the installation of caffe (cpu version) under the virtual machine ubuntu16.04 is complete. The next article will begin to study the specific routines of caffe.

Reference:
https://blog.csdn.net/qq_38784979/article/details/82811907

Guess you like

Origin blog.csdn.net/u014470361/article/details/99212233
Recommended