123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246 |
- function BG = trainBGwithArbitraryFeatures( allImages, settings )
-
-
- order = settings.order;
-
- interval = settings.interval;
-
-
- i_binSize = settings.i_binSize;
-
-
-
-
- BG.i_binSize = i_binSize;
- BG.interval = interval;
-
-
-
- BG.i_numCells = [2,2];
-
- i_truncDim = getFieldWithDefault ( settings, 'i_truncDim', -1 );
-
-
-
-
-
-
-
-
-
-
-
-
- if ( i_truncDim < 0 )
- if ( strcmp ( settings.fh_featureExtractor.name, 'Compute HOG features using WHO code' ) || ...
- strcmp ( settings.fh_featureExtractor.name, 'HOG and Patch Means concatenated' ) || ...
- strcmp ( settings.fh_featureExtractor.name, 'HOG and Color Names' )...
- )
- i_truncDim = 32;
- else
- i_truncDim = 0;
- end
- end
-
-
- i_numImgChannels = size ( readImage(allImages{1}),3);
- i_numDim = size( settings.fh_featureExtractor.mfunction ( zeros([3 3 i_numImgChannels]) ),3 );
-
- if ( i_truncDim > 0 )
- display('Ignoring last truncation feature');
- i_numDim = i_numDim-1;
- end
-
- neg = zeros(i_numDim,1);
-
-
-
- n = 0;
-
-
-
- fprintf('\nLearning negative mean\n');
-
-
- for i = 1:length(allImages)
-
-
- if( rem(i,10)==1 )
- fprintf('%d / %d\n', i,length(allImages) );
- end
-
- im = readImage( allImages{i} );
- if ( i_numImgChannels ~= size ( im,3) )
-
-
- continue;
- end
-
- pyra = featPyramidGeneric(im, BG, settings );
-
-
- for s = 1:length(pyra.feat)
- featIm = pyra.feat{s};
-
-
- if ( i_truncDim > 0 )
- featIm = featIm(:, :, 1:end~=i_truncDim );
- end
-
- [imy,imx,imz] = size(featIm);
-
-
- t = imy*imx;
-
-
-
- feat = reshape(featIm,t,imz);
-
- n = n + t;
-
- try
- neg = neg + sum(feat)';
- catch err
- err
- end
- end
- end
-
- neg = neg'/n;
- w = order;
- h = order;
- dxy = [];
- for x = 0:w-1,
- for y = 0:h-1,
- dxy = [dxy; [x y]];
- if x > 0 & y > 0,
- dxy = [dxy; [x -y]];
- end
- end
- end
-
- k = size(dxy,1);
- ns = zeros(k,1);
- cov = zeros(i_numDim,i_numDim,k);
-
- fprintf('\nLearning stationairy negative covariance\n');
- for i = 1:length(allImages)
-
-
- if( rem(i,10)==1 )
- fprintf('%d / %d\n', i,length(allImages) );
- end
- im = readImage( allImages{i} );
- if ( i_numImgChannels ~= size ( im,3) )
-
-
- continue;
- end
-
- pyra = featPyramidGeneric(im, BG, settings );
-
-
- for s = 1:length(pyra.feat),
- featIm = pyra.feat{s};
- if ( i_truncDim > 0 )
- featIm = featIm(:, :, 1:end~=i_truncDim );
- end
-
- [imy,imx,imz] = size(featIm);
- featIm = reshape(featIm,imy*imx,imz);
- featIm = bsxfun(@minus,featIm,neg);
- if ( imz > 1 )
- featIm = reshape(featIm,[imy imx imz]);
- else
- featIm = reshape(featIm,[imy imx]);
- end
- pyra.feat{s} = featIm;
- end
-
- for s = 1:length(pyra.feat),
- for i = 1:k,
- dx = dxy(i,1);
- dy = dxy(i,2);
- [imy,imx,~] = size(pyra.feat{s});
-
- if ( dy > 0 )
- y11 = 1;
- y12 = imy - dy;
- else
- y11 = -dy + 1;
- y12 = imy;
- end
-
- if ( dx > 0 )
- x11 = 1;
- x12 = imx - dx;
- else
- x11 = -dx + 1;
- x12 = imx;
- end
-
- if ( ( y12 < y11 ) || ( x12 < x11 ) )
- continue;
- end
-
- y21 = y11 + dy;
- y22 = y12 + dy;
- x21 = x11 + dx;
- x22 = x12 + dx;
- assert(y11 >= 1 && y12 <= imy && ...
- x21 >= 1 && x22 <= imx);
-
- t = (y12 - y11 + 1)*(x12 - x11 + 1);
- feat1 = reshape(pyra.feat{s}(y11:y12,x11:x12,:),t,i_numDim);
- feat2 = reshape(pyra.feat{s}(y21:y22,x21:x22,:),t,i_numDim);
-
- cov(:,:,i) = cov(:,:,i) + feat1'*feat2;
- ns(i) = ns(i) + t;
- end
- end
-
- end
- fprintf('\n');
- neg = neg';
- for i = 1:k
- cov(:,:,i) = cov(:,:,i) / ns(i);
- end
-
-
-
- BG.neg = neg;
- BG.cov = cov;
- BG.dxy = dxy;
- BG.ns = ns;
- BG.lambda = .01;
-
-
- end
|