learn_dataset.m 3.3 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798
  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. % Initialize model structure. (note: If you just have image patches, instead of bounding boxes, consider using learn.m directly)
  9. %
  10. % INPUT
  11. % pos -- is a struct array for the positive patches, with fields:
  12. % .im (full path to the image), .x1 (xmin), .y1 (ymin), .x2 (xmax), .y2 (ymax)
  13. % neg -- struct array for the negative patches with field:
  14. % im: full path to the image
  15. % Used only when the background statistics cannot be found.
  16. % bg -- (optional) pre-computed whitening info (cov mat, negative mean, ...)
  17. % settings -- struct with config settings
  18. %
  19. % OUTPUT
  20. % model -- learnt model
  21. %
  22. if ( isempty(neg) )
  23. allImages = {pos.im};
  24. else
  25. allImages = [ {pos.im}; {neg.im} ];
  26. end
  27. %use given background statistics if they exist; else build them
  28. if ( isempty(bg) )
  29. bg = trainBGwithArbitraryFeatures( allImages, settings );
  30. end
  31. % Define model structure
  32. % how many dimensions does our feature has?
  33. % compute a feature for a small, empty image to fetch the number of
  34. % dimensions every cell-feature has
  35. i_numImgChannels = size ( readImage( allImages{1}),3);
  36. i_numDim = size( settings.fh_featureExtractor.mfunction ( zeros([3 3 i_numImgChannels]) ),3 );
  37. clear ( 'allImages' );
  38. %computed lda variables
  39. settings.lda.bg = bg;
  40. %threshold to reject detection with score lwoer than th
  41. settings.lda.d_detectionThreshold = 0;
  42. % how many cells are useful in both directions?
  43. i_numCells = computeOptimalCellNumber ( pos, settings.i_binSize );
  44. settings.lda.bg.i_numCells = i_numCells;
  45. %======== ======== ======== ========
  46. % add here noise model for
  47. % modeling de-noising effect
  48. %======== ======== ======== ========
  49. % this adds noise on the main diagonal of the covariance matrix
  50. settings.lda.lambda = bg.lambda;
  51. %%% this additionally adds a drop-out noise model
  52. settings.lda.b_noiseDropOut = false;
  53. settings.lda.d_dropOutProb = 0.0;
  54. settings.lda.bg.interval = settings.interval;
  55. model = initmodel_static(settings, i_numDim);
  56. %skip models if the HOG window is too skewed
  57. if( max(model.i_numCells)<4*min(model.i_numCells) )
  58. %get image patches
  59. warpedTrainBlocks = warpBlocksToStandardSize( model, pos, settings.fh_featureExtractor );
  60. % pre-compute features from the seeding blocks (size normalized)
  61. feats = computeFeaturesForBlocks( warpedTrainBlocks, settings);
  62. [ldaStuff.R,ldaStuff.neg] = whitenWithDropout(model.bg, model.lda, model.i_numCells(2),model.i_numCells(1));
  63. % for the HOG features computed here, the 32nd dim is constant to
  64. % zero serving as truncation dim.
  65. i_truncDim = 32;
  66. model = learnWithGivenWhitening(model, ...
  67. ldaStuff.R, ldaStuff.neg, ...
  68. feats, i_truncDim );
  69. else
  70. model.maxsize
  71. error('HOG window is too skewed!');
  72. end
  73. model.w=model.w./(norm(model.w(:))+eps);
  74. model.thresh = 0.5;
  75. model.bg=[];
  76. end