-
Notifications
You must be signed in to change notification settings - Fork 1
/
TestTrial.m
271 lines (222 loc) · 12.4 KB
/
TestTrial.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
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% This script takes the current model loaded in the workspace and
% simulates a test trial with it:
% 1.000 testing episodes
% traking of vergence Error, reconstruction Error, muscle forces
% and metabolic costs
% No learning occures during this trial and the results are saved in a
% specified folder (under ./results/) in the file modelTestData.mat.
%%% Script is DEPRICATED!
function TestTrial(model, randomizationSeed, fileDescription)
% TODO: range of disparity ueberpruefen auf relCmd!!
numberTrials = 100;
modelTest = ModelTestData(numberTrials * model.interval, model.interval);
folder = strcat(model.savePath, './testResults/');
testModelDescription = sprintf('TestedModel_%s_%s', datestr(now), fileDescription);
mkdir(folder, testModelDescription);
modelTest.savePath = strcat(folder, testModelDescription);
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%%%%%%%%%%%%%%%%%%%%% predefining variables %%%%%%%%%%%%%%%%%%%%%%%%%%%
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
f = 257.34; %focal length [px]
baseline = 0.056; %interocular distance (baseline)
objDistMin = 0.5; %minimal object distance
objDistMax = 2; %maximal object distance
muscleInitMin = 0.00807; %minimal initial muscle innervation
muscleInitMax = 0.07186; %maximal --"--
degrees = load('Degrees.mat'); %loads tabular for resulting degrees as 'results_deg'
metCosts = load('MetabolicCosts.mat'); %loads tabular for metabolic costs as 'results'
% command = [0, 0]; %initialization of muscle commands
% Image process variables
patchSize = 8;
dsRatioL = model.scModel_Large.Dsratio; %downsampling ratio (Large scale) | original 8
dsRatioS = model.scModel_Small.Dsratio; %downsampling ratio (Small scale) | original 2
% fovea = [128 128];
foveaL = patchSize + patchSize^2 / 2^log2(dsRatioL); %fovea size (Large scale) | 16
foveaS = patchSize + patchSize^2 / 2^log2(dsRatioS); %fovea size (Small scale) | 40
stOvL = patchSize / dsRatioL; %steps of overlap in the ds image | 1
stOvS = patchSize / dsRatioS; %steps of overlap in the ds image | 4
ncL = foveaL - patchSize + 1; %number of patches per column (slide of 1 px) | 9
ncS = foveaS - patchSize + 1; %number of patches per column (slide of 1 px) | 33
% Prepare index matricies for image patches
columnIndL = [];
for kc = 1:stOvL:ncL
tmpInd = (kc - 1) * ncL + 1 : stOvL : kc * ncL;
columnIndL = [columnIndL tmpInd];
end
columnIndS = [];
for kc = 1:stOvS:ncS
tmpInd = (kc - 1) * ncS + 1 : stOvS : kc * ncS;
columnIndS = [columnIndS tmpInd];
end
% preparing Textures
% texturePath = sprintf('config/%s', 'Textures_New.mat');
texture = load('config/Textures_New.mat');
texture = texture.texture;
nTextures = length(texture);
%%% Helper function that maps muscle activities to resulting angle
function [angle] = getAngle(command)
cmd = (command * 10) + 1; % scale commands to table entries
angle = interp2(degrees.results_deg, cmd(1), cmd(2)); % interpolate in tabular
end
%%% Helper function that maps muscle activities to resulting metabolic costs
function [tmpMetCost] = getMetCost(command)
cmd = (command * 10) + 1; % scale commands to table entries
tmpMetCost = interp2(metCosts.results, cmd(1), cmd(2)); % interpolate in tabular
end
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%%%%%%%%%%%%%%%%%%%%%%%% starting the main loop %%%%%%%%%%%%%%%%%%%%%%%
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
t = 1;
rng(randomizationSeed);
tic
for iter1 = 1 : numberTrials
% pick random texture every #interval times
currentTexture = texture{(randi(nTextures, 1))};
% random depth
objDist = objDistMin + (objDistMax - objDistMin) * rand(1, 1);
% reset muscle activities to random values
command = [0, 0];
command(2) = muscleInitMin + (muscleInitMax - muscleInitMin) * rand(1, 1); %only for one muscle
angleNew = getAngle(command) * 2;
%generate two new pictures
[status, res] = system(sprintf('./checkEnvironment %s %d %d left.png right.png', ...
currentTexture, objDist, angleNew));
% Abort execution if error occured
if (status)
sprintf('Error in checkEnvironment:\n%s', res)
return;
end
for iter2 = 1 : model.interval
% Read input images and convert to gray scale
imgRawLeft = imread('left.png');
imgRawRight = imread('right.png');
imgGrayLeft = .2989 * imgRawLeft(:,:,1) + .5870 * imgRawLeft(:,:,2) + .1140 * imgRawLeft(:,:,3);
imgGrayRight = .2989 * imgRawRight(:,:,1) + .5870 * imgRawRight(:,:,2) + .1140 * imgRawRight(:,:,3);
anaglyph = stereoAnaglyph(imgGrayLeft, imgGrayRight);
imwrite(anaglyph, 'anaglyph.png');
% Image patch generation: left{small scale, large scale}, right{small scale, large scale}
[patchesLeftSmall] = preprocessImage(imgGrayLeft, foveaS, dsRatioS, patchSize, columnIndS);
[patchesLeftLarge] = preprocessImage(imgGrayLeft, foveaL, dsRatioL, patchSize, columnIndL);
[patchesRightSmall] = preprocessImage(imgGrayRight, foveaS, dsRatioS, patchSize, columnIndS);
[patchesRightLarge] = preprocessImage(imgGrayRight, foveaL, dsRatioL, patchSize, columnIndL);
% Image patches matrix (input to model)
currentView = {[patchesLeftLarge; patchesRightLarge] [patchesLeftSmall; patchesRightSmall]};
% Generate input feature vector from current images
[feature, reward, errorTotal, errorLarge, errorSmall] = model.generateFR(currentView);
% feature = [feature; 0; 0]; %just for testing purposes
%%% Feedback
% Absolute command feedback # concatination
feature = [feature; command(2) * model.lambdaMuscleFB];
% feature = [feature; command' * 0.01]; % just to make it how I trained it before ('ChongsParams')
% Relative command feedback # concatination
% if (iter2 > 1)
% feature = [feature; model.relCmd_hist(t-1) * model.lambdaMuscleFB];
% else
% feature = [feature; 0];
% end
%% Absolute command feedback # additive
% feature = feature + command(2) * model.lambdaMuscleFB;
%% Absolute command feedback # multiplicative
% feature = feature * (command(2) * model.lambdaMuscleFB);
%% Relative command feedback # additive
% if (iter2 > 1)
% feature = feature + model.relCmd_hist(t - 1) * model.lambdaMuscleFB;
% end
%% Relative command feedback # multiplicative
% if (iter2 > 1)
% feature = feature * model.relCmd_hist(t - 1) * model.lambdaMuscleFB;
% end
%%% Calculate metabolic costs
metCost = getMetCost(command) * 2;
%%% Calculate reward function
%%% Weight L1 regularization
rewardFunction = model.lambdaRec * reward ...
- model.lambdaMet * metCost ...
- model.lambdaV * (sum(sum(abs(model.rlModel.CCritic.v_ji)))) ...
- model.lambdaP1 * (sum(sum(abs(model.rlModel.CActor.wp_ji)))) ...
- model.lambdaP2 * (sum(sum(abs(model.rlModel.CActor.wp_kj))));
%%% Weight L2 regularization
% rewardFunction = model.lambdaRec * reward ...
% - model.lambdaMet * metCost ...
% - model.lambdaV * (sum(sum(model.rlModel.CCritic.v_ji .^ 2))) ...
% - model.lambdaP1 * (sum(sum(model.rlModel.CActor.wp_ji .^ 2))) ...
% - model.lambdaP2 * (sum(sum(model.rlModel.CActor.wp_kj .^ 2)));
% generation of motor command without learning and noise
% [relativeCommand, ~, ~] = model.rlModel.stepTrain(feature, rewardFunction, 0);
relativeCommand = model.rlModel.softmaxAct(feature);
% command = command + relativeCommand'; %two muscels
command(2) = command(2) + relativeCommand; %one muscel
command = checkCmd(command); %restrain motor commands to [0,1]
angleNew = getAngle(command) * 2; %resulting angle is used for both eyes
% generate new view (two pictures) with new vergence angle
[status, res] = system(sprintf('./checkEnvironment %s %d %d left.png right.png', ...
currentTexture, objDist, angleNew));
% Abort execution if error occured
if (status)
sprintf('Error in checkEnvironment:\n%s', res)
return;
end
%%%%%%%%%%%%%%%% TRACK ALL PARAMETERS %%%%%%%%%%%%%%%%%%
%Compute desired vergence command, disparity and vergence error
fixDepth = (baseline / 2) / tand(angleNew / 2);
angleDes = 2 * atand(baseline / (2 * objDist)); %desired vergence [deg]
anglerr = angleDes - angleNew; %vergence error [deg]
disparity = 2 * f * tand(anglerr / 2); %current disp [px]
%save them
modelTest.Z(t) = objDist;
modelTest.fixZ(t) = fixDepth;
modelTest.disp_hist(t) = disparity;
modelTest.vergerr_hist(t) = anglerr;
modelTest.recerr_hist(t, :) = [errorLarge; errorSmall];
modelTest.verge_actual(t) = angleNew;
modelTest.verge_desired(t) = angleDes;
modelTest.relCmd_hist(t, 2) = relativeCommand; %one muscle
% modelTest.relCmd_hist(t, :) = relativeCommand; %two muscles
modelTest.cmd_hist(t, :) = command;
% modelTest.reward_hist(t) = rewardFunction;
modelTest.metCost_hist(t) = metCost;
t = t + 1;
end
sprintf('Testing Iteration = %d\nCommand = [%.3g,\t%.3g]\tCurrent Vergence = %.3g\nRec Error = %.3g\tVergence Error =\n[%.3g, %.3g, %.3g, %.3g, %.3g, %.3g, %.3g, %.3g, %.3g, %.3g]', ...
t, command(1), command(2), angleNew, errorTotal, modelTest.vergerr_hist(t - modelTest.interval : t - 1))
end
elapsedTime = toc;
sprintf('Time = %.2f [h] = %.2f [min] = %f [sec]\nFrequency = %.4f [iterations/sec]', ...
elapsedTime / 3600, elapsedTime / 60, elapsedTime, t - 1 / elapsedTime)
% Save and plot results data
save(strcat(modelTest.savePath, '/modelTestData'), 'modelTest');
modelTest.testPlotSave();
end
%%% Saturation function that keeps motor commands in [0, 1]
% corresponding to the muscelActivity/metabolicCost tables
function [cmd] = checkCmd(cmd)
i0 = cmd < 0;
cmd(i0) = 0;
i1 = cmd > 1;
cmd(i1) = 1;
end
%%% Helper functions for image preprocessing
%% Patch generation
function [patches] = preprocessImage(img, fovea, downSampling, patchSize, columnIndicies)
% img = .2989 * img(:,:,1) + .5870 * img(:,:,2) + .1140 * img(:,:,3);
for i = 1:log2(downSampling)
img = impyramid(img, 'reduce');
end
% convert to double
img = double(img);
% cut fovea in the center
[h, w, ~] = size(img);
img = img(fix(h / 2 + 1 - fovea / 2) : fix(h / 2 + fovea / 2), ...
fix(w / 2 + 1 - fovea / 2) : fix(w / 2 + fovea / 2));
% cut patches and store them as col vectors
patches = im2col(img, [patchSize patchSize], 'sliding'); %slide window of 1 px
% take patches at steps of s (8 px)
patches = patches(:, columnIndicies); %81 patches
% pre-processing steps (0 mean, unit norm)
patches = patches - repmat(mean(patches), [size(patches, 1) 1]); %0 mean
normp = sqrt(sum(patches.^2)); %patches norm
% normalize patches to norm 1
normp(normp == 0) = eps; %regularizer
patches = patches ./ repmat(normp, [size(patches, 1) 1]); %normalized patches
end