matlab调试caffe,在matlab下调试caffe -ag凯发k8国际
caffe本身是c 、cuda语言编写的。在调试模型、参数时,根据运行log、snapshot很难实时反馈当前训练的权值情况,也难以捕捉算法存在的bug。
matlab则是非常适合算法设计、快速迭代的利器,只需要做少量工作就能编写出复杂的算法,调试非常方便,位于workspace中的变量随时都能打印,无论是一维、二维还是三维数据,都能直观显示,从而有利于定位算法设计问题,减少调试时间。
caffe中有两种wrapper:python和matlab。python是开源工具,用户无需付费即可使用,缺点是语法不够灵活,尤其算法描述,与商业软件不能比。matlab支持几乎你所知道的所有矩阵变换、数值计算、随机过程、概率论、最优化、自适应滤波、图像处理、神经网络等算法。
下面介绍如何用matlab调试caffe。本文假设操作系统为ubuntu 14.04.1 64bit .
1. 安装matlab r2014a
安装步骤类似windows,不表。安装到~/matlab/,~/.bashrc中添加 export path=~/matlab/bin:$path
2. 安装caffe
如果你希望自己编译依赖,可以到这里下载caffe所有依赖包(http://yunpan.taobao.com/s/1i1txcpysk3,提取码:yuqzm1)
3. 编译 matcaffe
修改makefile.config,加上这一句:
matlab_dir := ~/matlab
之后
make matcaffe
生成了 matlab/ caffe/private/caffe_.mex64,可以直接被matlab调用。
4. 运行matlab例子
在命令行中,配置好caffe运行所需要的环境变量后(否则matcaffe会运行失败),输入matlab&,这样就启动了matlab窗口。
在matlab命令窗口中进行以下步骤。
>> cd caffe_root_directory/
切换到了caffe根目录。
>> addpath('./matlab/ caffe/private');
添加matcaffe模块所在路径到matlab搜索路径,便于加载。
>> cd matlab/demo/
切到demo目录。
>> im = imread('../../examples/images/cat.jpg');
读取一张测试图片。
>> figure;imshow(im);
弹出一个窗口,显示猫的测试图片如下:
>> [scores, maxlabel] = classification_demo(im, 1);
elapsed time is 0.533388 seconds.
elapsed time is 0.511420 seconds.
cleared 0 solvers and 1 stand-alone nets
运行分类demo程序。分类的结果返回到scores,maxlabel两个工作空间变量中。
>> maxlabel
maxlabel =
282
说明最大分类概率的标签号为282,查找imagenet标签,对应的是n02123045 tabby, tabby cat(data/ilsvrc2012/synset_words.txt)
>> figure;plot(scores);
>> axis([0, 999, -0.1, 0.5]);
>> grid on
打印scores,一维图像如下:
说明这张图片被分到第282类的概率为0.2985。
到这里我们只是运行了简单的demo,接下来分析classification_demo.m这个文件内容。
function [scores, maxlabel] = classification_demo(im, use_gpu)
% [scores, maxlabel] = classification_demo(im, use_gpu)
% 使用bvlc caffenet进行图像分类的示例
% 重要:运行前,应首先从model zoo(http://caffe.berkeleyvision.org/model_zoo.html) 下载bvlc caffenet训练好的权值
%
% ****************************************************************************
% for detailed documentation and usage on caffe's matlab interface, please
% refer to caffe interface tutorial at
% http://caffe.berkeleyvision.org/tutorial/interfaces.html#matlab
% ****************************************************************************
%
% input
% im color image as uint8 hxwx3
% use_gpu 1 to use the gpu, 0 to use the cpu
%
% output
% scores 1000-dimensional ilsvrc score vector
% maxlabel the label of the highest score
%
% you may need to do the following before you start matlab:
% $ export ld_library_path=/opt/intel/mkl/lib/intel64:/usr/local/cuda-5.5/lib64
% $ export ld_preload=/usr/lib/x86_64-linux-gnu/libstdc .so.6
% or the equivalent based on where things are installed on your system
%
% usage:
% im = imread('../../examples/images/cat.jpg');
% scores = classification_demo(im, 1);
% [score, class] = max(scores);
% five things to be aware of:
% caffe uses row-major order
% matlab uses column-major order
% caffe uses bgr color channel order
% matlab uses rgb color channel order
% images need to have the data mean subtracted
% data coming in from matlab needs to be in the order
% [width, height, channels, images]
% where width is the fastest dimension.
% here is the rough matlab for putting image data into the correct
% format in w x h x c with bgr channels:
% % permute channels from rgb to bgr
% im_data = im(:, :, [3, 2, 1]);
% % flip width and height to make width the fastest dimension
% im_data = permute(im_data, [2, 1, 3]);
% % convert from uint8 to single
% im_data = single(im_data);
% % reshape to a fixed size (e.g., 227x227).
% im_data = imresize(im_data, [image_dim image_dim], 'bilinear');
% % subtract mean_data (already in w x h x c with bgr channels)
% im_data = im_data - mean_data;
% if you have multiple images, cat them with cat(4, ...)
% add caffe/matlab to you matlab search path to use matcaffe
if exist('../ caffe', 'dir')
addpath('..');
else
error('please run this demo from caffe/matlab/demo');
end
% set caffe mode
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
% initialize the network using bvlc caffenet for image classification
% weights (parameter) file needs to be downloaded from model zoo.
model_dir = '../../models/bvlc_reference_caffenet/'; % 模型所在目录
net_model = [model_dir 'deploy.prototxt']; % 模型描述文件,注意是deploy.prototxt,不包含data layers
net_weights = [model_dir 'bvlc_reference_caffenet.caffemodel']; % 模型权值文件,需要预先下载到这里
phase = 'test'; % run with phase test (so that dropout isn't applied) % 只进行分类,不做训练
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); % 初始化网络
if nargin
% for demo purposes we will use the cat image
fprintf('using caffe/examples/images/cat.jpg as input image\n');
im = imread('../../examples/images/cat.jpg'); % 获取输入图像
end
% 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); % 分类,得到scores
toc;
scores = scores{1};
scores = mean(scores, 2); % 取所有分类结果的平均值
[~, maxlabel] = max(scores); % 找到最大概率对应的标签号
% call caffe.reset_all() to reset caffe
caffe.reset_all();
% ------------------------------------------------------------------------
function crops_data = prepare_image(im)
% ------------------------------------------------------------------------
% caffe/matlab/ caffe/imagenet/ilsvrc_2012_mean.mat contains mean_data that
% is already in w x h x c with bgr channels
d = load('../ caffe/imagenet/ilsvrc_2012_mean.mat');
mean_data = d.mean_data;
image_dim = 256;
cropped_dim = 227;
% convert an image returned by matlab's imread to im_data in caffe's data
% format: w x h x c with bgr channels
im_data = im(:, :, [3, 2, 1]); % permute channels from rgb to bgr
im_data = permute(im_data, [2, 1, 3]); % flip width and height
im_data = single(im_data); % convert from uint8 to single
im_data = imresize(im_data, [image_dim image_dim], 'bilinear'); % resize im_data
im_data = im_data - mean_data; % subtract mean_data (already in w x h x c, bgr)
% oversample (4 corners, center, and their x-axis flips)
crops_data = zeros(cropped_dim, cropped_dim, 3, 10, 'single');
indices = [0 image_dim-cropped_dim] 1;
n = 1;
for i = indices
for j = indices
crops_data(:, :, :, n) = im_data(i:i cropped_dim-1, j:j cropped_dim-1, :);
crops_data(:, :, :, n 5) = crops_data(end:-1:1, :, :, n);
n = n 1;
end
end
center = floor(indices(2) / 2) 1;
crops_data(:,:,:,5) = ...
im_data(center:center cropped_dim-1,center:center cropped_dim-1,:);
crops_data(:,:,:,10) = crops_data(end:-1:1, :, :, 5);
总结
以上是ag凯发k8国际为你收集整理的matlab调试caffe,在matlab下调试caffe的全部内容,希望文章能够帮你解决所遇到的问题。
- 上一篇:
- 下一篇: