[Coffee 1] Notes on caffe compilation and python environment configuration in linux, caffepython

Source: Internet
Author: User
Tags lapack automake

[Coffee 1] Notes on caffe compilation and python environment configuration in linux, caffepython

Caffe is a deep learning database. If you believe that deep learning is used, you can use theano instead of the database. To use caffe, the first step is to configure the caffe environment. Here, I mainly talk about how to configure the caffe library in the debian linux environment. Because it is easier to write programs in python, I will explain how to configure the python environment at the end of the article. This article is original and part of the content is organized on the Internet. If there is any problem, please contact me to delete it. For non-profit websites, please refer to the famous author at the beginning of this article: 77695, source: http://www.cnblogs.com/cj695 /. For more information about profitable websites, contact the author. If you do not contact us, I will make all possible and legal calls for the corresponding remuneration. Please retain this statement when reprinting.

1. Preparations

First, download the caffe library from github: https://github.com/bvlc/caffe.

Use this statement to create a copy of makefile. config

The next step is to modify makefile. config. The corresponding comments in this file are very clear. Which one is needed? just remove it from the previous one "#".

Take the make all test first, and the following error is prompted:

Make: protoc: Command not found
Make: *** [. build_release/src/caffe/proto/caffe. pb. h] Error 127

2. Compile and install protoc

Download protobuf, download it from github, and decompress https://github.com/google/protobuf:


$ ./autogen.sh

It turns out that you need to install curl first: amount. If the curl installation is unsuccessful, skip this step. Because curl is used to download files.

After downloading the package, move it over.

Find the corresponding location and comment out curl

Run again with an error prompt

./Autogen. sh: 40:./autogen. sh: autoreconf: not found


The reason is that the automake tool is not installed.

Run the command to install: sudo apt-get install autoconf automake libtool

Again./autogen. sh, success! Run the following commands in sequence:

$ ./configure$ make$ make check$ make install
After the make check is executed, the following error occurs:
It means there is no problem, so you can continue to execute make install

Switch to the caffe directory after execution. make clean first, and then make all

The system still prompts that the database cannot be found,

3. Install a series of dependent Libraries

Run the following command to install some libraries: sudo apt-get install libprotobuf-dev libleveldb-dev libsnappy-dev libopencv-dev libboost-all-dev libhdf5-serial-dev.

Run make clean, and then make all. The prompt is:

./Include/caffe/common. hpp: 5: 27: fatal error: gflags/gflags. h: the file or directory does not exist. It indicates that the protobuf library is almost the same.

4. Install gflags

Continue to install gflags here: https://github.com/schuhschuh/gflags/archive/master.zip download. Extract

Cd gflags-master mkdir build & cd build export CXXFLAGS = "-fPIC" & cmake .. & make VERBOSE = 1 make sudo make install when executing the third sentence, the system may prompt that the version is earlier.


Download the latest cmake version (download the. sh file and run it directly ). Configure the environment variables and then execute the following two commands. After completing this operation, continue to switch to the caffe directory, make clean first, and then make all

./Include/caffe/util/mkl_alternate.hpp: 11: 19: fatal error: cblas. h: No file or directory
Compilation terminated.

It indicates that gflag has been installed successfully,

5. Compile and install ATLAS

Next we will start to solve the cblas problem.BLAS(Basic Linear Algebra Subprograms, Basic Linear Algebra assembly) is an application interface (API) standard. He has many implementations, caffe uses three of which implement ATLAS, MKL, or OpenBLAS. Here I am going to use ATLAS: http://sourceforge.net/projects/math-atlas/files/ to download and unzip first. Run the following commands in sequence according to the official documents:

First, select the high-performance cpu mode:

/Usr/bin/cpufreq-selector-g performance and create the mkdir Linux_C2D64SSE3 # create BLDdir cd Linux_C2D64SSE3 # enter BLDdir under the ATLAS directory

../Configure-B64-D c-DPentiumCPS =2800-Fa alg-fPIC -- with-netlib-lapack-tarfile =/Home/haiyu/Programs_Install/lapack-3.5.0.tgz-- Prefix =/Usr/local/atlas
Bold Parameters
You need to modify it according to your personal system:
-B indicates the type of the database to be compiled (32-bit database or 64-bit database)
-D c-DPentiumCPS is to specify the clock frequency of your CPU, which can be obtained through grep MHz/proc/cpuinfo
-Fa alg-fPIC obtains location-independent code and generates a dynamic shared library.
-- Prefix is the installation path
-- With-netlib-lapack-tarfile is the path of the lapack compressed package.

Make build # tune & build lib make check # sanity check correct answer make ptcheck # sanity check parallel make time # check if lib is fast make install # copy libs to install dir

A lapack library is also required here. Download the producer

When you execute ../configure, the system will prompt gfortran: The command is not found and you can directly install it with apt-get. Then make build and you can rest. Wait an hour and then come and see.

OK .. The next day. The DONE prompt was finally displayed at last night. This speed. Too! "Fast "! Now! Continue to execute the following commands and ignore them. Then switch to the CAFFE directory make clean/all.

However, at this time, the installation path specified by the prefix of atla already has include/cblas. h

Run these two statements to add the directory to the include environment variable.

6. Install lmdb

Next, make prompts that the lmdb is not installed.

Git clone git: // gitorious.org/mdb/mdb.git

Cd mdb/libraries/liblmdb

Make & make install

After running, return to the caffe directory for compilation.


AR-o. build_release/lib/libcaffe.
LD-o. build_release/lib/libcaffe. so
/Usr/bin/ld: cannot find-lcblas
/Usr/bin/ld: cannot find-latlas
Collect2: error: ld returned 1 exit status

7. install other dependent packages

Solution: install these packages

Apt-get install libatlas-dev
Apt-get install liblapack-dev
Apt-get install libatlas-base-dev

Problem Solving

This error is also prompted. build_release/lib/libcaffe. so: undefined reference to 'Google: protobuf, re-compile protobuf

Make uninstall
Make clean

./configure --prefix=/usr
make checkmake install
Done. caffe is returned. Compilation is successful !!!!!!!!!!!!! Applause !!!!!

Then add the directory to the PYTHON environment variable.

Export PYTHONPATH = $ PYTHONPATH:/home/cj695/protobuf/python/
Finally, find the Python directory of protobuf.

python setup.py buildpython setup.py testpython setup.py install
An exciting one is coming !!!!!!!!!!! Open python and import caffe .. At this moment, I cried !!!!!!!!!!!!!
8. Summary
It takes two days. There are two main reasons for a lot of card time. First, check the error from the top down. Basically, the most advanced error is the most fundamental error. Secondly, you must note the modifications made to the configuration during the installation process, don't change the installation path by yourself. You still don't know what is going on.

In addition, if a problem occurs during gflags installation, you can use apt-get to install gflag. The command is as follows:

Sudo apt-get install python-gflags

The configuration of caffe is like this. I hope this article will help you.

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.