caffe深度学习(一)fine-tune

转自:http://www.cnblogs.com/alexcai/p/5469478.html

我们来说一下如何fine tune。

所谓fine tune就是用别人训练好的模型,加上我们自己的数据,来训练新的模型。fine tune相当于使用别人的模型的前几层,来提取浅层特征,然后在最后再落入我们自己的分类中。

fine tune的好处在于不用完全重新训练模型,从而提高效率,因为一般新训练模型准确率都会从很低的值开始慢慢上升,但是fine tune能够让我们在比较少的迭代次数之后得到一个比较好的效果。在数据量不是很大的情况下,fine tune会是一个比较好的选择。但是如果你希望定义自己的网络结构的话,就需要从头开始了。

 

这里采用一个实际的例子,钱币分类

1、我们收集了2W张图片,将其中4000张作为测试集,剩下作为训练集。

2、接着我们使用上一篇博客中的方法,生成words.txt、train.txt、test.txt三个文件,这里可以不用生成lmdb,因为caffe支持直接指定图片文件。

3、编辑配置文件,这里我们参考finetune_flickr_style例子(它是用caffenet的训练结果进行finetune的),拷贝其配置文件:

solver.prototxt

复制代码
net: "examples/money_test/fine_tune/train_val.prototxt"
test_iter: 20
test_interval: 50
base_lr: 0.001
lr_policy: "step"
gamma: 0.1
stepsize: 2000
display: 1
max_iter: 10000
momentum: 0.9
weight_decay: 0.0005
snapshot: 1000
snapshot_prefix: "examples/money_test/fine_tune/finetune_money"
solver_mode: CPU 
复制代码

train_val.prototxt

其实fine tune使用的网络跟原有网络基本一样,只不过每层调整了一些参数,具体可以参照finetune_flickr_style和caffenet网络配置的对比

复制代码
name: "FlickrStyleCaffeNet"
layer {
  name: "data"
  type: "ImageData"
  top: "data"
  top: "label"
  include {
    phase: TRAIN
  }
  transform_param {
    mirror: true
    crop_size: 227
    mean_file: "data/ilsvrc12/imagenet_mean.binaryproto"
  }
  image_data_param {
    source: "examples/money_test/data/train.txt"
    batch_size: 50
    new_height: 256
    new_width: 256
  }
}
layer {
  name: "data"
  type: "ImageData"
  top: "data"
  top: "label"
  include {
    phase: TEST
  }
  transform_param {
    mirror: false
    crop_size: 227
    mean_file: "data/ilsvrc12/imagenet_mean.binaryproto"
  }
  image_data_param {
    source: "examples/money_test/data/test.txt"
    batch_size: 50
    new_height: 256
    new_width: 256
  }
}
..........
layer {
  name: "fc8_flickr"
  type: "InnerProduct"
  bottom: "fc7"
  top: "fc8_flickr"
  # lr_mult is set to higher than for other layers, because this layer is starting from random while the others are already trained
  param {
    lr_mult: 10
    decay_mult: 1
  }
  param {
    lr_mult: 20
    decay_mult: 0
  }
  inner_product_param {
    num_output: 17    #这里我们的分类数目
    weight_filler {
      type: "gaussian"
      std: 0.01
    }
    bias_filler {
      type: "constant"
      value: 0
    }
  }
}
.....
复制代码

deploy.prototxt

用于实际分类时的网络

复制代码
.........
layer {
  name: "fc8_flickr"
  type: "InnerProduct"
  bottom: "fc7"
  top: "fc8_flickr"
  # lr_mult is set to higher than for other layers, because this layer is starting from random while the others are already trained
  param {
    lr_mult: 10
    decay_mult: 1
  }
  param {
    lr_mult: 20
    decay_mult: 0
  }
  inner_product_param {
    num_output: 17
    weight_filler {
      type: "gaussian"
      std: 0.01
    }
    bias_filler {
      type: "constant"
      value: 0
    }
  }
}
...........
复制代码

4、开始训练

./build/tools/caffe train -solver examples/money_test/fine_tune/solver.prototxt -weights models/bvlc_reference_caffenet/bvlc_reference_caffenet.caffemodel

其中model指定的是caffenet训练好的model。

使用fine tune的效果比较好,经过3400多次迭代后,测试集上正确率达到92%(可以更高),实际测试效果也比较理想。这也许就是深度学习的优势,不需要仔细地挑选特征,只要数据足够,也能得到不错的效果。

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值