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
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.
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.
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
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
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
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'
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['prob']
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'
Recommended Posts