Caffe Matlab feature extraction 特征提取

来源:互联网 发布:dnf辅助源码论坛 编辑:程序博客网 时间:2024/06/03 23:39

Caffe 作为一款比较流行的DCNN特征提取框架已获得广泛应用。在CVPR/ICCV/ECCV关于DCNN的文章中屡屡出镜。Caffe的安装步骤比较繁琐,但是网上相关的配置文章也有很多,本文就不再啰嗦。

其中基于python的Caffe特征抽取可参考http://nbviewer.ipython.org/github/BVLC/caffe/blob/master/examples/net_surgery.ipynb

本人由于要对Deep CNN Feature 作进一步的处理,而处理的算法需要在Matlab环境中进行,所以需要Matcaffe提取图像的feature,现在总结了两种方法。


首先是Ross Girshick大神的解决方法,需要修改deploy.prototxt文件,然后直接 scores=net.forward(input_data),利用forward函数即可得到。该方法要求对deploy.prototxt文件比较熟悉,然后缺点就在于需要修改多次prototxt才能得到不同layer feature。


还有一种方法:caffe_feat=net.blobs('conv1').get_data(),得到4-D single 的矩阵,就为该层(conv1)的特征,caffe_feat(:,:,1,1)为其中一feature map。


另外,MatConvNet也是一个不错的工具,相对于Caffe更适用于Matlab,有兴趣可以尝试下。


——————————————————分割线2016.01.07————————————————————————

Caffe官网 http://caffe.berkeleyvision.org/tutorial/interfaces.html 给出了Python、Matlab下的各种接口,

caffe.set_mode_gpu();caffe.set_device(gpu_id);
net = caffe.Net(model, weights, 'test'); % create net and load weights


net.blobs('data').set_data(ones(net.blobs('data').shape));

可以更改各种参数:

net.params('conv1', 1).set_data(net.params('conv1', 1).get_data() * 10); % set weightsnet.params('conv1', 2).set_data(net.params('conv1', 2).get_data() * 10); % set biasnet.layers('conv1').params(1).set_data(net.layers('conv1').params(1).get_data() * 10);net.layers('conv1').params(2).set_data(net.layers('conv1').params(2).get_data() * 10);

然后保存网络,可以:

net.save('my_net.caffemodel');

net.blobs('data').set_data(data);net.forward_prefilled();prob = net.blobs('prob').get_data();

训练:

solver = caffe.Solver('./models/bvlc_reference_caffenet/solver.prototxt');
solver.solve();

solver.step(1000);iter = solver.iter();%To get its network:train_net = solver.net;test_net = solver.test_nets(1);solver.restore('your_snapshot.solverstate');

im_data = caffe.io.load_image('./examples/images/cat.jpg');im_data = imresize(im_data, [width, height]); % resize using Matlab's imresize%orim_data = imread('./examples/images/cat.jpg'); % read imageim_data = im_data(:, :, [3, 2, 1]); % convert from RGB to BGRim_data = permute(im_data, [2, 1, 3]); % permute width and heightim_data = single(im_data); % convert to single precision

Call caffe.reset_all() to clear all solvers and stand-alone nets you have created.




0 0