caffe提取网络各个层的特征
怎么把网络各个层的特征保存成数据呢?
调用
输入:
./build/tools/extract_features.bin \ //可执行程序
examples/mnist/lenet_iter_10000.caffemodel \ //预训练的网络,×.caffemodel
examples/mnist/lenet.prototxt \ //描述文件××.prototxt
conv1 \ //需要提取的层
myconv1 \ //保存的特征名
10 \ //做特征提取的数据批量数目
lmdb \ //输入数据的格式,lmdb/leveldb
GPU \ //cpu/gpu
0 //如果用GPU,则选择设备编号
GPU不行的话,现用CPU模式:
./build/tools/extract_features.bin examples/mnist/lenet_iter_10000.caffemodel examples/mnist/lenet.prototxt conv1 myconv1 10 lmdb CPU
调用的.prototxt文件内容
- 如果是deploy.prototxt文件的话
name: "CaffeNet"
input: "data"
input_shape {
dim: 10
dim: 3
dim: 227
dim: 227
}
deploy.prototxt里没有data层!那怎么输入图像???
caffe官网中的提取特征
Classification: Instant Recognition with Caffe
需要加上一层data层
name: "CaffeNet"
layer {
name: "data"
type: "ImageData"
top: "data"
top: "label"
transform_param {
mirror: false
crop_size: 227
mean_file: "data/ilsvrc12/imagenet_mean.binaryproto"
}
image_data_param {
source: "examples/_temp/file_list.txt"
batch_size: 50
new_height: 256
new_width: 256
}
}
运行实例
1、选择运行的数据
- 在examples/下新建一个文件夹,命名为_temp:
- 选择使用系统自带的examples\images下的图片:
- 将它们的路径,写成一个文本文档,命名为file_list.txt。
在每个路径的最后加上0,官方文档的解释是:“The ImageDataLayer we’ll use expects labels after each filenames, so let’s add a 0 to the end of each line” - file_list.txt截图如下:
2、定义特征提取网络结构
将examples/feature_extraction/imagenet_val.prototxt复制到之前新建的_temp文件夹。
在实际过程中,从数据集中减去图像均值可以显著的改善分类的准确率,所以使用ILSVRC数据集的均值图像,具体位置在data/ilsvrc12/imagenet_mean.binaryproto.
打开imagenet_val.prototxt,修改以下两个路径,对应准确即可:
3、提取特征
首先去models\bvlc_reference_caffenet文件夹中,查看是否有bvlc_reference_caffenet.caffemodel文件。若没有,需要自行下载。下载地址在此文件夹的readme文档中:
好了,现在需要的文件都准备好了。写一个bat文件,就可以进行特征的提取了。bat文件代码如下:
<span style="font-size:14px;">cd ../../
"Build/x64/Release/extract_features.exe" examples/temp/bvlc_reference_caffenet.caffemodel examples/temp/imagenet_val.prototxt conv5 examples/temp/features 10 leveldb
pause</span>
-
注意:
1. 我是先cd进入Caffe的根目录,所以代码中的路径这样填写。
2. conv5代表提取特征的blob的名称。我们也可以用其他层的名称,如fc7,pool3.
3. 对于数字10,官方文档解释为:“The last parameter above is the number of data mini-batches.”
4. 提取的特征保存在examples/_temp/features文件夹中。 -
在我的电脑上,bat文件运行结束的截图为:
-
进入examples/_temp/features,查看: