Skip to content
GitLab
Menu
Projects
Groups
Snippets
Loading...
Help
Help
Support
Community forum
Keyboard shortcuts
?
Submit feedback
Contribute to GitLab
Sign in
Toggle navigation
Menu
Open sidebar
rli
pbdlibmatlab
Commits
2cb8784d
Commit
2cb8784d
authored
Sep 16, 2015
by
Sylvain Calinon
Browse files
Homogenization of headers in m_fcts folder
parent
b738860a
Changes
38
Hide whitespace changes
Inline
Sidebyside
demo_stdPGMM01.m
View file @
2cb8784d
function
demo_stdPGMM01
% Parametric Gaussian mixture model (PGMM) used for task adaptation,
% with DSGMR employed to retrieve continuous movements.
% Parametric Gaussian mixture model (PGMM) used for task adaptation, with DSGMR employed
% to retrieve continuous movements. The approach is inspired by Wilson and Bobick (1999),
% with an implementation applied to the special case of Gaussian mixture models (GMM).
%
% Writing code takes time. Polishing it and making it available to others takes longer!
% If some parts of the code were useful for your research of for a better understanding
...
...
@@ 13,6 +14,16 @@ function demo_stdPGMM01
% journal="Intelligent Service Robotics",
% year="2015"
% }
%
% @article{Wilson99,
% author="Wilson, A. D. and Bobick, A. F.",
% title="Parametric Hidden {M}arkov Models for Gesture Recognition",
% journal="{IEEE} Trans. on Pattern Analysis and Machine Intelligence",
% year="1999",
% volume="21",
% number="9",
% pages="884900"
% }
%
% Copyright (c) 2015 Idiap Research Institute, http://idiap.ch/
% Written by Sylvain Calinon, http://calinon.ch/
...
...
m_fcts/DTW.m
View file @
2cb8784d
function
[
x_new
,
y_new
,
p
]
=
DTW
(
x
,
y
,
w
)
%Trajectory realignment through dynamic time warping
%Sylvain Calinon, 2015
function
[
x2
,
y2
,
p
]
=
DTW
(
x
,
y
,
w
)
% Trajectory realignment through dynamic time warping.
%
% Writing code takes time. Polishing it and making it available to others takes longer!
% If some parts of the code were useful for your research of for a better understanding
% of the algorithms, please reward the authors by citing the related publications,
% and consider making your own research available in this way.
%
% @article{Calinon15,
% author="Calinon, S.",
% title="A Tutorial on TaskParameterized Movement Learning and Retrieval",
% journal="Intelligent Service Robotics",
% year="2015"
% }
%
% Copyright (c) 2015 Idiap Research Institute, http://idiap.ch/
% Written by Sylvain Calinon, http://calinon.ch/
%
% This file is part of PbDlib, http://www.idiap.ch/software/pbdlib/
%
% PbDlib is free software: you can redistribute it and/or modify
% it under the terms of the GNU General Public License version 3 as
% published by the Free Software Foundation.
%
% PbDlib is distributed in the hope that it will be useful,
% but WITHOUT ANY WARRANTY; without even the implied warranty of
% MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
% GNU General Public License for more details.
%
% You should have received a copy of the GNU General Public License
% along with PbDlib. If not, see <http://www.gnu.org/licenses/>.
if
nargin
<
3
w
=
Inf
;
end
n
x
=
size
(
x
,
2
);
n
y
=
size
(
y
,
2
);
s
x
=
size
(
x
,
2
);
s
y
=
size
(
y
,
2
);
w
=
max
(
w
,
abs
(
n
x

n
y
));
w
=
max
(
w
,
abs
(
s
x

s
y
));
%Initialization
D
=
ones
(
n
x
+
1
,
n
y
+
1
)
*
Inf
;
D
=
ones
(
s
x
+
1
,
s
y
+
1
)
*
Inf
;
D
(
1
,
1
)
=
0
;
%DP loop
for
i
=
1
:
nx
for
j
=
max
(
i

w
,
1
):
min
(
i
+
w
,
n
y
)
for
j
=
max
(
i

w
,
1
):
min
(
i
+
w
,
s
y
)
D
(
i
+
1
,
j
+
1
)
=
norm
(
x
(:,
i
)

y
(:,
j
))
+
min
([
D
(
i
,
j
+
1
),
D
(
i
+
1
,
j
),
D
(
i
,
j
)]);
end
end
i
=
n
x
+
1
;
j
=
n
y
+
1
;
i
=
s
x
+
1
;
j
=
s
y
+
1
;
p
=
[];
while
i
>
1
&&
j
>
1
[
~
,
id
]
=
min
([
D
(
i
,
j

1
),
D
(
i

1
,
j
),
D
(
i

1
,
j

1
)]);
...
...
@@ 39,14 +68,9 @@ end
p
=
fliplr
(
p
(:,
1
:
end

1
)

1
);
x
_new
=
x
(:,
p
(
1
,:));
y
_new
=
y
(:,
p
(
2
,:));
x
2
=
x
(:,
p
(
1
,:));
y
2
=
y
(:,
p
(
2
,:));
%Resampling
x_new
=
spline
(
1
:
size
(
x_new
,
2
),
x_new
,
linspace
(
1
,
size
(
x_new
,
2
),
nx
));
y_new
=
spline
(
1
:
size
(
y_new
,
2
),
y_new
,
linspace
(
1
,
size
(
y_new
,
2
),
nx
));
x2
=
spline
(
1
:
size
(
x2
,
2
),
x2
,
linspace
(
1
,
size
(
x2
,
2
),
sx
));
y2
=
spline
(
1
:
size
(
y2
,
2
),
y2
,
linspace
(
1
,
size
(
y2
,
2
),
sx
));
m_fcts/EM_GMM.m
View file @
2cb8784d
function
[
model
,
GAMMA2
,
LL
]
=
EM_GMM
(
Data
,
model
)
% Training of a Gaussian mixture model (GMM) with an expectationmaximization (EM) algorithm.
%
% Author: Sylvain Calinon, 2014
% http://programmingbydemonstration.org/SylvainCalinon
% Writing code takes time. Polishing it and making it available to others takes longer!
% If some parts of the code were useful for your research of for a better understanding
% of the algorithms, please reward the authors by citing the related publications,
% and consider making your own research available in this way.
%
% @article{Calinon15,
% author="Calinon, S.",
% title="A Tutorial on TaskParameterized Movement Learning and Retrieval",
% journal="Intelligent Service Robotics",
% year="2015"
% }
%
% Copyright (c) 2015 Idiap Research Institute, http://idiap.ch/
% Written by Sylvain Calinon, http://calinon.ch/
%
% This file is part of PbDlib, http://www.idiap.ch/software/pbdlib/
%
% PbDlib is free software: you can redistribute it and/or modify
% it under the terms of the GNU General Public License version 3 as
% published by the Free Software Foundation.
%
% PbDlib is distributed in the hope that it will be useful,
% but WITHOUT ANY WARRANTY; without even the implied warranty of
% MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
% GNU General Public License for more details.
%
% You should have received a copy of the GNU General Public License
% along with PbDlib. If not, see <http://www.gnu.org/licenses/>.
%Parameters of the EM algorithm
nbMinSteps
=
5
;
%Minimum number of iterations allowed
...
...
@@ 10,8 +37,8 @@ nbMaxSteps = 100; %Maximum number of iterations allowed
maxDiffLL
=
1E4
;
%Likelihood increase threshold to stop the algorithm
nbData
=
size
(
Data
,
2
);
%diagRegularizationFactor = 1E6; %Regularization term is optional
, see Eq. (2.1.2) in doc/TechnicalReport.pdf
diagRegularizationFactor
=
1E4
;
%Regularization term is optional
, see Eq. (2.1.2) in doc/TechnicalReport.pdf
%diagRegularizationFactor = 1E6; %Regularization term is optional
diagRegularizationFactor
=
1E4
;
%Regularization term is optional
for
nbIter
=
1
:
nbMaxSteps
fprintf
(
'.'
);
...
...
@@ 22,13 +49,13 @@ for nbIter=1:nbMaxSteps
%Mstep
for
i
=
1
:
model
.
nbStates
%Update Priors
, see Eq. (2.0.6) in doc/TechnicalReport.pdf
%Update Priors
model
.
Priors
(
i
)
=
sum
(
GAMMA
(
i
,:))
/
nbData
;
%Update Mu
, see Eq. (2.0.7) in doc/TechnicalReport.pdf
%Update Mu
model
.
Mu
(:,
i
)
=
Data
*
GAMMA2
(
i
,:)
'
;
%Update Sigma
, see Eq. (2.0.8) in doc/TechnicalReport.pdf (regularization term is optional, see Eq. (2.1.2))
%Update Sigma
DataTmp
=
Data

