[关闭]
@hanxiaoyang 2016-10-10T13:07:56.000000Z 字数 4563 阅读 2776

caffe MATLAB 图像识别

未分类


code example by @caffe

  1. function [scores, maxlabel] = classification_demo(im, use_gpu)
  2. % [scores, maxlabel] = classification_demo(im, use_gpu)
  3. %
  4. % Image classification demo using BVLC CaffeNet.
  5. %
  6. % IMPORTANT: before you run this demo, you should download BVLC CaffeNet
  7. % from Model Zoo (http://caffe.berkeleyvision.org/model_zoo.html)
  8. %
  9. % ****************************************************************************
  10. % For detailed documentation and usage on Caffe's Matlab interface, please
  11. % refer to Caffe Interface Tutorial at
  12. % http://caffe.berkeleyvision.org/tutorial/interfaces.html#matlab
  13. % ****************************************************************************
  14. %
  15. % input
  16. % im color image as uint8 HxWx3
  17. % use_gpu 1 to use the GPU, 0 to use the CPU
  18. %
  19. % output
  20. % scores 1000-dimensional ILSVRC score vector
  21. % maxlabel the label of the highest score
  22. %
  23. % You may need to do the following before you start matlab:
  24. % $ export LD_LIBRARY_PATH=/opt/intel/mkl/lib/intel64:/usr/local/cuda-5.5/lib64
  25. % $ export LD_PRELOAD=/usr/lib/x86_64-linux-gnu/libstdc++.so.6
  26. % Or the equivalent based on where things are installed on your system
  27. %
  28. % Usage:
  29. % im = imread('../../examples/images/cat.jpg');
  30. % scores = classification_demo(im, 1);
  31. % [score, class] = max(scores);
  32. % Five things to be aware of:
  33. % caffe uses row-major order
  34. % matlab uses column-major order
  35. % caffe uses BGR color channel order
  36. % matlab uses RGB color channel order
  37. % images need to have the data mean subtracted
  38. % Data coming in from matlab needs to be in the order
  39. % [width, height, channels, images]
  40. % where width is the fastest dimension.
  41. % Here is the rough matlab for putting image data into the correct
  42. % format in W x H x C with BGR channels:
  43. % % permute channels from RGB to BGR
  44. % im_data = im(:, :, [3, 2, 1]);
  45. % % flip width and height to make width the fastest dimension
  46. % im_data = permute(im_data, [2, 1, 3]);
  47. % % convert from uint8 to single
  48. % im_data = single(im_data);
  49. % % reshape to a fixed size (e.g., 227x227).
  50. % im_data = imresize(im_data, [IMAGE_DIM IMAGE_DIM], 'bilinear');
  51. % % subtract mean_data (already in W x H x C with BGR channels)
  52. % im_data = im_data - mean_data;
  53. % If you have multiple images, cat them with cat(4, ...)
  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. % Set caffe mode
  61. if exist('use_gpu', 'var') && use_gpu
  62. caffe.set_mode_gpu();
  63. gpu_id = 0; % we will use the first gpu in this demo
  64. caffe.set_device(gpu_id);
  65. else
  66. caffe.set_mode_cpu();
  67. end
  68. % Initialize the network using BVLC CaffeNet for image classification
  69. % Weights (parameter) file needs to be downloaded from Model Zoo.
  70. model_dir = '../../models/bvlc_reference_caffenet/';
  71. net_model = [model_dir 'deploy.prototxt'];
  72. net_weights = [model_dir 'bvlc_reference_caffenet.caffemodel'];
  73. phase = 'test'; % run with phase test (so that dropout isn't applied)
  74. if ~exist(net_weights, 'file')
  75. error('Please download CaffeNet from Model Zoo before you run this demo');
  76. end
  77. % Initialize a network
  78. net = caffe.Net(net_model, net_weights, phase);
  79. if nargin < 1
  80. % For demo purposes we will use the cat image
  81. fprintf('using caffe/examples/images/cat.jpg as input image\n');
  82. im = imread('../../examples/images/cat.jpg');
  83. end
  84. % prepare oversampled input
  85. % input_data is Height x Width x Channel x Num
  86. tic;
  87. input_data = {prepare_image(im)};
  88. toc;
  89. % do forward pass to get scores
  90. % scores are now Channels x Num, where Channels == 1000
  91. tic;
  92. % The net forward function. It takes in a cell array of N-D arrays
  93. % (where N == 4 here) containing data of input blob(s) and outputs a cell
  94. % array containing data from output blob(s)
  95. scores = net.forward(input_data);
  96. toc;
  97. scores = scores{1};
  98. scores = mean(scores, 2); % take average scores over 10 crops
  99. [~, maxlabel] = max(scores);
  100. % call caffe.reset_all() to reset caffe
  101. caffe.reset_all();
  102. % ------------------------------------------------------------------------
  103. function crops_data = prepare_image(im)
  104. % ------------------------------------------------------------------------
  105. % caffe/matlab/+caffe/imagenet/ilsvrc_2012_mean.mat contains mean_data that
  106. % is already in W x H x C with BGR channels
  107. d = load('../+caffe/imagenet/ilsvrc_2012_mean.mat');
  108. mean_data = d.mean_data;
  109. IMAGE_DIM = 256;
  110. CROPPED_DIM = 227;
  111. % Convert an image returned by Matlab's imread to im_data in caffe's data
  112. % format: W x H x C with BGR channels
  113. im_data = im(:, :, [3, 2, 1]); % permute channels from RGB to BGR
  114. im_data = permute(im_data, [2, 1, 3]); % flip width and height
  115. im_data = single(im_data); % convert from uint8 to single
  116. im_data = imresize(im_data, [IMAGE_DIM IMAGE_DIM], 'bilinear'); % resize im_data
  117. im_data = im_data - mean_data; % subtract mean_data (already in W x H x C, BGR)
  118. % oversample (4 corners, center, and their x-axis flips)
  119. crops_data = zeros(CROPPED_DIM, CROPPED_DIM, 3, 10, 'single');
  120. indices = [0 IMAGE_DIM-CROPPED_DIM] + 1;
  121. n = 1;
  122. for i = indices
  123. for j = indices
  124. crops_data(:, :, :, n) = im_data(i:i+CROPPED_DIM-1, j:j+CROPPED_DIM-1, :);
  125. crops_data(:, :, :, n+5) = crops_data(end:-1:1, :, :, n);
  126. n = n + 1;
  127. end
  128. end
  129. center = floor(indices(2) / 2) + 1;
  130. crops_data(:,:,:,5) = ...
  131. im_data(center:center+CROPPED_DIM-1,center:center+CROPPED_DIM-1,:);
  132. crops_data(:,:,:,10) = crops_data(end:-1:1, :, :, 5);
添加新批注
在作者公开此批注前,只有你和作者可见。
回复批注