1.准备样本

  要训练自己的样本,首先需要把样本准备好,需要准备的是训练集和测试集,caffe支持直接使用图片,当然把样本转换为leveldb或lmdb格式的话训练起来会更快一点。这里我先偷个懒,直接使用图片吧 [尴尬.jpg]

  训练集和测试集是一样的,不过样本不要重叠。首先我把训练集的图片都放在一个目录,然后shift+右键选择该目录,打开cmd,使用命令 dir /s/b >train.txt ,这样就在该目录下生成了一份所有图片的列表,效果如下

  然后使用查找替换功能把它修改成下面这个样子,后面的0,1,,序号是为每个类别的样本分配的标签,需要从0开始:

位置/../xx1.jpg
位置/../xx2.jpg
位置/../xx3.jpg
位置/../xx4.jpg
位置/../xx5.jpg
位置/../xx6.jpg
.....................

  这里样本的准备已经差不多了,最后一步是需要生成均值文件 binaryproto,生成均值文件之前需要先将图片转换为lmdb,这里可以使用caffe自带的工具来生成(vs编译后会在bin文件夹下生成comput_image_mean.exe convert_imageset.exe

  打开cmd,然后运行:

convert_imageset -shuffle -resize_height= -resize_width= J:/Caffe/train/ J:/Caffe/train/train.txt J:/Caffe/train/lmdb

  使用命令生成均值文件:

compute_image_mean J:/Caffe/train/lmdb J:/Caffe/train/train.binaryproto

  到这里训练集已经准备好了,然后测试集同上,最后我把它们全放在了data文件夹下,该文件夹下包含训练和测试所需的所有图片图片列表train.txt和test.txt均值文件train.binaryproto(我的测试也是使用这个均值文件)

2. 编写配置文件solver,我在caffe目录下的face_example目录下新建了my_solver.prototxt,编写如下:

net: "face_example/my_train.prototxt" # 网络结构文件的位置
test_iter: # 迭代次数,根据batch大小来
test_interval: # 测试间隔 base_lr:0.001 # 学习率
lr_policy: "multistep"
gamma: 0.1 stepvalue: # 设置学习率什么时候减小
stepvalue:
stepvalue:
stepvalue:
max_iter: # 最大迭代次数 display: # 每训练100次显示一次
momentum: 0.9 # 设置冲量
weight_decay: 0.0005
snapshot: # 每200次保存一个快照文件
snapshot_prefix: "face_example/face_snapshot" # 快照文件保存位置 solver_mode:GPU # 使用GPU训练

3. 编写网络定义文件,新建 my_train.prototxt ,编写如下:

name: "my_caffe_test"

layer{
name: "data"
type: "ImageData"
top: "data"
top: "label"
include{
phase:TRAIN
}
transform_param{
mean_file:"face_example/train.binaryproto"
scale: 0.0078125
mirror:true
}
image_data_param{
source:"face_example/data/train.txt"
batch_size:
shuffle:true
}
} layer{
name: "data"
type: "ImageData"
top: "data"
top: "label"
include{
phase:TEST
}
transform_param{
mean_file:"face_example/train.binaryproto"
scale: 0.0078125
mirror:true
}
image_data_param{
source:"face_example/data/test.txt"
batch_size:
shuffle:true
}
} layer{
name: "conv1"
type: "Convolution"
bottom: "data"
top: "conv1"
param{
lr_mult: # 和base_lr相乘
decay_mult:
}
convolution_param{
num_output:
kernel_size:
stride:
weight_filler{
type: "xavier"
}
bias_filler{
type: "constant"
value:
}
}
} layer{
name: "relu1"
type: "PReLU"
bottom: "conv1"
top: "conv1"
} layer{
name: "conv2"
type: "Convolution"
bottom: "conv1"
top: "conv2"
param{
lr_mult:
decay_mult:
}
convolution_param{
num_output:
kernel_size:
stride: # 步长
weight_filler{
type: "xavier"
}
bias_filler{
type: "constant"
value:
}
}
} layer{
name: "relu2"
type: "PReLU"
bottom: "conv2"
top: "conv2"
} layer{
name: "pool1"
type: "Pooling"
bottom: "conv2"
top: "pool1"
pooling_param{
pool: MAX
kernel_size:
stride:
}
} layer{
name: "conv3"
type: "Convolution"
bottom: "pool1"
top: "conv3"
param{
lr_mult:
decay_mult:
}
convolution_param{
num_output:
kernel_size:
stride:
weight_filler{
type: "xavier"
}
bias_filler{
type: "constant"
value:
}
}
} layer{
name: "relu3"
type: "PReLU"
bottom: "conv3"
top: "conv3"
} layer{
name: "fc1"
type: "InnerProduct"
bottom: "conv3"
top: "fc1"
param{
lr_mult:
decay_mult:
}
inner_product_param{
num_output:
weight_filler{
type: "xavier"
}
bias_filler{
type: "constant"
value:
}
}
} layer{
name: "fc2"
type: "InnerProduct"
bottom: "fc1"
top: "fc2"
param{
lr_mult:
decay_mult:
}
inner_product_param{
num_output:
weight_filler{
type: "xavier"
}
bias_filler{
type: "constant"
value:
}
}
} layer{
name: "softmax_loss"
type: "SoftmaxWithLoss"
bottom: "fc2"
bottom: "label"
top: "softmax_loss"
}

4. 然后开始训练,打开cmd,输入命令:

caffe train -solver=face_example/my_solver.prototxt

  

caffe:自己搭建网络来训练的更多相关文章

  1. PyQt5+Caffe+Opencv搭建人脸识别登录界面

    PyQt5+Caffe+Opencv搭建人脸识别登录界面(转载) 最近开始学习Qt,结合之前学习过的caffe一起搭建了一个人脸识别登录系统的程序,新手可能有理解不到位的情况,还请大家多多指教. 我的 ...

  2. Pytorch从0开始实现YOLO V3指南 part2——搭建网络结构层

    本节翻译自:https://blog.paperspace.com/how-to-implement-a-yolo-v3-object-detector-from-scratch-in-pytorch ...

  3. Caffe fine-tuning 微调网络

    转载请注明出处,楼燚(yì)航的blog,http://www.cnblogs.com/louyihang-loves-baiyan/ 目前呢,caffe,theano,torch是当下比较流行的De ...

  4. 孤荷凌寒自学python第八十四天搭建jTessBoxEditor来训练tesseract模块

    孤荷凌寒自学python第八十四天搭建jTessBoxEditor来训练tesseract模块 (完整学习过程屏幕记录视频地址在文末) 由于本身tesseract模块针对普通的验证码图片的识别率并不高 ...

  5. 使用Caffe完成图像目标检测 和 caffe 全卷积网络

    一.[用Python学习Caffe]2. 使用Caffe完成图像目标检测 标签: pythoncaffe深度学习目标检测ssd 2017-06-22 22:08 207人阅读 评论(0) 收藏 举报 ...

  6. CNN tflearn处理mnist图像识别代码解说——conv_2d参数解释,整个网络的训练,主要就是为了学那个卷积核啊。

    官方参数解释: Convolution 2D tflearn.layers.conv.conv_2d (incoming, nb_filter, filter_size, strides=1, pad ...

  7. pytorch基础-搭建网络

    搭建网络的步骤大致为以下: 1.准备数据 2. 定义网络结构model 3. 定义损失函数4. 定义优化算法 optimizer5. 训练 5.1 准备好tensor形式的输入数据和标签(可选) 5. ...

  8. Caffe系列1——网络文件和求解分析

    1. 首先,我们先看一个完整的文件:lenet_train_test.prototxt name: "LeNet" #整个网络的名称 layer { #数据层——训练数据 name ...

  9. pytorch搭建网络,保存参数,恢复参数

    这是看过莫凡python的学习笔记. 搭建网络,两种方式 (1)建立Sequential对象 import torch net = torch.nn.Sequential( torch.nn.Line ...

随机推荐

  1. 两个java小练习

    网上看到的一个小练习,自己写了一个,但是时间限制并不符合,并且貌似还有些小问题,暂时放在这儿,代码格式什么的也不太规范. 1.班级排名时间限制: 1000ms 内存限制: 65536kB 描述 信息科 ...

  2. javascript 基本数据类型、引用数据类型

    阅读目录 数据类型 两种访问方式 两种类型复制 函数参数的传递 两种变量类型检测 回到目录   数据类型 1.   ECMAScript变量包含两种不同类型的值:基本类型值.引用类型值: 2.   基 ...

  3. python3_json模块使用与字符编码问题

    序列化:将对象的状态信息转换为可以存储或可以通过网络传输的过程,传输的格式可以是json,xml. 反序列化:就是从存储区域(json,xml)读取反序列化对象的状态,重新创建该对象 Json:一种轻 ...

  4. [转]MySQL查看数据库相关信息

    原文链接:MySQL查看数据库相关信息 使用MySQL时,需要了解当前数据库的情况,例如当前的数据库大小.字符集.用户等等.下面总结了一些查看数据库相关信息的命令 1:查看显示所有数据库 mysql& ...

  5. nuget发布自已的程序集

    1.nuget注册并获取apikey 2.下载nuget.exe 3.设置apikey nuget setApiKey <apikey> 4.开发程序集 5.进入.csproj目录生成描述 ...

  6. centos安装xdebug 和 phpstorm+Xdebug断点调试PHP

    转载地址:http://www.2cto.com/os/201304/206058.html   CentOS下安装xdebug   在CentOS 6.x 的系统中,是集成xdebug 的,   y ...

  7. Docker---大型项目容器化改造

           虚拟化和容器化是项目云化不可避免的两个问题.虚拟化由于是纯平台操作,一个运行于linux操作系统的项目几乎不需要做任何改造就可以支持虚拟化.而项目如果要支持容器化则需要做许多细致的改造工 ...

  8. 20135320赵瀚青LINUX第二章读书笔记

    第二章-从内核出发 获取内核代码 使用git 获取最新提交到版本树的一个副本 $ git clone git://git.kernel.org/pub/scm/linux/kernel/git/tor ...

  9. 输入n,求1~n累加

    最开始可能会使用for循环来计算,现在直接使用等差数据计算和公式:s=(a0+n)*n/2 long sum(int n) { long ret=0: ret = (1+n)* n /2: retur ...

  10. TensorFlow和深度学习入门教程(TensorFlow and deep learning without a PhD)【转】

    本文转载自:https://blog.csdn.net/xummgg/article/details/69214366 前言 上月导师在组会上交我们用tensorflow写深度学习和卷积神经网络,并把 ...