caffe操作技巧

查看网络结构:

(1)利用caffe自带的Python,可以将*.prototxt保存为一张图片,

sudo python python/draw_net.py *.prototxt *.png --rankdir=BT(或者,TB,LR,RL)

(2)利用Netscope,可以生成网络结构,并带有详细信息,

http://ethereon.github.io/netscope/quickstart.html

http://ethereon.github.io/netscope/#/editor

随机初始化训练:

./build/tools/caffe train --solver=models/bvlc_reference_caffenet/solver.prototxt --gpu=-0,1

微调:

./build/tools/caffe train --solver=models/bvlc_reference_caffenet/solver.prototxt --weights=models/bvlc_reference_caffenet/caffenet_train_iter_10000.caffemodel--gpu=-0,1

从中断处继续训练:

./build/tools/caffe train --solver=models/bvlc_reference_caffenet/solver.prototxt --snapshot=models/bvlc_reference_caffenet/caffenet_train_iter_10000.solverstate

统计在验证集(validation set)上的得分:

./build/tools/caffe test --model= models/bvlc_reference_caffenet/caffenet_train_iter_10000.prototxt--weights= models/bvlc_reference_caffenet/caffenet_train_iter_10000.caffemodel--gpu=0 --iterations=10000

统计训练时间:

# 在 CPU上, 10000iterations训练 caffenet的时间

./build/tools/caffe time --model= models/bvlc_reference_caffenet/caffenet_train_test.prototxt--iterations=10000

# 在 GPU上,默认的 50 iterations训练 caffenet的时间

./build/tools/caffe time --model= models/bvlc_reference_caffenet/caffenet_train_test.prototxt--gpu=0

# 在第一块 GPU上, 10000 iterations训练已给定权值的网络结构的时间

./build/tools/caffe time --model= models/bvlc_reference_caffenet/caffenet_train_test.prototxt--weights= models/bvlc_reference_caffenet/caffenet_train_iter_10000.caffemodel--gpu=0 --iterations=10000

查询GPU显卡参数信息:

# 查询第一块 GPU

./build/tools/caffe device_query --gpu=0

输出训练log日志到txt:

(1)GLOG_logtostderr=0 GLOG_log_dir=./Log/ ./build/tools /caffe train --solver=./deepid_solver.prototxt

(2) ./build/tools/caffe train --solver=./deepid_solver.prototxt >&log.txt&

解析日志:

会在当前文件夹下生成一个.train文件和一个.test文件

./TOOLS/extra/parse_log.sh *.log

生成曲线图:

./tools/extra/plot_training_log.py.example 0 *.png *.log

Notes:

1. Supporting multiple logs.

2. Log file name must end with the lower-cased ".log".

Supported chart types:

0: Test accuracy vs. Iters

1: Test accuracy vs. Seconds

2: Test loss vs. Iters

3: Test loss vs. Seconds

4: Train learning rate vs. Iters

5: Train learning rate vs.Seconds

6: Train loss vs. Iters

7: Train loss vs. Seconds

计算训练数据均值:

# sudo build/tools/compute_image_mean examples/mnist/mnist_train_lmdbexamples/mnist/mean.binaryproto

生成训练数据的LMDB文件:

convert_imageset [FLAGS] ROOTFOLDER/ LISTFILE DB_NAME

FLAGS:

--gray: 是否以灰度图的方式打开图片。程序调用OpenCV库中的imread()函数来打开图片,默认为false

--shuffle: 是否随机打乱图片顺序。默认为false

--backend:需要转换成的db文件格式,可选为leveldb或lmdb,默认为lmdb

--resize_width/resize_height: 改变图片的大小。在运行中,要求所有图片的尺寸一致,因此需要改变图片大小。程序调用opencv库的resize()函数来对图片放大缩小,默认为0,不改变

--check_size: 检查所有的数据是否有相同的尺寸。默认为false,不检查

--encoded: 是否将原图片编码放入最终的数据中,默认为false

--encode_type: 与前一个参数对应,将图片编码为哪一个格式:‘png\',\'jpg\'......

ROOTFOLDER:

图片的绝对路径

LISTFILE:

图片txt文件,格式为.txt,内容为,图片 标签

DB_NAME:

保存的文件名

build/tools/convert_imageset --shuffle --resize_height=256 --resize_width=256 /home/xxx/caffe/examples/images/ ./train.txt  ./img_train_lmdb

matlab写caffe程序注意事项:

由于matlab的长宽和c++中opencv的长宽正好相反,同时matlab中是rgb通道,opencv中是bgr通道,因此,程序需要做这么2个变换。这里给出2种处理方式,

(1)直接调用caffe接口,

im_data =caffe.io.load_image(\'./examples/images/cat.jpg\');

(2)用matlab函数自己实现,

im_data = imread(\'./examples/images/cat.jpg\');% read image

im_data = im_data(:, :, [3, 2, 1]); % 从 RGB转换为 BGR

im_data = permute(im_data, [2, 1, 3]); % 改变 width与 height位置

im_data = single(im_data); % 转换为单精度

caffe模型转tensorflow工具:

https://github.com/ethereon/caffe-tensorflow

手动标注图像,生成VOC支持的XML文件工具:

https://github.com/tzutalin/labelImg

matlab将caffe模型weights中不需要的部分去掉:

去掉模型的最后一个全连接层参数,减少模型的大小,适用于只提取特征而不进行分类的开集合场景应用。

net = caffe.NET(\'XX_deploy.prototxt\', \'XX.caffemodel\', \'test\');

net.save(\'XX_remove_the_last_fc.caffemodel\');