caffe_load_network.m 2.6 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950
  1. function [net, mean_data] = caffe_load_network ( s_pathtodeployfile, s_pathtomodel, s_phase, s_meanfile, b_reshape_for_single_image_processing)
  2. % function [net, mean_data] = caffe_load_network ( s_pathtodeployfile, s_pathtomodel, s_phase, s_meanfile, b_reshape_for_single_image_processing)
  3. %
  4. % BRIEF
  5. % Load a specified network with mean image for train or test.
  6. % Requires Caffe version from 17-07-2015 (hash: 6d92d8fcfe0eea9495ffbc)
  7. %
  8. % INPUT
  9. % s_pathtodeployfile -- string, path to file describing the network's
  10. % architecture, e.g., deploy.prototxt
  11. % s_pathtomodel -- string, path to file describing the network's
  12. % learned parameters, e.g. bvlc_reference_caffenet.caffemodel
  13. % s_phase -- string, 'test' or 'train'. 'test' will de-activate dropout
  14. % s_meanfile -- string, path to file which contains the mean
  15. % image of a dataset, e.g., imagenet_mean.binaryproto
  16. % b_reshape_for_single_image_processing
  17. % -- bool, true of the network shall
  18. % operate on single images instead of image batches
  19. %% load network
  20. net = caffe.Net(s_pathtodeployfile, s_pathtomodel, s_phase); % create net and load weights
  21. % for which input sizes has the network been trained?
  22. i_currentNetInputSize = net.blobs('data').shape;
  23. %% reshape network input for batch or single-image processing
  24. if ( b_reshape_for_single_image_processing )
  25. %
  26. % reshape network to run only a single image in a forward pass instead
  27. % of 10 (for which it was trained)
  28. net.blobs('data').reshape([i_currentNetInputSize(1) ...
  29. i_currentNetInputSize(2) ...
  30. i_currentNetInputSize(3) ...
  31. 1 ...
  32. ]); % reshape blob 'data'
  33. net.reshape();
  34. end
  35. %% load mean image and adapt to network input size
  36. mean_data = caffe.io.read_mean( s_meanfile );
  37. % crop center from mean file according to network size
  38. i_sizeMean = size( mean_data );
  39. offset_row = floor ( int32( i_sizeMean(1)-i_currentNetInputSize(1) ) / 2 ) + 1;
  40. offset_col = floor ( int32( i_sizeMean(2)-i_currentNetInputSize(2) ) / 2 ) + 1;
  41. mean_data = mean_data( offset_row:offset_row+i_currentNetInputSize(1)-1, ...
  42. offset_col:offset_col+i_currentNetInputSize(2)-1, ...
  43. : ...
  44. );
  45. end