Caffe 提供了matlab接口,可以用于提取图像的feature.
首先,打开终端,进入caffe的主目录下,然后打开Matlab ...
默认的文件是:classification_demo.m, 里面有两个函数。把路径设置完了之后,就可以试试运行了。我把它改成了我比较习惯的方式,即:xiao.m
clc;
close all;
clear all;
num1=1000;
% 提取多少张图像的feature
?
use_gpu=1;
% 运行的模式,gpu
or cpu ?
if
exist('../+caffe',
'dir')
addpath('..');
else
error('Please
run this demo from caffe/matlab/demo');
end
if
exist('use_gpu',
'var') &&
use_gpu
caffe.set_mode_gpu();
gpu_id
= 0; % we will use the first gpu in
this demo
caffe.set_device(gpu_id);
else
caffe.set_mode_cpu();
end
model_dir
= '../../models/bvlc_reference_caffenet/';
net_model
= [model_dir 'deploy.prototxt'];
net_weights
= [model_dir 'bvlc_reference_caffenet.caffemodel'];
phase
= 'train';
if
~exist(net_weights, 'file')
error('Please
download CaffeNet from Model Zoo before you run this demo');
end
%
Initialize a network
net
= caffe.Net(net_model, net_weights, phase);
fprintf('Read
PETA-dataset images and Extract features ...\n');
dataPath
= '../../Link to PETA
dataset/3DPeS/archive/';
images
= dir([dataPath,'*.bmp']);
for
num=1:num1
%
disp('read the', num2str(num) ,'th', '/', num2str(num1) ,'image,
please waiting ...');
im
= imread([dataPath, images(num).name]);
fid1
= fopen('train_feature_id.txt',
'a');
fprintf(fid1,
'%s \n',
images(num).name );
fclose(fid1);
%
prepare oversampled input
%
input_data is Height x Width x Channel x Num
%
tic;
input_data
= {prepare_image(im)};
%
toc;
%
do forward pass to get scores
%
scores are now Channels x Num, where Channels == 1000
%
tic;
%
The net forward function. It takes in a cell array of N-D arrays
%
(where N == 4 here) containing data of input blob(s) and outputs a
cell
%
array containing data from output blob(s)
scores
= net.forward(input_data);
Score
= scores{1,1};
fid
= fopen('train_feature.txt',
'a');
fprintf(fid,
'%f \n',
Score);
fclose(fid);
%
toc;
%
scores = scores{1};
%
scores = mean(scores, 2); % take average scores over 10 crops
%
[~, maxlabel] = max(scores);
%
call caffe.reset_all() to reset caffe
%
caffe.reset_all();
end
这个函数调用了
prepare_image.m,因为caffe对图像的处理方式和常规的方式不同,需要对读取的图像做一个转换工作,而这个过程是由
prepare_image.m来完成的。
运行之后,就会生成两个txt文件,一个是读取图像的图像名字,另一个是对应所提取的feature。
(a) train_feature_id (b) train_feature
由于这个网络的输入是255×255的图像,会对其做一个crop,生成10个227×227的图像,所以最后的feature为4096×10 的矩阵,而不是4096的向量。。。