Image classification based on Caffe (3)--Modifying the network and training the model

Source: Internet
Author: User
Tags constant

In front of the Lmdb and mean file, the following googlenet as an example to modify the network and train the model.


We will copy the Bvlc_googlenet folder under Caffe-master\models to Caffe-master\examples\imagenet. (Because our lmdb and mean are here, easy to put together)

Open Train_val.txt, modify:

1. Modify the data layer:

Layer {
  Name: "Data"
  Type: "Data"
  Top: "Data"
  Top: "label"
  include {
    phase:train
  }
  Transform_param {
    mirror:true
    crop_size:224
	mean_file: "Examples/imagenet/mydata_mean.binaryproto" # Mean file
    #mean_value: 104 #这些注释掉
    #mean_value: 117
    #mean_value: 123
  }
  data_param {
    Source: "Examples/imagenet/mydata_train_lmdb" #训练集的lmdb
    batch_size:32 #根据GPU修改
    backend:lmdb
  }
}

Layer {
  Name: "Data"
  Type: "Data"
  Top: "Data"
  Top: "label"
  include {
    phase:test
  }
  Transform_param {
    mirror:false
    crop_size:224
    mean_file: "Examples/imagenet/mydata_mean.binaryproto" #均值文件
    #mean_value: 104
    #mean_value: 117
    #mean_value: 123
  }
  data_param {
    Source: " Examples/imagenet/mydata_val_lmdb "#验证集lmdb
    batch_size:50 #和solver中的test_iter相乘约等于验证集大小
    Backend:lmdb
  }
}


2. Modify the output:

Since the googlenet has three output, so change three places, other networks generally have only one output, then change a place.

In the case of fine tuning, the layer name of the output layer also needs to be modified. (the parameter is initialized according to the layer name, because the output is changed, the layer parameter is wrong, so rename)

Layer {
  name: "Loss1/classifier"
  type: "Innerproduct"
  Bottom: "LOSS1/FC"
  Top: "Loss1/classifier"
  param {
    lr_mult:1
    decay_mult:1
  }
  param {
    lr_mult:2
    decay_mult:0
  }
  inner_ Product_param {
    num_output:1000 #改成你的数据集类别数
    weight_filler {
      type: "Xavier"
    }
    Bias_filler {
      type: "Constant"
      value:0
    }
  }
}

Layer {
  name: "Loss2/classifier"
  type: "Innerproduct"
  Bottom: "LOSS2/FC"
  Top: "Loss2/classifier "
  param {
    lr_mult:1
    decay_mult:1
  }
  param {
    lr_mult:2
    decay_mult:0
  }
  Inner_product_param {
    num_output:1000 #改成你的数据集类别数
    weight_filler {
      type: "Xavier"
    }
    Bias_filler {
      type: ' Constant '
      value:0
    }
  }
}

Layer {
  name: "Loss3/classifier"
  type: "Innerproduct"
  Bottom: "pool5/7x7_s1"
  Top: "Loss3/classifier "
  param {
    lr_mult:1
    decay_mult:1
  }
  param {
    lr_mult:2
    decay_mult:0
  }
  Inner _product_param {
    num_output:1000 #改成你的数据集类别数
    weight_filler {
      type: "Xavier"
    }
    Bias_filler {
      type: "Constant"
      value:0
    }
  }
}


3. Open Deploy.prototxt, modify:

Layer {
  name: "Loss3/classifier"
  type: "Innerproduct"
  Bottom: "pool5/7x7_s1"
  Top: "loss3/ Classifier "
  param {
    lr_mult:1
    decay_mult:1
  }
  param {
    lr_mult:2
    decay_mult:0
  }
  Inner_product_param {
    num_output:1000 #改成你的数据集类别数
    weight_filler {
      type: "Xavier"
    }
    Bias_ Filler {
      type: ' Constant '
      value:0
    }
  }
}
If it is fine-tuned, the layer name and Train_val.prototxt are modified consistently.


Next, open solver, modify:

NET: "Examples/imagenet/bvlc_googlenet/train_val.prototxt" #路径不要错
test_iter:1000 #前面已说明该值
test_interval: 4000 #迭代多少次测试一次
test_initialization:false
display:40
average_loss:40
base_lr:0.01
lr_policy : "Step"
stepsize:320000 #迭代多少次改变一次学习率
gamma:0.96
max_iter:10000000 #迭代次数
momentum:0.9
weight_decay:0.0002
snapshot:40000
snapshot_prefix: "Examples/imagenet/bvlc_googlenet" # The generated Caffemodel is saved under Imagenet, as Bvlc_googlenet_iter_***.caffemodel
Solver_mode:gpu

At this point, we go back to caffe-master\examples\imagenet, open train_caffenet.sh, modify:

(in the case of fine tuning, add-weights **/**/**.caffemodel to the script, i.e. Caffemodel path to fine-tune)

#!/usr/bin/env sh

./build/tools/caffe train \
    -solver Examples/imagenet/bvlc_googlenet/solver.prototxt- GPU 0
(If you have multiple GPUs, you can choose your own)
Then, execute the change script under Caffe-master to start training: $caffe-master./examples/imagenet/train_caffenet.sh


Trained Caffemodel can be used to do image classification, at this time, need (1) to get the Labels.txt, (2) Get the Mydata_ Mean.binaryproto, (3) obtained Caffemodel and modified Deploy.prototxt, a total of four files, the specific process to see: http://blog.csdn.net/sinat_30071459/ article/details/50974695

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.