Permalink
Browse files

more local updates

  • Loading branch information...
1 parent e1eb505 commit d57c4853c6853f9d4ead9a0073d49aa919772c46 @quantombone committed Dec 25, 2011
Showing with 92 additions and 172 deletions.
  1. +51 −51 esvm_apply_calibration.m
  2. +11 −79 esvm_detect.m
  3. +15 −17 esvm_detect_imageset.m
  4. +15 −25 esvm_train_exemplars.m
View
@@ -25,9 +25,8 @@
curids = cellfun2(@(x)x.curid,models);
end
-cls = models{1}.cls;
-
-excurids = cellfun2(@(x)x.curid,models);
+%cls = models{1}.cls;
+%excurids = cellfun2(@(x)x.curid,models);
bboxes = cell(1,length(grid));
maxos = cell(1,length(grid));
@@ -40,7 +39,7 @@
continue
end
- if length(grid{i}.extras)>0 && isfield(grid{i}.extras,'maxos')
+ if ~isempty(grid{i}.extras) && isfield(grid{i}.extras,'maxos')
maxos{i} = grid{i}.extras.maxos;
maxos{i}(grid{i}.extras.maxclass~=curcls) = 0;
end
@@ -51,8 +50,8 @@
badex = find(ismember(excurids,{curid}));
bboxes{i}(badex,:) = [];
- if length(grid{i}.extras)>0 && isfield(grid{i}.extras,'maxos')
- if length(maxos{i})>0
+ if ~isempty(grid{i}.extras) && isfield(grid{i}.extras,'maxos')
+ if ~isempty(maxos{i})
maxos{i}(badex) = [];
end
end
@@ -61,25 +60,25 @@
% perform within-exemplar NMS
% NOTE: this is already done during detection time
-if 0
- fprintf(1,'applying exemplar nms\n');
- for i = 1:length(bboxes)
- if size(bboxes{i},1) > 0
- bboxes{i}(:,5) = 1:size(bboxes{i},1);
- bboxes{i} = nms_within_exemplars(bboxes{i},.5);
- if length(grid{i}.extras)>0 && isfield(grid{i}.extras,'os')
- maxos{i} = maxos{i}(bboxes{i}(:,5));
- end
- end
- end
-end
+% if 0
+% fprintf(1,'applying exemplar nms\n');
+% for i = 1:length(bboxes)
+% if size(bboxes{i},1) > 0
+% bboxes{i}(:,5) = 1:size(bboxes{i},1);
+% bboxes{i} = nms_within_exemplars(bboxes{i},.5);
+% if length(grid{i}.extras)>0 && isfield(grid{i}.extras,'os')
+% maxos{i} = maxos{i}(bboxes{i}(:,5));
+% end
+% end
+% end
+% end
%Perform score rescaling
%1. no scaling
%2. platt's calibration (sigmoid scaling)
%3. raw score + 1
-if (exist('M','var') && (length(M)>0) && isfield(M,'betas') && ...
+if (exist('M','var') && (~isempty(M)) && isfield(M,'betas') && ...
~isfield(M,'neighbor_thresh'))
fprintf(1,'Applying betas to %d images:',length(bboxes));
@@ -95,10 +94,11 @@
calib_boxes = calib_boxes(oks,:);
bboxes{i} = calib_boxes;
end
-elseif exist('M','var') && length(M)>0 && isfield(M,'neighbor_thresh')
+elseif exist('M','var') && ~isempty(M) && isfield(M,'neighbor_thresh')
fprintf(1,'Applying M-matrix to %d images:',length(bboxes));
starter=tic;
+ nbrlist = cell(length(bboxes),1);
for i = 1:length(bboxes)
fprintf(1,'.');
if size(bboxes{i},1) == 0
@@ -108,7 +108,7 @@
bboxes{i}(:,end) = bboxes{i}(:,end)+1;
[xraw,nbrlist{i}] = get_box_features(bboxes{i},length(models), ...
- M.neighbor_thresh);
+ M.neighbor_thresh);
r2 = esvm_apply_M(xraw,bboxes{i},M);
bboxes{i}(:,end) = r2;
end
@@ -124,7 +124,7 @@
if size(bboxes{i},1) > 0
bboxes{i}(:,5) = 1:size(bboxes{i},1);
bboxes{i} = nms(bboxes{i},os_thresh);
- if length(grid{i}.extras)>0 && isfield(grid{i}.extras,'maxos')
+ if ~isempty(grid{i}.extras) && isfield(grid{i}.extras,'maxos')
maxos{i} = maxos{i}(bboxes{i}(:,5));
end
if exist('nbrlist','var')
@@ -134,33 +134,33 @@
end
end
-if 0
-if exist('M','var') && length(M)>0 && isfield(M,'betas')
-
- fprintf(1,'Propagating scores onto raw detections\n');
- %% propagate scores onto raw boxes
- for i = 1:length(bboxes)
- calib_boxes = calibrate_boxes(raw_boxes{i},M.betas);
- beta_scores = calib_boxes(:,end);
-
- osmat = getosmatrix_bb(bboxes{i},raw_boxes{i});
- for j = 1:size(osmat,1)
- curscores = (osmat(j,:)>.5) .* beta_scores';
- [aa,bb] = max(curscores);
- bboxes{i}(j,:) = raw_boxes{i}(bb,:);
- bboxes{i}(j,end) = aa;
- end
-
- % new_scores = beta_scores;
- % for j = 1:length(nbrlist{i})
- % new_scores(nbrlist{i}{j}) = max(new_scores(nbrlist{i}{j}),...
- % beta_scores(nbrlist{i}{j}).*...
- % bboxes{i}(nbrlist{i}{j},end));
- % end
- % bboxes{i}(:,end) = new_scores;
- end
-end
-end
+% if 0
+% if exist('M','var') && length(M)>0 && isfield(M,'betas')
+%
+% fprintf(1,'Propagating scores onto raw detections\n');
+% %% propagate scores onto raw boxes
+% for i = 1:length(bboxes)
+% calib_boxes = calibrate_boxes(raw_boxes{i},M.betas);
+% beta_scores = calib_boxes(:,end);
+%
+% osmat = getosmatrix_bb(bboxes{i},raw_boxes{i});
+% for j = 1:size(osmat,1)
+% curscores = (osmat(j,:)>.5) .* beta_scores';
+% [aa,bb] = max(curscores);
+% bboxes{i}(j,:) = raw_boxes{i}(bb,:);
+% bboxes{i}(j,end) = aa;
+% end
+%
+% % new_scores = beta_scores;
+% % for j = 1:length(nbrlist{i})
+% % new_scores(nbrlist{i}{j}) = max(new_scores(nbrlist{i}{j}),...
+% % beta_scores(nbrlist{i}{j}).*...
+% % bboxes{i}(nbrlist{i}{j},end));
+% % end
+% % bboxes{i}(:,end) = new_scores;
+% end
+% end
+% end
% Clip boxes to image dimensions since VOC testing annotation
% always fall within the image
@@ -178,11 +178,11 @@
%Create a string which summarizes the pooling type
calib_string = '';
-if exist('M','var') && length(M)>0 && isfield(M,'betas')
+if exist('M','var') && ~isempty(M) && isfield(M,'betas')
calib_string = '-calibrated';
end
-if exist('M','var') && length(M)>0 && isfield(M,'betas') && isfield(M,'w')
+if exist('M','var') && ~isempty(M) && isfield(M,'betas') && isfield(M,'w')
calib_string = [calib_string '-M'];
end
View
@@ -16,15 +16,15 @@
% resstruct: Sliding window output struct with
% resstruct.bbs{:}: Detection boxes and pyramid locations
% resstruct.xs{:}: Detection features
-% t: The Feature pyramid output
+% feat_pyramid: The Feature pyramid output
%
% Copyright (C) 2011-12 by Tomasz Malisiewicz
% All rights reserved.
%
% This file is part of the Exemplar-SVM library and is made
% available under the terms of the MIT license (see COPYING file).
-if length(models) == 0
+if isempty(models)
resstruct.bbs{1} = zeros(0,0);
resstruct.xs{1} = zeros(0,0);
feat_pyramid = [];
@@ -77,11 +77,11 @@
function [resstruct,t] = esvm_detectdriver(I, models, ...
localizeparams)
-%If the number of specified models is greater than 20, use the
+%NOTE: If the number of specified models is greater than 20, use the
%BLOCK-based method
MAX_MODELS_BEFORE_BLOCK_METHOD = 20;
if (length(models)>MAX_MODELS_BEFORE_BLOCK_METHOD) ...
- || (length(localizeparams.nnmode)>0)
+ || (~isempty(localizeparams.nnmode))
[resstruct,t] = esvm_detectdriverBLOCK(I, models, ...
localizeparams);
return;
@@ -93,7 +93,7 @@
%NOTE: all exemplars in this set must have the same sbin
sbin = models{1}.model.init_params.sbin;
-t = get_pyramid(I, sbin, length(models), localizeparams);
+t = get_pyramid(I, sbin, localizeparams);
resstruct.padder = t.padder;
resstruct.bbs = cell(N,1);
@@ -109,6 +109,7 @@
wxs = cellfun2(@(x)reshape(x.model.x(:,1),size(x.model.w)), ...
models);
ws2 = ws;
+ special_offset = zeros(length(ws2),1);
for q = 1:length(ws2)
ws2{q} = -2*ws{q}.*wxs{q};
special_offset(q) = ws{q}(:)'*(models{q}.model.x(:,1).^2);
@@ -236,7 +237,7 @@
templates(:,:,:,i) = t;
template_masks(:,:,:,i) = repmat(double(sum(t.^2,3)>0),[1 1 features]);
- if (length(localizeparams.nnmode) > 0) || ...
+ if (~isempty(localizeparams.nnmode)) || ...
(isfield(localizeparams,'wtype') && ...
strcmp(localizeparams.wtype,'dfun')==1)
x = zeros(S(1),S(2),features);
@@ -296,7 +297,7 @@
r2 = repmat(sum(W.*(U.^2),1)',1,size(X,2));
r = (W'*(X.^2) - 2*(W.*U)'*X + r2);
r = bsxfun(@minus, r, bs);
-elseif length(localizeparams.nnmode) == 0
+elseif isempty(localizeparams.nnmode)
%nnmode 0: Apply linear classifiers by performing one large matrix
%multiplication and subtract bias
r = exemplar_matrix' * X;
@@ -311,62 +312,8 @@
U = reshape(templates_x,[],length(models));
r2 = repmat(sum(W.*(U.^2),1)',1,size(X,2));
r = - (W'*(X.^2) - 2*(W.*U)'*X + r2);
-elseif strcmp(localizeparams.nnmode,'cosangle') == 1
- %nnmode 1: Apply linear classifiers by performing one large matrix
- %multiplication and subtract bias
-
- %mf = mean(X,1);
- %X = X - repmat(mf,size(X,1),1);
- %r = exemplar_matrix' * X;
-
- exemplar_matrix = reshape(templates_x, [], size(templates_x,4));
-
- % %% do normalization for each window
- % tm = template_masks;
- % tm = reshape(tm,[],length(models));
- % ut = unique(tm','rows');
- % [tmp,utids] = ismember(tm',ut,'rows');
-
- % r = zeros(length(models),size(finalf,2));
- % for j = 1:size(ut,1)
- % fprintf(1,'!');
- % curmask = repmat(reshape(ut(j,:),...
- % [size(templates_x,1) size(templates_x,2)]),...
- % [1 1 features]);
- % hits = find(utids == j);
-
- % curf = finalf;
- % curf = curf.*repmat(curmask(:),1,size(curf,2));
- % curr = slmetric_pw(exemplar_matrix(:,hits),curf,'nrmcorr');
-
- % r(hits,:) = curr;
- % end
-
- r = slmetric_pw(exemplar_matrix, X, 'nrmcorr');
-
- %% why am I not getting perfect hits for g-mode?
- %% ANSWER: because there is a padding which we cannot enforce on
- %test-windows efficiently...This will affect the normalization of
- %the test windows
-
- % exemplar_x_matrix = reshape(templates_x,[],size(templates_x,4));
- % res = sum(exemplar_matrix .* exemplar_x_matrix,1);
- % exemplar_matrix = exemplar_matrix ./ repmat(res+eps,size(exemplar_matrix,1),1);
- % r = exemplar_matrix' * finalf;
-
- %keyboard
-
- % rrr = randperm(size(www2,2));
- % rrr = rrr(1:10);
- % r2 = slmetric_pw(www2(:,rrr),finalf,'chisq');
- % r = zeros(size(www2,2),size(finalf,2));
- % r(rrr,:) = exp(-.001*r2);
-
- %r = exemplar_matrix' * finalf;
- %r = bsxfun(@minus, r, bs);
-
else
- error(sprintf('invalid nnmode=%s\n',localizeparams.nnmode));
+ error('invalid nnmode=%s\n',localizeparams.nnmode);
end
resstruct.bbs = cell(N,1);
@@ -442,15 +389,12 @@
end
end
-function t = get_pyramid(I, sbin, N, localizeparams)
+function t = get_pyramid(I, sbin, localizeparams)
%Extract feature pyramid from variable I (which could be either an image,
%or already a feature pyramid)
if isnumeric(I)
-
- flipstring = '';
if (localizeparams.FLIP_LR == 1)
- flipstring = '@F';
I = flip_image(I);
else
%take unadulterated "aka" un-flipped image
@@ -459,9 +403,6 @@
clear t
t.size = size(I);
- %fprintf(1,'Localizing %d in I=[%dx%d@%d%s]',N,...
- % t.size(1),t.size(2),localizeparams.lpo,flipstring);
-
%Compute pyramid
[t.hog,t.scales] = featpyramid2(I, sbin, localizeparams);
t.padder = localizeparams.pyramid_padder;
@@ -471,13 +412,7 @@
minsizes = cellfun(@(x)min([size(x,1) size(x,2)]), t.hog);
t.hog = t.hog(minsizes >= t.padder*2);
- t.scales = t.scales(minsizes >= t.padder*2);
-
- %if only_compute_pyramid == 1
- % resstruct = t;
- % return;
- %end
-
+ t.scales = t.scales(minsizes >= t.padder*2);
else
fprintf(1,'Already found features\n');
@@ -490,8 +425,5 @@
else
t = I;
end
-
- %fprintf(1,'Localizing %d in I=[%dx%d@%d]',N,...
- % t.size(1),t.size(2),localizeparams.lpo);
end
Oops, something went wrong.

0 comments on commit d57c485

Please sign in to comment.