• 设为首页
  • 点击收藏
  • 手机版
    手机扫一扫访问
    迪恩网络手机版
  • 关注官方公众号
    微信扫一扫关注
    迪恩网络公众号

学习笔记之 初试Caffe,Matlab接口提取feature

原作者: [db:作者] 来自: [db:来源] 收藏 邀请

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,生成10227×227的图像,所以最后的feature4096×10
的矩阵,而不是4096的向量。。。











鲜花

握手

雷人

路过

鸡蛋
该文章已有0人参与评论

请发表评论

全部评论

专题导读
上一篇:
matlab中如何定义函数发布时间:2022-07-18
下一篇:
matlab矩阵的表示和简单操作发布时间:2022-07-18
热门推荐
阅读排行榜

扫描微信二维码

查看手机版网站

随时了解更新最新资讯

139-2527-9053

在线客服(服务时间 9:00~18:00)

在线QQ客服
地址:深圳市南山区西丽大学城创智工业园
电邮:jeky_zhao#qq.com
移动电话:139-2527-9053

Powered by 互联科技 X3.4© 2001-2213 极客世界.|Sitemap