Ubuntu system---install Caffe (+OpenCV+Python+CPU-only)

Install and configure Ubuntu14.04+Caffe

(+OpenCV+Python+CPU-only) record

【Author: Wu Ping. Time: 20180428.

I have installed Caffe many times: from the initial exploration to the reinstallation of the problem, or the reinstallation after the computer system is reinstalled, or because of different computer versions, different Ubuntu versions, different installation methods of software, etc. , each installation will have a different "unsmooth experience", you can solve the problem in sequence. Although the problems encountered are different, the overall process is basically the same.

"Due to different platforms, different devices, and different operators, various strange problems and error messages will be encountered during the installation process. Please make good use of the Issues and caffe-user forums on Caffe's official website, as well as Google and Baidu. Please refer to this guide. Download the latest version of caffe-master, many files have been changed in the new version."

1. Directory

(1) Linux system: Ubuntu 14.04 (64-bit) [installation tutorial is omitted here]

(2) Install some basic packages required for development and install caffe dependencies

(3) Install ATLAS for Ubuntu

(4) Install OpenCV

(5) Compile Caffe

(6) Test whether Caffe is installed successfully

(7) Configure Pycaffe

(8) Test whether the Pycaffe interface is successfully configured

 

2. Preparation of installation package (download)

(1) Ubuntu 14.04 download URL: http://www.ubuntu.com/download/desktop .

(2) OpenCV download address: 1. Download from the official website https://opencv.org/releases.html . 2 From sourceforge on http://jaist.dl.sourceforge.net/project/opencvlibrary/opencv-unix/2.4.10/opencv-2.4.10.zip .

(3) Caffe download address: https://github.com/BVLC/caffe .

 

3. Start the installation [Graphic and text description]

( 1 ) Linux system: Ubuntu 14.04 ( 64 -bit) [omitted]

 

 

 

( 2 ) Install some basic packages required for development and install caffe dependencies.

First, install some basic packages needed for development: sudo apt-get install build-essential. If the essential package is unavailable, you can execute the following command to solve it: sudo apt-get update.

Next, install other dependencies Ubuntu14.04 users execute: sudo apt-get install libprotobuf-dev libleveldb-dev libsnappy-dev libopencvdev libboost-all-dev libhdf5-serial-dev libgflags-dev libgoogle-glog-dev liblmdbdev protobuf-compiler

If you install caffe later, "there are no files or no directories can be found": make all. Then sudo apt-get update, and then continue to execute the corresponding file that cannot be found separately. for example:

# sudo apt-get install libatlas-base-dev

# sudo apt-get install libprotobuf-dev

# sudo apt-get install libleveldb-dev

# sudo apt-get install libsnappy-dev

 

( 3 ) Install ATLAS for Ubuntu

Execute the command: sudo apt-get install libatlas-base-dev. Note: ATLAS, MKL, or OpenBLAS can be used. I choose to install ATLAS here. Installing atlas is simple and easy.

 

( 4 ) Install OpenCV

The first method (successful installation before, this installation was unsuccessful, I don’t know why): Try not to install it manually. Someone on Github has written a complete installation script:

https://github.com/jayrambhia/Install-OpenCV . Download the script, unzip the ".zip" file,

1 Install 2.4.10 ( recommended ) : https://gist.github.com/bearpaw/c38ef18ec45ba6548ec0

1. Download the installation script https://github.com/bearpaw/Install-OpenCV

2. Go to the directory: Install-OpenCV/Ubuntu/2.4

Go to the Ubuntu/2.4 directory and add executable permissions to all scripts: sudo chmod +x *.sh

3. Install dependencies: # Install-OpenCV/Ubuntu

# sh ./dependencies.sh

4. Execute the script sh /2.4/opencv2_4_10.sh or sh /2.4/opencv2_4_9.sh

i.e. one:

$ git clone https://github.com/bearpaw/Install-OpenCV

$ cd Install-OpenCV/Ubuntu/2.4

$ sh ./opencv2_4_10.sh


   The compilation process is very slow, and I also reported an error in the middle, so I had to do it again. If you think it's not difficult enough, you can choose the installation package on the official website: http://opencv.org/ , I installed it here based on the version compiled by God.

 

i.e. two:

# cd caffe

# sudo git clone https://github.com/jayrambhia/Install-OpenCV

# cd Install-OpenCV/Ubuntu

# sudo sh dependencies.sh

# cd 2.4

# sudo sh opencv2_4_10.sh

 

2 Install 2.4.9 (not recommended)

This time is relatively long, to ensure the network connection, you need to download the installation package online, about 30 minutes, can't remember. .

 

The second method (manual installation, this time is successful.) For details, please refer to the attached " Installing opencv2.4.10 in ubuntu14.04 " .

 

