该工作的主要目的是为了练习运用pycaffe来进行神经网络一站式训练,并从多个角度来分析对应的结果。
目标:
- python的运用训练
- pycaffe的接口熟悉
- 卷积网络(CNN)和全连接网络(DNN)的效果差异性
- 学会从多个角度来分析分类结果
- 哪些图片被分类错误并进行可视化?
- 为什么被分错?
- 每一类是否同等机会被分错?
- 在迭代过程中,每一类的错误几率如何变化?
- 是否开始被正确识别后来又被错误识别了?
测试数据集:mnist
代码:https://github.com/TiBAiL/PycaffeTrain-LeNet
环境:Ubuntu 16.04LTS训练,Windows 7+VS2013分析
关于网络架构,在caffe的训练过程中,会涉及到三种不同类型的prototxt文件,分别用于train、test(validation)以及deploy。这三种文件需要保持网络架构上的统一,方可使得程序正常工作。为了达到这一目的,可以通过程序自动生成对应的文件。这三种文件的主要区别在于输入数据层以及loss/accuracy/prob层。
与此同时,针对solver.prototxt文件,我也采用了python程序的方式进行生成。在求解过程中,为了能够统计train loss/test loss以及accuracy信息以及保存任意时刻的model参数,可以采用pycaffe提供的api接口进行处理。
代码如下:
import numpy as np
import caffe
from caffe import layers as L, params as P, proto, to_proto # file path
root = '/home/your-account/DL-Analysis/'
train_list = root + 'mnist/mnist_train_lmdb'
test_list = root + 'mnist/mnist_test_lmdb' train_proto = root + 'mnist/LeNet/train.prototxt'
test_proto = root + 'mnist/LeNet/test.prototxt' deploy_proto = root + 'mnist/LeNet/deploy.prototxt' solver_proto = root + 'mnist/LeNet/solver.prototxt' def LeNet(data_list, batch_size, IncludeAccuracy = False, deploy = False):
"""
LeNet define
""" if not(deploy):
data, label = L.Data(source = data_list,
backend = P.Data.LMDB,
batch_size = batch_size,
ntop = 2,
transform_param = dict(scale = 0.00390625))
else:
data = L.Input(input_param = {'shape': {'dim': [64, 1, 28, 28]}}) conv1 = L.Convolution(data,
kernel_size = 5,
stride = 1,
num_output = 20,
pad = 0,
weight_filler = dict(type = 'xavier')) pool1 = L.Pooling(conv1,
pool = P.Pooling.MAX,
kernel_size = 2,
stride = 2) conv2 = L.Convolution(pool1,
kernel_size = 5,
stride = 1,
num_output = 50,
pad = 0,
weight_filler = dict(type = 'xavier')) pool2 = L.Pooling(conv2,
pool = P.Pooling.MAX,
kernel_size = 2,
stride = 2) ip1 = L.InnerProduct(pool2,
num_output = 500,
weight_filler = dict(type = 'xavier')) relu1 = L.ReLU(ip1,
in_place = True) ip2 = L.InnerProduct(relu1,
num_output = 10,
weight_filler = dict(type = 'xavier')) #loss = L.SoftmaxWithLoss(ip2, label) if ( not(IncludeAccuracy) and not(deploy) ):
# train net
loss = L.SoftmaxWithLoss(ip2, label)
return to_proto(loss) elif ( IncludeAccuracy and not(deploy) ):
# test net
loss = L.SoftmaxWithLoss(ip2, label)
Accuracy = L.Accuracy(ip2, label)
return to_proto(loss, Accuracy) else:
# deploy net
prob = L.Softmax(ip2)
return to_proto(prob) def WriteNet():
"""
write proto to file
""" # train net
with open(train_proto, 'w') as file:
file.write( str(LeNet(train_list, 64, IncludeAccuracy = False, deploy = False)) ) # test net
with open(test_proto, 'w') as file:
file.write( str(LeNet(test_list, 100, IncludeAccuracy = True, deploy = False)) ) # deploy net
with open(deploy_proto, 'w') as file:
file.write( str(LeNet('not need', 64, IncludeAccuracy = False, deploy = True)) ) def GenerateSolver(solver_file, train_net, test_net):
"""
generate the solver file
""" s = proto.caffe_pb2.SolverParameter()
s.train_net = train_net
s.test_net.append(test_net)
s.test_interval = 100
s.test_iter.append(100)
s.max_iter = 10000
s.base_lr = 0.01
s.momentum = 0.9
s.weight_decay = 5e-4
s.lr_policy = 'step'
s.stepsize = 3000
s.gamma = 0.1
s.display = 100
s.snapshot = 0
s.snapshot_prefix = './lenet'
s.type = 'SGD'
s.solver_mode = proto.caffe_pb2.SolverParameter.GPU with open(solver_file, 'w') as file:
file.write( str(s) ) def Training(solver_file):
"""
training
""" caffe.set_device(0)
caffe.set_mode_gpu()
solver = caffe.get_solver(solver_file)
#solver.solve() # solve completely number_iteration = 10000 # collect the information
display = 100 # test information
test_iteration = 100
test_interval = 100 # loss and accuracy information
train_loss = np.zeros( np.ceil(number_iteration * 1.0 / display) )
test_loss = np.zeros( np.ceil(number_iteration * 1.0 / test_interval) )
test_accuracy = np.zeros( np.ceil(number_iteration * 1.0 / test_interval) ) # tmp variables
_train_loss = 0; _test_loss = 0; _test_accuracy = 0; # main loop
for iter in range(number_iteration):
solver.step(1) # save model during training
if iter in [10, 30, 60, 100, 300, 600, 1000, 3000, 6000, number_iteration - 1]:
string = 'lenet_iter_%(iter)d.caffemodel'%{'iter': iter}
solver.net.save(string) if 0 == iter % display:
train_loss[iter // display] = solver.net.blobs['SoftmaxWithLoss1'].data '''
# accumulate the train loss
_train_loss += solver.net.blobs['SoftmaxWithLoss1'].data
if 0 == iter % display:
train_loss[iter // display] = _train_loss / display
_train_loss = 0
''' if 0 == iter % test_interval:
for test_iter in range(test_iteration):
solver.test_nets[0].forward()
_test_loss += solver.test_nets[0].blobs['SoftmaxWithLoss1'].data
_test_accuracy += solver.test_nets[0].blobs['Accuracy1'].data test_loss[iter / test_interval] = _test_loss / test_iteration
test_accuracy[iter / test_interval] = _test_accuracy / test_iteration
_test_loss = 0
_test_accuracy = 0 # save for analysis
np.save('./train_loss.npy', train_loss)
np.save('./test_loss.npy', test_loss)
np.save('./test_accuracy.npy', test_accuracy) if __name__ == '__main__':
WriteNet()
GenerateSolver(solver_proto, train_proto, test_proto)
Training(solver_proto)
利用上述代码训练出来的model进行预测,并对结果进行分析(相关分析代码参见上述链接):
- 利用第10000步(最后一步)时的model进行预测,其分类错误率为0.91%。为了能够直观的观察哪些图片被分类错误,这里我们给出了所有分类错误的图片。在对应标题中,第一个数字为预测值,第二个数字为实际真实值。从中我们可以看到,如红框所示,有许多数字确实是鬼斧神工,人都几乎无法有效区分。
- 现在,我们来看一看,针对每一类,其究竟被分成了哪些数字?这个其实可以从上图看出,这里我给出他们的柱状图,其中子标题表示真实的label,横坐标表示被错误分类的label,纵坐标表示数量,最后一个子图表示在所有的错误分类中,每一类所占的比例。从中可以看出,3/5,4/6,7/2,9/4等较容易混淆,这个也可以非常容易理解。此外,我们也可以发现,数字1最容易分辨,这个可能是因为每个人写1都比较相似,变体较少导致的。
- 接着,让我们来考察一下,在迭代过程中,每个数字的分类准确度是如何变化的。其中,子标题表示真实的label,横坐标表示迭代步数,纵坐标表示分类错误的数量,最后一个子图表示迭代过程中,总的错误率的变化曲线。从该图中,我们可以看出,在迭代过程中,一个图片是有可能先被分类正确后来又被分类错误的(各子曲线并不呈现单调递减的关系),这点也可以从中间变量中进行定量分析看出(代码中有该变量)。
- 关于train loss、test loss以及accuracy的曲线。注意,这里的train loss是某一步的值(因此具有强随机性),而test loss以及accuracy则是100次的平均值(因此较为平滑)
- 最后,让我们来分析一下全连接网络(DNN)的结果。所采用的网络架构为LeNet-300-100。其最终的分类错误率为3.6%。这里我仅给出按比例随机挑选的100张错误图片,以及在迭代过程中每个数字错误率的变换,如下所示。可以看到,DNN网络在第10步时其错误率高达80%左右,而CNN网络在该步时的错误率为30%左右,这其中是否有某种深刻内涵呢?