caffe学习笔记-模型代码生成.prototxt文件
来源:互联网 发布:云计算 视界云 知乎 编辑:程序博客网 时间:2024/05/22 00:10
pycaffe网络定义
以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)
定义激活函数层:
relu3=L.ReLU(fc3, in_place=True)
定义全连接层:
fc3=L.InnerProduct(pool2, num_output=500,weight_filler=dict(type='xavier'))
计算损失函数:
loss = L.SoftmaxWithLoss(fc4, label)
计算精度:
acc = L.Accuracy(fc4, label)
保存网络定义.prototxt文件
以Lenet为例,网络结构代码如下:
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)
上述代码中,通过to_proto()函数,保存网络定义,保存为.prototxt文件,
with open(train_proto, 'w') as f: f.write(str(Lenet(train_list,batch_size=64)))
train_list为模型输入,train_proto为.prototxt文件名,保存的.prototxt文件内容如下:
layer { name: "ImageData1" type: "ImageData" top: "ImageData1" top: "ImageData2" transform_param { scale: 0.00390625 } image_data_param { source: "mnist/train/train.txt" batch_size: 64 root_folder: "" }}layer { name: "Convolution1" type: "Convolution" bottom: "ImageData1" top: "Convolution1" convolution_param { num_output: 20 pad: 0 kernel_size: 5 stride: 1 weight_filler { type: "xavier" } }}layer { name: "Pooling1" type: "Pooling" bottom: "Convolution1" top: "Pooling1" pooling_param { pool: MAX kernel_size: 2 stride: 2 }}layer { name: "Convolution2" type: "Convolution" bottom: "Pooling1" top: "Convolution2" convolution_param { num_output: 50 pad: 0 kernel_size: 5 stride: 1 weight_filler { type: "xavier" } }}layer { name: "Pooling2" type: "Pooling" bottom: "Convolution2" top: "Pooling2" pooling_param { pool: MAX kernel_size: 2 stride: 2 }}layer { name: "InnerProduct1" type: "InnerProduct" bottom: "Pooling2" top: "InnerProduct1" inner_product_param { num_output: 500 weight_filler { type: "xavier" } }}layer { name: "ReLU1" type: "ReLU" bottom: "InnerProduct1" top: "InnerProduct1"}layer { name: "InnerProduct2" type: "InnerProduct" bottom: "InnerProduct1" top: "InnerProduct2" inner_product_param { num_output: 10 weight_filler { type: "xavier" } }}layer { name: "SoftmaxWithLoss1" type: "SoftmaxWithLoss" bottom: "InnerProduct2" bottom: "ImageData2" top: "SoftmaxWithLoss1"}
由输入层可知,训练数据为mnist/train/train.txt.
网络训练
caffe训练,还需要一个solver.prototxt文件,用于定义训练的迭代次数,学习率等参数,可以手动编写该文件,也可以通过代码生成,
#编写一个函数,生成参数文件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))
生成solver_proto文件内容如下:
train_net: "mnist/train.prototxt"test_net: "mnist/test.prototxt"test_iter: 100test_interval: 938base_lr: 0.00999999977648display: 20max_iter: 9380lr_policy: "step"gamma: 0.10000000149momentum: 0.899999976158weight_decay: 0.000500000023749stepsize: 3000snapshot: 938snapshot_prefix: "mnist/lenet"solver_mode: GPUtype: "SGD"
训练代码编写:
caffe.set_device(0)caffe.set_mode_gpu()solver = caffe.SGDSolver(solver_proto)solver.solve()
完整代码如下:
#! /usr/bin/env python2.7#coding=utf-8import syscaffe_root='/home/program/caffe/'sys.path.insert(0, caffe_root + '/python')import caffefrom caffe import layers as L,params as P,proto,to_proto#设定文件的保存路径root='' #根目录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' #参数文件#编写一个函数,生成配置文件prototxtdef 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(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))#开始训练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)
代码参考自:http://www.cnblogs.com/denny402/p/5684431.html
阅读全文
0 0
- caffe学习笔记-模型代码生成.prototxt文件
- Caffe学习笔记(四):使用pycaffe生成train.prototxt、test.prototxt文件
- Caffe学习笔记(五):使用pycaffe生成solver.prototxt文件并进行训练
- caffe 学习笔记-prototxt文件定义与读取
- caffe学习笔记9-train_val.prototxt学习
- caffe学习笔记10-solver.prototxt学习
- caffe学习笔记11-deploy.prototxt学习
- Caffe学习笔记:cifar10_quick_train_test.prototxt配置文件分析
- 【Python】【Caffe】一、生成prototxt文件《python调用caffe模块》
- caffe源码学习——用python定义网络时,源代码生成prototxt文件的原理
- caffe for windows 模型中 solver.prototxt 代码解读
- caffe solver.prototxt文件
- caffe中train_val.prototxt文件和deploy.prototxt文件区别和转换--caffe学习(14)
- netscope——caffe的prototxt模型文件可视化工具
- Caffe solver.prototxt学习
- caffe生成lenet-5的deploy.prototxt文件
- 利用caffe的Python接口生成prototxt文件
- 利用python脚本生成caffe的prototxt文件
- 实现多图批量上传
- spark 与storm的对比及适用场景
- ThreadLocal在项目中的身份校验
- oracle-11g手工建库详细步骤
- 超级台阶
- caffe学习笔记-模型代码生成.prototxt文件
- 如何测试成像畸变?
- ios-RESful简单介绍
- javaweb项目发送邮件自定义发件人名称(javamail)
- [hihocoder]hiho一下 第163周 希尔伯特曲线
- 数据的排序及重排序问题(数据变化需要重排)
- 关于Android中HTTP请求中的EOFException解决办法
- Python面试题总结
- JQuery常用内容(更新中)