demo_TPtrajGMM01.m 9.18 KB
Newer Older
Sylvain Calinon's avatar
Sylvain Calinon committed
1 2 3 4 5 6 7 8 9 10 11 12 13 14
function demo_TPtrajGMM01
% Task-parameterized model with trajectory-GMM encoding (GMM with dynamic features)
%
% Sylvain Calinon, 2015
% http://programming-by-demonstration.org/lib/
%
% 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:
%
% @article{Calinon15,
%   author="Calinon, S.",
%   title="A tutorial on task-parameterized movement learning and retrieval",
%   year="2015",
% }
15 16 17

addpath('./m_fcts/');

Sylvain Calinon's avatar
Sylvain Calinon committed
18

19 20 21 22 23 24
%% Parameters
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
model.nbStates = 3; %Number of Gaussians in the GMM
model.nbFrames = 2; %Number of candidate frames of reference
model.nbVarPos = 2; %Dimension of position data (here: x1,x2)
model.nbDeriv = 3; %Number of static&dynamic features (D=2 for [x,dx], D=3 for [x,dx,ddx], etc.)
Sylvain Calinon's avatar
Sylvain Calinon committed
25
model.nbVar = model.nbVarPos * model.nbDeriv; %Dimension of state vector
26
model.dt = 0.01; %Time step
Sylvain Calinon's avatar
Sylvain Calinon committed
27 28 29
nbData = 200; %Number of datapoints in a trajectory
nbRepros = 5; %Number of reproductions with new situations randomly generated

30 31 32 33 34 35 36

%% Load 3rd order tensor data
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
disp('Load 3rd order tensor data...');
% The MAT file contains a structure 's' with the multiple demonstrations. 's(n).Data' is a matrix data for
% sample n (with 's(n).nbData' datapoints). 's(n).p(m).b' and 's(n).p(m).A' contain the position and
% orientation of the m-th candidate coordinate system for this demonstration. 'Data' contains the observations
Sylvain Calinon's avatar
Sylvain Calinon committed
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
% in the different frames. It is a 3rd order tensor of dimension DC x P x N, with D=2 the dimension of a
% datapoint, C=2 the number of derivatives (incl. position), P=2 the number of candidate frames, and N=TM 
% the number of datapoints in a trajectory (T=200) multiplied by the number of demonstrations (M=5).
load('data/DataWithDeriv02.mat');

% %Convert position data to position+velocity data
% load('data/Data01.mat');
% %Create transformation matrix to compute derivatives
% D = (diag(ones(1,nbData-1),-1)-eye(nbData)) / model.dt;
% D(end,end) = 0;
% %Create 3rd order tensor data and task parameters
% Data = zeros(model.nbVar, model.nbFrames, nbSamples*nbData);
% for n=1:nbSamples
% 	s(n).Data = zeros(model.nbVar,model.nbFrames,nbData);
% 	s(n).Data0 = s(n).Data0(2:end,:); %Remove time
% 	DataTmp = s(n).Data0;
% 	for k=1:model.nbDeriv-1
% 		DataTmp = [DataTmp; s(n).Data0*D^k]; %Compute derivatives
% 	end
% 	for m=1:model.nbFrames
% 		s(n).p(m).b = [s(n).p(m).b; zeros((model.nbDeriv-1)*model.nbVarPos,1)];
% 		s(n).p(m).A = kron(eye(model.nbDeriv), s(n).p(m).A);
% 		s(n).Data(:,m,:) = s(n).p(m).A \ (DataTmp - repmat(s(n).p(m).b, 1, nbData));
% 		Data(:,m,(n-1)*nbData+1:n*nbData) = s(n).Data(:,m,:);
% 	end
% end
% %Save new dataset including derivatives
% save('data/DataWithDeriv02.mat', 'Data','s','nbSamples');
65 66

%Construct PHI operator (big sparse matrix)
Sylvain Calinon's avatar
Sylvain Calinon committed
67
[PHI,PHI1] = constructPHI(model, nbData, nbSamples); 
68 69 70 71


%% TP-GMM learning
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
Sylvain Calinon's avatar
Sylvain Calinon committed
72
fprintf('Parameters estimation of TP-GMM with EM...');
73
%model = init_tensorGMM_timeBased(Data, model); %Initialization
Sylvain Calinon's avatar
Sylvain Calinon committed
74 75 76 77 78 79 80 81 82 83 84 85 86 87 88
%model = init_tensorGMM_kmeans(Data, model); %Initialization