repmat
(
model
.
Mu
(:,
i
),
1
,
nbData
);
model
.
Sigma
(:,:,
i
)
=
DataTmp
*
diag
(
GAMMA2
(
i
,:))
*
DataTmp
'
+
eye
(
size
(
Data
,
1
))
*
diagRegularizationFactor
;
end
...
...
@@ 46,9 +73,9 @@ end
disp
([
'The maximum number of '
num2str
(
nbMaxSteps
)
' EM iterations has been reached.'
]);
end
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
function
[
L
,
GAMMA
]
=
computeGamma
(
Data
,
model
)
%See Eq. (2.0.5) in doc/TechnicalReport.pdf
L
=
zeros
(
model
.
nbStates
,
size
(
Data
,
2
));
for
i
=
1
:
model
.
nbStates
L
(
i
,:)
=
model
.
Priors
(
i
)
*
gaussPDF
(
Data
,
model
.
Mu
(:,
i
),
model
.
Sigma
(:,:,
i
));
...
...
@@ 56,5 +83,3 @@ end
GAMMA
=
L
.
/
repmat
(
sum
(
L
,
1
)
+
realmin
,
model
.
nbStates
,
1
);
end
m_fcts/EM_HDGMM.m
View file @
2cb8784d
function
[
model
,
GAMMA2
]
=
EM_HDGMM
(
Data
,
model
)
%EM for High Dimensional Data Clustering (HDDC, HDGMM) model proposed by Bouveyron (2007)
%Sylvain Calinon, 2015
% EM for High Dimensional Data Clustering (HDDC, HDGMM) model proposed by Bouveyron (2007).
%
% Writing code takes time. Polishing it and making it available to others takes longer!
% If some parts of the code were useful for your research of for a better understanding
% of the algorithms, please reward the authors by citing the related publications,
% and consider making your own research available in this way.
%
% @article{Calinon15,
% author="Calinon, S.",
% title="A Tutorial on TaskParameterized Movement Learning and Retrieval",
% journal="Intelligent Service Robotics",
% year="2015"
% }
%
% @article{Bouveyron07,
% author = "Bouveyron, C. and Girard, S. and Schmid, C.",
% title = "Highdimensional data clustering",
% journal = "Computational Statistics and Data Analysis",
% year = "2007",
% volume = "52",
% number = "1",
% pages = "502519"
% }
%
% Copyright (c) 2015 Idiap Research Institute, http://idiap.ch/
% Written by Sylvain Calinon, http://calinon.ch/
%
% This file is part of PbDlib, http://www.idiap.ch/software/pbdlib/
%
% PbDlib is free software: you can redistribute it and/or modify
% it under the terms of the GNU General Public License version 3 as
% published by the Free Software Foundation.
%
% PbDlib is distributed in the hope that it will be useful,
% but WITHOUT ANY WARRANTY; without even the implied warranty of
% MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
% GNU General Public License for more details.
%
% You should have received a copy of the GNU General Public License
% along with PbDlib. If not, see <http://www.gnu.org/licenses/>.
%Parameters of the EM iterations
nbMinSteps
=
5
;
%Minimum number of iterations allowed
...
...
@@ 8,7 +47,7 @@ nbMaxSteps = 100; %Maximum number of iterations allowed
maxDiffLL
=
1E4
;
%Likelihood increase threshold to stop the algorithm
nbData
=
size
(
Data
,
2
);
diagRegularizationFactor
=
1E8
;
%Regularization term is optional
, see Eq. (2.1.2) in doc/TechnicalReport.pdf
diagRegularizationFactor
=
1E8
;
%Regularization term is optional
%EM loop
for
nbIter
=
1
:
nbMaxSteps
...
...
@@ 19,19 +58,19 @@ for nbIter=1:nbMaxSteps
GAMMA2
=
GAMMA
.
/
repmat
(
sum
(
GAMMA
,
2
),
1
,
nbData
);
%Mstep
%Update Priors
, see Eq. (2.0.6) in doc/TechnicalReport.pdf
%Update Priors
model
.
Priors
=
sum
(
GAMMA
,
2
)/
nbData
;
%Update Mu
, see Eq. (2.0.7) in doc/TechnicalReport.pdf
%Update Mu
model
.
Mu
=
Data
*
GAMMA2
'
;
%Update factor analyser params
%Update factor analyser param
eter
s
for
i
=
1
:
model
.
nbStates
%Compute covariance
DataTmp
=
Data

