-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathV1.m
355 lines (328 loc) · 14.9 KB
/
V1.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
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
function [outNormalized,outGrad,out] = V1(imageBitmap,degSize,V1Mode,pars,Gradidx)
%function [out,outGrad] = V1(imageBitmap,degSize,type,pars,Gradidx)
%
% this runs a log-Gabor pyramid decompostion and computes a normalization
% of the different channels.
%
% Gradient is only implemented for number 4+6, i.e. the constant size
% normalization pool variants
%
% There is a severe bug in the current gpuArray.ifft implementation for
% symmetric. I cured this for number 4 and 6 but all other variants of the model
% remain unchecked: Be freakishly careful with this....
%
% Derivatives can be restricted to only some parameters using the Gradidx
% argument. It should then contain the parameters you want the derivatives
% to.
global useGPU
if isempty(useGPU)
useGPU = false;
end
% pars are fixed in the model files and should not default here
CNaka = pars(1); % Naka rushton constant
ExNaka = pars(2)+pars(3); % Naka rushton Exponent top
ExNakaNorm = pars(2); % Naka rushton Exponent below
bw(1) = pars(4); % bandwidth in frequency (std of log-Gabor in octaves)
bw(2) = pars(5); % bandwidth in orientation (std log-Gabor in radiants)
nFreq = pars(6); % number of frequency bands
nOrient = pars(7); % number of orientations
poolSize = pars(8); % meaning depends on type, spatial size of normalization pool
poolbw = pars(9); % bandwidth of the normalization pool (Octaves in frequency)
minF = pars(10); % lowest frequency band
maxF = pars(11); % highest frequency band
if length(pars)>=12
poolO = pars(12);
end
if nargout >= 2
if ~exist('Gradidx','var')
Gradidx = [1,2,3,4,5,9,12];
end
GradOrder = nan(size(pars));
for iPar = 1:length(pars)
if ismember(iPar,Gradidx)
GradOrder(iPar) = find(Gradidx==iPar);
end
end
if useGPU
outGrad = gpuArray.zeros([size(imageBitmap),nOrient,nFreq,length(Gradidx)]);
else
outGrad = zeros([size(imageBitmap),nOrient,nFreq,length(Gradidx)]);
end
end
%% first independent of the type we calculate the frequency decomposition
% into log-Gabor frequency and orientation bands
if useGPU
imageBitmap = gpuArray(imageBitmap);
end
if nargout >=2 && any(~isnan(GradOrder(4:5)))
[out,frequencies,pyrGradBW] = decomp_Gabor(imageBitmap,degSize,[minF,maxF],nFreq,nOrient,bw);
ao = abs(out);
if all(~isnan(GradOrder(4:5)))
outGrad(:,:,:,:,GradOrder(4:5)) = bsxfun(@rdivide,bsxfun(@times,real(out),real(pyrGradBW))+bsxfun(@times,imag(out),imag(pyrGradBW)),max(ao,eps));
elseif ~isnan(GradOrder(4))
outGrad(:,:,:,:,GradOrder(4)) = bsxfun(@rdivide,bsxfun(@times,real(out),real(pyrGradBW(:,:,:,:,1)))+bsxfun(@times,imag(out),imag(pyrGradBW(:,:,:,:,1))),max(ao,eps));
elseif ~isnan(GradOrder(5))
outGrad(:,:,:,:,GradOrder(5)) = bsxfun(@rdivide,bsxfun(@times,real(out),real(pyrGradBW(:,:,:,:,2)))+bsxfun(@times,imag(out),imag(pyrGradBW(:,:,:,:,2))),max(ao,eps));
end
else
[out,frequencies] = decomp_Gabor(imageBitmap,degSize,[minF,maxF],nFreq,nOrient,bw);
ao = abs(out);
end
imSize = size(imageBitmap);
x = 1:imSize(2);
y = 1:imSize(1);
x = (x-ceil(mean(x))).*degSize(2)./imSize(2);
y = (y-ceil(mean(y))).*degSize(1)./imSize(1);
if nargout>= 2
r = bsxfun(@plus,x.^2,y'.^2);
end
switch V1Mode
case 1 % spatially pool whole image (Probably only sensible for foveal model)
% For obvious reasons this ignores the size of the pool in space
assert(length(pars)>=12,'You need to provide a std for orientation normalization pool')
lao = log(ao);
lao0 = lao;
lao0(ao==0) = 0;
%normalizer1 = abs(out).^ExNakaNorm;
normalizer1 = exp(lao.*ExNakaNorm);
normalizer = mean(mean(normalizer1,1),2);
fdiff = linspace(log2(minF)-log2(maxF),log2(maxF)-log2(minF),2*length(frequencies)-1);
gaussF = exp(-fdiff.^2./poolbw.^2./2);
gaussFN = gaussF./sum(gaussF(:));
gaussFN = reshape(gaussFN,1,1,1,[]);
o = ((0:(size(out,3)-1))-floor(size(out,3)/2))*pi/size(out,3);
if ~isfinite(poolO)
gaussO = ones(size(o));
elseif poolO == 0
gaussO = (o == 0);
else
gaussO = exp(-o.^2./poolO.^2./2);
end
gaussON = gaussO./sum(gaussO);
gaussONF = fft(ifftshift(gaussON));
%gaussO = reshape(gaussO,1,1,[]);
gaussONF = gaussONF(:);
% fourier space filtering to enable "wrap around"
normalizerF= fft(squeeze(normalizer));
if nargout>=2
if ~isnan(GradOrder(9))
gaussFD = exp(-fdiff.^2./poolbw.^2./2).*fdiff.^2./poolbw.^3;
gaussFD = (gaussFD(:)- gaussFN(:).*sum(gaussFD(:)))./sum(gaussF(:));
gaussFD = reshape(gaussFD,1,1,1,[]);
end
if ~isnan(GradOrder(12))
gaussOD = exp(-o.^2./poolO.^2./2).*o.^2./poolO.^3;
gaussOD = (gaussOD(:)- gaussON(:).*sum(gaussOD(:)))./sum(gaussO(:));
gaussODF = fft(ifftshift(gaussOD));
gaussODF = gaussODF(:);
BdO = bsxfun(@times,normalizerF,gaussODF);
BdO = ifft(BdO,'symmetric');
end
end
normalizerF = bsxfun(@times,normalizerF,gaussONF);
normalizer = ifft(normalizerF,'symmetric');
normalizer = reshape(normalizer,1,1,nOrient,nFreq);
normalizerPad = padarray(normalizer,[0,0,0,size(normalizer,4)-1]);
if nargout>=2
if ~isnan(GradOrder(12))
BdO = reshape(BdO,1,1,nOrient,nFreq);
BdOPad = padarray(BdO,[0,0,0,size(BdO,4)-1]);
BdO = convn(BdOPad,gaussFN,'valid');
end
if ~isnan(GradOrder(9))
BdF = convn(normalizerPad,gaussFD,'valid');
end
end
normalizer = convn(normalizerPad,gaussFN,'valid');
normalizerFin = (normalizer+CNaka.^ExNakaNorm);
%outNormalized = bsxfun(@rdivide,abs(out).^ExNaka,normalizerFin);
outNormalized = bsxfun(@rdivide,exp(lao.*ExNaka),normalizerFin);
if nargout >= 2 % calculate gradient
if ~isnan(GradOrder(2))
logNormalizer = lao0.*normalizer1;
logNormalizer = mean(mean(logNormalizer,1),2);
logNormalizerF= fft(squeeze(logNormalizer));
logNormalizerF = bsxfun(@times,logNormalizerF,gaussONF);
logNormalizer = ifft(logNormalizerF,'symmetric');
logNormalizer = reshape(logNormalizer,1,1,nOrient,nFreq);
logNormalizerPad = padarray(logNormalizer,[0,0,0,size(logNormalizer,4)-1]);
logNormalizer = convn(logNormalizerPad,gaussFN,'valid');
end
if ~isnan(GradOrder(4))
Bdf = exp(lao.*(ExNakaNorm-1)).*outGrad(:,:,:,:,GradOrder(4));
Bdf = ExNakaNorm*mean(mean(Bdf,1),2);
BdfF= fft(squeeze(Bdf));
BdfF = bsxfun(@times,BdfF,gaussONF);
Bdf = ifft(BdfF,'symmetric');
Bdf = reshape(Bdf,1,1,nOrient,nFreq);
BdAPad = padarray(Bdf,[0,0,0,size(Bdf,4)-1]);
Bdf = convn(BdAPad,gaussFN,'valid');
end
if ~isnan(GradOrder(5))
Bdphi = exp(lao.*(ExNakaNorm-1)).*outGrad(:,:,:,:,GradOrder(5));
Bdphi = ExNakaNorm*mean(mean(Bdphi,1),2);
BdphiF= fft(squeeze(Bdphi));
BdphiF = bsxfun(@times,BdphiF,gaussONF);
Bdphi = ifft(BdphiF,'symmetric');
Bdphi = reshape(Bdphi,1,1,nOrient,nFreq);
BdAPad = padarray(Bdphi,[0,0,0,size(Bdphi,4)-1]);
Bdphi = convn(BdAPad,gaussFN,'valid');
end
if any(~isnan(GradOrder([4,5,9,12])))
RdB = bsxfun(@rdivide,-outNormalized,normalizerFin);
end
if any(~isnan(GradOrder([4,5])))
RdA = ExNaka.* bsxfun(@rdivide,exp(lao.*(ExNaka-1)),normalizerFin);
end
if ~isnan(GradOrder(1))
outGrad(:,:,:,:,GradOrder(1)) = bsxfun(@rdivide,-outNormalized.*ExNakaNorm.*(CNaka.^(ExNakaNorm-1)),normalizerFin);
end
if ~isnan(GradOrder(2))
outGrad(:,:,:,:,GradOrder(2)) = outNormalized.*(bsxfun(@minus,lao0, bsxfun(@rdivide,logNormalizer+log(CNaka).*CNaka.^ExNakaNorm,normalizerFin)));
end
if ~isnan(GradOrder(3))
outGrad(:,:,:,:,GradOrder(3)) = lao0.*outNormalized;
end
if ~isnan(GradOrder(4))
outGrad(:,:,:,:,GradOrder(4)) = RdA.*outGrad(:,:,:,:,GradOrder(4))+bsxfun(@times,RdB,Bdf);
end
if ~isnan(GradOrder(5))
outGrad(:,:,:,:,GradOrder(5)) = RdA.*outGrad(:,:,:,:,GradOrder(5))+bsxfun(@times,RdB,Bdphi);
end
if ~isnan(GradOrder(9))
outGrad(:,:,:,:,GradOrder(9)) = bsxfun(@times,RdB,BdF);
end
if ~isnan(GradOrder(12))
outGrad(:,:,:,:,GradOrder(12)) = bsxfun(@times,RdB,BdO);
end
end
case 2 % local normalization -> only activations at this pixel count
assert(length(pars)>=12,'You need to provide a std for orientation normalization pool')
lao = log(ao);
lao0 = lao;
lao0(ao==0) = 0;
normalizer1 = exp(lao.*ExNakaNorm);
normalizer = normalizer1;
fdiff = linspace(log2(minF)-log2(maxF),log2(maxF)-log2(minF),2*length(frequencies)-1);
gaussF = exp(-fdiff.^2./poolbw.^2./2);
gaussFN = gaussF./sum(gaussF(:));
gaussFN = reshape(gaussFN,1,1,1,[]);
o = ((0:(size(out,3)-1))-floor(size(out,3)/2))*pi/size(out,3);
if ~isfinite(poolO)
gaussO = ones(size(o));
elseif poolO == 0
gaussO = (o == 0);
else
gaussO = exp(-o.^2./poolO.^2./2);
end
gaussON = gaussO./sum(gaussO);
gaussONF= fft(ifftshift(gaussON));
gaussONF= reshape(gaussONF,1,1,[]);
if useGPU
gaussONF = gpuArray(gaussONF);
gaussFN = gpuArray(gaussFN);
end
% fourier space filtering to enable "wrap around"
normalizerF = fft(normalizer,[],3);
if nargout >= 2
if ~isnan(GradOrder(9))
gaussFD = exp(-fdiff.^2./poolbw.^2./2).*fdiff.^2./poolbw.^3;
gaussFD = (gaussFD(:)- gaussFN(:).*sum(gaussFD(:)))./sum(gaussF(:));
gaussFD = reshape(gaussFD,1,1,1,[]);
if useGPU
gaussFD = gpuArray(gaussFD);
end
end
if ~isnan(GradOrder(12))
gaussOD = exp(-o.^2./poolO.^2./2).*o.^2./poolO.^3;
gaussOD = (gaussOD(:)- gaussON(:).*sum(gaussOD(:)))./sum(gaussO(:));
gaussODF= fft(ifftshift(gaussOD));
gaussODF= reshape(gaussODF,1,1,[]);
BdO = bsxfun(@times,normalizerF,gaussODF);
BdO = ifft(BdO,[],3,'symmetric');
end
if ~isnan(GradOrder(8))
BdS = bsxfun(@times,fft(BdS,[],3),gaussONF);
BdS = ifft(BdS,[],3,'symmetric');
end
end
normalizerF = bsxfun(@times,normalizerF,gaussONF);
normalizer = ifft(normalizerF,[],3,'symmetric');
clear normalizerF
normalizerPad = padarray(normalizer,[0,0,0,size(normalizer,4)-1]);
if nargout>=2
if ~isnan(GradOrder(12))
BdOPad = padarray(BdO,[0,0,0,size(BdO,4)-1]);
BdO = convn(BdOPad,gaussFN,'valid');
clear BdOPad
end
if ~isnan(GradOrder(9))
BdF = convn(normalizerPad,gaussFD,'valid');
end
if ~isnan(GradOrder(8))
BdSPad = padarray(BdS,[0,0,0,size(BdS,4)-1]);
BdS = convn(BdSPad,gaussFN,'valid');
clear BdSPad
end
end
normalizer = convn(normalizerPad,gaussFN,'valid');
clear normalizerPad
normalizerFin = (normalizer+CNaka.^ExNakaNorm);
outNormalized = bsxfun(@rdivide,exp(lao.*ExNaka),normalizerFin);
if nargout >= 2 % calculate gradient
if ~isnan(GradOrder(2))
logNormalizer = lao0.*normalizer1;
logNormalizerF= fft(logNormalizer,[],3);
logNormalizerF = bsxfun(@times,logNormalizerF,gaussONF);
logNormalizer = ifft(logNormalizerF,[],3,'symmetric');
logNormalizerPad = padarray(logNormalizer,[0,0,0,size(logNormalizer,4)-1]);
logNormalizer = convn(logNormalizerPad,gaussFN,'valid');
end
if ~isnan(GradOrder(4))
Bdf = ExNakaNorm.*exp(lao.*(ExNakaNorm-1)).*outGrad(:,:,:,:,GradOrder(4));
BdfF= fft(Bdf,[],3);
BdfF = bsxfun(@times,BdfF,gaussONF);
Bdf = ifft(BdfF,[],3,'symmetric');
BdAPad = padarray(Bdf,[0,0,0,size(Bdf,4)-1]);
Bdf = convn(BdAPad,gaussFN,'valid');
end
if ~isnan(GradOrder(5))
Bdphi = ExNakaNorm.*exp(lao.*(ExNakaNorm-1)).*outGrad(:,:,:,:,GradOrder(5));
BdphiF= fft(Bdphi,[],3);
BdphiF= bsxfun(@times,BdphiF,gaussONF);
Bdphi = ifft(BdphiF,[],3,'symmetric');
BdAPad= padarray(Bdphi,[0,0,0,size(Bdphi,4)-1]);
Bdphi = convn(BdAPad,gaussFN,'valid');
end
if any(~isnan(GradOrder([4,5,8,9,12])))
RdB = bsxfun(@rdivide,-outNormalized,normalizerFin);
end
if any(~isnan(GradOrder([4,5])))
RdA = ExNaka.* bsxfun(@rdivide,exp(lao.*(ExNaka-1)),normalizerFin);
end
if ~isnan(GradOrder(1))
outGrad(:,:,:,:,GradOrder(1)) = bsxfun(@rdivide,-outNormalized.*ExNakaNorm.*(CNaka.^(ExNakaNorm-1)),normalizerFin);
end
if ~isnan(GradOrder(2))
outGrad(:,:,:,:,GradOrder(2)) = outNormalized.*(bsxfun(@minus,lao0, bsxfun(@rdivide,logNormalizer+log(CNaka).*CNaka.^ExNakaNorm,normalizerFin)));
end
if ~isnan(GradOrder(3))
outGrad(:,:,:,:,GradOrder(3)) = lao0.*outNormalized;
end
if ~isnan(GradOrder(4))
outGrad(:,:,:,:,GradOrder(4)) = RdA.*outGrad(:,:,:,:,GradOrder(4))+bsxfun(@times,RdB,Bdf);
end
if ~isnan(GradOrder(5))
outGrad(:,:,:,:,GradOrder(5)) = RdA.*outGrad(:,:,:,:,GradOrder(5))+bsxfun(@times,RdB,Bdphi);
end
if ~isnan(GradOrder(8))
outGrad(:,:,:,:,GradOrder(8)) = bsxfun(@times,RdB,BdS);
end
if ~isnan(GradOrder(9))
outGrad(:,:,:,:,GradOrder(9)) = bsxfun(@times,RdB,BdF);
end
if ~isnan(GradOrder(12))
outGrad(:,:,:,:,GradOrder(12))= bsxfun(@times,RdB,BdO);
end
end
end