whitenWithDropout.m 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139
  1. % function [R,neg] = whitenWithDropout(bg,lda,nx,ny)
  2. % Obtain whitenixng matrix and mean from a general HOG model
  3. % by a cholesky decompoition on a stationairy covariance matrix
  4. % feat' = R\(feat - neg) has zero mean and unit covariance
  5. %
  6. % bg.neg: negative mean (nf by 1)
  7. % bg.cov: covariance for k spatial offsets (nf by nf by k)
  8. % bg.dxy: k spatial offsets (k by 2)
  9. % lda.lambda: regularizer
  10. function [R,neg,b_success] = whitenWithDropout(bg,lda,nx,ny)
  11. if ( nargout > 2 )
  12. b_success = true;
  13. end
  14. % % the following line is intended to prevent memory overwhelming due to
  15. % % impossibly large covariance matrices which could be desired for large
  16. % % selected regions...
  17. % %
  18. % % size(bg.cov,1) -> number of feature dimensions
  19. % % nx -> size of desired filter x dimension
  20. % % ny -> size of desired filter y dimension
  21. % %
  22. % if ( ((nx*ny)^2 * size(bg.cov,1)^2 )> 5*10e8 )
  23. % if ( nargout > 2 )
  24. % R = [];
  25. % neg = [];
  26. % b_success = false;
  27. % return;
  28. % end
  29. % end
  30. % now start the actual whitening
  31. neg = repmat(bg.neg',ny*nx,1);
  32. neg = neg(:);
  33. p = 1;
  34. % if ( ~isfield(lda,'b_noiseDropOut') || isempty(lda.b_noiseDropOut) )
  35. % b_noiseDropOut = true;
  36. % else
  37. % b_noiseDropOut = lda.b_noiseDropOut;
  38. % end
  39. %
  40. % if ( ~isfield(lda,'d_dropOutProb') || isempty(lda.d_dropOutProb) )
  41. % d_dropOutProb = 0.1;
  42. % else
  43. % d_dropOutProb = lda.d_dropOutProb;
  44. % end
  45. while(p~=0)
  46. [sig, b_success] = reconstructSig(nx,ny,bg.cov,bg.dxy);
  47. if ( ~b_success)
  48. R = [];
  49. neg = [];
  50. if ( nargout > 2 )
  51. b_success = false;
  52. end
  53. return;
  54. end
  55. % drop-out like noise model, as described by Chen et al. (Marginalized Denoising Autoencoders for Domain Adaptation)
  56. if ( lda.b_noiseDropOut )
  57. d=size(sig,1);
  58. q=ones(d,1) .* (1-lda.d_dropOutProb);
  59. sig=sig.*(q*q');
  60. end
  61. % Gaussian noise model for every dimension
  62. sig = sig + lda.lambda*eye(size(sig));
  63. [R,p] = chol(sig);
  64. if p ~= 0,
  65. %disp('Increasing lambda');
  66. lda.lambda = lda.lambda*5;
  67. %display('Sig is not positive definite, add a larger regularizer');
  68. %keyboard;
  69. end
  70. end
  71. end
  72. function [w, b_success] = reconstructSig(nx,ny,ww,dxy)
  73. % W = reconstructSig(nx,ny,ww,dxy)
  74. % W = n x n
  75. % n = ny * nx * nf
  76. if ( nargout > 1 )
  77. b_success = true;
  78. end
  79. k = size(dxy,1);
  80. nf = size(ww,1);
  81. n = ny*nx;
  82. try
  83. w = zeros(nf,nf,n,n);
  84. for x1 = 1:nx,
  85. for y1 = 1:ny,
  86. i1 = (x1-1)*ny + y1;
  87. for i = 1:k,
  88. x = dxy(i,1);
  89. y = dxy(i,2);
  90. x2 = x1 + x;
  91. y2 = y1 + y;
  92. if x2 >= 1 && x2 <= nx && y2 >= 1 && y2 <= ny,
  93. i2 = (x2-1)*ny + y2;
  94. w(:,:,i1,i2) = ww(:,:,i);
  95. end
  96. x2 = x1 - x;
  97. y2 = y1 - y;
  98. if x2 >= 1 && x2 <= nx && y2 >= 1 && y2 <= ny,
  99. i2 = (x2-1)*ny + y2;
  100. w(:,:,i1,i2) = ww(:,:,i)';
  101. end
  102. end
  103. end
  104. end
  105. % Permute [nf nf n n] to [n nf n nf]
  106. w = permute(w,[3 1 4 2]);
  107. w = reshape(w,n*nf,n*nf);
  108. % Make sure returned matrix is close to symmetric
  109. assert(sum(sum(abs(w - w'))) < 1e-5);
  110. w = (w+w')/2;
  111. catch err
  112. w = [];
  113. if ( nargout > 1 )
  114. b_success = false;
  115. end
  116. return;
  117. end
  118. end