repmat
(
model
.
Mu
(:,
i
),
1
,
nbData
);
S
(:,:,
i
)
=
DataTmp
*
diag
(
GAMMA2
(
i
,:))
*
DataTmp
'
+
eye
(
model
.
nbVar
)
*
diagRegularizationFactor
;
%HDGMM update
, see Eq. (2.2.2) in doc/TechnicalReport.pdf
%HDGMM update
[
V
,
D
]
=
eig
(
S
(:,:,
i
));
[
~
,
id
]
=
sort
(
diag
(
D
),
'descend'
);
% model.D(:,:,i) = D(id(1:model.nbFA), id(1:model.nbFA));
...
...
@@ 40,7 +79,7 @@ for nbIter=1:nbMaxSteps
model
.
D
(:,:,
i
)
=
diag
([
d
(
id
(
1
:
model
.
nbFA
));
repmat
(
mean
(
d
(
id
(
model
.
nbFA
+
1
:
end
))),
model
.
nbVar

model
.
nbFA
,
1
)]);
model
.
V
(:,:,
i
)
=
V
(:,
id
);
%Reconstruct Sigma
, see Eq. (2.2.1) in doc/TechnicalReport.pdf
%Reconstruct Sigma
model
.
Sigma
(:,:,
i
)
=
model
.
V
(:,:,
i
)
*
model
.
D
(:,:,
i
)
*
model
.
V
(:,:,
i
)
'
+
eye
(
model
.
nbVar
)
*
diagRegularizationFactor
;
end
...
...
@@ 57,9 +96,9 @@ end
disp
([
'The maximum number of '
num2str
(
nbMaxSteps
)
' EM iterations has been reached.'
]);
end
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
function
[
Lik
,
GAMMA
]
=
computeGamma
(
Data
,
model
)
%See Eq. (2.0.5) in doc/TechnicalReport.pdf
Lik
=
zeros
(
model
.
nbStates
,
size
(
Data
,
2
));
for
i
=
1
:
model
.
nbStates
Lik
(
i
,:)
=
model
.
Priors
(
i
)
*
gaussPDF
(
Data
,
model
.
Mu
(:,
i
),
model
.
Sigma
(:,:,
i
));
...
...
m_fcts/EM_MFA.m
View file @
2cb8784d
function
[
model
,
GAMMA2
]
=
EM_MFA
(
Data
,
model
)
%EM for Mixture of factor analysis
%Implementation inspired by "Parsimonious Gaussian Mixture Models" by McNicholas and Murphy, Appendix 8, p.17 (UUU version)
%Sylvain Calinon, 2015
% EM for Mixture of factor analysis (implementation based on "Parsimonious Gaussian
% Mixture Models" by McNicholas and Murphy, Appendix 8, p.17, UUU version).
%
% Writing code takes time. Polishing it and making it available to others takes longer!
% If some parts of the code were useful for your research of for a better understanding
% of the algorithms, please reward the authors by citing the related publications,
% and consider making your own research available in this way.
%
% @article{Calinon15,
% author="Calinon, S.",
% title="A Tutorial on TaskParameterized Movement Learning and Retrieval",
% journal="Intelligent Service Robotics",
% year="2015"
% }
%
% Copyright (c) 2015 Idiap Research Institute, http://idiap.ch/
% Written by Sylvain Calinon, http://calinon.ch/
%
% This file is part of PbDlib, http://www.idiap.ch/software/pbdlib/
%
% PbDlib is free software: you can redistribute it and/or modify
% it under the terms of the GNU General Public License version 3 as
% published by the Free Software Foundation.
%
% PbDlib is distributed in the hope that it will be useful,
% but WITHOUT ANY WARRANTY; without even the implied warranty of
% MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
% GNU General Public License for more details.
%
% You should have received a copy of the GNU General Public License
% along with PbDlib. If not, see <http://www.gnu.org/licenses/>.
%Parameters of the EM iterations
nbMinSteps
=
5
;
%Minimum number of iterations allowed
...
...
@@ 9,7 +38,7 @@ nbMaxSteps = 100; %Maximum number of iterations allowed
maxDiffLL
=
1E4
;
%Likelihood increase threshold to stop the algorithm
nbData
=
size
(
Data
,
2
);
diagRegularizationFactor
=
1E6
;
%
R
egularization term
is optional, see Eq. (2.1.2) in doc/TechnicalReport.pdf
diagRegularizationFactor
=
1E6
;
%
Optional r
egularization term
% %Circular initialization of the MFA parameters
% Itmp = eye(model.nbVar)*1E2;
...
...
@@ 22,7 +51,7 @@ for i=1:model.nbStates
[
V
,
D
]
=
eig
(
model
.
Sigma
(:,:,
i
)

model
.
P
(:,:,
i
));
[
~
,
id
]
=
sort
(
diag
(
D
),
'descend'
);
V
=
V
(:,
id
)
*
D
(
id
,
id
)
.^.
5
;
model
.
L
(:,:,
i
)
=
V
(:,
1
:
model
.
nbFA
);
%>Sigma=LL'+P
model
.
L
(:,:,
i
)
=
V
(:,
1
:
model
.
nbFA
);
%>
Sigma=LL'+P
end
for
nbIter
=
1
:
nbMaxSteps
for
i
=
1
:
model
.
nbStates
...
...
@@ 41,26 +70,26 @@ for nbIter=1:nbMaxSteps
GAMMA2
=
GAMMA
.
/
repmat
(
sum
(
GAMMA
,
2
),
1
,
nbData
);
%Mstep
%Update Priors
, see Eq. (2.2.10) in doc/TechnicalReport.pdf
%Update Priors
model
.
Priors
=
sum
(
GAMMA
,
2
)
/
nbData
;
%Update Mu
, see Eq. (2.2.11) in doc/TechnicalReport.pdf
%Update Mu
model
.
Mu
=
Data
*
GAMMA2
'
;
%Update factor analysers parameters
for
i
=
1
:
model
.
nbStates
%Compute covariance
, see Eq. (2.2.15) in doc/TechnicalReport.pdf
%Compute covariance
DataTmp
=
Data

repmat
(
model
.
Mu
(:,
i
),
1
,
nbData
);
S
(:,:,
i
)
=
DataTmp
*
diag
(
GAMMA2
(
i
,:))
*
DataTmp
'
+
eye
(
model
.
nbVar
)
*
diagRegularizationFactor
;
%Update B
, see Eq. (2.2.16) in doc/TechnicalReport.pdf
%Update B
B
(:,:,
i
)
=
model
.
L
(:,:,
i
)
' / (model.L(:,:,i) * model.L(:,:,i)'
+
model
.
P
(:,:,
i
));
%Update Lambda
, see Eq. (2.2.12) in doc/TechnicalReport.pdf
%Update Lambda
model
.
L
(:,:,
i
)
=
S
(:,:,
i
)
*
B
(:,:,
i
)
' / (eye(model.nbFA)  B(:,:,i) * model.L(:,:,i) + B(:,:,i) * S(:,:,i) * B(:,:,i)'
);
%Update Psi
, see Eq. (2.2.13) in doc/TechnicalReport.pdf
%Update Psi
model
.
P
(:,:,
i
)
=
diag
(
diag
(
S
(:,:,
i
)

model
.
L
(:,:,
i
)
*
B
(:,:,
i
)
*
S
(:,:,
i
)))
+
eye
(
model
.
nbVar
)
*
diagRegularizationFactor
;
%Reconstruct Sigma
, see Eq. (2.2.4) in doc/TechnicalReport.pdf
%Reconstruct Sigma
model
.
Sigma
(:,:,
i
)
=
model
.
L
(:,:,
i
)
*
model
.
L
(:,:,
i
)
'
+
model
.
P
(:,:,
i
);
end
%Compute average loglikelihood
...
...
@@ 76,9 +105,9 @@ end
disp
([
'The maximum number of '
num2str
(
nbMaxSteps
)
' EM iterations has been reached.'
]);
end
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
function
[
Lik
,
GAMMA
]
=
computeGamma
(
Data
,
model
)
%See Eq. (2.2.9) in doc/TechnicalReport.pdf
Lik
=
zeros
(
model
.
nbStates
,
size
(
Data
,
2
));
for
i
=
1
:
model
.
nbStates
Lik
(
i
,:)
=
model
.
Priors
(
i
)
*
gaussPDF
(
Data
,
model
.
Mu
(:,
i
),
model
.
Sigma
(:,:,
i
));
...
...
m_fcts/EM_MPPCA.m
View file @
2cb8784d
function
[
model
,
GAMMA2
]
=
EM_MPPCA
(
Data
,
model
)
%EM for mixture of probabilistic principal component analyzers,
%inspired by "Mixtures of Probabilistic Principal Component Analysers" by Michael E. Tipping and Christopher M. Bishop
%Sylvain Calinon, 2015
% EM for mixture of probabilistic principal component analyzers (implementation based on
% "Mixtures of Probabilistic Principal Component Analysers" by Michael E. Tipping and Christopher M. Bishop)
%
% Writing code takes time. Polishing it and making it available to others takes longer!
% If some parts of the code were useful for your research of for a better understanding
% of the algorithms, please reward the authors by citing the related publications,
% and consider making your own research available in this way.
%
% @article{Calinon15,
% author="Calinon, S.",
% title="A Tutorial on TaskParameterized Movement Learning and Retrieval",
% journal="Intelligent Service Robotics",
% year="2015"
% }
%
% Copyright (c) 2015 Idiap Research Institute, http://idiap.ch/
% Written by Sylvain Calinon, http://calinon.ch/
%
% This file is part of PbDlib, http://www.idiap.ch/software/pbdlib/
%
% PbDlib is free software: you can redistribute it and/or modify
% it under the terms of the GNU General Public License version 3 as
% published by the Free Software Foundation.
%
% PbDlib is distributed in the hope that it will be useful,
% but WITHOUT ANY WARRANTY; without even the implied warranty of
% MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
% GNU General Public License for more details.
%
% You should have received a copy of the GNU General Public License
% along with PbDlib. If not, see <http://www.gnu.org/licenses/>.
%Parameters of the EM iterations
nbMinSteps
=
5
;
%Minimum number of iterations allowed
...
...
@@ 29,29 +58,29 @@ for nbIter=1:nbMaxSteps
GAMMA2
=
GAMMA
.
/
repmat
(
sum
(
GAMMA
,
2
),
1
,
nbData
);
%Mstep
%Update Priors
, see Eq. (2.2.10) in doc/TechnicalReport.pdf
%Update Priors
model
.
Priors
=
sum
(
GAMMA
,
2
)
/
nbData
;
%Update Mu
, see Eq. (2.2.11) in doc/TechnicalReport.pdf
%Update Mu
model
.
Mu
=
Data
*
GAMMA2
'
;
%Update factor analyser params
for
i
=
1
:
model
.
nbStates
%Compute covariance
, see Eq. (2.2.19) in doc/TechnicalReport.pdf
%Compute covariance
DataTmp
=
Data

