根据 *_train_test.prototxt文件生成 *_deploy.prototxt文件
根据 *_train_test.prototxt文件生成 *_deploy.prototxt文件
发表于2016/8/6 19:43:11 1218人阅读
本文参考博文
(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文件
这是训练与测试网络配置文件
在博文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 #num,对待识别样本进行数据增广的数量,可自行定义。一般会进行5次crop,之后分别flip。如果该值为10则表示一个样本会变成10个,之后输入到网络进行识别。如果不进行数据增广,可以设置成1dim: 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}
}
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"
}
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"
}
根据 *_train_test.prototxt文件生成 *_deploy.prototxt文件相关推荐
- gsoap 学习 1-由wsdl文件生成h头文件
开始前先看一下用户向导吧 http://www.cs.fsu.edu/~engelen/soap.html 中左侧点击Documentation 英语水平确实有限,有些内容可能说的不准确,敬请参考向导 ...
- 使用C#从图片文件生成图标ICON文件(附源文件)
使用C#从图片文件生成图标ICON文件(附源文件) 看到网上有一些网站做图标转换工具,挺好的,所以研究了一下在C#中是如何实现的.其实很简单几行代码就可以搞定. 主要是使用System.Drawing ...
- 如何快速生成A2L文件(ASAP2 Studio):基于“前一版本A2L文件+最新map文件”生成新map文件对应A2L文件
目录 一.ASAP2 Studio概述: 二.如何在ASAP2 Studio中利用"前一版本A2L文件+最新map文件"生成新map文件对应A2L文件: 2.1 在ASAP2 St ...
- 在Springboot环境下,使用Docx4J + Freemarker 完成word docx文件生成与Pdf文件转换(附带兼容linux字体问题处理办法)
在Springboot环境下,使用Docx4J + Freemarker 完成word docx文件生成与Pdf文件转换(附带兼容linux字体问题处理办法) 前言 效果展示 正文 docx文件模板创 ...
- 源文件与模块文件生成时的文件不同,仍要调试器使用它吗
字体大小:大 中 小 源文件与模块生成时的文件不同,仍要让调试器使用它吗 (2011-03-21 20:06:58) 转载▼ 解决办法: 1.在工具-> 选项-> 调试-> 常规 里 ...
- 在Linux上将12导联心电文件生成PDF格式文件
1.把ECG文件和同名ini文件放在/usr/local/12ECGNew/20170504路径下, 2.打开命令执行窗口putty,进入以上目录, 3.执行 ./ECGAnalyserL 02453 ...
- linux设备文件生成,Linux设备文件自动生成(示例代码)
第一种是使用mknod手工创建:# mknod 第二种是自动创建设备节点:利用udev(mdev)来实现设备文件的自动创建,首先应保证支持udev(mdev),由busybox配置. 具体udev相关 ...
- python读xml文件生成.h头文件_PYTHON读写xml文件的方法
要生成的xml文件格式如下: [python] sample xml thing ma xiaoju Springs Widgets, Inc. First I think widgets are g ...
- python读xml文件生成.h头文件_Python创建xml文件示例
Python创建xml文件示例 这里有新鲜出炉的 Python 入门,程序狗速度看过来! Python 编程语言 Python 是一种面向对象.解释型计算机程序设计语言,由 Guido van Ros ...
最新文章
- ROS 总结(二):创建URDF模型
- SAP实施后你的关键用户还关键吗?
- 齐家网困境:进退两难,合作方和消费者两头都不讨好?
- mysql为什么添加索引_当我添加新索引时,为什么MySQL中索引的基数保持不变?
- 橡皮擦_日本推出改邪归正橡皮擦,看得我头顶一凉
- 程序员能为爱情做的,就是用他的技术告诉世界:我爱你
- 支付宝手机网站即时交易 自己封装的类
- vue项目中vue-fullcalendar的使用(行程日历)
- HG255D刷机及网络接口配置
- 源码提供!Android即时通讯和sns开源项目汇总
- 为什么要研究《双目结构光》?
- 视频播放器(二)——播放列表
- ios 扫码枪外设 键盘模式_iPadOS 显威力,苹果 iPad Pro 终于用上带触控板的外接键盘...
- 网络信息安全的重要性
- 人工智能于交通系统而言
- 我推荐这几个好玩有趣的小网站
- Linux虚拟机基本操作、Xshell连接、更改IP
- 广度优先搜索:迷宫问题
- MATLAB 批量改文件后缀
- 为你,千千万万遍——《追风筝的人》
热门文章
- java 创建string_Java 如何创建一个String
- java逻辑移位和算术移位,关于对移位运算的理解
- C++:读写二进制文件到double数组,根据二进制数据量进行读取
- C++继承时的名字遮蔽(一)
- jet nano 车道识别
- go 实现斐波那契数列
- 59. Leetcode 81. 搜索旋转排序数组 II(二分查找-局部有序)
- Leetcode 215.数组中第k个最大元素 (每日一题 20210713)
- Graph Attention Networks
- pytorch 笔记:torch_geometric (1)创建一张图