-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathExponentialModel.m
95 lines (88 loc) · 4.14 KB
/
ExponentialModel.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
close all;
disp(['================> Getting the exponential family model using negative images']);
%% read in negative examples
imageName = dir('negativeImage/*.jpg'); % folder that contains negative examples as q(I)
numImageNeg = size(imageName, 1); % number of negative examples
imageSize = zeros(numImageNeg, 2); % size of negative examples
for i = 1 : numImageNeg
tmpIm = imread(['negativeImage' '/' imageName(i).name]);
if size(tmpIm,3) == 3
tmpIm = rgb2gray(tmpIm);
end
I{i} = imresize(single(tmpIm), 1, 'nearest');
imageSize(i, :) = size(I{i});
end
sizex = min(imageSize(:, 1)); sizey = min(imageSize(:, 2));
for i = 1 : numImageNeg
I{i} = single(I{i}(1:sizex, 1:sizey));
end % make the sizes of the training images to be the same
%% filtering negative examples
for s = 1 : numScale
storeExponentialModelName = ['storedExponentialModel' num2str(s) '.mat'];
if exist(storeExponentialModelName,'file')
continue;
end
scaleFilter = scales(s);
localHalfx = floor(localHalfx1*scales(s)/scales(1));
localHalfy = floor(localHalfy1*scales(s)/scales(1));
[allFilter, allSymbol] = MakeFilter(scaleFilter, numOrient); % generate Gabor filters
halfFilterSize = (size(allFilter{1}, 1)-1)/2; % half size of Gabor
disp(['start filtering negative images at Gabor length ' num2str(halfFilterSize*2+1)]);
tic
allFilteredImage = ApplyFilterfftSame(I, allFilter, localOrNot, localHalfx, localHalfy, doubleOrNot, thresholdFactor); % filter training images
disp(['filtering time: ' num2str(toc) ' seconds']);
%% compute histogram of q()
numBin = floor(saturation/binSize)+1; % binnumbers
histog = zeros(numBin, 1, 'single'); % store F
disp(['start histogramming negative images at Gabor length ' num2str(halfFilterSize*2+1)]);
mexc_Histogram(numImageNeg, numOrient, allFilteredImage, halfFilterSize, sizex, sizey, binSize, numBin, histog, saturation);
disp(['histogramming time: ' num2str(toc) ' seconds']);
%% compute stored lambda, expectation, logZ
r = (0:(numBin-1))*binSize;
%{
figure; h1=plot(r, histog);
title('background density'); xlabel('sigmoid(response)'); ylabel('density');
set(gca,'FontSize',16); % adjust the display format and save the plot
set(get(gca,'XLabel'),'FontSize',18);
set(get(gca,'YLabel'),'FontSize',18);
set(get(gca,'Title'),'FontSize',20);
set(h1,'Linewidth',3);
set(gcf, 'PaperPositionMode', 'auto');
print('-depsc', ['output/backgroundDensity' num2str(s) '.eps']);
%}
storedExpectation = zeros(numStoredPoint, 1);
Z = zeros(numStoredPoint, 1);
for (k=1:numStoredPoint)
lambda = (k-1.)*spacing;
p = exp(lambda*r).*(histog');
Z(k) = sum(p*binSize); p = p/Z(k);
storedExpectation(k) = sum(r.*p*binSize);
end
storedlambda = (0:(numStoredPoint-1))*spacing;
storedLogZ = log(Z);
%{
figure; h1=plot(storedlambda, storedExpectation);
title('mean vs lambda'); xlabel('lambda'); ylabel('mean');
set(gca,'FontSize',16); % adjust the display format and save the plot
set(get(gca,'XLabel'),'FontSize',18);
set(get(gca,'YLabel'),'FontSize',18);
set(get(gca,'Title'),'FontSize',20);
set(h1,'Linewidth',3);
set(gcf, 'PaperPositionMode', 'auto');
print('-depsc', ['output/backgroundLink' num2str(s) '.eps']);
figure; h1=plot(storedlambda, storedLogZ);
title('logZ vs lambda'); xlabel('lambda'); ylabel('logZ');
set(gca,'FontSize',16); % adjust the display format and save the plot
set(get(gca,'XLabel'),'FontSize',18);
set(get(gca,'YLabel'),'FontSize',18);
set(get(gca,'Title'),'FontSize',20);
set(h1,'Linewidth',3);
set(gcf, 'PaperPositionMode', 'auto');
print('-depsc', ['output/backgroundLogZ' num2str(s) '.eps']);
%}
%% store filter properties and exponential model
Correlation = CorrFilter(allFilter, epsilon); % correlation between filters
save(storeExponentialModelName, 'storedlambda', 'storedExpectation', 'storedLogZ', ...
'scaleFilter', 'halfFilterSize', 'allFilter', 'allSymbol', 'Correlation', ...
'localHalfx', 'localHalfy','histog');
end