Install and fune-tune caffe on Ubuntu

Install and use caffe on Ubuntu


Install

__float128 is undefined

solve: change suffix.hpp line 510, from
extension typedef float128 float128_type;
to
__extension typedef long double float128_type;

Use

Data preparation

crop and resize

Crop and resize 800x600 images to 227x227 as we want to use pretrained models GoogleNet.
original image

copy file to remote server

将文件夹复制到服务器dlserver:
scp -r charger ga04@dlserver:/home/ga04/caffe/data/charger/
scp charger_*.txt ga04@dlserver:/home/ga04/caffe/data/charger/

convert_imageset

Make lmdb from image list.
convert_imageset ~/caffe/data/charger/ charger_train.txt charger_train_db
convert_imageset ~/caffe/data/charger/ charger_test.txt charger_test_db

第一次可能失败。把生成的文件夹charger_train_db 删掉再来一次,往往就可以了。。

compute_image_mean

error while loading shared libraries: libcaffe.so.1.0.0
solve: export LD_LIBRARY_PATH=/home/tkt/caffe/build/install/lib/x86_64-linux-gnu:$LD_LIBRARY_PATH

compute_image_mean charger_train_db charger_train_mean.binaryproto
compute_image_mean charger_test_db charger_test_mean.binaryproto

train (fine-tuning)

  1. Modify the solver.prototxt and train_val.prototxt files according to the data and model path. You can reference flickr_style for how to modify. Basically you need to
    1. change the name of the last fc8 layer in train_val.prototxt to something like fc8_charger, also the num_output to 4.
    2. change the learning rate in solver.prototxt to 1/10, stepsize to much smaller.
  2. At caffe root directory:
    caffe train -solver models/charger/solver.prototxt -weights
    models/bvlc_reference_caffenet/bvlc_reference_caffenet.caffemodel
    2>&1 | tee log/my_model.log

    where ... 2>&1 | tee -a log/my_model.log redirects the output from screen to my_model.log, can be omitted.

Now the cool things: Caffe has a script (/caffe/tools/extra/parse_log.py) to parse log files and return two much better formatted files.

# my_model.log.train
NumIters,Seconds,LearningRate,loss
6000.0,10.468114,1e-06,0.0476156
6020.0,17.372427,1e-06,0.0195639
6040.0,24.237645,1e-06,0.0556274
6060.0,31.084703,1e-06,0.0244656
6080.0,37.927866,1e-06,0.0325582
6100.0,44.778659,1e-06,0.0131274
6120.0,51.62342,1e-06,0.0607449
# my_model.log.test
NumIters,Seconds,LearningRate,accuracy,loss
6000.0,10.33778,1e-06,0.9944,0.0205859
6500.0,191.054363,1e-06,0.9948,0.0191656
7000.0,372.292923,1e-06,0.9951,0.0186095
7500.0,583.508988,1e-06,0.9947,0.0211263
8000.0,806.678746,1e-06,0.9947,0.0192824
8500.0,1027.549856,1e-06,0.9953,0.0183917
9000.0,1209.650574,1e-06,0.9949,0.0194651

And with a little bit trick, you can automate the parsing process and combine it with curve plotting using a script like this:

# visualize_log.sh
python ~/caffe/tools/extra/parse_log.py my_model.log .
gnuplot -persist gnuplot_commands
where gnuplot_commands is a file that stores a set of gnuplot commands.
# gnuplot_commands
set datafile separator ','
set term x11 0
plot '../my_model.log.train' using 1:4  with line title 'training loss',\
     '../my_model.log.test' using 1:5 with line title 'test loss'
set term x11 1
plot '../my_model.log.test' using 1:4 with line

Reference: http://shengshuyang.github.io/A-step-by-step-guide-to-Caffe.html

predict

create deploy.txt

To predict an image, you need to create a deploy.prototxt file from train_val.prototxt, which basically does following things:
1. replace the data layers with an input layer like this:

layer {
  name: "data"
  type: "Input"
  top: "data"
  input_param { shape: { dim: 10 dim: 3 dim: 227 dim: 227 } }
}
  1. remove weight and bias filler

create labels.txt

Create a labels.txt file as follows:

cn
eu
uk
us

run classification

Call classification as:
classification ../../models/charger/deploy.prototxt ../../models/charger/charger_train_iter_10000.caffemodel charger_train_mean.binaryproto labels.txt eu_template.bmp
Remember to do the exactly same preprocessing to image eu_template.bmp, otherwise classification result will be wrong.

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值