repmat
(
model
.
Mu
(:,
i
),
1
,
nbData
);
S
(:,:,
i
)
=
DataTmp
*
diag
(
GAMMA2
(
i
,:))
*
DataTmp
'
+
eye
(
model
.
nbVar
)
*
diagRegularizationFactor
;
%Update M
, see Eq. (2.2.20) in doc/TechnicalReport.pdf
%Update M
M
=
eye
(
model
.
nbFA
)
*
model
.
o
(
i
)
+
model
.
L
(:,:,
i
)
'
*
model
.
L
(:,:,
i
);
%Update Lambda
, see Eq. (2.2.17) in doc/TechnicalReport.pdf
%Update Lambda
Lnew
=
S
(:,:,
i
)
*
model
.
L
(:,:,
i
)
/
(
eye
(
model
.
nbFA
)
*
model
.
o
(
i
)
+
M
\
model
.
L
(:,:,
i
)
'
*
S
(:,:,
i
)
*
model
.
L
(:,:,
i
));
%Update of sigma^2
, see Eq. (2.2.21) in doc/TechnicalReport.pdf
%Update of sigma^2
model
.
o
(
i
)
=
trace
(
S
(:,:,
i
)

S
(:,:,
i
)
*
model
.
L
(:,:,
i
)
/
M
*
Lnew
'
)
/
model
.
nbVar
;
model
.
L
(:,:,
i
)
=
Lnew
;
%Update Psi
, see Eq. (2.2.18) in doc/TechnicalReport.pdf
%Update Psi
model
.
P
(:,:,
i
)
=
eye
(
model
.
nbVar
)
*
model
.
o
(
i
);
%Reconstruct Sigma
, see Eq. (2.2.4) in doc/TechnicalReport.pdf
%Reconstruct Sigma
model
.
Sigma
(:,:,
i
)
=
model
.
L
(:,:,
i
)
*
model
.
L
(:,:,
i
)
'
+
model
.
P
(:,:,
i
);
end
...
...
@@ 68,9 +97,9 @@ end
disp
([
'The maximum number of '
num2str
(
nbMaxSteps
)
' EM iterations has been reached.'
]);
end
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
function
[
Lik
,
GAMMA
]
=
computeGamma
(
Data
,
model
)
%See Eq. (2.0.5) in doc/TechnicalReport.pdf
Lik
=
zeros
(
model
.
nbStates
,
size
(
Data
,
2
));
for
i
=
1
:
model
.
nbStates
Lik
(
i
,:)
=
model
.
Priors
(
i
)
*
gaussPDF
(
Data
,
model
.
Mu
(:,
i
),
model
.
Sigma
(:,:,
i
));
...
...
m_fcts/EM_TPGMM.m
View file @
2cb8784d
...
...
@@ 3,21 +3,35 @@ function model = EM_TPGMM(Data, model)
% The approach allows the modulation of the centers and covariance matrices of the Gaussians with respect to
% external parameters represented in the form of candidate coordinate systems.
%
% Author: Sylvain Calinon, 2014
% http://programmingbydemonstration.org/SylvainCalinon
% Writing code takes time. Polishing it and making it available to others takes longer!
% If some parts of the code were useful for your research of for a better understanding
% of the algorithms, please reward the authors by citing the related publications,
% and consider making your own research available in this way.
%
% This source code is given for free! In exchange, I would be grateful if you cite
% the following reference in any academic publication that uses this code or part of it:
%
% @inproceedings{Calinon14ICRA,
% author="Calinon, S. and Bruno, D. and Caldwell, D. G.",
% title="A taskparameterized probabilistic model with minimal intervention control",
% booktitle="Proc. {IEEE} Intl Conf. on Robotics and Automation ({ICRA})",
% year="2014",
% month="MayJune",
% address="Hong Kong, China",
% pages="33393344"
% @article{Calinon15,
% author="Calinon, S.",
% title="A Tutorial on TaskParameterized Movement Learning and Retrieval",
% journal="Intelligent Service Robotics",
% year="2015"
% }
%
% Copyright (c) 2015 Idiap Research Institute, http://idiap.ch/
% Written by Sylvain Calinon, http://calinon.ch/
%
% This file is part of PbDlib, http://www.idiap.ch/software/pbdlib/
%
% PbDlib is free software: you can redistribute it and/or modify
% it under the terms of the GNU General Public License version 3 as
% published by the Free Software Foundation.
%
% PbDlib is distributed in the hope that it will be useful,
% but WITHOUT ANY WARRANTY; without even the implied warranty of
% MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
% GNU General Public License for more details.
%
% You should have received a copy of the GNU General Public License
% along with PbDlib. If not, see <http://www.gnu.org/licenses/>.
%Parameters of the EM algorithm
nbMinSteps
=
5
;
%Minimum number of iterations allowed
...
...
@@ 25,8 +39,8 @@ nbMaxSteps = 100; %Maximum number of iterations allowed
maxDiffLL
=
1E5
;
%Likelihood increase threshold to stop the algorithm
nbData
=
size
(
Data
,
3
);
diagRegularizationFactor
=
1E5
;
%
R
egularization term
is optional, see Eq. (2.1.2) in doc/TechnicalReport.pdf
%diagRegularizationFactor = 0; %
R
egularization term
is optional, see Eq. (2.1.2) in doc/TechnicalReport.pdf
diagRegularizationFactor
=
1E5
;
%
Optional r
egularization term
%diagRegularizationFactor = 0; %
Optional r
egularization term
for
nbIter
=
1
:
nbMaxSteps
fprintf
(
'.'
);
...
...
@@ 38,7 +52,7 @@ for nbIter=1:nbMaxSteps
%Mstep
for
i
=
1
:
model
.
nbStates
%Update Priors
, see Eq. (6.0.2) in doc/TechnicalReport.pdf
%Update Priors
model
.
Priors
(
i
)
=
sum
(
sum
(
GAMMA
(
i
,:)))
/
nbData
;
for
m
=
1
:
model
.
nbFrames
...
...
@@ 46,10 +60,10 @@ for nbIter=1:nbMaxSteps
DataMat
=
[];
DataMat
(
1
:
model
.
nbVars
(
m
),:)
=
Data
(
1
:
model
.
nbVars
(
m
),
m
,:);
%Update Mu
, see Eq. (6.0.3) in doc/TechnicalReport.pdf
%Update Mu
model
.
Mu
(
1
:
model
.
nbVars
(
m
),
m
,
i
)
=
DataMat
*
GAMMA2
(
i
,:)
'
;
%Update Sigma (regularization term is optional)
, see Eq. (6.0.4) in doc/TechnicalReport.pdf
%Update Sigma (regularization term is optional)
DataTmp
=
DataMat

repmat
(
model
.
Mu
(
1
:
model
.
nbVars
(
m
),
m
,
i
),
1
,
nbData
);
model
.
Sigma
(
1
:
model
.
nbVars
(
m
),
1
:
model
.
nbVars
(
m
),
m
,
i
)
=
DataTmp
*
diag
(
GAMMA2
(
i
,:))
*
DataTmp
'
+
eye
(
model
.
nbVars
(
m
))
*
diagRegularizationFactor
;
end
...
...
@@ 68,9 +82,9 @@ end
disp
([
'The maximum number of '
num2str
(
nbMaxSteps
)
' EM iterations has been reached.'
]);
end
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
function
[
Lik
,
GAMMA
,
GAMMA0
]
=
computeGamma
(
Data
,
model
)
%See Eq. (6.0.1) in doc/TechnicalReport.pdf
nbData
=
size
(
Data
,
3
);
Lik
=
ones
(
model
.
nbStates
,
nbData
);
GAMMA0
=
zeros
(
model
.
nbStates
,
model
.
nbFrames
,
nbData
);
...
...
@@ 85,7 +99,3 @@ for i=1:model.nbStates
end
GAMMA
=
Lik
.
/
repmat
(
sum
(
Lik
,
1
)
+
realmin
,
size
(
Lik
,
1
),
1
);
end
m_fcts/EM_stdPGMM.m
View file @
2cb8784d
function
[
model
,
s
,
LL
]
=
EM_stdPGMM
(
s
,
model
)
% Training of a parametric Gaussian mixture model (PGMM) with expectationmaximization (EM).
% The implementation follows the approach described by Wilson and Bobick (1999) "Parametric Hidden Markov
% Models for Gesture Recognition", IEEE Trans. on Pattern Analysis and Machine Intelligence, with an
% implementation applied to the special case of Gaussian mixture models (GMM).
% The approach is inspired by Wilson and Bobick (1999), with an implementation applied to
% the special case of Gaussian mixture models (GMM).
%
% Author: Sylvain Calinon, 2013
% http://programmingbydemonstration.org/SylvainCalinon
% Writing code takes time. Polishing it and making it available to others takes longer!
% If some parts of the code were useful for your research of for a better understanding
% of the algorithms, please reward the authors by citing the related publications,
% and consider making your own research available in this way.
%
% This source code is given for free! In exchange, I would be grateful if you cite
% the following references in any academic publication that uses this code or part of it:
%
% @inproceedings{Calinon12Hum,
% author="Calinon, S. and Li, Z. and Alizadeh, T. and Tsagarakis, N. G. and Caldwell, D. G.",
% title="Statistical dynamical systems for skills acquisition in humanoids",
% booktitle="Proc. {IEEE} Intl Conf. on Humanoid Robots ({H}umanoids)",
% year="2012",
% address="Osaka, Japan"
% @article{Calinon15,
% author="Calinon, S.",
% title="A Tutorial on TaskParameterized Movement Learning and Retrieval",
% journal="Intelligent Service Robotics",
% year="2015"
% }
%
% @article{Wilson99,
% author="Wilson, A. D. and Bobick, A. F.",
% title="Parametric Hidden {M}arkov Models for Gesture Recognition",
...
...
@@ 27,8 +25,23 @@ function [model, s, LL] = EM_stdPGMM(s, model)
% pages="884900"
% }
%
% The first reference presents an implementation of the approach described in the second reference, and
% applies it to the special case of Gaussian mixture model (GMM).
% Copyright (c) 2015 Idiap Research Institute, http://idiap.ch/
% Written by Sylvain Calinon, http://calinon.ch/
%
% This file is part of PbDlib, http://www.idiap.ch/software/pbdlib/
%
% PbDlib is free software: you can redistribute it and/or modify
% it under the terms of the GNU General Public License version 3 as