-
Notifications
You must be signed in to change notification settings - Fork 12
/
AAMPyramidModel.pas
403 lines (338 loc) · 12.4 KB
/
AAMPyramidModel.pas
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
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
// ###################################################################
// #### This file is part of the mrimageutils project, depends on
// #### the mathematics library project and is
// #### offered under the licence agreement described on
// #### http://www.mrsoft.org/
// ####
// #### Copyright:(c) 2014, Michael R. . All rights reserved.
// ####
// #### Unless required by applicable law or agreed to in writing, software
// #### distributed under the License is distributed on an "AS IS" BASIS,
// #### WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// #### See the License for the specific language governing permissions and
// #### limitations under the License.
// ###################################################################
unit AAMPyramidModel;
// #########################################################
// #### Pyramid model alignment
// #########################################################
interface
uses SysUtils, BaseAAMModel, Matrix, BaseMathPersistence;
// #########################################################
// #### Pyramid level implementation
type
TCustomAMMModelArr = Array of TCustomAAMModel;
TCustomPyramidAAMModel = class(TCustomAAMModel)
private
fActIdx : integer;
fNumLevels : integer;
fModels : TCustomAMMModelArr;
function GetModel(index: integer): TCustomAAMModel;
protected
procedure DefineProps; override;
function PropTypeOfName(const Name: string): TPropType; override;
procedure OnLoadBeginList(const Name : String; count : integer); override;
function OnLoadObject(Obj : TBaseMathPersistence) : boolean; override;
procedure OnLoadEndList; override;
public
property NumLevels : integer read fNumLevels;
property Models[index : integer] : TCustomAAMModel read GetModel;
constructor Create(Models : TCustomAMMModelArr);
destructor Destroy; override;
end;
TCustomPyramidAAMModelClass = class of TCustomPyramidAAMModel;
type
TPyramidAAMModel = class(TCustomPyramidAAMModel)
private
fNumIter : integer;
function MapParams(actStep: integer; nextStep : integer; params: TDoubleMatrix): TDoubleMatrix;
procedure Iteration(Sender : TObject; step : integer; P : TDoubleMatrix; StepImg : TDoubleMatrix);
protected
function GetLearnCombinedEnergy: double; override;
function GetLearnImgScale: double; override;
function GetLearnScaleDisturbPerc: double; override;
function GetLearnShapeEnergy: double; override;
function GetLearnStdDev: double; override;
function GetLearnTextureDisturbPerc: double; override;
function GetLearnTextureEnergy: double; override;
function GetBrightness: double; override;
function GetContrast: double; override;
function GetEigVal(index: integer): double; override;
function GetMeanShape: TDoubleMatrix; override;
function GetMeanTexture: TDoubleMatrix; override;
function GetNumEigVals: integer; override;
class function ClassIdentifier : String; override;
public
// params are taken from the largest model!
property TextureContrast : double read GetContrast;
property TextureBrightness : double read GetBrightness;
property MeanTexture : TDoubleMatrix read GetMeanTexture;
property MeanShape : TDoubleMatrix read GetMeanShape;
property EigVals[index : integer] : double read GetEigVal;
property NumEigVals : integer read GetNumEigVals;
// optimization assumes that the initial params are taken from the largest model
function Optimize(Img : TDoubleMatrix; InitParams : TDoubleMatrix) : TDoubleMatrix; override;
// learning properties
property LearnInitScale : double read GetLearnImgScale;
property LearnScaleDisturb : double read GetLearnScaleDisturbPerc;
property LearnTextureDisturb : double read GetLearnTextureDisturbPerc;
property LearnParamStdDev : double read GetLearnStdDev;
property LearnTextureEnergy : double read GetLearnTextureEnergy;
property LearnShapeEnergy : double read GetLearnShapeEnergy;
property LearnCombinedEnergy : double read GetLearnCombinedEnergy;
function TextureFromP(const p : TDoubleMatrix) : TDoubleMatrix; override;
procedure TextureFromPInImg(img : TDoubleMatrix; const p : TDoubleMatrix); override;
function RawTextureFromP(const p : TDoubleMatrix; AddMean : boolean) : TDoubleMatrix; override;
function ShapeFromP(const P: TDoubleMatrix): TDoubleMatrix; override;
end;
implementation
uses ImageResize, AAMConst, ImageMatrixConv;
{ TPyramidAAMModel }
const cPyramidAAMModels = 'PAAModels';
constructor TCustomPyramidAAMModel.Create(Models: TCustomAMMModelArr);
begin
fModels := Models;
fNumLevels := Length(Models);
fNumColorPlanes := Models[0].NumColorPlanes;
TextureWidth := Models[0].TextureWidth;
TextureHeight := Models[0].TextureHeight;
inherited Create;
end;
procedure TCustomPyramidAAMModel.DefineProps;
var i: Integer;
begin
inherited;
BeginList(cPyramidAAMModels, fNumLevels);
for i := 0 to fNumLevels - 1 do
AddObject(fModels[i]);
EndList;
end;
function TCustomPyramidAAMModel.PropTypeOfName(const Name: string): TPropType;
begin
if CompareText(Name, cPyramidAAMModels) = 0
then
Result := ptObject
else
Result := inherited PropTypeOfName(Name);
end;
destructor TCustomPyramidAAMModel.Destroy;
var i : Integer;
begin
for i := 0 to fNumLevels - 1 do
fModels[i].Free;
inherited;
end;
function TCustomPyramidAAMModel.GetModel(index: integer): TCustomAAMModel;
begin
assert((index >= 0) and (index < Length(fModels)), 'Index out of bounds');
Result := fModels[index];
end;
procedure TCustomPyramidAAMModel.OnLoadBeginList(const Name: String;
count: integer);
begin
fActIdx := -1;
if CompareText(Name, cPyramidAAMModels) = 0 then
begin
SetLength(fModels, count);
fNumLevels := count;
fActIdx := 0;
end
else
inherited;
end;
procedure TCustomPyramidAAMModel.OnLoadEndList;
begin
fActIdx := -1;
inherited;
end;
function TCustomPyramidAAMModel.OnLoadObject(
Obj: TBaseMathPersistence): boolean;
begin
Result := True;
if fActIdx >= 0 then
begin
fModels[fActIdx] := obj as TCustomAAMModel;
inc(fActIdx);
end
else
Result := inherited OnLoadObject(Obj);
end;
class function TPyramidAAMModel.ClassIdentifier: String;
begin
Result := 'PyramidAAM';
end;
function TPyramidAAMModel.GetBrightness: double;
begin
Result := Models[0].TextureBrightness;
end;
function TPyramidAAMModel.GetContrast: double;
begin
Result := Models[0].TextureContrast;
end;
function TPyramidAAMModel.GetEigVal(index: integer): double;
begin
Result := Models[0].EigVals[index];
end;
function TPyramidAAMModel.GetLearnCombinedEnergy: double;
begin
Result := Models[0].LearnCombinedEnergy;
end;
function TPyramidAAMModel.GetLearnImgScale: double;
begin
Result := Models[0].LearnInitScale;
end;
function TPyramidAAMModel.GetLearnScaleDisturbPerc: double;
begin
Result := Models[0].LearnScaleDisturb;
end;
function TPyramidAAMModel.GetLearnShapeEnergy: double;
begin
Result := Models[0].LearnShapeEnergy;
end;
function TPyramidAAMModel.GetLearnStdDev: double;
begin
Result := Models[0].LearnParamStdDev;
end;
function TPyramidAAMModel.GetLearnTextureDisturbPerc: double;
begin
Result := Models[0].LearnTextureDisturb;
end;
function TPyramidAAMModel.GetLearnTextureEnergy: double;
begin
Result := Models[0].LearnTextureEnergy;
end;
function TPyramidAAMModel.GetMeanShape: TDoubleMatrix;
begin
Result := Models[0].MeanShape;
end;
function TPyramidAAMModel.GetMeanTexture: TDoubleMatrix;
begin
Result := Models[0].MeanTexture;
end;
function TPyramidAAMModel.GetNumEigVals: integer;
begin
Result := Models[0].NumEigVals;
end;
procedure TPyramidAAMModel.Iteration(Sender: TObject; step: integer; P,
StepImg: TDoubleMatrix);
begin
if Assigned(OnIterationStep) then
OnIterationStep(Self, fNumIter, p, StepImg);
inc(fNumIter);
end;
function TPyramidAAMModel.MapParams(actStep : integer; nextStep : integer; params : TDoubleMatrix) : TDoubleMatrix;
var img : TDoubleMatrix;
resizedImg : TDoubleMatrix;
p : TDoubleMatrix;
y : Integer;
begin
assert(actStep > 0, 'Index error');
// ###################################################################
// #### Get texture-> map to next level -> map back.
// note: since there is no relationship between the param in different levels
// we need this procedure.
img := Models[actStep].RawTextureFromP(params, False);
try
resizedImg := ImgResize(TBilinearImageResize, img, Models[nextStep].NumColorPlanes, Models[nextStep].TextureWidth, Models[nextStep].TextureHeight);
try
resizedImg.ReshapeInPlace(1, resizedImg.Width*resizedImg.Height);
p := Models[nextStep].PFromRawTexture(resizedImg, False);
finally
resizedImg.Free;
end;
try
// and copy the pose params:
Result := TDoubleMatrix.Create(1, p.Height + cNumPoseParams);
Result.SetSubMatrix(0, 0, 1, p.Height);
Result.SetColumn(0, p);
params.UseFullMatrix;
Result.UseFullMatrix;
for y := 1 to cNumPoseParams do
Result[0, Result.Height - y] := params[0, params.Height - y];
finally
p.Free;
end;
finally
img.Free;
end;
end;
function TPyramidAAMModel.Optimize(Img,
InitParams: TDoubleMatrix): TDoubleMatrix;
var modelCnt: Integer;
actParams : TDoubleMatrix;
help : TDoubleMatrix;
isZero : boolean;
counter : integer;
y : integer;
begin
fNumIter := 0;
fNumIterations := 0;
for modelCnt := 0 to fNumLevels - 1 do
begin
if Assigned(OnIterationStep)
then
Models[modelCnt].OnIterationStep := Iteration
else
Models[modelCnt].OnIterationStep := nil;
end;
// ########################################################
// #### base aam pyramid optimization
// map the params from the highest to the lowest level first!
// check if the params are not all set to zero -> we can then short cut
// the mapping!
isZero := True;
for counter := 0 to InitParams.Height - cNumPoseParams - 1 do
isZero := isZero and (InitParams[0, counter] = 0);
if not isZero
then
actParams := MapParams(0, fNumLevels - 1, InitParams)
else
begin
actParams := TDoubleMatrix.Create(1, Models[fNumLevels - 1].NumEigVals + cNumPoseParams);
for y := 0 to cNumPoseParams - 1 do
actParams[0, actParams.Height - 1 - y] := InitParams[0, InitParams.Height - 1 - y];
end;
// ########################################################
// #### Go through all models an optimize them
for modelCnt := fNumLevels - 1 downto 0 do
begin
help := Models[modelCnt].Optimize(Img, actParams);
try
inc(fNumIterations, Models[modelCnt].NumOptimizationIters);
actParams.Free;
if modelCnt > 0
then
actParams := MapParams(modelCnt, modelCnt - 1, help)
else
begin
actParams := help;
help := nil;
end;
finally
help.Free;
end;
end;
Result := actParams;
end;
function TPyramidAAMModel.RawTextureFromP(
const p: TDoubleMatrix; AddMean : boolean): TDoubleMatrix;
begin
assert(fNumLevels > 0, 'Error no models assigned');
Result := Models[0].RawTextureFromP(p, AddMean);
end;
function TPyramidAAMModel.TextureFromP(const p: TDoubleMatrix): TDoubleMatrix;
begin
Result := Models[0].TextureFromP(p);
end;
function TPyramidAAMModel.ShapeFromP(const P: TDoubleMatrix): TDoubleMatrix;
begin
Result := Models[0].ShapeFromP(p);
end;
procedure TPyramidAAMModel.TextureFromPInImg(img: TDoubleMatrix;
const p: TDoubleMatrix);
begin
Models[0].TextureFromPInImg(img, p);
end;
initialization
RegisterMathIO(TPyramidAAMModel);
end.