caffe_features_single_image.m 2.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566
  1. function [ features ] = caffe_features_single_image( i_image, f_mean, net, s_layer)
  2. % function [ features ] = caffe_features_single_image( i_image, f_mean, net, s_layer)
  3. %
  4. % BRIEF:
  5. % Run a forward pass of a given net on a single image and grep features of a specified layer
  6. % Requires Caffe version from 17-07-2015 (hash: 6d92d8fcfe0eea9495ffbc)
  7. %
  8. % INPUT
  9. % i_image -- 2d or 3d matrix
  10. % f_mean -- The average image of your dataset. This should be the same that was used during training of the CNN model.
  11. % Required to be cropped to the input size of your
  12. % network! See caffe_load_network.m
  13. % net -- a previously loaded network, see caffe_load_network.m
  14. % s_layer -- optional (default: 'relu7'), string, specifies the layer used for feature exatraction
  15. %
  16. %% parse inputs
  17. if (nargin<2)
  18. error ( 'no mean passed');
  19. end
  20. if (nargin<3)
  21. error ( 'no network passed');
  22. end
  23. if (nargin<4)
  24. s_layer = 'relu7';
  25. end
  26. %% old caffe layout
  27. % % prepare image for caffe format
  28. % batch_data = zeros(i_width,i_width,3,1,'single');
  29. % batch_data(:,:,:,1) = caffe_prepare_image(i_image,f_mean,i_width);
  30. % batch_data = repmat(batch_data, [1,1,1, batch_size] );
  31. %
  32. %
  33. % %% grep output and adjust desired format
  34. % features = caffe_('get_features',{batch_data},layer);
  35. % features = reshape(features{1},size(features{1},1)*size(features{1},2)*size(features{1},3),size(features{1},4))';
  36. %
  37. % features = double(features(1,:)');
  38. %% new caffe layout
  39. % scale, permute dimensions, subtract mean
  40. data = caffe_prepare_image( i_image, f_mean );
  41. % check that network was prepared to work on single images
  42. tmp_netshape = net.blobs('prob').shape;
  43. assert ( tmp_netshape(2) == 1, 'network not reshaped for passing only a single image' );
  44. % run a single forward pass
  45. [~] = net.forward({data});
  46. % fetch activations from specified layer
  47. features = net.blobs( s_layer ).get_data();
  48. % vectorize and concatenate activation maps
  49. features = reshape( features, ...
  50. size(features,1)*size(features,2)*size(features,3), ...
  51. size(features,4)...
  52. );
  53. % convert output to double precision
  54. features = double(features);
  55. end