在MATLAB下调试Caffe

Caffe本身是C++、CUDA语言编写的。在调试模型、参数时,根据运行log、snapshot很难实时反馈当前训练的权值情况,也难以捕捉算法存在的bug。


MATLAB则是非常适合算法设计、快速迭代的利器,只需要做少量工作就能编写出复杂的算法,调试非常方便,位于workspace中的变量随时都能打印,无论是一维、二维还是三维数据,都能直观显示,从而有利于定位算法设计问题,减少调试时间。


Caffe中有两种Wrapper:Python和MATLAB。Python是开源工具,用户无需付费即可使用,缺点是语法不够灵活,尤其算法描述,与商业软件不能比。MATLAB支持几乎你所知道的所有矩阵变换、数值计算、随机过程、概率论、最优化、自适应滤波、图像处理、神经网络等算法。


下面介绍如何用MATLAB调试Caffe。本文假设操作系统为Ubuntu 14.04.1  64bit .


1. 安装MATLAB R2014A

可以到这里下载(http://yunpan.taobao.com/s/ZFLGQjNABU,提取码:dxBxMJ

安装步骤类似Windows,不表。安装到~/MATLAB/,~/.bashrc中添加 export PATH=~/MATLAB/bin:$PATH

2.  安装Caffe

参考步骤:http://caffe.berkeleyvision.org/install_apt.html。其他OS请参考http://caffe.berkeleyvision.org/installation.html

如果你希望自己编译依赖,可以到这里下载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这个文件内容。

[plain]  view plain  copy
 print ?
  1. function [scores, maxlabel] = classification_demo(im, use_gpu)  
  2. % [scores, maxlabel] = classification_demo(im, use_gpu)  
  3. % 使用BVLC CaffeNet进行图像分类的示例  
  4. % 重要:运行前,应首先从Model Zoo(http://caffe.berkeleyvision.org/model_zoo.html) 下载BVLC CaffeNet训练好的权值  
  5. %  
  6. % ****************************************************************************  
  7. % For detailed documentation and usage on Caffe's Matlab interface, please  
  8. % refer to Caffe Interface Tutorial at  
  9. % http://caffe.berkeleyvision.org/tutorial/interfaces.html#matlab  
  10. % ****************************************************************************  
  11. %  
  12. % input  
  13. %   im       color image as uint8 HxWx3  
  14. %   use_gpu  1 to use the GPU, 0 to use the CPU  
  15. %  
  16. % output  
  17. %   scores   1000-dimensional ILSVRC score vector  
  18. %   maxlabel the label of the highest score  
  19. %  
  20. % You may need to do the following before you start matlab:  
  21. %  $ export LD_LIBRARY_PATH=/opt/intel/mkl/lib/intel64:/usr/local/cuda-5.5/lib64  
  22. %  $ export LD_PRELOAD=/usr/lib/x86_64-linux-gnu/libstdc++.so.6  
  23. % Or the equivalent based on where things are installed on your system  
  24. %  
  25. % Usage:  
  26. %  im = imread('../../examples/images/cat.jpg');  
  27. %  scores = classification_demo(im, 1);  
  28. %  [score, class] = max(scores);  
  29. % Five things to be aware of:  
  30. %   caffe uses row-major order  
  31. %   matlab uses column-major order  
  32. %   caffe uses BGR color channel order  
  33. %   matlab uses RGB color channel order  
  34. %   images need to have the data mean subtracted  
  35.   
  36. % Data coming in from matlab needs to be in the order  
  37. %   [width, height, channels, images]  
  38. % where width is the fastest dimension.  
  39. % Here is the rough matlab for putting image data into the correct  
  40. % format in W x H x C with BGR channels:  
  41. %   % permute channels from RGB to BGR  
  42. %   im_data = im(:, :, [3, 2, 1]);  
  43. %   % flip width and height to make width the fastest dimension  
  44. %   im_data = permute(im_data, [2, 1, 3]);  
  45. %   % convert from uint8 to single  
  46. %   im_data = single(im_data);  
  47. %   % reshape to a fixed size (e.g., 227x227).  
  48. %   im_data = imresize(im_data, [IMAGE_DIM IMAGE_DIM], 'bilinear');  
  49. %   % subtract mean_data (already in W x H x C with BGR channels)  
  50. %   im_data = im_data - mean_data;  
  51.   
  52. % If you have multiple images, cat them with cat(4, ...)  
  53.   
  54. % Add caffe/matlab to you Matlab search PATH to use matcaffe  
  55. if exist('../+caffe', 'dir')  
  56.   addpath('..');  
  57. else  
  58.   error('Please run this demo from caffe/matlab/demo');  
  59. end  
  60.   
  61. % Set caffe mode  
  62. if exist('use_gpu', 'var') && use_gpu  
  63.   caffe.set_mode_gpu();  
  64.   gpu_id = 0;  % we will use the first gpu in this demo  
  65.   caffe.set_device(gpu_id);  
  66. else  
  67.   caffe.set_mode_cpu();  
  68. end  
  69.   
  70. % Initialize the network using BVLC CaffeNet for image classification  
  71. % Weights (parameter) file needs to be downloaded from Model Zoo.  
  72. model_dir = '../../models/bvlc_reference_caffenet/';    % 模型所在目录  
  73. net_model = [model_dir 'deploy.prototxt'];              % 模型描述文件,注意是deploy.prototxt,不包含data layers  
  74. net_weights = [model_dir 'bvlc_reference_caffenet.caffemodel'];   % 模型权值文件,需要预先下载到这里  
  75. phase = 'test'; % run with phase test (so that dropout isn't applied)   % 只进行分类,不做训练  
  76. if ~exist(net_weights, 'file')  
  77.   error('Please download CaffeNet from Model Zoo before you run this demo');  
  78. end  
  79.   
  80. % Initialize a network  
  81. net = caffe.Net(net_model, net_weights, phase);   % 初始化网络  
  82.   
  83. if nargin < 1  
  84.   % For demo purposes we will use the cat image  
  85.   fprintf('using caffe/examples/images/cat.jpg as input image\n');  
  86.   im = imread('../../examples/images/cat.jpg');    % 获取输入图像  
  87. end  
  88.   
  89. % prepare oversampled input  
  90. % input_data is Height x Width x Channel x Num  
  91. tic;  
  92. input_data = {prepare_image(im)};         % 图像冗余处理  
  93. toc;  
  94.   
  95. % do forward pass to get scores  
  96. % scores are now Channels x Num, where Channels == 1000  
  97. tic;  
  98. % The net forward function. It takes in a cell array of N-D arrays  
  99. % (where N == 4 here) containing data of input blob(s) and outputs a cell  
  100. % array containing data from output blob(s)  
  101. scores = net.forward(input_data);      %  分类,得到scores  
  102. toc;  
  103.   
  104. scores = scores{1};  
  105. scores = mean(scores, 2);  % 取所有分类结果的平均值  
  106.   
  107. [~, maxlabel] = max(scores);  % 找到最大概率对应的标签号  
  108.   
  109. % call caffe.reset_all() to reset caffe  
  110. caffe.reset_all();  
  111.   
  112. % ------------------------------------------------------------------------  
  113. function crops_data = prepare_image(im)  
  114. % ------------------------------------------------------------------------  
  115. % caffe/matlab/+caffe/imagenet/ilsvrc_2012_mean.mat contains mean_data that  
  116. % is already in W x H x C with BGR channels  
  117. d = load('../+caffe/imagenet/ilsvrc_2012_mean.mat');  
  118. mean_data = d.mean_data;  
  119. IMAGE_DIM = 256;  
  120. CROPPED_DIM = 227;  
  121.   
  122. % Convert an image returned by Matlab's imread to im_data in caffe's data  
  123. % format: W x H x C with BGR channels  
  124. im_data = im(:, :, [3, 2, 1]);  % permute channels from RGB to BGR  
  125. im_data = permute(im_data, [2, 1, 3]);  % flip width and height  
  126. im_data = single(im_data);  % convert from uint8 to single  
  127. im_data = imresize(im_data, [IMAGE_DIM IMAGE_DIM], 'bilinear');  % resize im_data  
  128. im_data = im_data - mean_data;  % subtract mean_data (already in W x H x C, BGR)  
  129.   
  130. % oversample (4 corners, center, and their x-axis flips)  
  131. crops_data = zeros(CROPPED_DIM, CROPPED_DIM, 3, 10, 'single');  
  132. indices = [0 IMAGE_DIM-CROPPED_DIM] + 1;  
  133. n = 1;  
  134. for i = indices  
  135.   for j = indices  
  136.     crops_data(:, :, :, n) = im_data(i:i+CROPPED_DIM-1, j:j+CROPPED_DIM-1, :);  
  137.     crops_data(:, :, :, n+5) = crops_data(end:-1:1, :, :, n);  
  138.     n = n + 1;  
  139.   end  
  140. end  
  141. center = floor(indices(2) / 2) + 1;  
  142. crops_data(:,:,:,5) = ...  
  143.   im_data(center:center+CROPPED_DIM-1,center:center+CROPPED_DIM-1,:);  
  144. crops_data(:,:,:,10) = crops_data(end:-1:1, :, :, 5);  
  • 1
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值