深度学习的第一个实例通常都是mnist,只要这个例子彻底弄懂了,其它的就是触类旁通的事了。因为篇幅缘由,本文不具体介绍配置文件里面每一个参数的具体函义,若是想弄明白的,请参看我之前的博文:html
数据层及参数python
视觉层及参数linux
1、数据准备windows
官网提供的mnist数据并非图片,但咱们之后作的实际项目多是图片。所以有些人并不知道该怎么办。在此我将mnist数据进行了转化,变成了一张张的图片,咱们练习就从图片开始。mnist图片数据我放在了百度云盘。app
mnist图片数据下载:http://pan.baidu.com/s/1pLMV4Kz编辑器
数据分红了训练集(60000张共10类)和测试集(共10000张10类),每一个类别放在一个单独的文件夹里。而且将全部的图片,都生成了txt列表清单(train.txt和test.txt)。你们下载下来后,直接解压到当前用户根目录下就能够了。因为我是在windows下压缩的,所以是winrar文件。若是你们要在linux下解压缩,须要安装rar的linux版本,也是十分简单ide
sudo apt-get install rar
2、导入caffe库,并设定文件路径函数
我是将mnist直接放在根目录下的,因此代码以下:学习
# -*- coding: utf-8 -*- import caffe from caffe import layers as L,params as P,proto,to_proto #设定文件的保存路径 root='/home/xxx/' #根目录 train_list=root+'mnist/train/train.txt' #训练图片列表 test_list=root+'mnist/test/test.txt' #测试图片列表 train_proto=root+'mnist/train.prototxt' #训练配置文件 test_proto=root+'mnist/test.prototxt' #测试配置文件 solver_proto=root+'mnist/solver.prototxt' #参数文件
其中train.txt 和test.txt文件已经有了,其它三个文件,咱们须要本身编写。
此处注意:通常caffe程序都是先将图片转换成lmdb文件,但这样作有点麻烦。所以我就不转换了,我直接用原始图片进行操做,所不一样的就是直接用图片操做,均值很难计算,所以能够不减均值。
2、生成配置文件
配置文件实际上就是一些txt文档,只是后缀名是prototxt,咱们能够直接到编辑器里编写,也能够用代码生成。此处,我用python来生成。
#编写一个函数,生成配置文件prototxt def Lenet(img_list,batch_size,include_acc=False): #第一层,数据输入层,以ImageData格式输入 data, label = L.ImageData(source=img_list, batch_size=batch_size, ntop=2,root_folder=root, transform_param=dict(scale= 0.00390625)) #第二层:卷积层 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) #全链接层 fc3=L.InnerProduct(pool2, num_output=500,weight_filler=dict(type='xavier')) #激活函数层 relu3=L.ReLU(fc3, in_place=True) #全链接层 fc4 = L.InnerProduct(relu3, num_output=10,weight_filler=dict(type='xavier')) #softmax层 loss = L.SoftmaxWithLoss(fc4, label) if include_acc: # test阶段须要有accuracy层 acc = L.Accuracy(fc4, label) return to_proto(loss, acc) else: return to_proto(loss) def write_net(): #写入train.prototxt with open(train_proto, 'w') as f: f.write(str(Lenet(train_list,batch_size=64))) #写入test.prototxt with open(test_proto, 'w') as f: f.write(str(Lenet(test_list,batch_size=100, include_acc=True)))
配置文件里面存放的,就是咱们所说的network。我这里生成的network,可能和原始的Lenet不太同样,不过影响不大。
3、生成参数文件solver
一样,能够在编辑器里面直接书写,也能够用代码生成。
#编写一个函数,生成参数文件 def gen_solver(solver_file,train_net,test_net): s=proto.caffe_pb2.SolverParameter() s.train_net =train_net s.test_net.append(test_net) s.test_interval = 938 #60000/64,测试间隔参数:训练完一次全部的图片,进行一次测试 s.test_iter.append(100) #10000/100 测试迭代次数,须要迭代100次,才完成一次全部数据的测试 s.max_iter = 9380 #10 epochs , 938*10,最大训练次数 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 = 20 #屏幕显示间隔 s.snapshot = 938 #保存caffemodel的间隔 s.snapshot_prefix =root+'mnist/lenet' #caffemodel前缀 s.type ='SGD' #优化算法 s.solver_mode = proto.caffe_pb2.SolverParameter.GPU #加速 #写入solver.prototxt with open(solver_file, 'w') as f: f.write(str(s))
4、开始训练模型
训练过程当中,也在不停的测试。
#开始训练 def training(solver_proto): caffe.set_device(0) caffe.set_mode_gpu() solver = caffe.SGDSolver(solver_proto) solver.solve()
最后,调用以上的函数就能够了。
if __name__ == '__main__': write_net() gen_solver(solver_proto,train_proto,test_proto) training(solver_proto)
5、完成的python文件
mnist.py
# -*- coding: utf-8 -*- import caffe from caffe import layers as L,params as P,proto,to_proto #设定文件的保存路径 root='/home/xxx/' #根目录 train_list=root+'mnist/train/train.txt' #训练图片列表 test_list=root+'mnist/test/test.txt' #测试图片列表 train_proto=root+'mnist/train.prototxt' #训练配置文件 test_proto=root+'mnist/test.prototxt' #测试配置文件 solver_proto=root+'mnist/solver.prototxt' #参数文件 #编写一个函数,生成配置文件prototxt def Lenet(img_list,batch_size,include_acc=False): #第一层,数据输入层,以ImageData格式输入 data, label = L.ImageData(source=img_list, batch_size=batch_size, ntop=2,root_folder=root, transform_param=dict(scale= 0.00390625)) #第二层:卷积层 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) #全链接层 fc3=L.InnerProduct(pool2, num_output=500,weight_filler=dict(type='xavier')) #激活函数层 relu3=L.ReLU(fc3, in_place=True) #全链接层 fc4 = L.InnerProduct(relu3, num_output=10,weight_filler=dict(type='xavier')) #softmax层 loss = L.SoftmaxWithLoss(fc4, label) if include_acc: # test阶段须要有accuracy层 acc = L.Accuracy(fc4, label) return to_proto(loss, acc) else: return to_proto(loss) def write_net(): #写入train.prototxt with open(train_proto, 'w') as f: f.write(str(Lenet(train_list,batch_size=64))) #写入test.prototxt with open(test_proto, 'w') as f: f.write(str(Lenet(test_list,batch_size=100, include_acc=True))) #编写一个函数,生成参数文件 def gen_solver(solver_file,train_net,test_net): s=proto.caffe_pb2.SolverParameter() s.train_net =train_net s.test_net.append(test_net) s.test_interval = 938 #60000/64,测试间隔参数:训练完一次全部的图片,进行一次测试 s.test_iter.append(500) #50000/100 测试迭代次数,须要迭代500次,才完成一次全部数据的测试 s.max_iter = 9380 #10 epochs , 938*10,最大训练次数 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 = 20 #屏幕显示间隔 s.snapshot = 938 #保存caffemodel的间隔 s.snapshot_prefix = root+'mnist/lenet' #caffemodel前缀 s.type ='SGD' #优化算法 s.solver_mode = proto.caffe_pb2.SolverParameter.GPU #加速 #写入solver.prototxt with open(solver_file, 'w') as f: f.write(str(s)) #开始训练 def training(solver_proto): caffe.set_device(0) caffe.set_mode_gpu() solver = caffe.SGDSolver(solver_proto) solver.solve() # if __name__ == '__main__': write_net() gen_solver(solver_proto,train_proto,test_proto) training(solver_proto)
我将此文件放在根目录下的mnist文件夹下,所以可用如下代码执行
sudo python mnist/mnist.py
在训练过程当中,会保存一些caffemodel。多久保存一次,保存多少次,均可以在solver参数文件里进行设置。
我设置为训练10 epoch,9000屡次,测试精度能够达到99%