zoukankan      html  css  js  c++  java
  • caffe简易上手指南(三)—— 使用模型进行fine tune

    之前的教程我们说了如何使用caffe训练自己的模型,下面我们来说一下如何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_ 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_ 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%,实际测试效果也比较理想。这也许就是深度学习的优势,不需要仔细地挑选特征,只要数据足够,也能得到不错的效果。

  • 相关阅读:
    UBUNTU 自动挂载 NTFS
    automake autoconf 学习笔记(转载)
    error: X11/extensions/XInput.h: No such file or directory
    error: undefined macro: AC_PROG_LIBTOOL
    Linux下tar.xz结尾的文件的解压方法
    Ubuntu 修改hosts
    ubuntu主目录下的中文文件夹名改回英文
    ./configure: No such file or directory
    Ubuntu下GTK的安装、编译和测试
    图像适配源码
  • 原文地址:https://www.cnblogs.com/alexcai/p/5469478.html
Copyright © 2011-2022 走看看