Since I have installed Caffe, here I will adopt it to my own application. The aim of my examples is for binary classification of images. I will exploit the trained bvlc_reference_caffenet model and then fine tune it for my application.

The whole steps are as follows:


[TOC]

Data Preparation

Prepare original data

You need to prepare four files:

  • train folder which contains the training images
  • val folder which contains the testing images
  • train.txt file which contains the labels of training images.
  • val.txt file which contains the labels of testing images.

    Note that the order of image name in *.txt file is equal to that in train folder and val folder.

The train.txt looks like:

1

That means train folder includes two subfolders cat and dog. In each subfolder, it contains the cat or dog images relatively.

What you need to do is going to caffe_master/data folder, creating a new folder named myself, then putting the four files above into it.

So now in myself folder, you can see four files: train, val, train.txt, val.txt

Transform data format

Later we will use some tools to transform the image files to the data format which ImageNet model consumes.

  1. Copy all *.sh files in caffe-master/examples/imagenet to myself folder.
  2. Change the file path in create_imagenet.sh
  3. Run it and then it will generate myself_train_lmdb and myself_val_lmdb in myself

Compute the mean value

  1. Change the file path in make_imagenet_mean.sh
  2. Run it and it will generate myself_mean.binaryproto in myself

Okay, till now, you have prepared all the data the ImageNet Model needs.

Under myself folder, you can see:

  • myself_train_lmdb folder: it contains the training data
  • myself_val_lmdb folder: it contains the testing data
  • myself_mean.binaryproto: it is the mean value

Fine Tune the trained model

Firstly, we need to download the trained model.

1
2
# the root path is "caffe-master"
./scripts/download_model_binary.py models/bvlc_reference_caffenet # it will take some time to download it

After that you need to fine tune it.

  • change the train_val.prototxt
    (1) change the input data path relatively

    (2) We change the name of last layer from “fc8” to “fc8_myself”. Because there is no layer named “fc8_myself”, it’s weights will be retrained according to random weights.

    (3) change the params of the last layer

1
2
3
4
5
6
7
8
9
10
11
12
13
param{
lr_mult: 10
decay_mult: 1
}
param{
lr_mult: 20
decay_mult: 0
}
inner_product_param{
num_output: 2
...
...
}
  • set up the solver.prototxt
1
2
3
4
5
6
7
8
9
10
11
12
13
14
  net:"models/myself_caffenet/train_val.prototxt" 
test_iter: 100
test_interval: 500
base_lr: 0.001
lr_policy: "step"
gamma: 0.1
stepsize: 10000
display: 20
max_iter: 50000 # numofTotalImg / batch_size * 10
momentum: 0.9
weight_decay: 0.0005
snapshot: 2000
snapshot_prefix: "models/myself_caffenet/caffenet_train"
solver_mode: GPU

Train the model

1
2
3
./build/tools/caffe train \
-solver models/myself_caffenet/solver.prototxt \
-weights models/myself_caffenet/bvlc_reference_caffenet.caffemodel

The results will be showed as follows:
train

Test the model

1
./build/tools/caffe test -model=models/myself_caffenet/train_val.prototxt -weights=models/myself_caffenet/caffenet_iter_1000.caffemodel

The results will be showed:
train

相关文章
评论
分享
  • T Bill vs T Notes vs T Bonds

    T 指的是 Treasury, 国债,债券发行机构为美国中央政府(federal government),为了解决政府日常运营所需资金的问题,例如遇到财政赤字时,不仅可以通过增加税收的方式,也可以通过发债的方式解决政府资金短缺。 美...

    T Bill vs T Notes vs T Bonds
  • 加德满都/巴德岗4日游--最幸福的地区

    时间:20190605-20190608; 从广州乘坐飞机直达加德满都,4个小时。 飞机上可以远远看到珠峰 加德满都海拔1340米左右,比北京晚2小时15分 D1: 加德满都泰米尔到达加德满都机场(标记1)为当地时间11点左右。订的酒...

    加德满都/巴德岗4日游--最幸福的地区
  • I graduated from Nanjing Normal University, majoring in computer science and technology. Now I am applying for Master...

  • 身份信息泄露,南京警方让回户籍所在地报警,户籍所在地警方让去支付宝所在的派出所报警,我到底该去哪里报警??为什么求助那么困难??

    本人南京市溧水区石湫镇人,南京大学毕业,目前香港工作。前段时间遇到一件特别心塞的事情,身份信息被盗,总计盗款38000元左右,已报警,不立案。报警过程更加心塞。 事情大概是这样的: 身份被盗:我的身份证号码(未丢失)+银行卡号码(...

    身份信息泄露,南京警方让回户籍所在地报警,户籍所在地警方让去支付宝所在的派出所报警,我到底该去哪里报警??为什么求助那么困难??
  • 美西之行七 Bryce Canyon

    美西之行七 Bryce Canyon今天前往Bryce Canyon。 路上的视野很开阔,颜色也较之前变得小清新了一些。 看到一辆RV车,载着一家随处转悠 可爱的大叔主动配合照相 马场 小木屋 到达Bryce Canyon 又开始浓...

    美西之行七 Bryce Canyon
  • 美西之旅六 Arches Canyon

    美西之旅六 Arches Canyon离开blanding之后,驱车前往Arches Canyon,中文名为拱石国家公园。 沿途的颜色开始变成橙黄 路上碰到一只被撞死的小鹿,身体还是温热的。这在西部是经常发生的事情 晚上住在一个叫...

    美西之旅六 Arches Canyon
  • 美西之旅五大峡谷到Blanding

    美西之旅 Grand Canyon –> Blanding小镇 离开Page之后,计划去羚羊谷,但是去羚羊谷要经过一片私人领地,造成景区价格偏高,朋友说他之前去过并没有网上照片拍的那么美,同时我们也要赶路,所以决定不再进入羚羊...

    美西之旅五大峡谷到Blanding
  • 美西之旅四大峡谷

    美西之旅 大峡谷(Grand Canyon)进入国家公园第一站--大峡谷。 大峡谷模拟图 我们直接坐蓝线到底,之后一站一个景点下来走 大峡谷 光影倒映在山谷,为其增添新色彩 走走走,拍拍拍 逛完南峡谷,准备驱车进入北峡谷。 北峡谷入...

    美西之旅四大峡谷
  • 美西之旅三 拉斯维加斯到大峡谷

    美西之旅 拉斯维加斯->大峡谷 离开Vegas之后,我们驱车前往Grand Canyon大峡谷。Vegas外围的民宅,沙漠中的家园。每家每户门前都栽种着一颗绿树,即使再荒凉,心中依然绿树成荫。 在去往大峡谷的中途会经过Hover...

    美西之旅三 拉斯维加斯到大峡谷
  • 美西之旅之拉斯维加斯

    美西之旅 洛杉矶->拉斯维加斯 早上9点多从洛杉矶出发,沿着15号公路向东北方向驶去,进入拉斯维加斯。从洛杉矶到拉斯维加斯大概4个小时车程,途中经过两个很大的OutLet,买买买忘了时间,大概到晚上12点多才到拉斯。原本计划只在...

    美西之旅之拉斯维加斯
Please check the comment setting in config.yml of hexo-theme-Annie!