-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathVisualizeClassifier.m
More file actions
142 lines (115 loc) · 3.46 KB
/
VisualizeClassifier.m
File metadata and controls
142 lines (115 loc) · 3.46 KB
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
function im = VisualizeClassifier(jabfile)
%% params.
params = getParams;
npatches = params.npatches;
psize = params.psize;
nbins = params.nbins;
patchsz = params.patchsz;
scale = params.scale;
wd = params.wd;
%% load an example fly.
Q = load(jabfile,'-mat');
bdir = Q.x.expDirNames{1};
moviename = fullfile(bdir,'movie.ufmf');
trackfilename = fullfile(bdir,'trx.mat');
tracks = load(trackfilename);
tracks = tracks.trx;
[readfcn,nframes,fid,headerinfo] = get_readframe_fcn(moviename);
trackndx = 1; fly = 1;
fnum = tracks(fly).firstframe;
im1 = readfcn(fnum);
locy = round(tracks(fly).y(trackndx));
locx = round(tracks(fly).x(trackndx));
im1 = extractPatch(im1,...
locy,locx,tracks(fly).theta(trackndx),patchsz);
im1 =[im1 im1];
%% Find Hog/HOF components.
ncl = numel(Q.x.classifierStuff);
for clnum = 1:ncl
H{clnum} = zeros(npatches,npatches,nbins);
F{clnum} = H{clnum};
cl = Q.x.classifierStuff(clnum).params;
fnames = Q.x.classifierStuff(clnum).featureNames;
for ndx = 1:numel(cl)
curf = fnames{cl(ndx).dim}{1};
val = cl(ndx).alpha;
prts = strsplit(curf,'_');
loc = str2double(prts(end-2:end));
if strcmp(curf(1:2),'hf')
H{clnum}(loc(1),loc(2),loc(3))=H{clnum}(loc(1),loc(2),loc(3))+val;
else
F{clnum}(loc(1),loc(2),loc(3))=F{clnum}(loc(1),loc(2),loc(3))+val;
end
end
end
%% Draw them
% For HOG
bincenters = linspace(0,pi,nbins+1);
bincenters = bincenters(1:nbins);
dt = mean(diff(bincenters));
binedges = [bincenters(1)-dt/2,(bincenters(1:end-1)+bincenters(2:end))/2,bincenters(end)+dt/2];
% For HOF
bincenters2 = bincenters*2;
dt = mean(diff(bincenters2));
binedges2 = [bincenters2(1)-dt/2,(bincenters2(1:end-1)+bincenters2(2:end))/2,bincenters2(end)+dt/2];
maxv = 3;
im = {};
for clnum = 1:ncl
hfig = figure;
clf;
hax = axes('Position',[0,0,1,1]);
set(hfig,'Units','pixels','Position',get(0,'ScreenSize'));
im1curr = im1;
him = imshow(imresize(im1curr,scale));
axis image;
truesize;
colormap gray;
hold on;
axis off;
colors = hsv(nbins);
[nr,nc,~] = size(im1);
nc = nc/2;
h = [];
for xi = 1:ceil(nc/psize),
cx = (psize/2 + (xi-1)*psize)*scale + 1 ;
if cx+psize/2 > nc*scale,
break;
end
for yi = 1:ceil(nr/psize),
cy = (psize/2 + (yi-1)*psize)*scale + 1;
if cy+psize/2 > nr*scale,
break;
end
for bini = 1:nbins,
tmp = linspace(binedges2(bini),binedges2(bini+1),20);
xcurr = cx + [0,psize/2*cos(tmp),0]*scale;
ycurr = cy + [0,psize/2*sin(tmp),0]*scale;
h(yi,xi,bini) = patch(xcurr,ycurr,colors(bini,:),'LineStyle','none','FaceAlpha',min(1,F{clnum}(yi,xi,bini)/maxv));
end
end
end
colors = hsv(nbins);
colors = colors([ (end/2+1):end 1:end/2],:);
hogpatch = [wd wd -wd -wd wd;-psize psize psize -psize -psize]/2;
h = [];
for xi = 1:ceil(nc/psize),
cx = (psize/2 + 1 + (xi-1)*psize)*scale;
if cx+psize/2 > nc*scale,
break;
end
for yi = 1:ceil(nr/psize),
cy = (psize/2 + 1 + (yi-1)*psize)*scale;
if cy+psize/2 > nr*scale,
break;
end
for bini = 1:nbins,
tmp = bincenters(bini);
curpatch = [cos(tmp) -sin(tmp); sin(tmp) cos(tmp)]*hogpatch;
xcurr = nc*scale + cx + curpatch(1,:)*scale;
ycurr = cy + curpatch(2,:)*scale;
h(yi,xi,bini) = patch(xcurr,ycurr,colors(bini,:),'LineStyle','none','FaceAlpha',min(1,H{clnum}(yi,xi,bini)/maxv));
end
end
end
im{clnum} = getframe(hax);
end