%Initialization based on position data
model0 = init_tensorGMM_kmeans(Data(1:model.nbVarPos,:,:), model);
[~,~,GAMMA2] = EM_tensorGMM(Data(1:model.nbVarPos,:,:), model0);
model.Priors = model0.Priors;
for i=1:model.nbStates
	for m=1:model.nbFrames
		DataTmp = squeeze(Data(:,m,:));
		model.Mu(:,m,i) = DataTmp * GAMMA2(i,:)';
		DataTmp = DataTmp - repmat(model.Mu(:,m,i),1,nbData*nbSamples);
		model.Sigma(:,:,m,i) = DataTmp * diag(GAMMA2(i,:)) * DataTmp';
	end
end

89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108
model = EM_tensorGMM(Data, model);


%% Reproduction for the task parameters used to train the model
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
disp('Reproductions...');
for n=1:nbSamples
	%Products of linearly transformed Gaussians
	for i=1:model.nbStates
		SigmaTmp = zeros(model.nbVar);
		MuTmp = zeros(model.nbVar,1);
		for m=1:model.nbFrames
			MuP = s(n).p(m).A * model.Mu(:,m,i) + s(n).p(m).b;
			SigmaP = s(n).p(m).A * model.Sigma(:,:,m,i) * s(n).p(m).A';
			SigmaTmp = SigmaTmp + inv(SigmaP);
			MuTmp = MuTmp + SigmaP\MuP;
		end
		r(n).Sigma(:,:,i) = inv(SigmaTmp);
		r(n).Mu(:,i) = r(n).Sigma(:,:,i) * MuTmp;
	end
Sylvain Calinon's avatar
Sylvain Calinon committed
109 110 111 112 113
	%Create single Gaussian N(MuQ,SigmaQ) based on state sequence q, see Eq. (27)
	[~,r(n).q] = max(model.Pix(:,(n-1)*nbData+1:n*nbData),[],1); %works also for nbStates=1
	r(n).MuQ = reshape(r(n).Mu(:,r(n).q), model.nbVarPos*model.nbDeriv*nbData, 1);
	r(n).SigmaQ = zeros(model.nbVarPos*model.nbDeriv*nbData);
	for t=1:nbData
114 115 116
		id1 = (t-1)*model.nbVarPos*model.nbDeriv+1:t*model.nbVarPos*model.nbDeriv;
		r(n).SigmaQ(id1,id1) = r(n).Sigma(:,:,r(n).q(t));
	end
Sylvain Calinon's avatar
Sylvain Calinon committed
117
	%Retrieval of data with trajectory GMM, see Eq. (30)
118 119 120
	PHIinvSigmaQ = PHI1'/r(n).SigmaQ;
	Rq = PHIinvSigmaQ * PHI1;
	rq = PHIinvSigmaQ * r(n).MuQ;
Sylvain Calinon's avatar
Sylvain Calinon committed
121
	r(n).Data = reshape(Rq\rq, model.nbVarPos, nbData); %Reshape data for plotting
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
end


%% Reproduction for new task parameters
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
disp('New reproductions...');
for n=1:nbRepros
	for m=1:model.nbFrames
		%Random generation of new task parameters
		id=ceil(rand(2,1)*nbSamples);
		w=rand(2); w=w/sum(w);
		rnew(n).p(m).b = s(id(1)).p(m).b * w(1) + s(id(2)).p(m).b * w(2);
		rnew(n).p(m).A = s(id(1)).p(m).A * w(1) + s(id(2)).p(m).A * w(2);
	end
	%GMM products
	for i=1:model.nbStates
		SigmaTmp = zeros(model.nbVar);
		MuTmp = zeros(model.nbVar,1);
		for m=1:model.nbFrames
			MuP = rnew(n).p(m).A * model.Mu(:,m,i) + rnew(n).p(m).b;
			SigmaP = rnew(n).p(m).A * model.Sigma(:,:,m,i) * rnew(n).p(m).A';
			SigmaTmp = SigmaTmp + inv(SigmaP);
			MuTmp = MuTmp + SigmaP\MuP;
		end
		rnew(n).Sigma(:,:,i) = inv(SigmaTmp);
		rnew(n).Mu(:,i) = rnew(n).Sigma(:,:,i) * MuTmp;
	end
Sylvain Calinon's avatar
Sylvain Calinon committed
149 150 151 152 153
	%Create single Gaussian N(MuQ,SigmaQ) based on state sequence q, see Eq. (27)
	[~,rnew(n).q] = max(model.Pix(:,1:nbData),[],1); %works also for nbStates=1
	rnew(n).MuQ = reshape(rnew(n).Mu(:,rnew(n).q), model.nbVarPos*model.nbDeriv*nbData, 1);
	rnew(n).SigmaQ = zeros(model.nbVarPos*model.nbDeriv*nbData);
	for t=1:nbData
154 155 156
		id1 = (t-1)*model.nbVarPos*model.nbDeriv+1:t*model.nbVarPos*model.nbDeriv;
		rnew(n).SigmaQ(id1,id1) = rnew(n).Sigma(:,:,rnew(n).q(t));
	end
