learnWithGivenWhitening.m 3.1 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889
  1. function modelNew = learnWithGivenWhitening(model,R, neg, pyraFeats, i_truncDim)
  2. % function modelNew = learnWithGivenWhitening(model,R, neg, pyraFeats, i_truncDim)
  3. % BRIEF:
  4. % Learn model by linear discriminant analysis with already given
  5. % negative mean and covariance matrix
  6. %
  7. % INPUT:
  8. % model -- struct, previously untrained model, contains at
  9. % least the following fields:
  10. % .w -- previous (most likely empty) weight vector of
  11. % model, relevant for determining the correct size
  12. % .i_numCells -- number of cells, specified per dimension (copied
  13. % only)
  14. % .i_binSize -- number of pixel of every cell in both directions
  15. % (copied only)
  16. % .interval -- how many octaves are used for feature extraction
  17. % (copied only)
  18. % .d_detectionThreshold -- minimum scores for accepting a response
  19. %
  20. % R -- covariance matrix learned previously
  21. % neg -- negative mean learned previously
  22. % pyraFeats -- features of positive examples
  23. % i_truncDim -- int, indicating which dimension, if any, serves as
  24. % truncation feature by being constant to zero ( as
  25. % done for DPM HOG features )
  26. %
  27. % OUTPUT:
  28. % modelNew -- struct, learned model, with fields 'w',
  29. % 'i_numCells', 'i_binSize', 'interval', 'd_detectionThreshold
  30. %
  31. % author: Alexander Freytag
  32. % last time modified: 27-02-2014 (dd-mm-yyyy)
  33. [ny,nx,nf] = size(model.w);
  34. if ( i_truncDim > 0 )
  35. nf = nf - 1;
  36. end
  37. %num is the number of blocks we have for this cache
  38. num = length(pyraFeats);
  39. feats = zeros(ny*nx*nf,num);
  40. % flatten features into single vectors
  41. for i = 1:num
  42. % get current feature
  43. feat = pyraFeats(i).feature;
  44. % possibly remove unneeded truncation feature
  45. if ( i_truncDim > 0 )
  46. feat = feat(:, :, 1:end~=i_truncDim );
  47. end
  48. % flatten vector and store in feats-struct
  49. feats(:,i) = feat(:);
  50. end
  51. % average all features together
  52. pos = mean(feats,2);
  53. % perform actual LDA training
  54. w=R\(R'\(pos-neg));
  55. % bring weight vector into correct layout
  56. w = reshape(w,[ny nx nf]);
  57. if ( i_truncDim > 0 )
  58. % Add in occlusion feature
  59. %note: might only be troublesome if very first dim is the td...
  60. w = cat( 3, w(:,:,1:(i_truncDim-1) ), ...
  61. zeros ( size(w,1),size(w,2),1,class(w) ), ...
  62. w(:, :, i_truncDim:end) );
  63. end
  64. modelNew.w = w;
  65. % size of root filter in HOG cells
  66. modelNew.i_numCells = model.i_numCells;
  67. % size of each cell in pixel
  68. modelNew.i_binSize = model.i_binSize;
  69. % strange interval
  70. modelNew.interval = model.interval;
  71. %threshold to reject detection with score lower than that
  72. modelNew.d_detectionThreshold = model.d_detectionThreshold;
  73. end