ubuntu18.04+matcaffe(matlabR2014a)调试

1.终端测试通过:

  $ sudo make matcaffe
   ...
    $sudo make mattest

本人的 gcc version 5.5.0 ,忽略不兼容警告

  $ make mattest
    cd matlab; /usr/local/MATLAB/R2014a/bin/matlab -nodisplay -r 'caffe.run_tests(), exit()'
                            < M A T L A B (R) >
                  Copyright 1984-2014 The MathWorks, Inc.
                    R2014a (8.3.0.532) 64-bit (glnxa64)
                             February 11, 2014
    To get started, type one of these: helpwin, helpdesk, or demo.
    For product information, visit www.mathworks.com.
    ...
    Done caffe.test.test_solver_______
    Running caffe.test.test_io
    Done caffe.test.test_io_________
    Cleared 0 solvers and 0 stand-alone nets
    ans = 
      1x7 TestResult array with properties:
        Name
        Passed
        Failed
        Incomplete
        Duration
    Totals:
       7 Passed, 0 Failed, 0 Incomplete.

   0.47519 seconds testing time.

2.用个demo测试matcaffe,出现问题:

2.1   Invalid MEX-file '/home/cm/caffe/matlab/+caffe/private/caffe_.mexa64': /usr/
2.2  找不到 libstdc++.so.6..


解决办法:

$sudo rm /usr/local*/**MATLAB/R2014a**/*sys/os/glnxa64/libstdc++.so.6
 $sudo ln -s /usr/lib/x86_64-linux-gnu/libstdc++.so.6 /usr/local*/**MATLAB/R2014a**/*sys/os/glnxa64/libstdc++.so.6 

$export LD_PRELOAD=/usr/lib/x86_64-linux-gnu/***libopencv_highgui.so.3.2:***/usr/lib/x86_64-linux-gnu***/**libopencv_imgproc.so.3.2:**/***usr/lib/x86_64-linux-gnu*/**libopencv_core.so.3.2:***/usr/lib/x86_64-linux-gnu/libstdc++.so.6:/usr/lib/x86_64-linux-gnu/libfreetype.so.6
$export LD_LIBRARY_PATH=/usr/lib/x86_64-linux-gnu

***处根据自己的路径更改!
3.调试matcaffe:

$matlab

在matlab的Command Window输入:

>> cd /../../caffe/     %输入caffe的根目录
  >>addpath('./matlab/+caffe/private');



  >>cd matlab/demo/
  >>im = imread('/home/cm/caffe/examples/images/cat.jpg');
 >>figure;imshow('im');

显示一张cat.jpg

>> figure;imshow(im);
>> [scores, maxlabel] = classification_demo(im, 1);

Elapsed time is 0.066673 seconds.
Elapsed time is 0.202347 seconds.
Cleared 0 solvers and 1 stand-alone nets

>> maxlabel

maxlabel =

282

>> figure;plot(scores);
>> axis([0,999,-0.1,0.5]);
>> grid on;
>> classification_demo

using caffe/examples/images/cat.jpg as input image
Elapsed time is 0.025638 seconds.
Elapsed time is 1.108067 seconds.
Cleared 0 solvers and 1 stand-alone nets

运行一个:
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 < 1
  % 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);

感谢大佬们的分享!

©️2020 CSDN 皮肤主题: 大白 设计师:CSDN官方博客 返回首页