Sylvain Calinon's avatar
Sylvain Calinon committed
157
	%Retrieval of data with trajectory GMM, see Eq. (30)
158 159 160
	PHIinvSigmaQ = PHI1'/rnew(n).SigmaQ;
	Rq = PHIinvSigmaQ * PHI1;
	rq = PHIinvSigmaQ * rnew(n).MuQ;
Sylvain Calinon's avatar
Sylvain Calinon committed
161
	rnew(n).Data = reshape(Rq\rq, model.nbVarPos, nbData); %Reshape data for plotting
162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182
end


%% Plots
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
figure('position',[20,50,1300,500]);
xx = round(linspace(1,64,nbSamples));
clrmap = colormap('jet');
clrmap = min(clrmap(xx,:),.95);
limAxes = [-1.2 0.8 -1.1 0.9];
colPegs = [[.9,.5,.9];[.5,.9,.5]];

%DEMOS
subplot(1,3,1); hold on; box on; title('Demonstrations');
for n=1:nbSamples
	%Plot frames
	for m=1:model.nbFrames
		plot([s(n).p(m).b(1) s(n).p(m).b(1)+s(n).p(m).A(1,2)], [s(n).p(m).b(2) s(n).p(m).b(2)+s(n).p(m).A(2,2)], '-','linewidth',6,'color',colPegs(m,:));
		plot(s(n).p(m).b(1), s(n).p(m).b(2),'.','markersize',30,'color',colPegs(m,:)-[.05,.05,.05]);
	end
	%Plot trajectories
Sylvain Calinon's avatar
Sylvain Calinon committed
183 184
	plot(s(n).Data0(1,1), s(n).Data0(2,1),'.','markersize',12,'color',clrmap(n,:));
	plot(s(n).Data0(1,:), s(n).Data0(2,:),'-','linewidth',1.5,'color',clrmap(n,:));
185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203
end
axis(limAxes); axis square; set(gca,'xtick',[],'ytick',[]);

%REPROS
subplot(1,3,2); hold on; box on; title('Reproductions');
for n=1:nbSamples
	%Plot frames
	for m=1:model.nbFrames
		plot([s(n).p(m).b(1) s(n).p(m).b(1)+s(n).p(m).A(1,2)], [s(n).p(m).b(2) s(n).p(m).b(2)+s(n).p(m).A(2,2)], '-','linewidth',6,'color',colPegs(m,:));
		plot(s(n).p(m).b(1), s(n).p(m).b(2),'.','markersize',30,'color',colPegs(m,:)-[.05,.05,.05]);
	end
end
for n=1:nbSamples
	%Plot trajectories
	plot(r(n).Data(1,1), r(n).Data(2,1),'.','markersize',12,'color',clrmap(n,:));
	plot(r(n).Data(1,:), r(n).Data(2,:),'-','linewidth',1.5,'color',clrmap(n,:));
end
for n=1:nbSamples
	%Plot Gaussians
Sylvain Calinon's avatar
Sylvain Calinon committed
204
	plotGMM(r(n).Mu(1:2,:,1), r(n).Sigma(1:2,1:2,:,1), [.5 .5 .5], .4);
205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223
end
axis(limAxes); axis square; set(gca,'xtick',[],'ytick',[]);

%NEW REPROS
subplot(1,3,3); hold on; box on; title('New reproductions');
for n=1:nbRepros
	%Plot frames
	for m=1:model.nbFrames
		plot([rnew(n).p(m).b(1) rnew(n).p(m).b(1)+rnew(n).p(m).A(1,2)], [rnew(n).p(m).b(2) rnew(n).p(m).b(2)+rnew(n).p(m).A(2,2)], '-','linewidth',6,'color',colPegs(m,:));
		plot(rnew(n).p(m).b(1), rnew(n).p(m).b(2), '.','markersize',30,'color',colPegs(m,:)-[.05,.05,.05]);
	end
end
for n=1:nbRepros
	%Plot trajectories
	plot(rnew(n).Data(1,1), rnew(n).Data(2,1),'.','markersize',12,'color',[.2 .2 .2]);
	plot(rnew(n).Data(1,:), rnew(n).Data(2,:),'-','linewidth',1.5,'color',[.2 .2 .2]);
end
for n=1:nbRepros
	%Plot Gaussians
Sylvain Calinon's avatar
Sylvain Calinon committed
224
	plotGMM(rnew(n).Mu(1:2,:,1), rnew(n).Sigma(1:2,1:2,:,1), [.5 .5 .5], .4);
225 226 227
end
axis(limAxes); axis square; set(gca,'xtick',[],'ytick',[]);

Sylvain Calinon's avatar
Sylvain Calinon committed
228
%print('-dpng','graphs/demo_TPtrajGMM01.png');
229 230 231 232
%pause;
%close all;