01 系统初始化
vmware workstatiion pro 12 不支持GPU硬件虚拟化,所以在这个虚拟机里,不能安装GPU版本。只能使用CPU版本。
# 在vmware workstatiion pro 12安装ubuntu-16.04-desktop-amd64.iso
# 安装vmware-tools
# 调整分辨率,4k屏需要调整下桌面显示比例,否则字体太小
# 更新系统
sudo apt-get -y update && sudo apt-get -y upgrade
# 我的网速比较慢,夜里更新的,第二天又执行了一遍
sudo apt-get -y update && sudo apt-get -y upgrade
# 更新系统后,gcc -v 看一下,版本是5.4.0
# 安装vim git,个人经常使用vim,git操作源码必须工具
sudo apt-get install -y vim git
# 安装sougou拼音输入法,不是必须的
# 下载搜狗拼音输入法http://pinyin.sogou.com/linux/download.php?f=linux&bit=64
# 保存到 /home/username/Downloads/sogoupinyin_2.1.0.0082_amd64.deb
# 安装 sudo pkdg -i sogoupinyin_2.1.0.0082_amd64.deb
# 安装依赖 sudo apt-get install -f
# 配置搜狗输入法
# 制作系统快照,snapshot_install01
02 安装caffe CPU_ONLY依赖库
# 安装caffe必备组件,因为需要编译版本,所以安装的组件基本都是开发版本
sudo apt-get install libprotobuf-dev libleveldb-dev libsnappy-dev libopencv-dev libhdf5-serial-dev protobuf-compiler
# 这里并没有指定明确的boost版本
sudo apt-get install --no-install-recommends libboost-all-dev
# 基本线性代数子程序库(矩阵、向量计算)
sudo apt-get install libatlas-base-dev
# python开发库
sudo apt-get install python-dev
# gflags:参数解析;glog:google日志库;lmdb:key-value数据库
sudo apt-get install libgflags-dev libgoogle-glog-dev liblmdb-dev
03 下载代码
git clone https://github.com/bvlc/caffe.git
cd caffe
git tag
git branch -a
# 注意,如果编译最新的rc5,pycaffe可能需要调整代码
git checkout -b myrc4 rc4
git branch -vv
04 编译CPU_ONLY版本
04.01 编辑Makefile.config文件
cp Makefile.config.example Makefile.config
04.02 设置使用CPU模式
# 修改Makefile.config的CPU_ONLY配置项,取消CPU_ONLY前面的注释#符号
04.03 修改Makefile.config的hdf5头文件引用
# 修改Makefile.config
# 在INCLUDE_DIRS后面加上/usr/include/hdf5/serial/
# 否则,找不到hdf5.h
INCLUDE_DIRS := $(PYTHON_INCLUDE) /usr/local/include /usr/include/hdf5/serial/
04.04 修改Makefile文件hdf5的库文件
# 修改Makefile文件的LIBRARIES中的hdf5_hl hdf5 ==> hdf5_serial_hl hdf5_serial
# LIBRARIES += glog gflags protobuf boost_system boost_filesystem m hdf5_hl hdf5
# LIBRARIES += glog gflags protobuf boost_system boost_filesystem m hdf5_serial_hl hdf5_serial
# 否则会找不到 hdf5_hl hdf5 库
# /usr/bin/ld: cannot find -lhdf5_hl
# /usr/bin/ld: cannot find -lhdf5
LIBRARIES += glog gflags protobuf boost_system boost_filesystem m hdf5_serial_hl hdf5_serial
04.05 编译
make
# 注意,这里不能make install
# 否则,会提示:make: *** No rule to make target 'install'. Stop.
# 在这里caffe命令行接口可用,python接口,matlab接口都不可用
05 caffe 命令行接口测试案例(CPU_ONLY)
05.01 mnist数据测试
./data/mnist/get_mnist.sh
tree ./data/mnist/
./examples/mnist/create_mnist.sh
***********************************************************
soft@ubuntu:~/caffe$ ls -l examples/mnist/mnist_train_lmdb/
total 60320
-rw-rw-r-- 1 soft soft 61763584 3月 22 12:33 data.mdb
-rw-rw-r-- 1 soft soft 8192 3月 22 12:33 lock.mdb
soft@ubuntu:~/caffe$ ls -l examples/mnist/mnist_test_lmdb/
total 10100
-rw-rw-r-- 1 soft soft 10338304 3月 22 12:33 data.mdb
-rw-rw-r-- 1 soft soft 8192 3月 22 12:33 lock.mdb
***********************************************************
# 注释掉 examples/mnist/lenet_solver.prototxt 中 solver_mode: GPU
# # solver_mode: GPU # 注释掉默认使用CPU
# 训练minist数据
./build/tools/caffe train --solver=examples/mnist/lenet_solver.prototxt
# 用训练好的模型对数据进行预测
./build/tools/caffe.bin test -model examples/mnist/lenet_train_test.prototxt -weights examples/mnist/lenet_iter_10000.caffemodel -iterations 100
05.02 cifar10 数据测试
05.02.01下载测试数据
./data/cifar10/get_cifar10.sh
# 查看下载情况
tree ./data/cifar10/
05.02.02 训练数据
# 生成lmdb数据库
./examples/cifar10/create_cifar10.sh
# 设置CPU训练模式
vim ./examples/cifar10/cifar10_quick_solver.prototxt
# 注释掉默认的GPU模式,solver_mode: GPU
# solver_mode: GPU
vim ./examples/cifar10/cifar10_quick_solver_lr1.prototxt
# 注释掉默认的GPU模式,solver_mode: GPU
# solver_mode: GPU
# 训练
./examples/cifar10/train_quick.sh
06 pycaffe接口编译
06.01 安装依赖库
sudo apt-get update
sudo apt-get install python-pip python-dev python-numpy
sudo apt-get install gfortran
sudo pip install pydot graphviz
sudo pip install lmdb
根据caffe\python\requirements.txt内容,安装python接口依赖项。
如果一次安装失败,多试几次。不需翻墙,很可能需要多次尝试。
其中protobuf建议安装2.6.1版本。 如果编译时出现如下错误: 如果有关于protubuf的错误提示,可能是版本不匹配
AttributeError: ‘int’ object has no attribute ‘_values’
或者
ImportError: cannot import name symbol_database
使用 dpkg -l protobuf* 看看相关的版本号
soft@ubuntu:~/caffe$ dpkg -l protobuf*
||/ Name Version Architecture Description
+++-==============-============-============
ii protobuf-compi 2.6.1-1.3 amd64 compiler for protocol buffer defi可以根据上面的版本号 2.6.1 安装指定的protobuf版本
卸载当前版本protobuf
sudo pip uninstall protobuf
安装指定版本号的protobuf
sudo pip install protobuf=2.6.1
caffe\python\requirements.txt内容:
Cython>=0.19.2
numpy>=1.7.1
scipy>=0.13.2
scikit-image>=0.9.3
matplotlib>=1.3.1
ipython>=3.0.0
h5py>=2.2.0
leveldb>=0.191
networkx>=1.8.1
nose>=1.3.0
pandas>=0.12.0
python-dateutil>=1.4,<2
protobuf>=2.5.0
python-gflags>=2.0
pyyaml>=3.10
Pillow>=2.3.0
six>=1.1.0
安装caffe\python\requirements.txt指定的pycaffe依赖包
sudo pip install cython
sudo pip install numpy
sudo pip install scipy
sudo pip install scikit-image
sudo pip install matplotlib
sudo pip install ipython
sudo pip install h5py
sudo pip install leveldb
sudo pip install networkx
sudo pip install nose
sudo pip install pandas
sudo pip install python-dateutil
# sudo pip install protobuf # 默认3.2,caffe rc4不支持
sudo pip install protobuf==2.6.1
sudo pip install python-gflags
sudo pip install pillow
sudo pip install six
06.02 编译pycaffe
为了支持python自定义层,开启Makefile.config选项 (删除这一行前面的#注释符)
WITH_PYTHON_LAYER := 1
在caffe根目录下运行编译命令:
make clean
make all
make pycaffe
编译成功后,把pycaffe的目录加入ubuntu的环境变量
可以加入3个文件中的一个:~/.bashrc;~/.profile;/etc/profile
本实验加入~/.profile;
sudo vim ~/.profile
export PYTHONPATH=/home/soft/caffe/python:$PYTHONPATH
source ~/.profile # 使加入的内容立即生效
06.03 验证pycaffe接口编译成功
A:python中引用caffe库不报错,如下图:
import caffe;
caffe.set_mode_cpu();
B:用caffe的python接口画loss曲线
这里使用spyder python工具。安装spyder。
sudo apt-get install spyder
在caffe根目录下,编辑python文件 /home/soft/caffe/loss.py
import sys,os;
import numpy as np;
import matplotlib.pyplot as plt;
#caffe_root='home/soft/caffe/build/';
#sys.path.insert(0, caffe_root+'python');
import caffe;
caffe.set_mode_cpu();
solver = caffe.SGDSolver('examples/mnist/lenet_solver.prototxt');
niter = 1000;
test_interval = 200;
train_loss = np.zeros(niter);
test_acc = np.zeros(int(np.ceil(niter / test_interval)));
# the main solver loop
for it in range(niter):
solver.step(1) # SGD by Caffe
train_loss[it] = solver.net.blobs['loss'].data;
solver.test_nets[0].forward(start = 'conv1');
if it % test_interval == 0:
acc = solver.test_nets[0].blobs['accuracy'].data;
print('Iteration', it, 'testing...', 'accuracy:', acc);
test_acc[it // test_interval] = acc;
_, ax1 = plt.subplots();
ax2 = ax1.twinx();
ax1.plot(np.arange(niter), train_loss);
ax2.plot(test_interval * np.arange(len(test_acc)), test_acc, 'r');
ax1.set_xlabel('iteration');
ax1.set_ylabel('train loss');
ax2.set_ylabel('test accuracy');
plt.show();
运行结果:
07 编译matcaffe接口
07.01 安装matlab2016b和配置matlab2016b环境
主要参考如下3个blog,安装和配置matlab2016b
http://www.linuxdiyf.com/linux/27432.html
http://blog.csdn.net/rt5rte54654/article/details/54742981
http://www.cnblogs.com/laiqun/p/6031925.html
# 设置matlab的环境变量是必须的
sudo vim ~/.profile
export PATH=/usr/local/MATLAB/R2016b/bin:$PATH
sudo vim /home/soft/caffe/Makefile.config
MATLAB_DIR := /usr/local/MATLAB/R2016b
根据http://www.cnblogs.com/laiqun/p/6031925.html
在caffe根目录下下一个启动matlab的脚本/home/soft/caffe/matlab.sh
export LD_LIBRARY_PATH=/usr/lib/x86_64-linux-gnu/
LD_PRELOAD=/usr/lib/x86_64-linux-gnu/libopencv_highgui.so.2.4:/usr/lib/x86_64-linux-gnu/libopencv_imgproc.so.2.4:/usr/lib/x86_64-linux-gnu/libopencv_core.so.2.4:/usr/lib/x86_64-linux-gnu/libstdc++.so.6:/usr/lib/x86_64-linux-gnu/libfreetype.so.6 matlab
在caffe目录下,运行 ./matlab.sh
,即可启动matlab2016b。
本次实验,matlab2016b安装在 /usr/local/MATLAB/R2016b/
可以在命令行通过如下语句启动matlab2016测试,运行matcaffe接口需要用./matlab.sh
启动。
sudo /usr/local/MATLAB/R2016b/bin/matlab
07.02 测试,运行一些可视化测试用例
07.02.01 cifar10 数据可视化
需要前面先下载cifar10的实验数据(./data/cifar10/get_cifar10.sh)
matlab代码:/home/soft/caffe/show_cifar10_data.m
% /home/soft/caffe/show_cifar10_data.m
clear;
clc;
close all;
strings = {
'airplane'
'automobile'
'bird'
'cat'
'deer'
'dog'
'frog'
'horse'
'ship'
'truck'
};
image_file_name = 'data/cifar10/data_batch_1.bin';
fid1 = fopen(image_file_name, 'rb');
images_data = fread(fid1, 'uint8');
fclose(fid1);
images_data = reshape(images_data, 3073, [])';
image_idx = images_data(:, 1);
for k = 1: 100 : size(images_data, 1)
figure(100);
for t = 1 : 100
image_r = reshape(images_data(k + t - 1, 2: 1025), 32, [])';
image_g = reshape(images_data(k + t - 1, 1026 : 2049), 32, [])';
image_b = reshape(images_data(k + t - 1, 2050 : 3073), 32, [])';
image_buffer = cat(3, image_r, image_g, image_b);
subplot(10, 10, t);
imshow(uint8(image_buffer));
title(strings{image_idx(k + t - 1) + 1});
end
input('Press Enter to next picture :');
pause;
end
运行结果:
07.02.02 mnist数据可视化
需要前面先下载mnist的实验数据(./data/mnist/get_mnist.sh)
matlab代码:/home/soft/caffe/show_mnist_data.m
% /home/soft/caffe/show_mnist_data.m
clear;
clc;
close all;
image_file_name = '/home/soft/caffe/data/mnist/t10k-images-idx3-ubyte';
index_file_name = '/home/soft/caffe/data/mnist/t10k-labels-idx1-ubyte';
fid1 = fopen(image_file_name, 'rb');
fid2 = fopen(index_file_name, 'rb');
images_data = fread(fid1, 'uint8');
indexs_data = fread(fid2, 'uint8');
fclose(fid1);
fclose(fid2);
images_data = images_data(17:end);
indexs_data = indexs_data(9:end);
image_buffer = zeros(28, 28);
for k = 1 : 100: length(images_data)/28/28
figure(100);
for t = 1 : 100
image_buffer = reshape(images_data((k + t - 2) * 28 * 28 + 1 : (k + t - 1) * 28 * 28), 28, 28);
subplot(10, 10, t);
imshow(uint8(image_buffer)');
title(num2str(indexs_data(k + t -1)));
end
pause;
end
运行结果:
07.02.03 网络权值可视化
A:Caffenet第一卷积层可视化
matlab代码 /home/soft/caffe/conv_weights_vis.m
% /home/soft/caffe/conv_weights_vis.m
clear;
clc;
close all;
addpath('matlab');
caffe.set_mode_cpu();
caffe.version();
net = caffe.Net('models/bvlc_reference_caffenet/deploy.prototxt', 'models/bvlc_reference_caffenet/bvlc_reference_caffenet.caffemodel', 'test');
net.layer_names
net.blob_names
conv1_layer = net.layer_vec(2);
blob1 = conv1_layer.params(1);
w = blob1.get_data();
size(w)
W = zeros(11*3, 11*96);
for u = 1: 3
for v = 1: 96
W(11 * ( u - 1) + (1 : 11), 11 * (v - 1) + (1: 11)) = w(:, :, u, v)';
end
end
W = W - min(min(W));
W = W / (max(max(W))) * 255;
W = uint8(W);
W = [W, zeros(size(W, 1), 4 * 11)];
WW = cat(3, W(1:11, :), W(12:22, :), W(23:33, :));
W = zeros(10 * 12, 10 * 12, 3);
for u = 1: 10
for v = 1: 10
W((u - 1)*12 + (1:11), (v - 1) * 12 + (1:11), :) = WW(:, (u - 1) * 11 * 10 + (v - 1) * 11 + (1: 11), :);
end
end
W = uint8(W);
figure;imshow(W);
运行结果:
B CaffeNet 各层权值提取
/home/soft/caffe/visualize_weights.m
% /home/soft/caffe/visualize_weights.m
function [] = visualize_weights(w, s)
h = max(size(w, 1), size(w, 2)); % Kernel size
g = h + s; % Grid size, larger than Kernel size for better visual effects.
% Normalization for gray scale
w = w - min(min(min(min(w))));
w = w / max(max(max(max(w)))) * 255;
w = uint8(w);
W = zeros(g * size(w, 3), g * size(w, 4));
for u = 1:size(w, 3)
for v = 1: size(w, 4)
W(g * (u - 1) + (1: h), g * (v - 1) + (1:h)) = w(:, :, u, v)';
end
end
W = uint8(W);
/home/soft/caffe/caffenet_weights_vis.m
% /home/soft/caffe/caffenet_weights_vis.m
clear;
clc;
close all;
addpath('matlab');
caffe.set_mode_cpu();
fprintf(['Caffe Version = ', caffe.version(), '\n']);
net = caffe.Net('models/bvlc_reference_caffenet/deploy.prototxt', 'models/bvlc_reference_caffenet/bvlc_reference_caffenet.caffemodel', 'test');
fprintf('Load net done. Net layers : ');
net.layer_names
fprintf('Net blobs : ');
net.blob_names
% Conv1 Weight Visualizetion
conv1_layer = net.layer_vec(2);
blob1 = conv1_layer.params(1);
w1 = blob1.get_data();
fprintf('Conv1 Weight shape: ');
size(w1)
visualize_weights(w1, 1);
%Conv2 Weight Visualizetion
conv2_layer = net.layer_vec(6);
blob2 = conv2_layer.params(1);
w2 = blob2.get_data();
fprintf('Conv2 Weight shape: ');
size(w2)
visualize_weights(w2, 1);
%Conv3 Weight Visualizetion
conv3_layer = net.layer_vec(10);
blob3 = conv3_layer.params(1);
w3 = blob3.get_data();
fprintf('Conv3 Weight shape: ');
size(w3)
visualize_weights(w3, 1);
%Conv3 Weight Visualizetion
conv3_layer = net.layer_vec(10);
blob3 = conv3_layer.params(1);
w3 = blob3.get_data();
fprintf('Conv3 Weight shape: ');
size(w3)
visualize_weights(w3, 1);
%Conv4 Weight Visualizetion
conv4_layer = net.layer_vec(10);
blob4 = conv4_layer.params(1);
w4 = blob4.get_data();
fprintf('Conv4 Weight shape: ');
size(w4)
visualize_weights(w4, 1);
%Conv5 Weight Visualizetion
conv5_layer = net.layer_vec(14);
blob5 = conv5_layer.params(1);
w5 = blob5.get_data();
fprintf('Conv5 Weight shape: ');
size(w5)
visualize_weights(w5, 1);
07.02.04 特征图可视化
根据/home/soft/caffe/models/bvlc_reference_caffenet/readme.md介绍,下载Caffe Model Zoo 中的caffemodel。
cd models/bvlc_reference_caffenet/
wget http://dl.caffe.berkeleyvision.org/bvlc_reference_caffenet.caffemodel
/home/soft/caffe/visualize_feature_maps.m
% /home/soft/caffe/visualize_feature_maps.m
function [] = visualize_feature_maps(w, s)
h = max(size(w, 1), size(w, 2)); % Feature map size
g = h + s;
c = size(w, 3);
cv = ceil(sqrt(c));
W = zeros(g * cv, g * cv);
for u = 1: cv
for v = 1: cv
tw = zeros(h, h);
if (((u - 1) * cv + v) <= c)
tw = w(:, :, (u - 1) * cv + v, 1)';
tw = tw - min(min(tw));
tw = tw / max(max(tw)) * 255;
end
W(g * (u - 1) + (1: h), g * (v -1) + (1:h)) = tw;
end
end
W = uint8(W);
figure;imshow(W);
/home/soft/caffe/fm_visual.m
% /home/soft/caffe/fm_visual.m
clear;
clc;
close all;
% /home/soft/caffe/matlab
mat_fullname = mfilename('fullpath');
i = strfind(mat_fullname,'/');
work_path=mat_fullname(1: i(end));
userpath(work_path);
cd(work_path);
addpath('matlab');
caffe.set_mode_cpu();
fprintf(['Caffe Version = ', caffe.version(), '\n']);
net = caffe.Net('models/bvlc_reference_caffenet/deploy.prototxt', 'models/bvlc_reference_caffenet/bvlc_reference_caffenet.caffemodel', 'test');
fprintf('Load net done. Net layers : ');
net.layer_names
fprintf('Net blobs : ');
net.blob_names
fprintf('Now preparing data...\n');
im = imread('examples/images/cat.jpg');
figure;imshow(im);title('Original Image');
d = load('matlab/+caffe/imagenet/ilsvrc_2012_mean.mat');
mean_data = d.mean_data;
IMAGE_DIM = 256;
CROPPED_DIM = 227;
% Convert an image returned by Matlab's imread to im_data in caffe's data
% format: W X H X C with BGR channels
im_data = im(:, :, [3, 2, 1]); % permute channels from RGB to BGR
im_data = permute(im_data, [2, 1, 3]); % flip width and height
im_data = single(im_data); % convert from uint8 to single
im_data = imresize(im_data, [IMAGE_DIM IMAGE_DIM], 'bilinear'); % resize im_data
im_data = im_data - mean_data; % subtract mean_data (already in W X H X C, BGR)
im = imresize(im_data, [CROPPED_DIM CROPPED_DIM], 'bilinear'); % resize im_data
km = cat(4, im, im, im, im, im);
pm = cat(4, km, km);
input_data = {pm};
scores = net.forward(input_data);
scores = scores{1};
scores = mean(scores, 2); % take average scores over 10 crops
[~, maxlabel] = max(scores);
maxlabel
figure;plot(scores);
fm_data = net.blob_vec(1);
dl = fm_data.get_data();
fprintf('Data size = ');
size(dl)
visualize_feature_maps(dl, 1);
fm_conv1 = net.blob_vec(2);
f1 = fm_conv1.get_data();
fprintf('Feature map conv1 size = ');
size(f1)
visualize_feature_maps(f1,1);
fm_conv2 = net.blob_vec(5);
f2 = fm_conv2.get_data();
fprintf('Feature map conv2 size = ');
size(f2)
visualize_feature_maps(f2,1);
fm_conv3 = net.blob_vec(8);
f3 = fm_conv3.get_data();
fprintf('Feature map conv3 size = ');
size(f3)
visualize_feature_maps(f3,1);
fm_conv4 = net.blob_vec(9);
f4 = fm_conv4.get_data();
fprintf('Feature map conv4 size = ');
size(f4)
visualize_feature_maps(f4,1);
fm_conv5 = net.blob_vec(10);
f5 = fm_conv5.get_data();
fprintf('Feature map conv5 size = ');
size(f5)
visualize_feature_maps(f5,1);