根据 train_test.prototxt文件生成 deploy.prototxt文件
本文参考博文
(1)介绍 *_train_test.prototxt文件与 *_deploy.prototxt文件的不同:http://blog.csdn.net/sunshine_in_moon/article/details/49472901
(2)生成deploy文件的Python代码:http://www.cnblogs.com/denny402/p/5685818.html
*_train_test.prototxt文件
这是训练与测试网络配置文件
*_deploy.prototxt文件
这是模型构造文件
在博文http://www.cnblogs.com/denny402/p/5685818.html 中给出了生成 deploy.prototxt文件的Python源代码,但是每个网络不同,修改起来比较麻烦,下面给出该博文中以mnist为例生成deploy文件的源代码,可根据自己网络的设置做出相应修改:(下方代码未测试)
# -*- coding: utf-8 -*- from caffe import layers as L,params as P,to_proto
root='/home/xxx/'
deploy=root+'mnist/deploy.prototxt' #文件保存路径 def create_deploy():
#少了第一层,data层
conv1=L.Convolution(bottom='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'))
#最后没有accuracy层,但有一个Softmax层
prob=L.Softmax(fc4)
return to_proto(prob)
def write_deploy():
with open(deploy, 'w') as f:
f.write('name:"Lenet"\n')
f.write('input:"data"\n')
f.write('input_dim:1\n')
f.write('input_dim:3\n')
f.write('input_dim:28\n')
f.write('input_dim:28\n')
f.write(str(create_deploy()))
if __name__ == '__main__':
write_deploy()
用代码生成deploy文件还是比较麻烦。我们在构建深度学习网络时,肯定会先定义好训练与测试网络的配置文件——*_train_test.prototxt文件,我们可以通过修改*_train_test.prototxt文件 来生成 deploy 文件。以cifar10为例先简单介绍一下两者的区别。
(1)deploy 文件中的数据层更为简单,即将*_train_test.prototxt文件中的输入训练数据lmdb与输入测试数据lmdb这两层删除,取而代之的是,
layer {
name: "data"
type: "Input"
top: "data"
input_param { shape: { dim: 1 dim: 3 dim: 32 dim: 32 } }
}
注:shape: { dim: 1 dim: 3 dim: 32 dim: 32 }代表含义:
shape {
dim: 1 #num,对待识别样本进行数据增广的数量,可自行定义。一般会进行5次crop,之后分别flip。如果该值为10则表示一个样本会变成10个,之后输入到网络进行识别。如果不进行数据增广,可以设置成1
dim: 3 #通道数,表示RGB三个通道
dim: 32 #图像的长和宽,通过 *_train_test.prototxt文件中数据输入层的crop_size获取
dim: 32}
(2)卷积层和全连接层中weight_filler{}与bias_filler{}两个参数不用再填写,因为这两个参数的值,由已经训练好的模型*.caffemodel文件提供。如下所示代码,将*_train_test.prototxt文件中的weight_filler、bias_filler全部删除。
layer { # weight_filler、bias_filler删除
name: "ip2"
type: "InnerProduct"
bottom: "ip1"
top: "ip2"
param {
lr_mult: 1 #权重w的学习率倍数
}
param {
lr_mult: 2 #偏置b的学习率倍数
}
inner_product_param {
num_output: 10
weight_filler {
type: "gaussian"
std: 0.1
}
bias_filler {
type: "constant"
}
}
}
删除后变为
layer {
name: "ip2"
type: "InnerProduct"
bottom: "ip1"
top: "ip2"
param {
lr_mult: 1
}
param {
lr_mult: 2
}
inner_product_param {
num_output: 10
}
}
(3)输出层的变化
1)没有了test模块测试精度 ,将该层删除
2)输出层
1)*_deploy.prototxt文件的构造和*_train_test.prototxt文件的构造最为明显的不同点是,deploy文件没有test网络中的test模块,只有训练模块,即将*_train_test.prototxt中最后部分的test模块测试精度删除,即将如下代码删除。
layer { #删除该层
name: "accuracy"
type: "Accuracy"
bottom: "ip2"
bottom: "label"
top: "accuracy"
include {
phase: TEST
}
}
2) 输出层
*_train_test.prototxt文件
layer{
name: "loss" #注意此处层名称与下面的不同
type: "SoftmaxWithLoss" #注意此处与下面的不同
bottom: "ip2"
bottom: "label" #注意标签项在下面没有了,因为下面的预测属于哪个标签,因此不能提供标签
top: "loss"
}
*_deploy.prototxt文件
layer {
name: "prob"
type: "Softmax"
bottom: "ip2"
top: "prob"
}
注意在两个文件中输出层的类型都发生了变化一个是SoftmaxWithLoss,另一个是Softmax。另外为了方便区分训练与应用输出,训练是输出时是loss,应用时是prob。
下面给出CIFAR10中的配置文件cifar10_quick_train_test.prototxt与其模型构造文件 cifar10_quick.prototxt 直观展示两者的区别。
cifar10_quick_train_test.prototxt文件代码
name: "CIFAR10_quick"
layer { #该层去掉
name: "cifar"
type: "Data"
top: "data"
top: "label"
include {
phase: TRAIN
}
transform_param {
mean_file: "examples/cifar10/mean.binaryproto"
}
data_param {
source: "examples/cifar10/cifar10_train_lmdb"
batch_size: 100
backend: LMDB
}
}
layer { #该层去掉
name: "cifar"
type: "Data"
top: "data"
top: "label"
include {
phase: TEST
}
transform_param {
mean_file: "examples/cifar10/mean.binaryproto"
}
data_param {
source: "examples/cifar10/cifar10_test_lmdb"
batch_size: 100
backend: LMDB
}
}
layer { #将下方的weight_filler、bias_filler全部删除
name: "conv1"
type: "Convolution"
bottom: "data"
top: "conv1"
param {
lr_mult: 1
}
param {
lr_mult: 2
}
convolution_param {
num_output: 32
pad: 2
kernel_size: 5
stride: 1
weight_filler {
type: "gaussian"
std: 0.0001
}
bias_filler {
type: "constant"
}
}
}
layer {
name: "pool1"
type: "Pooling"
bottom: "conv1"
top: "pool1"
pooling_param {
pool: MAX
kernel_size: 3
stride: 2
}
}
layer {
name: "relu1"
type: "ReLU"
bottom: "pool1"
top: "pool1"
}
layer { #weight_filler、bias_filler删除
name: "conv2"
type: "Convolution"
bottom: "pool1"
top: "conv2"
param {
lr_mult: 1
}
param {
lr_mult: 2
}
convolution_param {
num_output: 32
pad: 2
kernel_size: 5
stride: 1
weight_filler {
type: "gaussian"
std: 0.01
}
bias_filler {
type: "constant"
}
}
}
layer {
name: "relu2"
type: "ReLU"
bottom: "conv2"
top: "conv2"
}
layer {
name: "pool2"
type: "Pooling"
bottom: "conv2"
top: "pool2"
pooling_param {
pool: AVE
kernel_size: 3
stride: 2
}
}
layer { #weight_filler、bias_filler删除
name: "conv3"
type: "Convolution"
bottom: "pool2"
top: "conv3"
param {
lr_mult: 1
}
param {
lr_mult: 2
}
convolution_param {
num_output: 64
pad: 2
kernel_size: 5
stride: 1
weight_filler {
type: "gaussian"
std: 0.01
}
bias_filler {
type: "constant"
}
}
}
layer {
name: "relu3"
type: "ReLU"
bottom: "conv3"
top: "conv3"
}
layer {
name: "pool3"
type: "Pooling"
bottom: "conv3"
top: "pool3"
pooling_param {
pool: AVE
kernel_size: 3
stride: 2
}
}
layer { #weight_filler、bias_filler删除
name: "ip1"
type: "InnerProduct"
bottom: "pool3"
top: "ip1"
param {
lr_mult: 1
}
param {
lr_mult: 2
}
inner_product_param {
num_output: 64
weight_filler {
type: "gaussian"
std: 0.1
}
bias_filler {
type: "constant"
}
}
}
layer { # weight_filler、bias_filler删除
name: "ip2"
type: "InnerProduct"
bottom: "ip1"
top: "ip2"
param {
lr_mult: 1
}
param {
lr_mult: 2
}
inner_product_param {
num_output: 10
weight_filler {
type: "gaussian"
std: 0.1
}
bias_filler {
type: "constant"
}
}
}
layer { #将该层删除
name: "accuracy"
type: "Accuracy"
bottom: "ip2"
bottom: "label"
top: "accuracy"
include {
phase: TEST
}
}
layer { #修改
name: "loss" #---loss 修改为 prob
type: "SoftmaxWithLoss" # SoftmaxWithLoss 修改为 softmax
bottom: "ip2"
bottom: "label" #去掉
top: "loss"
}
以下为cifar10_quick.prototxt
layer { #将两个输入层修改为该层
name: "data"
type: "Input"
top: "data"
input_param { shape: { dim: 1 dim: 3 dim: 32 dim: 32 } } #注意shape中变量值的修改,CIFAR10中的 *_train_test.protxt文件中没有 crop_size
}
layer {
name: "conv1"
type: "Convolution"
bottom: "data"
top: "conv1"
param {
lr_mult: 1 #权重W的学习率倍数
}
param {
lr_mult: 2 #偏置b的学习率倍数
}
convolution_param {
num_output: 32
pad: 2 #加边为2
kernel_size: 5
stride: 1
}
}
layer {
name: "pool1"
type: "Pooling"
bottom: "conv1"
top: "pool1"
pooling_param {
pool: MAX #Max Pooling
kernel_size: 3
stride: 2
}
}
layer {
name: "relu1"
type: "ReLU"
bottom: "pool1"
top: "pool1"
}
layer {
name: "conv2"
type: "Convolution"
bottom: "pool1"
top: "conv2"
param {
lr_mult: 1
}
param {
lr_mult: 2
}
convolution_param {
num_output: 32
pad: 2
kernel_size: 5
stride: 1
}
}
layer {
name: "relu2"
type: "ReLU"
bottom: "conv2"
top: "conv2"
}
layer {
name: "pool2"
type: "Pooling"
bottom: "conv2"
top: "pool2"
pooling_param {
pool: AVE #均值池化
kernel_size: 3
stride: 2
}
}
layer {
name: "conv3"
type: "Convolution"
bottom: "pool2"
top: "conv3"
param {
lr_mult: 1
}
param {
lr_mult: 2
}
convolution_param {
num_output: 64
pad: 2
kernel_size: 5
stride: 1
}
}
layer {
name: "relu3"
type: "ReLU" #使用ReLU激励函数,这里需要注意的是,本层的bottom和top都是conv3>
bottom: "conv3"
top: "conv3"
}
layer {
name: "pool3"
type: "Pooling"
bottom: "conv3"
top: "pool3"
pooling_param {
pool: AVE
kernel_size: 3
stride: 2
}
}
layer {
name: "ip1"
type: "InnerProduct"
bottom: "pool3"
top: "ip1"
param {
lr_mult: 1
}
param {
lr_mult: 2
}
inner_product_param {
num_output: 64
}
}
layer {
name: "ip2"
type: "InnerProduct"
bottom: "ip1"
top: "ip2"
param {
lr_mult: 1
}
param {
lr_mult: 2
}
inner_product_param {
num_output: 10
}
}
layer {
name: "prob"
type: "Softmax"
bottom: "ip2"
top: "prob"
}
---------------------
作者:修炼打怪的小乌龟
来源:CSDN
原文:https://blog.csdn.net/u010417185/article/details/52137825
版权声明:本文为博主原创文章,转载请附上博文链接!
根据 train_test.prototxt文件生成 deploy.prototxt文件的更多相关文章
- 将WSDL文件生成的Java文件
- gsoap 学习 1-由wsdl文件生成h头文件
开始前先看一下用户向导吧 http://www.cs.fsu.edu/~engelen/soap.html 中左侧点击Documentation 英语水平确实有限,有些内容可能说的不准确,敬请参考向导 ...
- caffe生成deploy.prototxt文件
参考: http://blog.csdn.net/cham_3/article/details/52682479 以caffe工程自带的mnist数据集,lenet网络为例: 将lenet_train ...
- train_val.prototxt文件和deploy.prototxt文件开头的区别
1.开头不同 对train_val.prototxt文件来说,开头部分定义训练和测试的网络及参数 对deploy.prototxt文件来说,开头部分定义实际运用场景的配置文件,其参数不定义数据来源,仅 ...
- webuploader 上传文件 生成链接下载文件
最近 在asp.net MVC 项目 需要实现一个Excel和 图片上传功能.之前有使用过SWFUpload 做过上传图片功能,在本次实现过程中,有人推荐WebUploader 上传组件,因此采用we ...
- caffe中根据 *_train_test.prototxt文件生成 *_deploy.prototxt文件 (转载)
见博客:http://blog.csdn.net/u010417185/article/details/52137825
- salesforce 零基础学习(五十三)多个文件生成一个zip文件(使用git上封装的代码)
此篇参考git代码:https://github.com/pdalcol/Zippex 学习salesforce可以访问一个朋友的网站:https://www.xgeek.net 首先感谢git上提供 ...
- visual studio code 中隐藏从 ts 文件生成的 js 文件和 map 文件
typescript 文件编译产生的 js 和 map 文件不需要手工编辑,打开[文件][首选项][工作区设置],放入以下代码: // 将设置放入此文件中以覆盖默认值和用户设置. { "fi ...
- 利用字节流文件生成包含多文件的zip文件
InputStream[] inputStreamsList = new InputStream[jsonArr.size()]; String[] fileNameList = new String ...
随机推荐
- java根据word模板导出word文件
1.word模板文件处理,如下图所示在word 文档中填值的地方写入占位变量 2.将word文档另存为xml文件.编辑如下图,找到填写的占位,修改为${bcrxm}格式 3.将文件后缀名改为.ftl文 ...
- WPF插件开发:使用FrameworkElementAdapters时VS报错的问题
使用MAF开发插件时FrameworkElementAdapters是个坑,查帮助手册发现这个类位于System.AddIn.Pipeline命名空间中,但是添加System.AddIn的引用后发现V ...
- poj 2031 给出每个结点的3维坐标 以及结点的半径 (MST)
3维空间中有N个圆球,给出x y z 以及圆球的半径 ,求最小生成树 边的权值为两个圆球间的距离 如果圆球相互接触 则权值为0 求最小的权值和 Sample Input 3 //n10.000 10. ...
- hdu 1027 输出第m个全排列(next_permutation)
Sample Input6 4 //输出第4个全排列11 8 Sample Output1 2 3 5 6 41 2 3 4 5 6 7 9 8 11 10 # include <cstdio& ...
- Jetty部署
http://www.orchome.com/127 https://blog.csdn.net/zhanngle/article/details/77591526
- spring 中常用的两种事务配置方式以及事务的传播性、隔离级别
一.注解式事务 1.注解式事务在平时的开发中使用的挺多,工作的两个公司中看到很多项目使用了这种方式,下面看看具体的配置demo. 2.事务配置实例 (1).spring+mybatis 事务配置 &l ...
- Docker 记一次容器内部修改宿主机挂载目录用户权限后宿主机目录变化
一.需求: 因公司需求,需制作mysql5.7.22 docker基础镜像,每个项目以此镜像启动一个数据库容器,并且每个项目挂载一个宿主机目录到镜像中数据存储下面用于数据持久化保存以便后期迁移至阿里云 ...
- Gitlab管理用户、组、权限(一)
&& 内容: 添加用户 创建组 用户权限 保护主要分支 配置项目的可见性 移除用户 一. 添加用户 1. 用root 管理员登陆. 2. 点击管理区域 3. 点击New User 4. ...
- 递归回溯groupSum
- 《Gradle权威指南》--Android Gradle高级自定义
No1: 指定共享库 <uses-library android:name="com.google.android.maps" android:required=" ...