caffe的python接口生成配置文件学习
作者:denny402 时间:2023-07-09 04:46:41
引言
caffe是C++语言写的,可能很多人不太熟悉,因此想用更简单的脚本语言来实现。caffe提供matlab接口和python接口,这两种语言就非常简单,而且非常容易进行可视化,使得学习更加快速,理解更加深入。
半年前,我在学习CAFFE的时候,为了加深理解,因此写下了随笔,有了一系列的caffe学习文章。半年过去,很多人问到关于python接口和可视化的一些问题,现在有点空闲时间,就再次写下一些随笔,大家一起来学习。有些重复的内容,我就不再多讲,如果大家有兴趣可移步:
如何配置CAFFE的python接口?
如何将图片转换成LMDB文件?
如何计算训练数据的均值文件?
以上这些操作都是训练之前的预处理操作,不管是用什么接口,都要用到。
如何写配置文件
首先,我们需要掌握的,就是如何写配置文件,通过下面的代码来学习:
# -*- coding: utf-8 -*-
"""
Spyder Editor
"""
from caffe import layers as L,params as P,to_proto
path='/home/xxx/data/' #保存数据和配置文件的路径
train_lmdb=path+'train_db' #训练数据LMDB文件的位置
val_lmdb=path+'val_db' #验证数据LMDB文件的位置
mean_file=path+'mean.binaryproto' #均值文件的位置
train_proto=path+'train.prototxt' #生成的训练配置文件保存的位置
val_proto=path+'val.prototxt' #生成的验证配置文件保存的位置
#编写一个函数,用于生成网络
def create_net(lmdb,batch_size,include_acc=False):
#创建第一层:数据层。向上传递两类数据:图片数据和对应的标签
data, label = L.Data(source=lmdb, backend=P.Data.LMDB, batch_size=batch_size, ntop=2,
transform_param=dict(crop_size=40,mean_file=mean_file,mirror=True))
#创建第二屋:卷积层
conv1=L.Convolution(data, kernel_size=5, stride=1,num_output=16, pad=2,weight_filler=dict(type='xavier'))
#创建激活函数层
relu1=L.ReLU(conv1, in_place=True)
#创建池化层
pool1=L.Pooling(relu1, pool=P.Pooling.MAX, kernel_size=3, stride=2)
conv2=L.Convolution(pool1, kernel_size=3, stride=1,num_output=32, pad=1,weight_filler=dict(type='xavier'))
relu2=L.ReLU(conv2, in_place=True)
pool2=L.Pooling(relu2, pool=P.Pooling.MAX, kernel_size=3, stride=2)
#创建一个全连接层
fc3=L.InnerProduct(pool2, num_output=1024,weight_filler=dict(type='xavier'))
relu3=L.ReLU(fc3, in_place=True)
#创建一个dropout层
drop3 = L.Dropout(relu3, in_place=True)
fc4 = L.InnerProduct(drop3, num_output=10,weight_filler=dict(type='xavier'))
#创建一个softmax层
loss = L.SoftmaxWithLoss(fc4, label)
if include_acc: #在训练阶段,不需要accuracy层,但是在验证阶段,是需要的
acc = L.Accuracy(fc4, label)
return to_proto(loss, acc)
else:
return to_proto(loss)
def write_net():
#将以上的设置写入到prototxt文件
with open(train_proto, 'w') as f:
f.write(str(create_net(train_lmdb,batch_size=64)))
#写入配置文件
with open(val_proto, 'w') as f:
f.write(str(create_net(val_lmdb,batch_size=32, include_acc=True)))
if __name__ == '__main__':
write_net()
通过上面这个文件的执行,我们就会得到两个配置文件:train.prototxt和val.prototxt,分别用于训练阶段和验证阶段。
图片转换成LMDB文件
这种方式生成配置文件,必须有个前提,就是要先把原始图片转换成LMDB文件才行。如果我们已经把原始图片做成了一个列表清单(txt文件,一行一张图片),则可以不用LMDB格式作为输入数据,可以用ImageData作为数据源输入,代码如下:
# -*- coding: utf-8 -*-
from caffe import layers as L,params as P,to_proto
path='/home/xxx/data/'
train_list=path+'train.txt'
val_list=path+'val.txt'
train_proto=path+'train.prototxt'
val_proto=path+'val.prototxt'
def create_net(img_list,batch_size,include_acc=False):
data,label=L.ImageData(source=img_list,batch_size=batch_size,new_width=48,new_height=48,ntop=2,
transform_param=dict(crop_size=40,mirror=True))
conv1=L.Convolution(data, kernel_size=5, stride=1,num_output=16, pad=2,weight_filler=dict(type='xavier'))
relu1=L.ReLU(conv1, in_place=True)
pool1=L.Pooling(relu1, pool=P.Pooling.MAX, kernel_size=3, stride=2)
conv2=L.Convolution(pool1, kernel_size=53, stride=1,num_output=32, pad=1,weight_filler=dict(type='xavier'))
relu2=L.ReLU(conv2, in_place=True)
pool2=L.Pooling(relu2, pool=P.Pooling.MAX, kernel_size=3, stride=2)
conv3=L.Convolution(pool2, kernel_size=53, stride=1,num_output=32, pad=1,weight_filler=dict(type='xavier'))
relu3=L.ReLU(conv3, in_place=True)
pool3=L.Pooling(relu3, pool=P.Pooling.MAX, kernel_size=3, stride=2)
fc4=L.InnerProduct(pool3, num_output=1024,weight_filler=dict(type='xavier'))
relu4=L.ReLU(fc4, in_place=True)
drop4 = L.Dropout(relu4, in_place=True)
fc5 = L.InnerProduct(drop4, num_output=7,weight_filler=dict(type='xavier'))
loss = L.SoftmaxWithLoss(fc5, label)
if include_acc:
acc = L.Accuracy(fc5, label)
return to_proto(loss, acc)
else:
return to_proto(loss)
def write_net():
#
with open(train_proto, 'w') as f:
f.write(str(create_net(train_list,batch_size=64)))
#
with open(val_proto, 'w') as f:
f.write(str(create_net(val_list,batch_size=32, include_acc=True)))
if __name__ == '__main__':
write_net()
即第一层由原来的Data类型,变成了ImageData类型,不需要LMDB文件和均值文件,但需要一个txt文件。
来源:https://www.cnblogs.com/denny402/p/5679037.html
标签:caffe,python,接口,配置文件
![](/images/zang.png)
![](/images/jiucuo.png)
猜你喜欢
ElementUI的this.$notify.close()调用不起作用的解决
2024-05-09 09:53:16
![](https://img.aspxhome.com/file/2023/3/127163_0s.png)
Go语言编程中对文件读写的基本方法整理
2023-06-24 09:31:07
Flask中Cookie和Session理解与作用介绍
2022-05-03 21:50:35
![](https://img.aspxhome.com/file/2023/6/68956_0s.png)
Django中的ajax请求
2022-10-19 10:28:14
![](https://img.aspxhome.com/file/2023/5/99035_0s.png)
可以在线创建文件夹吗?
2009-11-01 18:07:00
Python实现获取系统临时目录及临时文件的方法示例
2022-03-11 00:42:04
Typescript 封装 Axios拦截器方法实例
2023-07-02 16:38:19
PHP的历史和优缺点
2023-11-16 18:32:26
asp用正则过滤html标签
2008-01-19 16:34:00
js遍历详解(forEach, map, for, for...in, for...of)
2024-04-29 13:20:06
Pytorch实现神经网络的分类方式
2021-02-26 05:20:47
如何“看看”服务器上的那个文件还在不在?
2009-11-02 20:17:00
python 如何查看pytorch版本
2021-02-05 05:38:59
一篇文章搞定Mysql日期时间函数
2024-01-20 19:32:58
如何使用Python破解ZIP或RAR压缩文件密码
2022-03-24 19:28:45
![](https://img.aspxhome.com/file/2023/5/92055_0s.png)
MySQL中使用流式查询避免数据OOM
2024-01-23 01:23:00
![](https://img.aspxhome.com/file/2023/9/118169_0s.png)
Selenium 滚动页面至元素可见的方法
2022-06-01 14:01:05
SQL SERVER的优化建议与方法
2024-01-20 15:14:58
win10环境下使用Hyper-V进行虚拟机创建的教程(图解)
2022-08-01 02:25:06
![](https://img.aspxhome.com/file/2023/5/131875_0s.jpg)
asp如何实现对Session 数组的定义和调用?
2010-05-18 18:40:00