Install the CPU version of Caffe on Ubuntu

Original blog: [Doi Technical Team] (http://blog.doiduoyi.com/)
Link address: https://blog.doiduoyi.com/authors/1584446358138
Original intention: record the learning experience of an excellent Doi technical team

Preface#

Caffe is currently a more commonly used deep learning framework. The installation of this framework is not as simple as other mainstream frameworks. It can be installed directly using the pip command. It is more commonly installed using compilation. So write this article to record.

Install Caffe on Ubuntu

If the Ubuntu version is >= 17.04, you can install Caffe in the following way. Note that the Python 3 version is installed.

apt install caffe-cpu

If it is lower than this version, it is necessary to use the source code to compile. The author's system is 64-bit Ubuntu 16.04. The installation steps are introduced below, using Python 2.

Installation dependent environment##

First of all, we need to install the dependent environment. There are a lot of dependent environments, and we need to ensure that they are installed to avoid errors during compilation. If it has been installed before, there is no problem with repeating the command.

apt-get install libprotobuf-dev libleveldb-dev libsnappy-dev libopencv-dev libhdf5-serial-dev protobuf-compiler
apt-get install --no-install-recommends libboost-all-dev  
apt-get install libgflags-dev libgoogle-glog-dev liblmdb-dev
apt-get install libatlas-base-dev
apt-get install python-numpy
apt-get install libhdf5-serial-dev
apt-get install python-dev
apt install python-pip
pip install scikit-image

Modify the compiled file##

We use source code compilation, so we must clone the source code first, and then copy the official compilation configuration examples.

# Switch to the opt directory
cd /opt
# Clone caffe source code
git clone git://github.com/BVLC/caffe.git
# Cut into the source root directory
cd caffe/
# Copy the official compilation configuration file example
cp Makefile.config.example Makefile.config
# Start writing configuration information
vim Makefile.config

Modify this configuration file as follows:

CPU_ONLY :=1
# Whatever else you find you need goes here.
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

Start compiling##

Now you can start to compile. The first two are compiling source code, and the latter two are compiling tests. -j4 means using 4 threads to compile in parallel to speed up the compilation.

make -j4 pycaffe
make -j4 all
make -j4 test
make -j4 runtest

Add environment variables##

Use the command vim /etc/profile and add the following line of code at the end of the file.

export PYTHONPATH=/opt/caffe/python:$PYTHONPATH

We can simply test whether the installation is successful. If it is normal, the version information of caffe can be output.

# python
Python 2.7.12(default, Dec  42017,14:50:18)[GCC 5.4.020160609] on linux2
Type "help","copyright","credits" or "license"for more information.>>>import caffe
>>> caffe.__version__

The following information will be output:

'1.0.0'

Use model to predict pictures#

After installing caffe, we can use the model to predict the picture. The author is here download the caffe model. In the code below, we use the modelnet V2 model.

# coding=utf-8from __future__ import print_function
import numpy as np
import caffe

# Modify picture size
def get_img(img_path, nh, nw):
 im = caffe.io.load_image(img_path)
 h, w, _ = im.shape
 if h < w:
  off =(w - h)/2
  im = im[:, off:off + h]else:
  off =(h - w)/2
  im = im[off:off + h,:]
 im = caffe.io.resize_image(im,[nh, nw])return im

def eval(image_path, nh, nw):
 caffe.set_mode_cpu()
 # Load model
 net = caffe.Net("mobilenet_v2_deploy.prototxt","mobilenet_v2.caffemodel", caffe.TEST)

	#Set the way to preprocess the image
 transformer = caffe.io.Transformer({'data': net.blobs['data'].data.shape})
 transformer.set_transpose('data',(2,0,1))  # row to col
 transformer.set_channel_swap('data',(2,1,0))  # RGB to BGR
 transformer.set_raw_scale('data',255)  # [0,1] to [0,255]
 # Subtract the mean of the picture
 img_mean = np.array([103.94,116.78,123.68], dtype=np.float32)
 transformer.set_mean('data', img_mean)
 # Multiplied by a ratio
 transformer.set_input_scale('data',0.017)
 # Change picture dimensions
 net.blobs['data'].reshape(1,3, nh, nw)

 im =get_img(image_path, nh, nw)
 net.blobs['data'].data[...]= transformer.preprocess('data', im)
 out = net.forward()
 # Get the predicted result
 prob = out['prob']
 prob = np.squeeze(prob)
 idx = np.argsort(-prob)
 # Get the label with the greatest probability
 label = idx[0]
 # Read label file content
 label_names = np.loadtxt('synset.txt', str, delimiter='\t')print('%.5f - %s'%(prob[label], label_names[label]))if __name__ =='__main__':
 image_path ="cat.jpg"
 nh, nw =224,224eval(image_path, nh, nw)

The following function is to change the size of the image to meet the needs of the model. Note that if it is Python 3, the division must use // instead of /. This is a change of Python 3.

def get_img(img_path, nh, nw):
 im = caffe.io.load_image(img_path)
 h, w, _ = im.shape
 if h < w:
  off =(w - h)/2
  im = im[:, off:off + h]else:
  off =(h - w)/2
  im = im[off:off + h,:]
 im = caffe.io.resize_image(im,[nh, nw])return im

The following code is the definition file and weight file that specify the CPU and load model. These two files are downloaded from GitHub.

 caffe.set_mode_cpu()
 net = caffe.Net("mobilenet_v2_deploy.prototxt","mobilenet_v2.caffemodel", caffe.TEST)

The following code snippet defines the preprocessing method of the picture, such as changing the channel order, subtracting the mean value from each pixel, and multiplying each pixel by a ratio.

 transformer = caffe.io.Transformer({'data': net.blobs['data'].data.shape})
 transformer.set_transpose('data',(2,0,1))  # row to col
 transformer.set_channel_swap('data',(2,1,0))  # RGB to BGR
 transformer.set_raw_scale('data',255)  # [0,1] to [0,255]
 img_mean = np.array([103.94,116.78,123.68], dtype=np.float32)
 transformer.set_mean('data', img_mean)
 transformer.set_input_scale('data',0.017)
 net.blobs['data'].reshape(1,3, nh, nw)

The following code snippet is to load the picture to get the prediction result. The output of out[&#39;prob&#39;] is the probability of each label. The synset.txt used is also the file on GitHub just now. The content of this file is every The name corresponding to each label. The final output is the probability corresponding to the label with the highest probability and the name corresponding to this label.

 im =get_img(image_path, nh, nw)
 net.blobs['data'].data[...]= transformer.preprocess('data', im)
 out = net.forward()
 prob = out['prob']
 prob = np.squeeze(prob)
 idx = np.argsort(-prob)
 label = idx[0]
 label_names = np.loadtxt('synset.txt', str, delimiter='\t')print('%.5f - %s'%(prob[label], label_names[label]))

The output is as follows:

0.25784- ' n02123159 tiger cat'

References#

  1. http://caffe.berkeleyvision.org/installation.html
  2. https://blog.csdn.net/qq_25737169/article/details/77773884
  3. https://github.com/shicai/MobileNet-Caffe

Recommended Posts

Install the CPU version of Caffe on Ubuntu
ubuntu16.04 install caffe cpu version
Install the old version of seurat under Ubuntu
Install TensorFlow (python2.7 version) on Ubuntu
Centos install the latest version of cmake
Install the flat theme latabulous on Ubuntu 16.04
Install NeoVim on Ubuntu: the easiest way
Install the latest version of Thunderbird and exchange plugins under ubuntu
install vscode on ubuntu18
Install nvtop on Ubuntu 18.04
Install postgresql-10 on Ubuntu 18.04
Install docker on Ubuntu
Solve the problem of installing VMwareTools on Ubuntu 18.04
Install Caffe under Ubuntu 14.04
Install Docker on ubuntu18.04
Install nodejs10 on Ubuntu16
Install mysql on Ubuntu 14.04
Install Django on ubuntu
Install Pytorch+CUDA on Ubuntu 16.04
Install Python3 on Ubuntu 14.04
Install rJava on Ubuntu18
Install JDK10+ on Ubuntu
Install Python3 on Ubuntu 16.04
The ultimate guide to install gooderp on ubuntu
Install KDE on Ubuntu16.04.2
Install Docker on Ubuntu18
The latest method of installing Mongodb on Ubuntu 16.04
Install Python3.7 on Ubuntu
Install flashplayer on Ubuntu
Solve the problem of installing Theano on Ubuntu19
How to install the latest MySQL on Ubuntu 18.04
The default installation path of apt-get install of ubuntu (transfer)
Install the Ethereum client Mist on windows and ubuntu
Docker practice (1): install Docker on Ubuntu 16.04
Install ubuntu on virtual machine
Install OpenSSL 1.0.2 on Ubuntu Server 14.04
Install QQ robot on Ubuntu 16.04
Ubuntu install the latest Python 3.
Install Python 3.7 on Ubuntu 18.04 LTS
Install Android Studio on Ubuntu
Install scrapy framework on ubuntu
Install Mysql offline on Ubuntu
Install Oracle Java8 on Ubuntu
Install Odoo 11 stack on Ubuntu 16.04
Install Go locale on Ubuntu16
Install common software on Ubuntu 16.04
Install non-portable QQ on ubuntu
Install Docker on Ubuntu 18.04 offline
[Share] Solve the problem of failed installation of Vitis 2019.2 on Ubuntu 16.04
Install and configure the latest version of RabbitMQ-3.8.5 (with video)
The screen is blank after upgrading the new version of ubuntu
How to install Ruby on Ubuntu 20.04
How to install Memcached on Ubuntu 20.04
How to install Java on Ubuntu 20.04
How to install MySQL on Ubuntu 20.04
How to install VirtualBox on Ubuntu 20.04
How to install Elasticsearch on Ubuntu 20.04
How to install Protobuf 3 on Ubuntu
How to install Nginx on Ubuntu 20.04
How to install Apache on Ubuntu 20.04
How to install Git on Ubuntu 20.04