-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathlearning_segments.m
377 lines (320 loc) · 11 KB
/
learning_segments.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
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
%% gmm settings
use_pca = false;
use_loglik = true;
use_kmeans = true;
use_coordinates = true;
use_dtw = false;
DEMO_LEN = 50;
MIN_CLUSTERS = 1;
MAX_CLUSTERS = 3;
USE_EFFORT = true;
USE_XY = false;
USE_DIFF = true;
USE_PARAM = false;
USE_SURFACE_PROXIMITY = false;
USE_IN_TISSUE = true;
USE_IN_GATE = false;
USE_TIME = true;
SHOW_SEGMENTS_EXAMPLE = true;
SHOW_SEGMENTS_GATES = true;
SHOW_GATE_POINTS = true;
SHOW_DATA_LOGLIKELIHOOD = false;
SHOW_RESULTS = false;
SKIP_IMG = true;
NORMALIZE = true;
USE_AVG_LEN = true;
ALWAYS_USE_PREV_GATE = true;
%% set up bmm experiments
SKIP_FORWARD_BACKWARD = true;
USE_EXIT_FROM_GATE_MODEL = true;
SHOW_BMM = false;
%% LEVEL SETUP
%DELETE_MOVEMENT_ROTATION = true;
NUM_LEVELS = 12;
NFIG = 1;
NDIM = 7;
NEFFORT_FEATURES = 2;
NGATE_FEATURES = 4; %6; %7;
NEXIT_FEATURES = 2; %3;
MARGIN = 0;
HOLD_OUT = false;
%% initialize random number generator
rng('default')
learning_test_bmm;
%% create individual segments
ap = create_segments(bmm, trials, envs, predicates, MARGIN, NUM_LEVELS);
training_data = cell(size(ap));
if SHOW_SEGMENTS_EXAMPLE
draw_registered_action_primitives(bmm,ap);
end
if use_pca
fprintf('PCA not yet implemented!\n');
end
% create list of models
models = cell(bmm.k,1);
models = analyze_primitives(ap,models);
%% loop over the different actions
% create a model for each one
for k=1:bmm.k
% iterate over all models
%% which environmental features are important for this action?
use_gate = true;
use_prev_gate = true;
use_exit = true;
if USE_TIME
vars = {'time'};
in = 1;
in_na = 1;
next_in = 2;
else
vars = {'time'};
in = [];
in_na = [];
next_in = 2;
end
if USE_IN_GATE
in = [in next_in];
vars{next_in} = 'in_gate';
next_in = next_in+1;
end
if USE_IN_TISSUE
in = [in next_in];
vars{next_in} = 'in_tissue';
next_in = next_in+1;
end
if USE_XY
in = [in next_in:next_in+2];
vars{next_in} = 'xy_x';
vars{next_in+1} = 'xy_y';
vars{next_in+2} = 'xy_w';
in_na = [in_na next_in:next_in+1];
next_in = next_in+3;
end
if USE_EFFORT
in = [in next_in:next_in+1];
%in = [in next_in];
vars{next_in} = 'effort_rotation';
vars{next_in+1} = 'effort_rotation';
%next_in = next_in+1;
next_in = next_in+2;
end
if USE_SURFACE_PROXIMITY
in = [in next_in];
vars{next_in} = 'surface_proximity';
in_na = [in_na next_in];
next_in = next_in + 1;
end
for i = 1:length(ap{k})
use_gate = use_gate && ap{k}(i).has_gate;
use_prev_gate = use_prev_gate && ap{k}(i).has_prev_gate;
use_exit = use_exit && ap{k}(i).use_exit;
end
use_prev_gate = use_prev_gate || ALWAYS_USE_PREV_GATE;
if bmm.coef(1,k)
models{k}.in_gate = true;
use_gate = false;
else
models{k}.in_gate = false;
end
if use_gate
fprintf('1) Action primitive %d always relates to a gate.\n',k);
in = [in next_in:(next_in+NGATE_FEATURES-1)];
for ii = 1:NGATE_FEATURES
vars{next_in + ii - 1} = 'gate_features';
end
in_na = [in_na next_in:(next_in+NGATE_FEATURES-3)];
next_in = max(in) + 1;
if USE_PARAM
in = [in next_in:next_in+1];
vars{next_in} = 'gate_width';
vars{next_in+1} = 'gate_height';
in_na = [in_na next_in:next_in+1];
next_in = next_in+2;
end
else
fprintf('1) Action primitive %d does not need a gate.\n',k);
end
if use_prev_gate
fprintf('2) Action primitive %d always relates to a previous gate.\n',k);
in = [in next_in:(next_in+NGATE_FEATURES-1)];
for ii = 1:NGATE_FEATURES
vars{next_in + ii - 1} = 'prev_gate_features';
end
in_na = [in_na next_in:(next_in+NGATE_FEATURES-3)];
next_in = max(in) + 1;
if USE_PARAM
in = [in next_in:next_in+1];
vars{next_in} = 'gate_width';
vars{next_in+1} = 'gate_height';
in_na = [in_na next_in:next_in+1];
next_in = next_in+2;
end
else
fprintf('2) Action primitive %d does not need a previous gate.\n',k);
end
if use_exit
fprintf('3) Action primitive %d always relates to the level exit.\n',k);
in = [in next_in:(next_in+NEXIT_FEATURES-1)];
for ii = 1:NEXIT_FEATURES
vars{next_in + ii - 1} = 'exit_features';
end
in_na = [in_na next_in:(next_in+NEXIT_FEATURES-3)];
next_in = max(in) + 1;
else
fprintf('3) Action primitive %d does not need the level exit.\n',k);
end
% set up for tp-gmm model
%models{k}.nbVar = NDIM;
%models{k}.nbFrames = 1 + use_gate + use_prev_gate;
models{k}.nbStates = MIN_CLUSTERS;
models{k}.color = gmmColors(:,k)';
models{k}.marker = markers(k);
models{k}.use_gate = use_gate;
models{k}.use_param = USE_PARAM;
models{k}.use_prev_gate = use_prev_gate;
models{k}.use_exit = use_exit;
models{k}.use_effort = USE_EFFORT;
models{k}.use_xy = USE_XY;
models{k}.use_diff = USE_DIFF;
models{k}.use_in_gate = USE_IN_GATE;
models{k}.use_in_tissue = USE_IN_TISSUE;
models{k}.use_surface_proximity = USE_SURFACE_PROXIMITY;
models{k}.use_time = USE_TIME;
models{k}.normalize = NORMALIZE;
models{k}.use_avg_len = USE_AVG_LEN;
models{k}.in = in;
models{k}.in_na = in_na;
models{k}.var_names = vars;
% set of indices for features that don't use effort/angles starting at
% the first "feature", i.e. not configuration. So we need to start
% counting at 3 when including effort features.
if USE_EFFORT
%models{k}.in_naf = in_na(4:end) - min(in_na(4:end)) + 3;
models{k}.in_naf = in_na(2:end) - 1;
else
%models{k}.in_naf = in_na(4:end) - min(in_na(4:end)) + 1;
models{k}.in_naf = in_na(2:end) - 1;
end
models{k}.nbVar = max(in);
models{k}.nbOut = 2;
if use_dtw
ap{k} = do_dtw(ap{k},models{k});
end
if USE_DIFF
if USE_TIME
models{k}.in = [models{k}.in (models{k}.in(2:end)+next_in-2)];
else
models{k}.in = [models{k}.in (models{k}.in(1:end)+next_in-2)];
end
end
lens = zeros(length(ap{k}),1);
for i = 1:length(ap{k})
lens(i) = ap{k}(i).end - ap{k}(i).start; %size(ap{k}(i).data,2);
end
models{k}.steps = round(mean(lens));
%% start by exploring what the gates look like
if use_gate && SHOW_SEGMENTS_GATES && SHOW_GATE_POINTS && k==1
figure(NFIG);clf;
for i = 1:min(16,length(ap{k}))
subplot(4,4,i); hold on;
draw_gates({ap{k}(i).gate});
plot(ap{k}(i).gate.corners(1,1),ap{k}(i).gate.corners(2,1),'+');
plot(ap{k}(i).gate.corners(1,2),ap{k}(i).gate.corners(2,2),'*');
plot(ap{k}(i).gate.corners(1,3),ap{k}(i).gate.corners(2,3),'o');
draw_segments(ap{k}(i),NFIG);
end
NFIG = NFIG + 1;
end
%% FEATURES: GATE CORNERS
% ---------------------------------------------------------------------
%if k ~= 2
% continue
%end
%% learning
[trainingData,controls,norm_mean,norm_std] = create_primitive_training_data(models{k},ap{k});
if HOLD_OUT
testData = create_primitive_training_data(models{k},apt{k});
end
% set features for model fitting
training_data{k}.features = trainingData;
training_data{k}.controls = controls;
models{k}.norm_mean = norm_mean;
models{k}.norm_std = norm_std;
if models{k}.normalize
trainingData = trainingData - repmat(norm_mean,1,size(trainingData,2));
trainingData = trainingData ./ repmat(norm_std,1,size(trainingData,2));
end
max_clusters = min(size(trainingData,2), MAX_CLUSTERS);
best_ab = Inf;
best_num = 0;
if any(isnan(trainingData(1,:)))
isnan(trainingData(1,:))
fprintf('ERROR: invalid training data!\n');
pause;
break;
end
for j=MIN_CLUSTERS:max_clusters
fprintf('Task element %d, GMM iteration %d...\n',k,j);
if ~use_pca
if j == 1
Priors = 1;
Mu = mean(trainingData,2);
Sigma = cov(trainingData')';
else
if use_kmeans
[Priors, Mu, Sigma] = EM_init_kmeans(trainingData, j);
else
[Priors, Mu, Sigma] = EM_init_time(trainingData, j);
end
[Priors, Mu, Sigma] = EM(trainingData, Priors, Mu, Sigma);
end
tmp_model = struct('priors',Priors,'mu',Mu,'sigma',Sigma);
ll = gmmLogLikelihood(trainingData,tmp_model,j);
else
[Priors, Mu, Sigma] = EM_init_kmeans(trainingData, j);
[Priors, Mu, Sigma] = EM(trainingData, Priors, Mu, Sigma);
tmp_model = struct('priors',Priors,'mu',Mu,'sigma',Sigma);
ll = gmmLogLikelihood(trainingData,tmp_model,j);
end
fprintf(' - log likelihood = %f\n',ll);
D = size(tmp_model.mu,1);
free_params = (length(tmp_model.priors) - 1) + ...
(j*D) + ...
(j * (0.5*D*(D+1)));
fprintf(' - num free params = %d\n',free_params);
fprintf(' - num training examples = %d\n', size(trainingData,2));
if -1*ll < best_ab
best_ab = -1*ll;
best_model = tmp_model;
best_num = j;
end
end
fprintf('Best model used %d clusters\n',best_num);
models{k}.nbStates = best_num;
models{k}.Mu = best_model.mu;
models{k}.Sigma = best_model.sigma;
models{k}.Priors = best_model.priors;
models{k}.out = 5:6;
for j=1:models{k}.nbStates
models{k}.inInvSigmaNT(:,:,j) = inv(models{k}.Sigma(models{k}.in(2:end),models{k}.in(2:end),j));
end
for j=1:best_num
%models{k}.invSigma(:,:,j) = inv(best_model.sigma(:,:,j));
models{k}.inInvSigma(:,:,j) = inv(best_model.sigma(models{k}.in,models{k}.in,j));
end
if SHOW_RESULTS
[img, expath] = show_pca_results(ap,models,k,SKIP_IMG);
end
if HOLD_OUT && SHOW_DATA_LOGLIKELIHOOD
test_ll = compute_loglik(testData(models{k}.in,:),models{k}.Mu,models{k}.Sigma,models{k},models{k}.in);
train_ll = compute_loglik(trainingData(models{k}.in,:),models{k}.Mu,models{k}.Sigma,models{k},models{k}.in);
figure();hold on;
scatter(testData(1,:),test_ll);
scatter(trainingData(1,:),train_ll);
end
end
models{1}.num_primitives = 4;
models{2}.num_primitives = 1;
models{3}.num_primitives = 4;
models{4}.num_primitives = 4;
models{5}.num_primitives = 1;