当我们训练完一个网络模型后,就该考虑怎么使用这个辛苦训练出来的模型了。此时,我们需要修改原本用于train和val的网络结构。
一、去除训练用的网络层
- data layer
如:
layer {
name: "mnist"
type: "Data"
top: "data"
top: "label"
include {
phase: TRAIN
}
transform_param {
scale: 0.00390625
}
data_param {
source: "examples/mnist/mnist_train_lmdb"
batch_size: 64
backend: LMDB
}
}
accuracy layer
layer { name: "accuracy" type: "Accuracy" bottom: "ip2" bottom: "label" top: "accuracy" include { phase: TEST }
- loss layer
如:
layer {
name: "loss"
type: "SoftmaxWithLoss"
bottom: "ip2"
bottom: "label"
top: "loss"
}
二、指定输入数据格式
caffe 官方给出的例子如下:
input: "data"
input_shape {
dim: 1 # batchsize
dim: 3 # number of colour channels - rgb
dim: 32 # width
dim: 32 # height
}
三、添加需要的softmax
当模型用于多分类时,经常用到softmax来处理,所以需要添加不带loss的softmax层:
layer {
name: "loss"
type: "Softmax"
bottom: "ip2"
top: "loss"
}
四、编写deploy.sh文件
当deploy.prototxt编写好了之后,就要应用它。
TOOLS=/path/to/your/caffe/build/tools
$TOOLS/caffe test --weight=/path/to/your/caffemodel
--model=/path/to/your/deploy