( 5 ) Compile Caffe

 After the above basic environment is ready, download: https://github.com/BVLC/caffe , enter the caffe directory, and copy a copy of the Makefile.config command: cp Makefile.config.example Makefile.config.

Then, modify the corresponding content according to your environment:

   1 Uncomment before CPU_ONLY := 1.

   2. Change the cuda installation path to your own path: CUDA_DIR := /usr/local/cuda-6.5

   3. Configuration path: Implement caffe's support for python and matlab interfaces.

Here only configure the environment under the CPU so only modify CPU_ONLY := 1.

Next, you can compile Caffe:

1,sudo make all -j2

2,sudo make test

3,sudo make runtest

The screenshot after Make all is as follows:

 

The screenshot after Make test is as follows:

 

The screenshot after Make runtest is as follows:

 

( 6 ) Test whether Caffe is installed successfully

Take mnist as an example to check whether caffe is installed successfully. The following commands are run in the root directory of CAFFE:

1. Prepare the data

sh ./data/mnist/get_mnist.sh

sh ./examples/mnist/create_mnist.sh

2. Train the model:

sh ./examples/mnist/train_lenet.sh

Need to modify sudo vi lenet_solver.prototxt First enter the command mode, use a to enter the editing mode, after the modification, use esc to exit the editing mode, enter the last line mode, and then use ":wq" to save the modification and exit (":q!" is exit without saving changes):

# solver mode: CPU or GPU

solver_mode: CPU

3. Test the model:

sh ./examples/mnist/test_lenet.sh

New test_lenet.sh:

1. #!/usr/bin/env

2. "Blank Line"

3.

./build/tools/caffe test model=examples/mnist/lenet_train_test.prototxt weights=examples/mnist/lenet_iter_10000.caffemodel iterations 100

 

( 7 ) Configure Pycaffe

Step 1: Install the dependencies of the Python-caffe interface.

The first method (directly execute the package in the installation requirement, without success). Make sure that the previous: 1, sudo make all -j2; 2, sudo make test; 3, sudo make runtest, etc. are not wrong, if you want to try again after an error is reported in a line, it is recommended to make clean and then start again. Update installation dependencies.

cd caffe 

cat python/requirements.txt | xargs -L 1 sudo pip install 

Make sure you are using the python environment provided by anaconda, then enter caffe/python, and execute the following commands,

Make sure pip is installed

sudo apt-get install python-pip

for req in $(cat requirements.txt); do pip install $req; done #python runs the required libraries. 

Going back to the caffe root directory again we can execute:

In the python folder of the caffe root directory, there is a requirements.txt manifest file, which lists the required dependency libraries, and you can install it according to this list.

When installing the scipy library, the fortran compiler (gfortran) is required. If there is no such compiler, an error will be reported, so we can install it first.

Go back to the root directory of caffe and execute the installation code:

cd ~ / caffe

sudo apt-get install gfortran

cd ./python

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

After the installation is complete, go back to the caffe root directory again and we can execute:

sudo pip install -r python/requirements.txt

You will see that if the installation is successful, Requirement already satisfied will be displayed, and if the installation is not successful, the installation will continue.

 

The second method (direct installation, this time successful). According to the manual installation in Requirement.txt, execute the following command: 

sudo apt-get install Cython python-numpy python-scipy python-skimage python-matplotlib ipython python-h5py python-leveldb python-networkx python-nose python-pandas python-gflags python-protobuf python-gflags python-yaml 

sudo apt-get install python-sklearn  protobuf-c-compiler protobuf-compiler

The problem with make pycaffe is as follows:

 

 

Because there is a low version, it always reports an error. I expect to uninstall the low version and then upgrade, and then it can be achieved by the following methods:

sudo pip install six --upgrade --target="/usr/lib/python2.7/dist-packages" 
类似的:
sudo pip install leveldb --upgrade --target="/usr/lib/python2.7/dist-packages"
 

Step 2: Join PythonPath .

             The first method: add ~/caffe/python to $PYTHONPATH.

$ gedit ~/.bashrc #vim or gedit, open bashrc 

           export PYTHONPATH=/home/usrname/caffe/python:$PYTHONPATH #Add to the end of the file, I am export PYTHONPATH=/home/wp/caffe/python:$PYTHONPATH.

$ source ~/.bashrc #Make changes take effect. In this way, you can also import caffe by opening python elsewhere.

 

Second method: add ~/caffe/python to $PYTHONPATH.

$ sudo gedit /etc/profile

With sudo, only gedit /etc/profile cannot be saved after modification

# Add at the end: export PYTHONPATH=/path/to/caffe/python:$PYTHONPATH

Mine is: export PYTHONPATH=/home/wp/CAFEE/caffemaster/python:$PYTHONPATH

#export PYTHONPATH=The python path under your caffe folder must be your own path! ! !

