-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdisplayActivations.m
168 lines (148 loc) · 6.52 KB
/
displayActivations.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
% *****************************************************
% display activated templates on selected images
% *****************************************************
storeExponentialModelName = ['storedExponentialModel' num2str(1)];
load(storeExponentialModelName);
load partLocConfig
colors = colormap(hsv(numCluster));
load learning_result bestActivations bestS2Templates
activations = bestActivations;
showPartBoundingBox = true;
partSizeX = templateSize(1);
partSizeY = templateSize(2);
%% preparation
% transform the templates
S2Templates = bestS2Templates;
TransformedTemplate = cell(nTransform,numCluster);
selectedScale = zeros(1,numElement,'single');
for cc = 1:numCluster
for iT = 1:nTransform
templateScaleInd = templateTransform{iT}(1);
rowScale = templateTransform{iT}(2);
colScale = templateTransform{iT}(3);
rotation = templateTransform{iT}(4);
[tmpSelectedRow tmpSelectedCol tmpSelectedOri tmpSelectedScale] = ...
mexc_TemplateAffineTransform( templateScaleInd, rowScale,...
colScale, rotation, S2Templates{cc}.selectedRow, S2Templates{cc}.selectedCol,...
S2Templates{cc}.selectedOri, selectedScale, numOrient );
TransformedTemplate{iT,cc}.selectedRow = tmpSelectedRow;
TransformedTemplate{iT,cc}.selectedCol = tmpSelectedCol;
TransformedTemplate{iT,cc}.selectedOri = tmpSelectedOri;
TransformedTemplate{iT,cc}.selectedScale = tmpSelectedScale;
TransformedTemplate{iT,cc}.selectedLambda = S2Templates{cc}.selectedLambda;
TransformedTemplate{iT,cc}.selectedLogZ = S2Templates{cc}.selectedLogZ;
end
end
%% begin displaying
buf_length = 0;
for img = selected_img
for k = 1:buf_length
fprintf(1,'\b');
end
str = sprintf('%d',img);
fprintf(1,str);
buf_length = length(str);
% load SUM1/MAX1 map
SUM1MAX1mapName = ['working/SUM1MAX1map' 'image' num2str(img) 'scale' num2str(1)];
load(SUM1MAX1mapName, 'SUM1map', 'MAX1map', 'ARGMAX1map', 'M1RowShift', 'M1ColShift',...
'M1OriShifted', 'J');
% find activations
ind = find( activations(1,:) == img );
% initialize the mask of bounding boxes and sketched image
matchedBoundingBox = zeros([size(J{end}),3]); % the bounding box map is of the highest resolution
matchedSym = zeros(size(J{end}),'uint8'); % the sketched image is of the highest resolution
colorSketch = 254*ones([size(J{end}),3],'uint8');
% Gabor basis elements locations
for t = ind(end:-1:1) % for each activated template (or codeword, or part)
% track Gabor elements of activated templates for this template only
gaborXX = [];
gaborYY = [];
gaborOO = [];
gaborMM = [];
iCluster = ceil( ( activations(5,t) + 1 ) / nTransform );
iTransform = activations(5,t) + 1 - (iCluster-1) * nTransform;
iRes = activations(4,t) + 1;
for j = 1:numElement
gaborX = floor(activations(2,t) + TransformedTemplate{iTransform,iCluster}.selectedRow(j));
gaborY = floor(activations(3,t) + TransformedTemplate{iTransform,iCluster}.selectedCol(j));
gaborO = TransformedTemplate{iTransform,iCluster}.selectedOri(j);
if gaborX > 0 && gaborX <= size(MAX1map{iRes,1},1) && gaborY > 0 && gaborY <= size(MAX1map{iRes,1},2)
trace = ARGMAX1map{iRes,gaborO+1}(gaborX,gaborY) + 1;
dx = M1RowShift{gaborO+1}(trace);
dy = M1ColShift{gaborO+1}(trace);
shiftedo = M1OriShifted{gaborO+1}(trace);
gaborX = floor(.5 + gaborX + single(dx));
gaborY = floor(.5 + gaborY + single(dy));
gaborO = single(shiftedo);
end
gaborXX = [gaborXX;gaborX];
gaborYY = [gaborYY;gaborY];
gaborOO = [gaborOO;gaborO];
if gaborX > 0 && gaborX <= size(MAX1map{iRes,1},1) && gaborY > 0 && gaborY <= size(MAX1map{iRes,1},2)
val = SUM1map{iRes,gaborO+1}(gaborX,gaborY);
else
val = 0;
end
gaborMM = [gaborMM; max(0,sqrt(val)-.2)];
gaborMM = [gaborMM; 1];
end
% render the sketches for this activated template
tmpMatchedSym = displayMatchedTemplate([size(J{iRes},1) size(J{iRes},2)],gaborXX,...
gaborYY,gaborOO,zeros(length(gaborXX),1,'single'),gaborMM,allSymbol,numOrient);
scaling = double(size(J{end},1)) / double(size(J{iRes},1));
tmpMatchedSym = imresize(tmpMatchedSym,size(matchedSym),'nearest');
matchedSym = max( matchedSym, tmpMatchedSym );
tmpColorSketch = repmat(double(tmpMatchedSym),[1 1 3]);
ind = find(tmpColorSketch);
for cc = 1:3
tmpColorSketch(:,:,cc) = tmpColorSketch(:,:,cc) * colors(iCluster,cc);
end
colorSketch(ind) = uint8(tmpColorSketch(ind));
if showPartBoundingBox
margin = 2;
largerPartSizeX = floor(partSizeX * scaling+.5);
largerPartSizeY = floor(partSizeY * scaling+.5);
xx = repmat((1:largerPartSizeX),1,margin*2);
yy = [];
for y = [1:margin largerPartSizeY-margin+1:largerPartSizeY]
yy = [yy,ones(1,largerPartSizeX)*y];
end
yy = [yy,repmat((1:largerPartSizeY),1,margin*2)];
for x = [1:margin largerPartSizeX-margin+1:largerPartSizeX]
xx = [xx,ones(1,largerPartSizeY)*x];
end
inRow = single(xx-floor(largerPartSizeX/2)); inCol = single(yy-floor(largerPartSizeY/2));
tScale = 0; rScale = 1; cScale = 1; inO = zeros(numel(inRow),1,'single'); inS = zeros(numel(inRow),1,'single');
actualPartRotation = iTransform-1;
[outRow, outCol] = ...
mexc_TemplateAffineTransform(tScale,rScale,cScale,...
actualPartRotation,inRow,inCol,inO,inS,numOrient);
% directly overwrite the corresponding pixels
for p = 1:length(outRow)
x = floor(.5 + outRow(p) + activations(2,t)*scaling); y = floor(.5 + outCol(p) + activations(3,t)*scaling);
if x > 0 && x <= size(matchedBoundingBox,1) && y > 0 && y <= size(matchedBoundingBox,2)
matchedBoundingBox(x,y,:) = [.5 .9 .6];
end
end
end
end
% overlay
matchedSym = repmat(-single(matchedSym),[1 1 3]);
matchedSym = 1 * (matchedSym-min(matchedSym(:)))/(max(matchedSym(:))-min(matchedSym(:)));
if showPartBoundingBox
for y = 1:size(matchedSym,2)
for x = 1:size(matchedSym,1)
if sum(abs(matchedBoundingBox(x,y,:))) > 0
matchedSym(x,y,:) = matchedBoundingBox(x,y,:);
end
end
end
end
tmp = single(repmat(J{end},[1 1 3])) / 255;
alpha = .6;
tmp = tmp * alpha + matchedSym * (1-alpha);
imwrite( tmp, sprintf('%s/overlayed_image%d.png',destFolder,img) );
imwrite( matchedSym, sprintf('%s/sketch_image%d.png',destFolder,img) );
imwrite( colorSketch, sprintf('%s/colorsketch_image%d.png',destFolder,img) );
end
fprintf(1,'\n');