-
Notifications
You must be signed in to change notification settings - Fork 16
/
Copy pathextract_FV_features_fast.m
135 lines (112 loc) · 3.76 KB
/
extract_FV_features_fast.m
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
function extract_FV_features_fast(opts)
GMM = readGMM(opts.fileGMM);
PCA = readPCA(opts.filePCA);
imagesTOC = readImagesToc(opts.fileImages);
nWords = length(imagesTOC);
imagesPerBatch = 256;
nBatches = int32(ceil(nWords/imagesPerBatch));
featsBatch = zeros(opts.FVdim,imagesPerBatch,'single');
% Write output header
fid = fopen(opts.fileFeatures, 'w');
fwrite(fid, nWords, 'int32');
fwrite(fid, opts.FVdim, 'int32');
fclose(fid);
tic;
for cb=1:nBatches
sp = (cb-1)*imagesPerBatch + 1;
ep = sp + imagesPerBatch -1;
if ep > nWords
ep = nWords;
end
nInBatch = ep-sp+1;
fprintf('Extracting FV batch %d/%d (%d images)\n',cb,nBatches,nInBatch);
% Read image batch
[fid,msg] = fopen(opts.fileImages, 'r');
readIm = @(x) readImage(fid, imagesTOC, x);
imagesBatch = arrayfun(readIm, [sp:ep], 'uniformOutput', false);
fclose(fid);
parfor i=1:length(imagesBatch)
im = imagesBatch{i};
[height,width] = size(im);
im = im2single(im);
% get PHOW features
[frames, descrs] = vl_phow(im, opts.phowOpts{:}) ;
descrs = descrs / 255;
if opts.doMinibox == 0
% XY at GT coordinate space
fx = single(frames(1,:)/width-0.5);
fy = single(frames(2,:)/height-0.5);
else
% XY at word coordinate space
bb = DoBB(im);
w = bb(2)-bb(1)+1;
h = bb(4)-bb(3)+1;
cx = round(bb(1)+w/2);
cy = round(bb(3)+h/2);
fx = single((frames(1,:)-cx)/w);
fy = single((frames(2,:)-cy)/h);
end
xy = [fx; fy];
descrs = [descrs; xy];
[descrs,frames] = normalizeSift(opts,descrs,frames);
featsBatch(:,i) = single(getImageDescriptorFV(opts, GMM, PCA, descrs));
end
featsBatch(isnan(featsBatch)) = 0;
% Write the batch
fid = fopen(opts.fileFeatures, 'r+');
fseek(fid, 2*4 + (int64(cb)-1)*imagesPerBatch*opts.FVdim * 4, 'bof');
fwrite(fid, featsBatch(:,1:nInBatch), 'single');
end
disp(toc);
end
% -------------------------------------------------------------------------
function fv = getImageDescriptorFV(opts, GMM, PCA, descrs)
% -------------------------------------------------------------------------
% Project into PCA space
xy = descrs(opts.SIFTDIM+1:end,:);
descrs=bsxfun(@minus, descrs(1:opts.SIFTDIM,:), PCA.mean);
descrs=PCA.eigvec'*descrs;
descrs = [descrs; xy];
% Extracts FV representation using the GMM
fv = vl_fisher(descrs, GMM.mu, GMM.sigma, GMM.we, 'Improved');
end
function X = normFV(X)
% -------------------------------------------------------------------------
X = sign(X).*sqrt(abs(X));
X = bsxfun(@rdivide, X, sqrt(sum(X.*X)));
X(isnan(X)) = 0;
end
function [descrs_normalized,frames_normalized] = normalizeSift(opts,descrs,frames)
% -------------------------------------------------------------------------
descrs_normalized = descrs;
xy = descrs_normalized(opts.SIFTDIM+1:end,:);
descrs_normalized = descrs_normalized(1:opts.SIFTDIM,:);
% Remove empty ones
idx = find(sum(descrs_normalized)==0);
descrs_normalized(:,idx)=[];
if nargin < 3
frames_normalized = [];
else
frames_normalized = frames;
frames_normalized(:,idx) = [];
end
% Square root:
descrs_normalized = sqrt(descrs_normalized);
% 1/4 norm
X = sum(descrs_normalized.*descrs_normalized).^-0.25;
descrs_normalized = bsxfun(@times, descrs_normalized,X);
xy(:,idx) = [];
descrs_normalized = [descrs_normalized; xy];
descrs_normalized(isnan(descrs_normalized))=0;
end
function im = adjustImage(im)
imOrig = im;
im = im2bw(im);
[h,w] = size(im);
x = find(im==0);
w1 = ceil(min(x)/h);
w2 = floor(max(x)/h);
h1 = min(mod(x,h))+1;
h2 = max(mod(x,h))-1;
im = imOrig(h1:h2,w1:w2);
end