MATLAB调试caffe,在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
安装步骤类似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);
MATLAB调试caffe,在MATLAB下调试Caffe相关推荐
- IE+调试修改html,IE下调试CSS与JS
IE下调试CSS与JS 启动调试工具:IE浏览器9.0 ,菜单〉〉F12开发人员工具--. 1. 调试CSS 1.1找到页面元素 "HTML" TAB页,展开HTML元素 ...
- linux中调试脚本,在Linux下调试 Shell 脚本
在大多数编程语言中都有调试工具可用于调试. 调试工具可以运行需要调试的程序或脚本,使我们可以在运行时检查脚本或程序的内部执行过程. 在shell脚本中我们没有任何调试工具,只能借助命令行选项(-n,- ...
- 深度学习之Windows下安装caffe及配置Python和matlab接口
去年下半年看了相关目标检测的论文,一些传统的算法,一些CVPR,TPAMI,ECCV,ICCV,,NIPS,比较前沿的进展,主要都是基于深度学习卷积神经网络方面的,包括RCNN,SPP-NET,Fas ...
- matlab debug出现k,MATLAB下的程序调试
文章主要内容摘自<MATLAB 7.0从入门到精通>,求是科技编著. 程序错误一般分为两种:语法错误和逻辑错误.对于语法错误通常MATLAB会报错,并指出错误所在位置方便用户纠正.对于逻辑 ...
- Linux x64 下 Matlab R2013a 300 kb 脚本文件调试的 CPU 占用过高问题的解决办法
(1) 系统+软件版本 CentOS 6.5 (Final), 64 位,内核initramfs-2.6.32-431.5.1.el6.x86_64, MATLAB Version: 8.1.0.60 ...
- windows下使用Caffe框架和matlab实现SRCNN官方代码的步骤
步骤 step1 搭建caffe环境 在windows系统上搭建caffe环境,并配置matlab接口(需要下载 caffe-master.zip 以及 VS2013 ) 我的环境为:windows1 ...
- matlab添加路径报错,Win10 + Caffe + CPU + MATLAB (包括各种问题详细解决)(二)
转载自:https://blog.csdn.net/u014546828/article/details/80447583 这里介绍一下 Win10 下,如何搭建 Caffe,仅有 CPU.因为我需要 ...
- Caffe和MATLAB
这篇总结是对前面的一个补课吧,caffe配matlab,包括ubuntu下面matlab2016b的破解安装,其实我这是想跑一个demo看看效果而已.OK先安装matlab 安装matlab2016b ...
- Ubuntu caffe 测试matlab接口
这是17年8月份新增的: make matcaffe error 255 解决: 在Makefile里面,大约第410行那一句话CXXFLAGS += -MMD -MP下面添加CXXFLAGS += ...
最新文章
- 电子漫画 - 轻轻松松
- 密度聚类(Density peaks Clustering)Python实现
- 利用OpenCV的threshold函数实现双阈值法二值化操作的源码
- C# 数组与 list 互相转换案例
- 如何获取Oracle数据库中某表及索引、约束、触发器、对象权限的创
- 阿里云数据库RDS PG联合电商SaaS领导者班牛,助力1500+品牌数智化
- Linux 阻塞和非阻塞IO 实验
- docker管理应用程序数据、容器网络
- vue 获取响应头里set-cookie的值_最简化 VUE的响应式原理
- 「傻瓜」才能写出好代码!
- 历经外企、创业公司、大厂的程序员告诉你:第一份工作有多重要!
- CSS:结合clip-path实现目录的隐藏显示以及提示框的隐藏显示
- 2008 DHCP中继器代理服务
- 2022年房地产市场趋势展望
- spy++是可以获取浏览器当前页面的标题的,并且可以根据“窗口标题”和“类名”实现对浏览器的显示和隐藏,
- 如何理解结构化、非结构化和半结构化数据?
- 电商运营到底做什么?说出来你也不信。
- Linux入门——1、Linux的安装(Ubuntu)
- 一枚小江湖视角下的 2020
- 0 公式 0 基础学习电磁兼容 — 1. EMC 测试类型简介
热门文章
- 在C#中使用Selenium WebDriver执行JavaScript
- gitblit无法安装windows服务或者启动服务失败:Failed creating java
- 天猫总架构师何崚:好的技术团队不是“需求翻译机”或“架构优化机”
- mysql获取当前时间,前一天,后一天(执行效率比较高)
- REDIS实践之请勿踩多进程共用一个实例连接的坑
- PHP类实例教程(七):析构函数与PHP的垃圾回收
- mysql分库分表实战及php代码操作完整实例
- html设置表格宽度最小,css如何设置表格宽度?
- c 窗体程序 mysql_C\C++开发MySQL程序简介(下)
- python条件表达式连起来写一段话_python学习笔记十三条件表达式应用