官網(wǎng)也有提供demo
http://nbviewer./github/BVLC/caffe/blob/master/examples/net_surgery.ipynb
本文整理了pycaffe中常用的API
Packages導(dǎo)入
1
2
3
|
import caffe
from caffe import layers as L
from caffe import params as P
|
Layers定義
Data層定義
lmdb/leveldb Data層定義
1
2
3
4
5
6
7
8
9
10
|
L.Data(
source=lmdb,
backend=P.Data.LMDB,
batch_size=batch_size, ntop=2,
transform_param=dict(
crop_size=227,
mean_value=[104, 117, 123],
mirror=True
)
)
|
HDF5 Data層定義
1
2
3
4
5
6
7
8
9
|
L.HDF5Data(
hdf5_data_param={
'source': './training_data_paths.txt',
'batch_size': 64
},
include={
'phase': caffe.TRAIN
}
)
|
ImageData Data層定義
適用于txt文件一行記錄一張圖片的數(shù)據(jù)源
1
2
3
4
5
6
7
8
|
L.ImageData(
source=list_path,
batch_size=batch_size,
new_width=48,
new_height=48,
ntop=2,
ransform_param=dict(crop_size=40,mirror=True)
)
|
Convloution層定義
1
2
3
4
5
6
7
8
|
L.Convolution(
bottom,
kernel_size=ks,
stride=stride,
num_output=nout,
pad=pad,
group=group
)
|
LRN層定義
1
2
3
4
5
6
|
L.LRN(
bottom,
local_size=5,
alpha=1e-4,
beta=0.75
)
|
Activation層定義
ReLU層定義
1
2
3
4
|
L.ReLU(
bottom,
in_place=True
)
|
Pooling層定義
1
2
3
4
5
6
|
L.Pooling(
bottom,
pool=P.Pooling.MAX,
kernel_size=ks,
stride=stride
)
|
FullConnect層定義
1
2
3
4
|
L.InnerProduct(
bottom,
num_output=nout
)
|
Dropout層定義
1
2
3
4
|
L.Dropout(
bottom,
in_place=True
)
|
Loss層定義
1
2
3
4
|
L.SoftmaxWithLoss(
bottom,
label
)
|
Accuracy層定義
1
2
3
4
|
L.Accuracy(
bottom,
label
)
|
轉(zhuǎn)換為proto文本
1
2
3
4
|
caffe.to_proto(
loss,
acc #訓(xùn)練階段可以刪去Accuracy層
)
|
Solver定義
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
|
from caffe.proto import caffe_pb2
s = caffe_pb2.SolverParameter()
path='/home/xxx/data/'
solver_file=path+'solver.prototxt' #solver文件保存位置
s.train_net = path+'train.prototxt' # 訓(xùn)練配置文件
s.test_net.append(path+'val.prototxt') # 測試配置文件
s.test_interval = 782 # 測試間隔
s.test_iter.append(313) # 測試迭代次數(shù)
s.max_iter = 78200 # 最大迭代次數(shù)
s.base_lr = 0.001 # 基礎(chǔ)學(xué)習(xí)率
s.momentum = 0.9 # momentum系數(shù)
s.weight_decay = 5e-4 # 權(quán)值衰減系數(shù)
s.lr_policy = 'step' # 學(xué)習(xí)率衰減方法
s.stepsize=26067 # 此值僅對step方法有效
s.gamma = 0.1 # 學(xué)習(xí)率衰減指數(shù)
s.display = 782 # 屏幕日志顯示間隔
s.snapshot = 7820
s.snapshot_prefix = 'shapshot'
s.type = “SGD” # 優(yōu)化算法
s.solver_mode = caffe_pb2.SolverParameter.GPU
with open(solver_file, 'w') as f:
f.write(str(s))
|
Model訓(xùn)練
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
|
# 訓(xùn)練設(shè)置
# 使用GPU
caffe.set_device(gpu_id) # 若不設(shè)置,默認(rèn)為0
caffe.set_mode_gpu()
# 使用CPU
caffe.set_mode_cpu()
# 加載Solver,,有兩種常用方法
# 1. 無論模型中Slover類型是什么統(tǒng)一設(shè)置為SGD
solver = caffe.SGDSolver('/home/xxx/data/solver.prototxt')
# 2. 根據(jù)solver的prototxt中solver_type讀取,,默認(rèn)為SGD
solver = caffe.get_solver('/home/xxx/data/solver.prototxt')
# 訓(xùn)練模型
# 1.1 前向傳播
solver.net.forward() # train net
solver.test_nets[0].forward() # test net (there can be more than one)
# 1.2 反向傳播,計(jì)算梯度
solver.net.backward()
# 2. 進(jìn)行一次前向傳播一次反向傳播并根據(jù)梯度更新參數(shù)
solver.step(1)
# 3. 根據(jù)solver文件中設(shè)置進(jìn)行完整model訓(xùn)練
solver.solve()
|
如果想在訓(xùn)練過程中保存模型參數(shù),調(diào)用
1
|
solver.net.save('mymodel.caffemodel')
|
分類圖片
加載Model數(shù)據(jù)
1
2
3
4
5
|
net = caffe.Net(
deploy_prototxt_path, # 用于分類的網(wǎng)絡(luò)定義文件路徑
caffe_model_path, # 訓(xùn)練好模型路徑
caffe.TEST # 設(shè)置為測試階段
)
|
中值文件轉(zhuǎn)換
1
2
3
4
5
6
7
8
9
10
11
12
13
|
# 編寫一個(gè)函數(shù),,將二進(jìn)制的均值轉(zhuǎn)換為python的均值
def convert_mean(binMean,npyMean):
blob = caffe.proto.caffe_pb2.BlobProto()
bin_mean = open(binMean, 'rb' ).read()
blob.ParseFromString(bin_mean)
arr = np.array( caffe.io.blobproto_to_array(blob) )
npy_mean = arr[0]
np.save(npyMean, npy_mean )
# 調(diào)用函數(shù)轉(zhuǎn)換均值
binMean='examples/cifar10/mean.binaryproto'
npyMean='examples/cifar10/mean.npy'
convert_mean(binMean,npyMean)
|
圖片預(yù)處理
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
|
# 設(shè)定圖片的shape格式為網(wǎng)絡(luò)data層格式
transformer = caffe.io.Transformer({'data': net.blobs['data'].data.shape})
# 改變維度的順序,,由原始圖片維度(width, height, channel)變?yōu)?channel, width, height)
transformer.set_transpose('data', (2,0,1))
# 減去均值,注意要先將binaryproto格式均值文件轉(zhuǎn)換為npy格式[此步根據(jù)訓(xùn)練model時(shí)設(shè)置可選]
transformer.set_mean('data', np.load(mean_file_path).mean(1).mean(1))
# 縮放到[0,255]之間
transformer.set_raw_scale('data', 255)
# 交換通道,,將圖片由RGB變?yōu)锽GR
transformer.set_channel_swap('data', (2,1,0))
# 加載圖片
im=caffe.io.load_image(img)
# 執(zhí)行上面設(shè)置的圖片預(yù)處理操作,,并將圖片載入到blob中
net.blobs['data'].data[...] = transformer.preprocess('data',im)
|
執(zhí)行測試
1
2
3
4
5
6
7
8
9
10
11
12
|
#執(zhí)行測試
out = net.forward()
labels = np.loadtxt(labels_filename, str, delimiter='\t') #讀取類別名稱文件
prob= net.blobs['Softmax1'].data[0].flatten() #取出最后一層(Softmax)屬于某個(gè)類別的概率值,并打印
print prob
order=prob.argsort()[0] #將概率值排序,,取出最大值所在的序號
print 'the class is:',labels[order] #將該序號轉(zhuǎn)換成對應(yīng)的類別名稱,并打印
# 取出前五個(gè)較大值所在的序號
top_inds = prob.argsort()[::-1][:5]
print 'probabilities and labels:' zip(prob[top_inds], labels[top_inds])
|
各層信息顯示
1
2
3
4
5
6
7
|
# params顯示:layer名,,w,,b
for layer_name, param in net.params.items():
print layer_name + '\t' + str(param[0].data.shape), str(param[1].data.shape)
# blob顯示:layer名,輸出的blob維度
for layer_name, blob in net.blobs.items():
print layer_name + '\t' + str(blob.data.shape)
|
自定義函數(shù):參數(shù)/卷積結(jié)果可視化
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
|
import numpy as np
import matplotlib.pyplot as plt
import matplotlib.image as mpimg
import caffe
%matplotlib inline
plt.rcParams['figure.figsize'] = (8, 8)
plt.rcParams['image.interpolation'] = 'nearest'
plt.rcParams['image.cmap'] = 'gray'
def show_data(data, padsize=1, padval=0):
"""Take an array of shape (n, height, width) or (n, height, width, 3)
and visualize each (height, width) thing in a grid of size approx. sqrt(n) by sqrt(n)"""
# data歸一化
data -= data.min()
data /= data.max()
# 根據(jù)data中圖片數(shù)量data.shape[0],,計(jì)算最后輸出時(shí)每行每列圖片數(shù)n
n = int(np.ceil(np.sqrt(data.shape[0])))
# padding = ((圖片個(gè)數(shù)維度的padding),(圖片高的padding), (圖片寬的padding), ....)
padding = ((0, n ** 2 - data.shape[0]), (0, padsize), (0, padsize)) + ((0, 0),) * (data.ndim - 3)
data = np.pad(data, padding, mode='constant', constant_values=(padval, padval))
# 先將padding后的data分成n*n張圖像
data = data.reshape((n, n) + data.shape[1:]).transpose((0, 2, 1, 3) + tuple(range(4, data.ndim + 1)))
# 再將(n, W, n, H)變換成(n*w, n*H)
data = data.reshape((n * data.shape[1], n * data.shape[3]) + data.shape[4:])
plt.figure()
plt.imshow(data,cmap='gray')
plt.axis('off')
# 示例:顯示第一個(gè)卷積層的輸出數(shù)據(jù)和權(quán)值(filter)
print net.blobs['conv1'].data[0].shape
show_data(net.blobs['conv1'].data[0])
print net.params['conv1'][0].data.shape
show_data(net.params['conv1'][0].data.reshape(32*3,5,5))
|
自定義:訓(xùn)練過程Loss&Accuracy可視化
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
|
import matplotlib.pyplot as plt
import caffe
caffe.set_device(0)
caffe.set_mode_gpu()
# 使用SGDSolver,,即隨機(jī)梯度下降算法
solver = caffe.SGDSolver('/home/xxx/mnist/solver.prototxt')
# 等價(jià)于solver文件中的max_iter,即最大解算次數(shù)
niter = 10000
# 每隔100次收集一次loss數(shù)據(jù)
display= 100
# 每次測試進(jìn)行100次解算
test_iter = 100
# 每500次訓(xùn)練進(jìn)行一次測試
test_interval =500
#初始化
train_loss = zeros(ceil(niter * 1.0 / display))
test_loss = zeros(ceil(niter * 1.0 / test_interval))
test_acc = zeros(ceil(niter * 1.0 / test_interval))
# 輔助變量
_train_loss = 0; _test_loss = 0; _accuracy = 0
# 進(jìn)行解算
for it in range(niter):
# 進(jìn)行一次解算
solver.step(1)
# 統(tǒng)計(jì)train loss
_train_loss += solver.net.blobs['SoftmaxWithLoss1'].data
if it % display == 0:
# 計(jì)算平均train loss
train_loss[it // display] = _train_loss / display
_train_loss = 0
if it % test_interval == 0:
for test_it in range(test_iter):
# 進(jìn)行一次測試
solver.test_nets[0].forward()
# 計(jì)算test loss
_test_loss += solver.test_nets[0].blobs['SoftmaxWithLoss1'].data
# 計(jì)算test accuracy
_accuracy += solver.test_nets[0].blobs['Accuracy1'].data
# 計(jì)算平均test loss
test_loss[it / test_interval] = _test_loss / test_iter
# 計(jì)算平均test accuracy
test_acc[it / test_interval] = _accuracy / test_iter
_test_loss = 0
_accuracy = 0
# 繪制train loss,、test loss和accuracy曲線
print '\nplot the train loss and test accuracy\n'
_, ax1 = plt.subplots()
ax2 = ax1.twinx()
# train loss -> 綠色
ax1.plot(display * arange(len(train_loss)), train_loss, 'g')
# test loss -> 黃色
ax1.plot(test_interval * arange(len(test_loss)), test_loss, 'y')
# test accuracy -> 紅色
ax2.plot(test_interval * arange(len(test_acc)), test_acc, 'r')
ax1.set_xlabel('iteration')
ax1.set_ylabel('loss')
ax2.set_ylabel('accuracy')
plt.show() |
|