caffe 版本有GPU 、CPU版本,本文为虚拟机上面搭建的CPU版本,采用ubuntu16.4系统,opencv3.4版本。

CPU版本不需要cuda 等。

安装模块如下:

1. 依赖lib安装

2.opencv3.4安装

3.安装caffe

4.pycaffe安装

5.验证


安装步骤如下:

1. 各种依赖包安装

感觉不止这多,包括opencv 、lmdb等

sudo apt-get install libprotobuf-dev libleveldb-dev libsnappy-dev libopencv-dev libhdf5-serial-dev protobuf-compiler

sudo apt-get install --no-install-recommends libboost-all-dev

sudo apt-get install libopenblas-dev liblapack-dev libatlas-base-dev

sudo apt-get install libgflags-dev libgoogle-glog-dev liblmdb-dev

sudo apt-get install git cmake build-essential
sudo apt-get install python-dev python-numpy libavcodec-dev libavformat-dev libswscale-dev

获取opencv包,官网获取最新包

caffe-master 官网获取最新

2. 安装opencv3.4

cd 到opencv-3.4.0

mkdir build

cd build

cmake ..

make

make install

配置opencv的环境变量

vi /etc/profile

export PKG_CONFIG_PATH=$PKG_CONFIG_PATH:/usr/local/lib/pkgconfig

export LD_LIBRARY_PATH=$LD_LIBRARY_PATH:/usr/local/lib

source /etc/profile

安装完毕,检查是否安装成功

pkg-config --libs --cflags opencv

-I/usr/local/include/opencv -I/usr/local/include -L/usr/local/lib -lopencv_dnn -lopencv_ml -lopencv_objdetect -lopencv_shape -lopencv_stitching -lopencv_superres -lopencv_videostab -lopencv_calib3d -lopencv_features2d -lopencv_highgui -lopencv_videoio -lopencv_imgcodecs -lopencv_video -lopencv_photo -lopencv_imgproc -lopencv_flann -lopencv_core

安装成功。


3.安装caffe

解压缩到/usr/local/caffe目录

cp Makefile.config.example Makefile.config

修改Makefile.config,以下为修改项,其他的都注释掉了。

CPU_ONLY := 1

USE_OPENCV := 1
USE_LEVELDB := 1
USE_LMDB := 1

BLAS := open

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

PYTHON_LIB := /usr/lib

WITH_PYTHON_LAYER := 1

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 /usr/lib/x86_64-linux-gnu/hdf5/serial

BUILD_DIR := build
DISTRIBUTE_DIR := distribute

修改Makefile

INCLUDE_DIRS += $(BUILD_INCLUDE_DIR) ./src ./include /usr/include/hdf5/serial

LIBRARIES += glog gflags protobuf boost_system boost_filesystem m hdf5_serial_hl hdf5_serial \
lmdb\
opencv_imgcodecs  opencv_core opencv_highgui opencv_imgproc opencv_imgcodecs

修改完毕

make all

make runtest

caffe 安装完毕。

当然编译caffe 遇到不少坑,不要慌,关键是修改Makefile.config、Makefile 这两个文件,选择需要的依赖文件和库。


4. 安装pycaffe

sudo apt-get install gfortran

ce /usr/local/caffe/python

安装pycaffe 需要的库

pip install -r requirements.txt

配置/etc/profile

添加pycaffe

export PYTHONPATH="/usr/local/caffe/python"

source /etc/profile

安装完毕。

执行python

import caffe 能正常加载代表安装成功。


5. caffe 验证

minist手写数据库验证

cd /usr/local/caffe/ 目录

执行 获取库,可能有点慢。

./data/mnist/get_mnist.sh

生成以下四个文件

t10k-images-idx3-ubyte

t10k-labels-idx1-ubyte

train-images-idx3-ubyte

train-labels-idx1-ubyte

数据存放到lmdb库中

执行

./examples/mnist/create_mnist.sh

在/usr/local/caffe/examples/mnist 目录下面生成两个lmdb库

mnist_test_lmdb

mnist_train_lmdb

接下来进行训练测试

修改/usr/local/caffe/examples/mnist/lenet_solver.prototxt

默认是GPU模式

solver_mode: CPU

执行

./examples/mnist/train_lenet.sh 

可能需要耗时10分钟训练完毕

生成四个文件

lenet_iter_10000.caffemodel

lenet_iter_10000.solverstate

lenet_iter_5000.caffemodel

lenet_iter_5000.solverstate

日志打印

I1227 16:23:15.944425 26074 solver.cpp:447] Snapshotting to binary proto file examples/mnist/lenet_iter_10000.caffemodel
I1227 16:23:15.954344 26074 sgd_solver.cpp:273] Snapshotting solver state to binary proto file examples/mnist/lenet_iter_10000.solverstate
I1227 16:23:15.979086 26074 solver.cpp:310] Iteration 10000, loss = 0.00299525
I1227 16:23:15.979142 26074 solver.cpp:330] Iteration 10000, Testing net (#0)
I1227 16:23:18.947849 26079 data_layer.cpp:73] Restarting data prefetching from start.
I1227 16:23:19.070807 26074 solver.cpp:397]     Test net output #0: accuracy = 0.9903
I1227 16:23:19.071033 26074 solver.cpp:397]     Test net output #1: loss = 0.029476 (* 1 = 0.029476 loss)
I1227 16:23:19.071115 26074 solver.cpp:315] Optimization Done.
I1227 16:23:19.071183 26074 caffe.cpp:259] Optimization Done.


安装完毕,祝你安装顺利。






Logo

华为开发者空间,是为全球开发者打造的专属开发空间,汇聚了华为优质开发资源及工具,致力于让每一位开发者拥有一台云主机,基于华为根生态开发、创新。

更多推荐