日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問(wèn) 生活随笔!

生活随笔

當(dāng)前位置: 首頁(yè) > 人工智能 > Caffe >内容正文

Caffe

MATLAB调试caffe,在MATLAB下调试Caffe

發(fā)布時(shí)間:2024/9/18 Caffe 168 豆豆
生活随笔 收集整理的這篇文章主要介紹了 MATLAB调试caffe,在MATLAB下调试Caffe 小編覺(jué)得挺不錯(cuò)的,現(xiàn)在分享給大家,幫大家做個(gè)參考.

Caffe本身是C++、CUDA語(yǔ)言編寫的。在調(diào)試模型、參數(shù)時(shí),根據(jù)運(yùn)行l(wèi)og、snapshot很難實(shí)時(shí)反饋當(dāng)前訓(xùn)練的權(quán)值情況,也難以捕捉算法存在的bug。

MATLAB則是非常適合算法設(shè)計(jì)、快速迭代的利器,只需要做少量工作就能編寫出復(fù)雜的算法,調(diào)試非常方便,位于workspace中的變量隨時(shí)都能打印,無(wú)論是一維、二維還是三維數(shù)據(jù),都能直觀顯示,從而有利于定位算法設(shè)計(jì)問(wèn)題,減少調(diào)試時(shí)間。

Caffe中有兩種Wrapper:Python和MATLAB。Python是開(kāi)源工具,用戶無(wú)需付費(fèi)即可使用,缺點(diǎn)是語(yǔ)法不夠靈活,尤其算法描述,與商業(yè)軟件不能比。MATLAB支持幾乎你所知道的所有矩陣變換、數(shù)值計(jì)算、隨機(jī)過(guò)程、概率論、最優(yōu)化、自適應(yīng)濾波、圖像處理、神經(jīng)網(wǎng)絡(luò)等算法。

下面介紹如何用MATLAB調(diào)試Caffe。本文假設(shè)操作系統(tǒng)為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調(diào)用。

4. 運(yùn)行MATLAB例子

在命令行中,配置好Caffe運(yùn)行所需要的環(huán)境變量后(否則matcaffe會(huì)運(yùn)行失敗),輸入matlab&,這樣就啟動(dòng)了MATLAB窗口。

在MATLAB命令窗口中進(jìn)行以下步驟。

>> cd Caffe_root_directory/

切換到了Caffe根目錄。

>> addpath('./matlab/+caffe/private');

添加matcaffe模塊所在路徑到MATLAB搜索路徑,便于加載。

>> cd matlab/demo/

切到demo目錄。

>> im = imread('../../examples/images/cat.jpg');

讀取一張測(cè)試圖片。

>> figure;imshow(im);

彈出一個(gè)窗口,顯示貓的測(cè)試圖片如下:

>> [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

運(yùn)行分類demo程序。分類的結(jié)果返回到scores,maxlabel兩個(gè)工作空間變量中。

>> maxlabel

maxlabel =

282

說(shuō)明最大分類概率的標(biāo)簽號(hào)為282,查找ImageNet標(biāo)簽,對(duì)應(yīng)的是n02123045 tabby, tabby cat(data/ilsvrc2012/synset_words.txt)

>> figure;plot(scores);

>> axis([0, 999, -0.1, 0.5]);

>> grid on

打印scores,一維圖像如下:

說(shuō)明這張圖片被分到第282類的概率為0.2985。

到這里我們只是運(yùn)行了簡(jiǎn)單的demo,接下來(lái)分析classification_demo.m這個(gè)文件內(nèi)容。

function?[scores,?maxlabel]?=?classification_demo(im,?use_gpu)

%?[scores,?maxlabel]?=?classification_demo(im,?use_gpu)

%?使用BVLC?CaffeNet進(jìn)行圖像分類的示例

%?重要:運(yùn)行前,應(yīng)首先從Model?Zoo(http://caffe.berkeleyvision.org/model_zoo.html)?下載BVLC?CaffeNet訓(xùn)練好的權(quán)值

%

%?****************************************************************************

%?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'];???%?模型權(quán)值文件,需要預(yù)先下載到這里

phase?=?'test';?%?run?with?phase?test?(so?that?dropout?isn't?applied)???%?只進(jìn)行分類,不做訓(xùn)練

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);???%?初始化網(wǎng)絡(luò)

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);??%?取所有分類結(jié)果的平均值

[~,?maxlabel]?=?max(scores);??%?找到最大概率對(duì)應(yīng)的標(biāo)簽號(hào)

%?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);

總結(jié)

以上是生活随笔為你收集整理的MATLAB调试caffe,在MATLAB下调试Caffe的全部?jī)?nèi)容,希望文章能夠幫你解決所遇到的問(wèn)題。

如果覺(jué)得生活随笔網(wǎng)站內(nèi)容還不錯(cuò),歡迎將生活随笔推薦給好友。