# Use the full path, don't use ~

$ source /etc/profile #Make it effective

 

( 8 ) Test whether the Pycaffe interface is successfully configured

Compile the python interface: $ make pycaffe -j8

 

 

 

Du Niang, some say that this is the configuration, and some say that it is not well-configured, because the dependencies are not fully installed. I remember that it was configured before, and it seems that this is not the case. After five or six times of reconfiguration [configuration and problem solving are all the results, there is no alternative but not to affect the use. Go on.

 

Run python, enter caffe/python, run

$ python2.7

Python 2.7.12 (default, Jul  1 2016, 15:12:24)

[GCC 5.4.0 20160609] on linux2

Type "help", "copyright", "credits" or "license" for more information.

>>> import caffe

>>>

 

Test 1, execute in any path:

[code]$ python

Python 2.7.6 (default, Jun 22 2015, 17:58:13)

[GCC 4.8.2] on linux2

Type "help", "copyright", "credits" or "license" for more information.

>>> import caffe

>>> 

 

Test 2:

wp@ubuntu:~$ ipython

Python 2.7.6 (default, Oct 26 2016, 20:30:19)

Type "copyright", "credits" or "license" for more information.

IPython 1.2.1 -- An enhanced Interactive Python.

? -> Introduction and overview of IPython's features.

%quickref -> Quick reference.

help -> Python's own help system.

object? -> Details about 'object', use 'object??' for extra details.

In [1]: import caffe

In [2]:

Enter python, if it is displayed as shown in the red box, it means that the current python is the default python of the system. It means that the environment variable added just now does not work and needs to be activated with the command: source .bashrc If it is displayed as shown in the blue box, it means that the current python is python in Anaconda. Successful installation! For example the result:

 

 

appendix:

Specifically, install opencv2.4.10 in " ubuntu14.04 " .

Install index:

Part 1 - Installation

1. Download the source code 2.4.10

2. Unzip $unzip opencv-2.4.10.zip

3. Create the release directory

   $cd opencv-2.4.9

   $mkdir release

4. Install the dependency package (web search tutorial)

5. Go to the release folder

   $cd release

6. Compile and install (it takes a long time)

   $cmake

   $make all

   $make install

The second part --- test whether the installation is successful

1. Create a test.cpp file

2. Write a cmake makefile (CMakeLists.txt)

3. Compile + run + example test

The above can be referred to https://blog.csdn.net/wb4916/article/details/54021717.

The specific realization of the graphic process:

1. First download the source code of OpenCV. From sourceforge on http://jaist.dl.sourceforge.net/project/opencvlibrary/opencv-unix/2.4.10/opencv-2.4.10.zip . Or download from the official website https://opencv.org/releases.html .

 

2. Unzip to any directory.

Terminal command $ unzip opencv-2.4.10.zip, or extract directly.

 

3. Enter the source code directory and create a release directory

$ cd opencv-2.4.10

$ mkdir release

 

4. You can see that there is a CMakeLists.txt file in the OpenCV directory, and you need to install some software in advance

$ sudo apt-get install build-essential cmake libgtk2.0-dev pkg-config python-dev python-numpy libavcodec-dev libavformat-dev libswscale-dev

 

5. Enter the release directory and install OpenCV so that all files will be placed in this release directory

$ cd release

 

6. cmake compiles the OpenCV source code, and all lib files installed will be installed in the /usr/local directory

cmake -D CMAKE_BUILD_TYPE=RELEASE -D CMAKE_INSTALL_PREFIX=/usr/local ..

 

Check it out: wp@wp-ms-7519:~/opencv-2.4.10/release$ cmake --version

cmake version 2.8.12.2

 

7. Installation

$ sudo make install

 

8. Test, create a test.cpp file in a directory

#include <cv.h>

#include <highgui.h>

using namespace cv;

int main(int argc, char* argv[])

{

    Mat image;

    image = imread(argv[1], 1);

 

    if (argc != 2 || !image.data)

    {

        printf("No image data\n");

        return -1;

    }

 

    namedWindow("Display Image", CV_WINDOW_AUTOSIZE);

    imshow("Display Image", image);

    waitKey(0);

    return 0;

}

 

9. Write a cmake makefile, also called CMakeLists.txt

project(test)

find_package(OpenCV REQUIRED)

add_executable(test test)

target_link_libraries(test ${OpenCV_LIBS})

cmake_minimum_required(VERSION 2.8)

 

10. Compile + Run

(note that there is a dot after camke)

cmake .

make

 

In this way, an executable file test is obtained.

 

11. Find a xx.jpg image for a test, note that it should be placed in the same directory as the test executable file above, and the name here is test.jpg.

 

12. Execute $ ./test test.jpg under executable. If you can see the photo, it means success.

 

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=325126067&siteId=291194637