whitenWithDropout.m 2.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293
  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] = whitenWithDropout(bg,lda,nx,ny)
  11. neg = repmat(bg.neg',ny*nx,1);
  12. neg = neg(:);
  13. p=1;
  14. % if ( ~isfield(lda,'b_noiseDropOut') || isempty(lda.b_noiseDropOut) )
  15. % b_noiseDropOut = true;
  16. % else
  17. % b_noiseDropOut = lda.b_noiseDropOut;
  18. % end
  19. %
  20. % if ( ~isfield(lda,'d_dropOutProb') || isempty(lda.d_dropOutProb) )
  21. % d_dropOutProb = 0.1;
  22. % else
  23. % d_dropOutProb = lda.d_dropOutProb;
  24. % end
  25. while(p~=0)
  26. sig = reconstructSig(nx,ny,bg.cov,bg.dxy);
  27. % drop-out like noise model, as described by Chen et al. (Marginalized Denoising Autoencoders for Domain Adaptation)
  28. if ( lda.b_noiseDropOut )
  29. d=size(sig,1);
  30. q=ones(d,1) .* (1-lda.d_dropOutProb);
  31. sig=sig.*(q*q');
  32. end
  33. % Gaussian noise model for every dimension
  34. sig = sig + lda.lambda*eye(size(sig));
  35. [R,p] = chol(sig);
  36. if p ~= 0,
  37. disp('Increasing lambda');
  38. lda.lambda = lda.lambda+0.01;
  39. %display('Sig is not positive definite, add a larger regularizer');
  40. %keyboard;
  41. end
  42. end
  43. end
  44. function w = reconstructSig(nx,ny,ww,dxy)
  45. % W = reconstructSig(nx,ny,ww,dxy)
  46. % W = n x n
  47. % n = ny * nx * nf
  48. k = size(dxy,1);
  49. nf = size(ww,1);
  50. n = ny*nx;
  51. w = zeros(nf,nf,n,n);
  52. for x1 = 1:nx,
  53. for y1 = 1:ny,
  54. i1 = (x1-1)*ny + y1;
  55. for i = 1:k,
  56. x = dxy(i,1);
  57. y = dxy(i,2);
  58. x2 = x1 + x;
  59. y2 = y1 + y;
  60. if x2 >= 1 && x2 <= nx && y2 >= 1 && y2 <= ny,
  61. i2 = (x2-1)*ny + y2;
  62. w(:,:,i1,i2) = ww(:,:,i);
  63. end
  64. x2 = x1 - x;
  65. y2 = y1 - y;
  66. if x2 >= 1 && x2 <= nx && y2 >= 1 && y2 <= ny,
  67. i2 = (x2-1)*ny + y2;
  68. w(:,:,i1,i2) = ww(:,:,i)';
  69. end
  70. end
  71. end
  72. end
  73. % Permute [nf nf n n] to [n nf n nf]
  74. w = permute(w,[3 1 4 2]);
  75. w = reshape(w,n*nf,n*nf);
  76. % Make sure returned matrix is close to symmetric
  77. assert(sum(sum(abs(w - w'))) < 1e-5);
  78. w = (w+w')/2;
  79. end