MatCaffe用法总结

最近做实验用到MatCaffe,本文就简单总结以下MatCaffe的用法。还没安装的赶快看官网教程安装吧。MatCaffe的用法与PyCaffe挺类似的。

model ='./models/bvlc_reference_caffenet/deploy.prototxt';
weights='./models/bvlc_reference_caffenet/bvlc_reference_caffenet.caffemodel';

上面是设置好的你的prototxt文件路径(网络结构文件)以及caffemodel文件路径(模型文件)。

设置mode和device

Mode和device应该在你创建net或者solver之前设置。
用CPU或者GPU:

caffe.set_mode_cpu();
Use GPU and specify its gpu_id:
caffe.set_mode_gpu();
caffe.set_device(gpu_id);

创建网络:

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

或者可以后来载入weights:

net = caffe.Net(model, 'test'); 
% create net but not load weights
net.copy_from(weights); 
% load weights

网络的具体结构是下面的样子:

       layer_vec: [1x23 caffe.Layer]
        
  • 5
    点赞
  • 15
    收藏
    觉得还不错? 一键收藏
  • 3
    评论
clear all; % TODO: Edit this to point to the folder your caffe mex file is in. % path_to_matcaffe = '/data/jkrause/cs231b/caffe-rc2/matlab/caffe'; path_to_matcaffe = 'C:/Users/DELL/Downloads/caffe-master/windows'; addpath(path_to_matcaffe) % Load up the image im = imread('peppers.png'); % Get some random image regions (format of each row is [x1 y1 x2 y2]) % Note: If you want to change the number of regions you extract features from, % then you need to change the first input_dim in cnn_deploy.prototxt. regions = [ 1 1 100 100; 100 50 400 250; 1 1 512 284; 200 200 230 220 100 100 300 200]; % Convert image from RGB to BGR and single, which caffe requires. im = single(im(:,:,[3 2 1])); % Get the image mean and crop it to the center mean_data = load('ilsvrc_2012_mean.mat'); image_mean = mean_data.image_mean; cnn_input_size = 227; % Input size to the cnn we trained. off = floor((size(image_mean,1) - cnn_input_size)/2)+1; image_mean = image_mean(off:off+cnn_input_size-1, off:off+cnn_input_size-1, :); % Extract each region ims = zeros(cnn_input_size, cnn_input_size, 3, size(regions, 1), 'single'); for i = 1:size(regions, 1) r = regions(i,:); reg = im(r(2):r(4), r(1):r(3), :); % Resize to input CNN size and subtract mean reg = imresize(reg, [cnn_input_size, cnn_input_size], 'bilinear', 'antialiasing', false); reg = reg - image_mean; % Swap dims 1 and 2 to work with caffe ims(:,:,:,i) = permute(reg, [2 1 3]); end % Initialize caffe with our network. % -cnn_deploy.prototxt gives the structure of the network we're using for % extracting features and is how we specify we want fc6 features. % -cnn512.caffemodel is the binary network containing all the learned weights. % -'test' indicates that we're only going to be extracting features and not % training anything init_key = caffe('init', 'cnn_deploy.prototxt', 'cnn512.caffemodel', 'test'); caffe('set_device', 0); % Specify which gpu we want to use. In this case, let's use the first gpu. caffe('set_mode_gpu'); %caffe('set_mode_cpu'); % Use if you want to use a cpu for whatever reason % Run the CNN f = caffe('forward', {ims}); % Convert the features to (num. dims) x (num. regions) feat = single(reshape(f{1}(:), [], size(ims, 4)));
05-22

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 3
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值