这次的笔记是记录如何从预先训练的模型中提取特征。仍然是由于Windows版本Caffe的问题,绕了一些弯路。
参考资料:Caffe官方文档:Extracting Features
正文如下:
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/eature_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/Debug/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,查看: