caffe:自己搭建网络来训练

1.准备样本

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

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

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

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

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

  打开cmd,然后运行:

convert_imageset -shuffle -resize_height=150 -resize_width=150 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:1200 # 迭代次数,根据batch大小来
test_interval:100 # 测试间隔

base_lr:0.001 # 学习率
lr_policy: "multistep" 
gamma: 0.1

stepvalue: 200 # 设置学习率什么时候减小
stepvalue: 400
stepvalue: 700
stepvalue: 1000
max_iter: 2000 # 最大迭代次数

display: 100 # 每训练100次显示一次
momentum: 0.9 # 设置冲量
weight_decay: 0.0005 
snapshot: 200 # 每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:1
        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:1
        shuffle:true
    }
}

layer{
    name: "conv1"
    type: "Convolution"
    bottom: "data"
    top: "conv1"
    param{
        lr_mult: 1 # 和base_lr相乘
        decay_mult: 1
    }
    convolution_param{
        num_output: 32
        kernel_size: 3
        stride: 1
        weight_filler{
            type: "xavier"
        }
        bias_filler{
            type: "constant"
            value: 0
        }
    }
}

layer{
    name: "relu1"
    type: "PReLU"
    bottom: "conv1"
    top: "conv1"
}

layer{
    name: "conv2"
    type: "Convolution"
    bottom: "conv1"
    top: "conv2"
    param{
        lr_mult: 1
        decay_mult: 1
    }
    convolution_param{
        num_output: 64
        kernel_size: 3
        stride: 1 # 步长
        weight_filler{
            type: "xavier"
        }
        bias_filler{
            type: "constant"
            value: 0
        }
    }
}

layer{
    name: "relu2"
    type: "PReLU"
    bottom: "conv2"
    top: "conv2"
}

layer{
    name: "pool1"
    type: "Pooling"
    bottom: "conv2"
    top: "pool1"
    pooling_param{
        pool: MAX
        kernel_size: 2
        stride: 2
    }
}

layer{
    name: "conv3"
    type: "Convolution"
    bottom: "pool1"
    top: "conv3"
    param{
        lr_mult: 1
        decay_mult: 1
    }
    convolution_param{
        num_output: 128
        kernel_size: 3
        stride: 1
        weight_filler{
            type: "xavier"
        }
        bias_filler{
            type: "constant"
            value: 0
        }
    }
}

layer{
    name: "relu3"
    type: "PReLU"
    bottom: "conv3"
    top: "conv3"
}

layer{
    name: "fc1"
    type: "InnerProduct"
    bottom: "conv3"
    top: "fc1"
    param{
        lr_mult: 1
        decay_mult:10
    }
    inner_product_param{
        num_output: 256
        weight_filler{
            type: "xavier"
        }
        bias_filler{
            type: "constant"
            value: 0
        }
    }
}

layer{
    name: "fc2"
    type: "InnerProduct"
    bottom: "fc1"
    top: "fc2"
    param{
        lr_mult: 1
        decay_mult:10
    }
    inner_product_param{
        num_output: 12
        weight_filler{
            type: "xavier"
        }
        bias_filler{
            type: "constant"
            value: 0
        }
    }
}

layer{
    name: "softmax_loss"
    type: "SoftmaxWithLoss"
    bottom: "fc2"
    bottom: "label"
    top: "softmax_loss"
}

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

caffe train -solver=face_example/my_solver.prototxt

 

 

  

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值