learn_dataset.m 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127
  1. function model = learn_dataset( pos, neg, bg, settings )
  2. % model = learn_dataset(pos, neg, name, settings )
  3. %
  4. % author: Alexander Freytag
  5. % date: 13-02-2014 (dd-mm-yyyy) (last updated)
  6. %
  7. % BRIEF
  8. % Learn an LDA model from provided positive examples. Negative data is
  9. % either provided using a pre-computed struct (containing negative mean
  10. % as well as covariance matrix) or will be computed from images in neg
  11. % using several positions and scales of potential subimages
  12. %
  13. % INPUT
  14. % pos -- is a struct array for the positive patches, with fields:
  15. % .im (full path to the image),
  16. % .x1 (optional, xmin),
  17. % .y1 (optional, ymin),
  18. % .x2 (optional, xmax),
  19. % .y2 (optional, ymax)
  20. % neg -- struct array for the negative patches with field:
  21. % im: full path to the image
  22. % Used only when the background statistics cannot be found.
  23. % bg -- (optional) pre-computed whitening info (cov mat, negative mean, ...)
  24. % settings -- struct with config settings
  25. %
  26. % OUTPUT
  27. % model -- learnt model
  28. %
  29. % have we any negative examples given?
  30. if ( isempty(neg) )
  31. allImages = {pos.im};
  32. else
  33. allImages = [ {pos.im}; {neg.im} ];
  34. end
  35. % have bounding boxes been provided? If not, take the entire image
  36. % instead
  37. % this is only needed for the posive images, since negative examples
  38. % serve only for computing background statistics
  39. if ( ~isfield( pos, 'x1') || ...
  40. ~isfield( pos, 'x1') || ...
  41. ~isfield( pos, 'x1') || ...
  42. ~isfield( pos, 'x1') ...
  43. )
  44. s_imgfns = {pos.im};
  45. for i_idx = 1:length(s_imgfns)
  46. s_fn = s_imgfns{i_idx};
  47. img = imread ( s_fn );
  48. pos(i_idx).x1 = 1;
  49. pos(i_idx).y1 = 1;
  50. pos(i_idx).x2 = size(img,2);
  51. pos(i_idx).y2 = size(img,1);
  52. end
  53. end
  54. %use given background statistics if they exist; else build them
  55. if ( isempty(bg) )
  56. bg = trainBGwithArbitraryFeatures( allImages, settings );
  57. end
  58. % Define model structure
  59. % how many dimensions does our feature has?
  60. % compute a feature for a small, empty image to fetch the number of
  61. % dimensions every cell-feature has
  62. i_numImgChannels = size ( readImage( allImages{1}),3);
  63. i_numDim = size( settings.fh_featureExtractor.mfunction ( zeros([3 3 i_numImgChannels]) ),3 );
  64. clear ( 'allImages' );
  65. %computed lda variables
  66. settings.lda.bg = bg;
  67. %threshold to reject detection with score lwoer than th
  68. settings.lda.d_detectionThreshold = 0;
  69. % how many cells are useful in both directions?
  70. i_numCells = computeOptimalCellNumber ( pos, settings.i_binSize );
  71. settings.lda.bg.i_numCells = i_numCells;
  72. %======== ======== ======== ========
  73. % add here noise model for
  74. % modeling de-noising effect
  75. %======== ======== ======== ========
  76. % this adds noise on the main diagonal of the covariance matrix
  77. settings.lda.lambda = bg.lambda;
  78. %%% this additionally adds a drop-out noise model
  79. settings.lda.b_noiseDropOut = false;
  80. settings.lda.d_dropOutProb = 0.0;
  81. settings.lda.bg.interval = settings.interval;
  82. model = initmodel_static(settings, i_numDim);
  83. %skip models if the HOG window is too skewed
  84. if( max(model.i_numCells)<4*min(model.i_numCells) )
  85. %get image patches
  86. warpedTrainBlocks = warpBlocksToStandardSize( model, pos, settings.fh_featureExtractor );
  87. % pre-compute features from the seeding blocks (size normalized)
  88. feats = computeFeaturesForBlocks( warpedTrainBlocks, settings);
  89. [ldaStuff.R,ldaStuff.neg] = whitenWithDropout(model.bg, model.lda, model.i_numCells(2),model.i_numCells(1));
  90. % for the HOG features computed here, the 32nd dim is constant to
  91. % zero serving as truncation dim.
  92. i_truncDim = 32;
  93. model = learnWithGivenWhitening(model, ...
  94. ldaStuff.R, ldaStuff.neg, ...
  95. feats, i_truncDim );
  96. else
  97. model.maxsize
  98. error('HOG window is too skewed!');
  99. end
  100. model.w=model.w./(norm(model.w(:))+eps);
  101. model.thresh = 0.5;
  102. model.bg=[];
  103. end