准备数据集
train.txt test.txt
每行一个图片绝对路径+空格+类别标号,eg “绝对path/001.jpg 0”
构建model
VGG主页 http://www.robots.ox.ac.uk/~vgg/research/very_deep/
下载weights(VGG_ILSVRC_16_layers.caffemodel)和网络prototxt(VGG_ILSVRC_16_layers_deploy.prototxt)
VGG_ILSVRC_16_layers_deploy.prototxt 只包含vgg的backbone部分,还需要自己加入data layer和loss layer
除特殊声明,涉及的路径均为相对于caffe_root的相对路径。
VGG_train_val.prototxt
name: "VGG_ILSVRC_16_layers"
layer {
name: "data"
type: "ImageData"
# type: "AnnotatedData"
include {
phase: TRAIN
}
transform_param {
#crop_size: 224
mean_value: 104
mean_value: 117
mean_value: 123
mirror: true
}
image_data_param {
source: "examples/garbage/trainval.txt"
batch_size: 8
new_height: 224
new_width: 224
}
# data_param {
# source: "examples/garbage/garbage_trainval_lmdb"
# batch_size: 8
# backend: LMDB
# }
top: "data"
top: "label"
}
layer {
name: "data"
type: "ImageData"
# type: "AnnotatedData"
include {
phase: TEST
}
transform_param {
#crop_size: 224
mean_value: 104
mean_value: 117
mean_value: 123
mirror: false
}
image_data_param {
source: "examples/garbage/trainval.txt"
batch_size: 8
new_height: 224
new_width: 224
}
# data_param {
# source: "examples/garbage/garbage_trainval_lmdb"
# batch_size: 8
# backend: LMDB
# }
top: "data"
top: "label"
}
###########################################
# VGG_ILSVRC_16_layers_deploy.prototxt
##########################################
layer {
name: "loss"
type: "SoftmaxWithLoss"
bottom: "fc8"
bottom: "label"
top: "loss/loss"
}
layer {
name: "accuracy/top1"
type: "Accuracy"
bottom: "fc8"
bottom: "label"
top: "accuracy@1"
include: { phase: TEST }
accuracy_param {
top_k: 1
}
}
layer {
name: "accuracy/top5"
type: "Accuracy"
bottom: "fc8"
bottom: "label"
top: "accuracy@5"
include: { phase: TEST }
accuracy_param {
top_k: 5
}
}
注意,原VGG用于imagenet分类任务,有1000类别,所有fc8的num_output为1000,因此需要将fc8的num_output改成自己的类别数。同时修改fc8的name,比如改成“fc8_new”,这样加载预训练model时便会跳过这一层,否则会因为shape不匹配报错。(当然也可以直接修改VGG_ILSVRC_16_layers.caffemodel,将fc8去掉)
layer {
name: "fc8_new"
bottom: "fc7"
top: "fc8"
type: "InnerProduct"
inner_product_param {
num_output: 40 #1000
weight_filler {
type: "xavier"
}
bias_filler {
type: "constant"
value: 0
}
}
param {
lr_mult: 1
decay_mult :1
}
param {
lr_mult: 2
decay_mult: 0
}
}
solver.prototxt
net: "models/garbage/512x512/VGG_train_val.prototxt"
test_iter: 10000
test_interval: 40000
test_initialization: false
display: 200
base_lr: 0.0001
lr_policy: "step"
stepsize: 320000
gamma: 0.1
max_iter: 10000000
momentum: 0.9
weight_decay: 0.0005
snapshot: 800000
snapshot_prefix: "models/vgg/vgg"
solver_mode: GPU
train命令
cd [caffe_root]
./build/tools/caffe train \
--solver="models/garbage/VGG_512x512/solver.prototxt" \
--weights="models/VGG_ILSVRC_16_layers.caffemodel" \
--gpu 0,1 2>&1
test命令
cd [caffe_root]
./build/tools/caffe test \
--model="models/garbage/VGG_512x512/VGG_train_val.prototxt" \
--weights="models/garbage/VGG_512x512/VGG_iter_XX.caffemodel" \
--gpu 0,1 2>&1