-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathrun_crf_upper.m
executable file
·284 lines (234 loc) · 8.43 KB
/
run_crf_upper.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
function predictions = run_crf_upper(new_annorect, geometric_binary, C, single_person, num_iter, debugFlag)
if ~exist('debugFlag','var')
debugFlag = 0;
end
if ~exist('num_iter','var')
num_iter = 15;% num trw-s iters
end
predictions = [];
%% params
useGeo = 1;
min_weight = 1e-30; % min unary weights
dist_bound = 1;
use_restricted_mid = 1;
if (single_person)
se4 = strel('disk',4,0);
else
se4 = strel('disk',1,0);
end
pairs = C.CRF_pairs_ex4_r;
N_points_dense = C.N_dense_pts;
load('./dists_maps/offset_b_given_a_small_res12.mat');
%% part1 points
toPredict1 = [C.pelvis,C.thorax,C.upper_neck,C.head_top]+1;
pairs_part1 = [...
C.head_top, C.head_center;
C.head_center, C.upper_neck;
C.head_top, C.upper_neck
C.thorax, C.mid_body;
C.mid_body, C.pelvis;
C.thorax, C.pelvis;
C.upper_neck, C.thorax;
];
N_trip1 = 2;
%%
toPredict2 = [C.head_top, C.upper_neck, C.l_shoulder, C.r_shoulder,C.l_hip, C.r_hip]+1;
pairs_part2 = [...
C.head_top, C.head_center;
C.head_center, C.upper_neck;
C.head_top, C.upper_neck;
%
C.r_shoulder, C.r_body;
C.r_body, C.r_hip;
C.r_shoulder, C.r_hip;
C.l_shoulder, C.l_body;
C.l_body, C.l_hip;
C.l_shoulder, C.l_hip
C.r_shoulder, C.thorax;
C.thorax, C.l_shoulder;
C.r_shoulder, C.l_shoulder;
C.upper_neck, C.r_shoulder;
C.upper_neck, C.l_shoulder;
C.upper_neck, C.thorax];
N_trip2 = 4;
%%
pairs_part3 = [...
C.head_top, C.head_center;
C.head_center, C.upper_neck;
C.head_top, C.upper_neck;
C.r_shoulder, C.r_up_arm;
C.r_up_arm, C.r_elbow;
C.r_shoulder, C.r_elbow;
C.r_elbow, C.r_low_arm;
C.r_low_arm, C.r_wrist;
C.r_elbow, C.r_wrist;
C.l_shoulder, C.l_up_arm;
C.l_up_arm, C.l_elbow;
C.l_shoulder, C.l_elbow;
C.l_elbow, C.l_low_arm;
C.l_low_arm, C.l_wrist;
C.l_elbow, C.l_wrist;
C.thorax, C.mid_body;
C.mid_body, C.pelvis;
C.thorax, C.pelvis;
C.r_shoulder, C.thorax;
C.thorax, C.l_shoulder;
C.r_shoulder, C.l_shoulder;
C.upper_neck, C.r_shoulder;
C.upper_neck, C.l_shoulder;
C.upper_neck, C.thorax;
C.r_wrist, C.r_hand;
C.l_wrist, C.l_hand];
N_trip3 = 7;
[w1,w2] = meshgrid(logspace(-1,3,15),logspace(-1,3,15));
w1_part1 = w1(16);
w2_part1 = w2(16);
w1_part2 = w1(16);
w2_part2 = w2(16);
w1 = w1(95);
w2 = w2(95);
if (~useGeo)
warning('No geometric binary');
w2_part1 = 0;
w2_part2 = 0;
w2 = 0;
end
n_rects = length(new_annorect);
for i_rect=1:n_rects
fprintf('run crf on person %d/%d\n', i_rect, n_rects);
imsize = size(new_annorect(i_rect).image); imsize = imsize(1:2);
pretrainedData = new_annorect(i_rect);
sz_coarse = size(pretrainedData.CRF_pairs(1).binary_a_b);
sz_coarse = sz_coarse(1:2);
%% unary potentials
UE = cell(N_points_dense,1);
sz_unary = size(pretrainedData.probs.unary{1});
sz_coarse_unary = size(pretrainedData.probs.coarse_unary{1});
d = (sz_coarse_unary-sz_coarse)/2;
for ic = 1:N_points_dense
heat = pretrainedData.probs.coarse_unary{ic}(d+1:end-d,d+1:end-d);
UE{ic} = -log(double(heat(:))+min_weight)';
end; clear heat;
UE2 = UE;
%% force mid_body to be in center
if use_restricted_mid
d = (sz_coarse_unary-sz_coarse)/2;
heat = pretrainedData.mid_prob.coarse_unary(d+1:end-d,d+1:end-d);
UE2{C.mid_body+1} = -log(double(heat(:))+min_weight)';
[~,mid_loc] = max(heat(:));
[r_loc,c_loc] = ind2sub(size(heat),mid_loc);
temp = false(sz_coarse);
temp(r_loc,c_loc) = true;
temp = ~imdilate(temp, se4);
UE2{C.mid_body+1}(temp(:)) = 1e10; clear temp;
end
%%
CRF_pairs = pretrainedData.CRF_pairs;
offset_d = sqrt(sum(offset_b_given_a.^2,2));
coarse_scale = 12;
N_pairs_dense = size(pairs,1);
[x,y] = meshgrid(1:sz_coarse(1),1:sz_coarse(2));
x = x(:); y = y(:);
total_binary_from_net = cell(N_pairs_dense,1);
for pair = 1:N_pairs_dense
temp = zeros(prod(sz_coarse),prod(sz_coarse));
binary_a_b = CRF_pairs(pair).binary_a_b;
if (~isempty(binary_a_b))
for i = 1:numel(x)
b = squeeze(binary_a_b(x(i),y(i),:));
location = bsxfun(@plus,offset_b_given_a,[x(i),y(i)]);
valid = location(:,1)>0 & location(:,2)>0 & location(:,1)<= sz_coarse(1) & location(:,2)<= sz_coarse(2) ;
if dist_bound
valid = valid & (offset_d<=pairs(pair,3)/coarse_scale);
end
b = b(valid);
location = location(valid,:);
b_idx = sub2ind(sz_coarse,location(:,1),location(:,2));
a_idx = sub2ind(sz_coarse,x(i),y(i));
temp(a_idx,b_idx(:)) = b;
end
total_binary_from_net{pair} = -log(temp+min_weight);
end
end
%% part1
disp('part 1');
[UE_part,total_part,geom_part,PI_part] = predictPartUtil(toPredict1,pairs_part1,N_trip1,UE2,total_binary_from_net,geometric_binary);
if (useGeo)
PE_part = cellfun(@(x,y) x*w1_part1-w2_part1*log(y),total_part,geom_part,'UniformOutput',0);
else
PE_part = cellfun(@(x) x*w1_part1,total_part,'UniformOutput',0);
end
L = vgg_trw_bp(UE_part, PI_part, PE_part,int32([1,0,num_iter]));
%% part 2
disp('part 2');
oldPred = -1*ones(size(L));
oldPred([C.pelvis,C.thorax]+1) = L([C.pelvis,C.thorax]+1);
[UE_part,total_part,geom_part,PI_part] = predictPartUtil(toPredict2,pairs_part2,N_trip2,UE2,total_binary_from_net,geometric_binary,oldPred,3);
if (useGeo)
PE_part = cellfun(@(x,y) x*w1_part2-w2_part2*log(y),total_part,geom_part,'UniformOutput',0);
else
PE_part = cellfun(@(x) x*w1_part2,total_part,'UniformOutput',0);
end
L = vgg_trw_bp(UE_part, PI_part, PE_part,int32([1,0,num_iter]));
%% part 3
disp('part 3');
oldPred = -1*ones(size(L));
oldPred(toPredict2) = L(toPredict2);
toPredict = 1:18;
toPredict(find(oldPred>0)) = [];
[UE_part,total_part,geom_part,PI_part] = predictPartUtil(toPredict,pairs_part3,N_trip3,UE2,total_binary_from_net,geometric_binary,oldPred,2);
if (useGeo)
PE_part = cellfun(@(x,y) x*w1-w2*log(y),total_part,geom_part,'UniformOutput',0);
else
PE_part = cellfun(@(x) x*w1,total_part,'UniformOutput',0);
end
L = vgg_trw_bp(UE_part, PI_part, PE_part,int32([1,0,num_iter]));
%%
coarse_pred = zeros(numel(UE_part),2);
fine_pred = zeros(numel(UE_part),2);
sz_up_unary = size(pretrainedData.probs.up_unary{1});
d = (sz_coarse_unary-sz_coarse)/2;
for i_pt = 1:size(coarse_pred,1)
ind = L(i_pt);
[coarse_pred(i_pt,1),coarse_pred(i_pt,2)] = ind2sub(sz_coarse,ind);
pt_coarse_loc = coarse_pred(i_pt,:) + d;
indx_pt = pretrainedData.probs.arg_coarse_unary{i_pt}(pt_coarse_loc(1),pt_coarse_loc(2));
[r_pt,c_pt]=ind2sub(sz_up_unary, 1+indx_pt);
fine_pred(i_pt,:) = [r_pt,c_pt]-(sz_up_unary-imsize)./2;
end
pred = fine_pred;
pred(C.r_ankle+1:C.l_ankle+1,:)=nan;
predictions{i_rect} = pred;
end
%%
if (debugFlag)
figure; imagesc(new_annorect(i_rect).image); axis equal; axis off;
pairs_to_view = C.Dense_map;
hold on
for i = 1:size(pairs_to_view,1)
start = [pred(pairs_to_view(i,1)+1,2),pred(pairs_to_view(i,1)+1,1)];
finish = [pred(pairs_to_view(i,2)+1,2),pred(pairs_to_view(i,2)+1,1)];
plot([start(1), finish(1)],[start(2),finish(2)],'LineWidth',2, 'Color', 'r');
end
% title(sprintf('w1 = %f, w2 = %f',w1,w2))
labels = strrep(C.Pts_list(1:18), '_', '.');
text(pred(:,2), pred(:,1), labels, 'Color', 'y', 'VerticalAlignment', 'bottom');
title('prediction');
showHeatMap = false;
if (showHeatMap)
img = new_annorect(i_rect).image;
key_pt_names = {'l_elbow', 'l_shoulder'};
for key_pt_name = key_pt_names
pt_id = eval(['C.' key_pt_name{1}]);
xxx = reshape(exp(-UE2{pt_id+1}),sz_coarse);
pp=imresize(xxx./max(xxx(:)),12);
d = (size(pp,1)-size(img,1))/2;
pp = pp(d+1:end-d,d+1:end-d,:);
rgb_mask = uint8(round(255*ind2rgb(round(255*pp./max(pp(:))), jet(256))));
figure; imagesc(0.5*img+0.5*rgb_mask); axis equal; axis off;
nn = key_pt_name{1}; nn(nn=='_')='.';
title(nn);
end
end
end
end