Install and fune-tune caffe on Ubuntu

来源:互联网 发布:知乎哪个国家公司 编辑:程序博客网 时间:2024/06/14 03:31

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.trainNumIters,Seconds,LearningRate,loss6000.0,10.468114,1e-06,0.04761566020.0,17.372427,1e-06,0.01956396040.0,24.237645,1e-06,0.05562746060.0,31.084703,1e-06,0.02446566080.0,37.927866,1e-06,0.03255826100.0,44.778659,1e-06,0.01312746120.0,51.62342,1e-06,0.0607449# my_model.log.testNumIters,Seconds,LearningRate,accuracy,loss6000.0,10.33778,1e-06,0.9944,0.02058596500.0,191.054363,1e-06,0.9948,0.01916567000.0,372.292923,1e-06,0.9951,0.01860957500.0,583.508988,1e-06,0.9947,0.02112638000.0,806.678746,1e-06,0.9947,0.01928248500.0,1027.549856,1e-06,0.9953,0.01839179000.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.shpython ~/caffe/tools/extra/parse_log.py my_model.log .gnuplot -persist gnuplot_commandswhere gnuplot_commands is a file that stores a set of gnuplot commands.# gnuplot_commandsset datafile separator ','set term x11 0plot '../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 1plot '../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:

cneuukus

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.

原创粉丝点击