trainBGwithArbitraryFeatures.m 6.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246
  1. function BG = trainBGwithArbitraryFeatures( allImages, settings )
  2. % function BG = trainBGwithArbitraryFeatures( allImages, settings )
  3. %
  4. % BRIEF
  5. % Trains a spatial autocorrelation function from a list of images
  6. %
  7. % OUTPUT
  8. % ...TODO
  9. %
  10. % author: Alexander Freytag
  11. % date: 13-02-2014 (dd-mm-yyyy)
  12. %% (1) check input
  13. order = settings.order;
  14. interval = settings.interval;
  15. % number of cells in every dimension
  16. i_binSize = settings.i_binSize;
  17. % those three assignments need to be done here already, since the BG
  18. % object is passed to pyramid construction methods
  19. % (featPyramidGeneric), e.g., in line 84 (currently)
  20. BG.i_binSize = i_binSize;
  21. BG.interval = interval;
  22. % size of root filter in cells
  23. % we want to extract at least two cells per dim to ensure save
  24. % calcaluations (strange things happened if set to [1,1] only)
  25. BG.i_numCells = [2,2];
  26. i_truncDim = getFieldWithDefault ( settings, 'i_truncDim', -1 );
  27. %NOTE perhaps we should explicitely specify to NOT use any padding at
  28. %all...
  29. %% (2) set necessary variables
  30. % Ignoring an empty feature dimension? For DPM HOG features, this is
  31. % an empty (===0) truncation feature
  32. % if no dim was specified, we compare against the feature types we
  33. % know...
  34. if ( i_truncDim < 0 )
  35. if ( strcmp ( settings.fh_featureExtractor.name, 'Compute HOG features using WHO code' ) || ...
  36. strcmp ( settings.fh_featureExtractor.name, 'HOG and Patch Means concatenated' ) || ...
  37. strcmp ( settings.fh_featureExtractor.name, 'HOG and Color Names' )...
  38. )
  39. i_truncDim = 32;
  40. else
  41. i_truncDim = 0;
  42. end
  43. end
  44. % compute a feature for a small, empty image to fetch the number of
  45. % dimensions every cell-feature has
  46. i_numImgChannels = size ( readImage(allImages{1}),3);
  47. i_numDim = size( settings.fh_featureExtractor.mfunction ( zeros([3 3 i_numImgChannels]) ),3 );
  48. if ( i_truncDim > 0 )
  49. display('Ignoring last truncation feature');
  50. i_numDim = i_numDim-1;
  51. end
  52. neg = zeros(i_numDim,1);
  53. % will be the total number of cells extracted from all images on all
  54. % scales
  55. n = 0;
  56. %% (3) start learning negative mean
  57. fprintf('\nLearning negative mean\n');
  58. % average features over all images, and all possible locations and
  59. % scales
  60. for i = 1:length(allImages)
  61. % progressbar-like output
  62. if( rem(i,10)==1 )
  63. fprintf('%d / %d\n', i,length(allImages) );
  64. end
  65. im = readImage( allImages{i} );
  66. if ( i_numImgChannels ~= size ( im,3) )
  67. % if by chance there are some images not fitting to the other
  68. % ones...
  69. continue;
  70. end
  71. % Extract feature pryamid, removing hallucinated octave
  72. pyra = featPyramidGeneric(im, BG, settings );
  73. % run over all scales
  74. for s = 1:length(pyra.feat)
  75. featIm = pyra.feat{s};
  76. % possibly remove last dimension
  77. if ( i_truncDim > 0 )
  78. featIm = featIm(:, :, 1:end~=i_truncDim );
  79. end
  80. [imy,imx,imz] = size(featIm);
  81. % total number of extractable from the image at the current scale
  82. t = imy*imx;
  83. % compress all cells in each channel into a single long feature
  84. % vector
  85. feat = reshape(featIm,t,imz);
  86. % increase number of totally inspected features
  87. n = n + t;
  88. % add features to previous universal mean
  89. try
  90. neg = neg + sum(feat)';
  91. catch err
  92. err
  93. end
  94. end
  95. end
  96. % normalize mean accordingly
  97. neg = neg'/n;
  98. w = order;
  99. h = order;
  100. dxy = [];
  101. for x = 0:w-1,
  102. for y = 0:h-1,
  103. dxy = [dxy; [x y]];
  104. if x > 0 & y > 0,
  105. dxy = [dxy; [x -y]];
  106. end
  107. end
  108. end
  109. %% (4) start learning covariance matrix
  110. k = size(dxy,1);
  111. ns = zeros(k,1);
  112. cov = zeros(i_numDim,i_numDim,k);
  113. fprintf('\nLearning stationairy negative covariance\n');
  114. for i = 1:length(allImages)
  115. % progressbar-like output
  116. if( rem(i,10)==1 )
  117. fprintf('%d / %d\n', i,length(allImages) );
  118. end
  119. im = readImage( allImages{i} );
  120. if ( i_numImgChannels ~= size ( im,3) )
  121. % if by chance there are some images not fitting to the other
  122. % ones...
  123. continue;
  124. end
  125. % Extract feature pryamid
  126. pyra = featPyramidGeneric(im, BG, settings );
  127. % Subtract mean from all features extracted from current image
  128. for s = 1:length(pyra.feat),
  129. featIm = pyra.feat{s};
  130. if ( i_truncDim > 0 )
  131. featIm = featIm(:, :, 1:end~=i_truncDim );
  132. end
  133. [imy,imx,imz] = size(featIm);
  134. featIm = reshape(featIm,imy*imx,imz);
  135. featIm = bsxfun(@minus,featIm,neg);
  136. if ( imz > 1 )
  137. featIm = reshape(featIm,[imy imx imz]);
  138. else
  139. featIm = reshape(featIm,[imy imx]);
  140. end
  141. pyra.feat{s} = featIm;
  142. end
  143. for s = 1:length(pyra.feat),
  144. for i = 1:k,
  145. dx = dxy(i,1);
  146. dy = dxy(i,2);
  147. [imy,imx,~] = size(pyra.feat{s});
  148. if ( dy > 0 )
  149. y11 = 1;
  150. y12 = imy - dy;
  151. else
  152. y11 = -dy + 1;
  153. y12 = imy;
  154. end
  155. if ( dx > 0 )
  156. x11 = 1;
  157. x12 = imx - dx;
  158. else
  159. x11 = -dx + 1;
  160. x12 = imx;
  161. end
  162. if ( ( y12 < y11 ) || ( x12 < x11 ) )
  163. continue;
  164. end
  165. y21 = y11 + dy;
  166. y22 = y12 + dy;
  167. x21 = x11 + dx;
  168. x22 = x12 + dx;
  169. assert(y11 >= 1 && y12 <= imy && ...
  170. x21 >= 1 && x22 <= imx);
  171. t = (y12 - y11 + 1)*(x12 - x11 + 1);
  172. feat1 = reshape(pyra.feat{s}(y11:y12,x11:x12,:),t,i_numDim);
  173. feat2 = reshape(pyra.feat{s}(y21:y22,x21:x22,:),t,i_numDim);
  174. cov(:,:,i) = cov(:,:,i) + feat1'*feat2;
  175. ns(i) = ns(i) + t;
  176. end
  177. end
  178. end %for i = 1:length(allImages)
  179. fprintf('\n');
  180. neg = neg';
  181. for i = 1:k
  182. cov(:,:,i) = cov(:,:,i) / ns(i);
  183. end
  184. %% (5) write results to output object
  185. BG.neg = neg;
  186. BG.cov = cov;
  187. BG.dxy = dxy;
  188. BG.ns = ns;
  189. BG.lambda = .01;
  190. end