caffe技巧总结

caffe的一些小技巧

启动脚本的参考版本

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
#!/usr/bin/env sh
set -e

log_path="logs/"
mkdir -p $log_path

save_model_path="caffemodel/"
mkdir -p $save_model_path

# training log
file_prefix="VDSR-20_"
log_file=$(date -d "today" +"%Y-%m-%d-%H:%M:%S")
log_file=$log_path$file_prefix$log_file".log"

# caffe execute file path
caffe_bin="/home/tony/caffe/build/tools/caffe"


# trainning
# $caffe_bin train --solver=VDSR_solver.prototxt 2>&1 | tee -a $log_file
# resume training
$caffe_bin train --solver=VDSR_solver.prototxt --snapshot='./caffemodel/VDSR-20_iter_71000.solverstate' -gpu 0 2>&1 | tee -a $log_file
# fine-tuning
# $caffe_bin train --solver=VDSR_solver.prototxt --weights='./caffemodel/VDSR-20_iter_24000.caffemodel' -gpu 0 2>&1 | tee -a $log_file

利用convert_imageset生成lmdb格式的训练、测试数据

路径:/caffe/build/tools
作用:生成lmdb格式的数据
用法:

1
2
./convert_imageset 图片路径 标签路径 生成的lmdb文件的存放路径
./convert_imageset /mnist/MNIST_data/train-images.idx3-ubyte /mnist/MNIST_data/train-labels.idx1-ubyte /lmdb/train_lmdb
报错:当生成的lmdb文件的存放路径已经存在的时候,使用convert_imageset会报错。

利用compute_image_mean计算均值文件

路径:/caffe/build/tools
作用:计算lmdb格式数据的均值文件,均值文件主要用于数据预处理的减均值处理,做减均值处理属于数据归一化的一种方式,进行这样的操作主要是出于以下几方面的考虑:
1、在自然图像上进行训练时,对每一个像素单独估计均值和方差意义不大,因为(理论上)图像任一部分的统计性质都应该和其它部分相同,图像的这种特性被称作平稳性(stationarity)。
2、基本在deep learning中只要你是使用gradient descent来训练模型的话都要在数据预处理步骤进行数据归一化。为什么要采用这种预处理方式呢? 首先如果输入层 x 很大,在back propagation时传递到输入层的梯度就会很大,如下式: 我们知道如果梯度非常大,学习率就必须非常小(否则会跳过local minimum(局部最小值)),因此,学习率(学习率初始值)的选择需要参考输入层的数值,不如直接将数据归一化,这样学习率就不必再根据数据范围作调整。

用法:

1
2
./convert_imageset lmdb文件路径(以这个文件路径中的lmdb文件来计算均值文件) 计算得到的均值文件的存放路径
./convert_imageset /lmdb/train_lmdb /mnist/mean_file/mean.binaryproto

利用draw_net.py绘制网络结构图

路径: /caffe/python 作用:绘制神经网络的结构 依赖:

1
2
pip install protobuf
sudo apt install python-pydot python-pydot-ng graphviz

用法:

1
2
3
4
5
6
7
python draw_net.py --rankdir 生成网络的图像方向(TB(从上到下)、BT、LR(从左到右)、RL)

python python/draw_net.py --rankdir TB examples/mnist/lenet_train_test.prototxt ./mnist_TB.png
Drawing net to ./mnist_TB.png

python python/draw_net.py --rankdir LR examples/mnist/lenet_train_test.prototxt ./mnist_TB.png
Drawing net to ./mnist_LR.png
从上到下

从左到右

另外也可以使用在线画图网站

http://ethereon.github.io/netscope/#/editor 或 https://dgschwend.github.io/netscope/#/editor


本博客所有文章除特别声明外,均采用 CC BY-SA 4.0 协议 ,转载请注明出处!