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. PKU 2352 Stars(裸一维树状数组)

    题目大意:原题链接 就是求每个小星星左小角的星星的个数.坐标按照Y升序,Y相同X升序的顺序给出由于y轴已经排好序,可以按照x坐标建立一维树状数组 关键是要理解树状数组中的c[maxn]数组的构成方式, ...

  2. 3.1.7. Cross validation of time series data

    3.1.7. Cross validation of time series data Time series data is characterised by the correlation bet ...

  3. Winter-2-STL-G Team Queue 解题报告及测试数据

    Time Limit:3000MS     Memory Limit:0KB Description Queues and Priority Queues are data structures wh ...

  4. ASP.NET MVC BundleConfig介绍和使用

    1.BundleConfig介绍: 在创建ASP.NET MVC5项目时,默认在App_Start文件夹中创建了BudleConfig.cs文件. public class BundleConfig ...

  5. Spark高级数据分析· 2数据分析

    wget https://archive.ics.uci.edu/ml/machine-learning-databases/00210/donation.zip 数据清洗 cd /Users/eri ...

  6. Statement与PreparedStatement

    Statement 用于通用查询,能批处理 PreparedStatement(简称PS) 用于执行参数化查询,能批处理 什么是参数化查询? 指在设计与数据库链接并访问数据时,在需要填入数值或数据的地 ...

  7. 2017杭电ACM集训队单人排位赛 - 6

    2017杭电ACM集训队单人排位赛 - 6 排名 1000 1001 1002 1003 1004 1005 1006 1007 1008 1009 1010 59 1 X X 1 1 X X 0 1 ...

  8. sqoop将mysql的tinyint类型转化为boolean类型

    当mysql中的字段为tinyint类型时,通过sqoop导入数据到hdfs上,在hdfs上显示的是true,false值.为了显示0,1. 解决方法: jdbc:mysql://<hostna ...

  9. 资源 | TensorFlow推出新工具Seedbank:即刻使用的预训练模型库【转】

    本文转载自:http://tech.ifeng.com/a/20180713/45062331_0.shtml 选自TensorFlow 作者:Michael Tyka 机器之心编译 参与:路.王淑婷 ...

  10. vROPS中获取虚拟机在VC中的UUID

    vROPS中虚拟机对象的ID为resourceID,跟vCenter中虚拟机的UUID是不一致的,因此想要将vROPS中的虚拟机和vCenter中的虚拟机对应肯定不能靠虚拟机名称,而是一定要靠UUID ...