RetargetToolbox
test_retarget.m
Go to the documentation of this file.
00001 % This is the new version of test_retarget.m
00002 % This prepares the test set (held in SourceTest and TargetTest) using the AlgnParams
00003 % if spatial alignment was done.
00004 % Call this after retargetdemo_pca to see how well the sequences match
00005 % and to compute the errors.
00006 
00007 clear errors meanerrors t s tt meanerrors2 errors2
00008 % clear epsilon* *update neg* sparse*
00009 % clear target* source*
00010 % Get th eminimum number of frames out sequence has to have in order to be used.
00011 if(exist('n1win','var'))
00012     n1 = length(find(n1win<0));
00013 end
00014 if(~exist('n1win','var'))
00015     n1win = -n1:0;
00016 end
00017 if(exist('CENTER_SOURCE','var') && CENTER_SOURCE==1)
00018     minframes = max(2*n1+1,n2+1+n1); % Need n1 ahead of the maximum n1 or n2
00019 else
00020     minframes = max(n1,n2);
00021 end
00022 % Min number of frames need for initialization
00023 minstartframes = max(n1,n2);
00024 % Number of frames to ignore at end due to centered source window.
00025 endframes = length(find(n1win>0));
00026 
00027 fprintf('XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX\n');
00028 
00029 %%%%%%%%%%%%%%%%5
00030 % Backwards compatibility
00031 %%%%%%%%%%%%%%%%%
00032 if(~exist('AlignedSource','var'))
00033     fprintf('Backwards comparible is running to get train/test cell arrays. !!!!!!!!!!!!!!\n');
00034     % Before this point, All data is untouched.
00035     [AlignedSource,AlignedTarget,AlgnParams] = align_trainsets(OrigSource,OrigTarget,space_align);
00036     if(length(data_std_s)~=1)
00037         multi_std = 1;
00038     else
00039         multi_std = 0;
00040     end
00041     % Normalize the datasets and copuing training set statistics.
00042     [NormedSource,NormedTarget,dimparams,pcaparams,normparams] = ...
00043         preprocess_trainsets(AlignedSource,AlignedTarget,USE_PCA,DataType,multi_std);
00044     % Apply the same training set alignment to the test set.
00045     try
00046         [AlignedSourceTest,AlignedTargetTest,AlgnParamsTest] = align_testsets(OrigSourceTest,OrigTargetTest,AlgnParams);
00047     catch
00048         [AlignedSourceTest,AlignedTargetTest,AlgnParamsTest] = align_testsets(SourceTest,TargetTest,AlgnParams);
00049     end
00050     % Apply the same normalization from the training set to the test set
00051     [NormedSourceTest,NormedTargetTest,normparamstest] = preprocess_testsets(AlignedSourceTest,AlignedTargetTest,dimparams,pcaparams,normparams);
00052     %     % Now convert the new cells into the data we need.
00053     %     % Train on these
00054     %     sourceTraindata = cell2mat(NormedSource');
00055     %     targetTraindata = cell2mat(NormedTarget');
00056     %     % Keep the unoromalized (but aligned) data for computing errors during traiing scripts.
00057     %     alignedSourceTraindata = cell2mat(AlignedSource');
00058     %     alignedTargetTraindata = cell2mat(AlignedTarget');
00059     
00060     if(~exist('TRAIN_TYPE','var'))
00061         TRAIN_TYPE = '';
00062     end
00063 end
00064 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
00065 
00066 
00067 
00068 
00069 
00070 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
00071 % Convert each sequence into the model space
00072 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
00073 %%%
00074 % If you want to see performance on the training set.
00075 %%%
00076 % TargetTest = OrigTarget;
00077 % SourceTest = OrigSource;
00078 % TrueTargetTest = OrigTarget;
00079 %%%
00080 
00081 %%%%
00082 % Code below wants NewTargetTest and NewSourceTest to be set to the normalied data.
00083 %%%%
00084 NewTargetTest = NormedTargetTest;
00085 NewSourceTest = NormedSourceTest;
00086 %%%%%%%%%%%
00087 
00088 
00089 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
00090 % Generate the sequences in normalized space.
00091 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
00092 % fprintf('XXXX')
00093 for testseq=1:length(NormedTargetTest)
00094     % Only proceed if you have enough frames in this sequence.
00095     if(minframes<size(NewTargetTest{testseq},1))
00096         % Get sequence from source and target in normalized and aligned space.
00097         source=NewSourceTest{testseq};
00098         targetinit=NewTargetTest{testseq}(1:max(n2,n1),:); % Only a few frames.
00099         % Get the true target (not normalized, but aligned, untouched before alignment).
00100         try
00101             ttarget = CompareTargetTest{testseq};
00102         catch
00103             ttarget = AlignedTargetTest{testseq}; % The whole sequence. (before anything done to it.).
00104         end
00105         
00106         
00107         %         figure(2232), hist(ttarget(:),100); title('target');
00108         %         figure(2233), hist(source(:),100); title('source');
00109         %         figure(2234), hist(targetinit(:),100); title('targetinit');
00110         
00111         %%%%%%%%%%%%
00112         % Retarget
00113         switch TRAIN_TYPE
00114             case 'linreg'
00115                 fprintf('Generating test sequence with linear regression.: %d\r',testseq);
00116                 % Least squares uses the previous source to predict the current target.
00117                 temp = LR*[ones(size(source,1)-1,1) source(1:end-1,:)]';
00118                 target = zeros(size(temp')+[1 0]);
00119                 target(2:end,:) = temp';
00120                 target(1,:) = NewTargetTest{testseq}(1,:);
00121             case {'autoreg','autoreg2'}
00122                 fprintf('Generating test sequence with autoreg: %d\r',testseq);
00123                 % Stack the previous frames and current frame of source and previous target frames.
00124                 target = apply_autoReg(source,targetinit,LAR,n1win,n2);
00125             case {'neural','neural2'}
00126                 fprintf('Generating test sequence with Neural Net: %d\r',testseq);
00127                 % Stack the previous frames and current frame of source and previous target frames.
00128                 target = retarget_neuralNet(source,targetinit,wpen,wtop,n1win,n2);
00129             case 'fac' % Factored RBM
00130                 fprintf('Generating test sequence with Factored RBM: %d\r',testseq);
00131                 target = retarget_fac(source,targetinit,visfac,featfac,hidfac,pastfacA,visfacA,pastfacB,hidfacB,...
00132                     sourcefeat,visbiases,hidbiases,n1win);
00133             case 'fac_nofeatfac'
00134                 fprintf('Generating test sequence with Factored (nofeatfac) RBM: %d\r',testseq);
00135                 target = retarget_fac_nofeatfac(source,targetinit,visfac,sourcefac,hidfac,pastfacA,visfacA,pastfacB,hidfacB,...
00136                     visbiases,hidbiases,n1win);
00137             case 'fac_cond'
00138                 fprintf('Generating test sequence with Factored + Conditional RBM: %d\r',testseq);
00139                 target = retarget_fgrbm(source,targetinit,A,B,sourcefac,hidfac,visfac,bi,bj,n1win,gsd);
00140             case 'iorbm'
00141                 fprintf('Generating test sequence with ioRBM (no aut connections): %d\r',testseq);
00142                 % This returns the filled in sequence of size(source,1).
00143                 % Compute errors only from (minstartframes:end-end_frames,;)
00144                 [target,hidstatesret,hposteriorsret]=retarget(source,targetinit,w,A,B,P,Q,bi,bj,1,1,n1win);
00145             otherwise
00146                 fprintf('Generating test sequence with ioTRBM (all connections): %d\r',testseq);
00147                 % This returns the filled in sequence of size(source,1).
00148                 % Compute errors only from (minstartframes:end-end_frames,;)
00149                 [target,hidstatesret,hposteriorsret]=retarget(source,targetinit,w,A,B,P,Q,bi,bj,1,1,n1win);
00150         end
00151         %%%%%%%%%%%%
00152         
00153         % Scale back up, then unto PCA, and add back constant dimensions.
00154         try
00155             [source,target] = postprocess_data({source},{target},dimparams,pcaparams,normparams);
00156         catch
00157             [source,target] = postprocess_data({source},{target},dimparams,pcaparams,normparamstest);
00158         end
00159         % Undo velocity or other transforms (not Affine though, compare in Affine space).
00160         [source,target] = unaligned_data(source,target,AlgnParams);
00161         % Just store the output target (this is after alignment but before PCA or zero meaning).
00162         % If doing alignment you should compare to this.
00163         if(strcmp(space_align,'Velocity')==0)
00164             target{1}(1:minstartframes,:) = AlignedTargetTest{testseq}(1:minstartframes,:);
00165         else
00166             %         target{1}(1:minstartframes,:) = OrigTargetTest{testseq}(1:minstartframes,:);
00167         end
00168         
00169         % Make sure the start frames match the true (known) target frames
00170         % Have to do this after pca (dont above).
00171         %         ttarget(1:minstartframes,:) = 0;
00172         
00173         s{testseq} = source{1};
00174         tt{testseq} = ttarget;
00175         t{testseq} = target{1};
00176         
00177         
00178         
00179         if(exist('USING_STOCKS','var') && USING_STOCKS==1)
00180             s1 = Source{1}(1,:);
00181             t1 = OrigTargetTest{1}(:,:); % Get the initial value (not percent change).
00182             tt{testseq} = OrigTargetTest{testseq};
00183             % Integrate up the target
00184             %             tg = target(1,:); % Store temp percent change.
00185             newtarget = zeros(size(target,1)+1,size(target,2));
00186             newtarget(i,:) = t1(1,:);
00187             for i=1:size(target,1)
00188                 % Get back to original value by multiplying the percent change by it
00189                 % and adding back the value.
00190                 newtarget(i+1,:) = (target(i,:)+1).*t1(i,:);
00191                 %                 tg = target(i+1,:); % Store temp.
00192                 %                 target(i,:) = t1;
00193             end
00194             %             target = [zeros(1,size(target,2));target];
00195             t{testseq} = newtarget(2:end,:);
00196         end
00197     else
00198         s{testseq} = [];
00199         tt{testseq} = [];
00200         t{testseq} = [];
00201     end
00202 end
00203 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
00204 
00205 %
00206 % %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
00207 % % Undo the normalization on the generated sequences.
00208 % %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
00209 % % If you want the origin marker to be changes in marker (other markers are just relative to origin (not changes)).
00210 % if(exist('AlgnParamsTest','var') && isfield(AlgnParamsTest,'USE_DELTA_ORIGIN') && AlgnParamsTest.USE_DELTA_ORIGIN)
00211 %     % Integrate the data back up.
00212 %     [s,t] = integrate_origin(s,t,AlgnParamsTest);
00213 % end
00214 %
00215 % %%%
00216 % % warp the predicted target back into the space of the original frames.
00217 % if(strcmp(space_align,'SAlgn'))
00218 %     [s,t] = globalspace_unalign(s,t,AlgnParamsTest);
00219 % elseif(strcmp(space_align,'Auto'))
00220 %     [s,t] = undoJA(s,t,W1,W2,bout1,bout2,jatest);
00221 % end
00222 % %%%%%%
00223 % % Plot
00224 % %%%%%%
00225 % % Don't have to warp the ttarget and target because they should be in the same space.
00226 % %  CompareSequence(tt{testseq+startss},t{testseq+startss},1);
00227 % %%%%%%
00228 
00229 if(~isempty(regexp(space_align,'Affine','ONCE')))
00230     ttused = tt;
00231     try
00232         tt = CompareTargetTest;
00233     catch
00234         tt = AlignedTargetTest; % Get the affine transformed Targets instead of the original space.
00235     end
00236 end
00237 if(strcmp(space_align,'Velocity'))
00238     tt = OrigTargetTest;
00239 end
00240 
00241 
00242 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
00243 % compute errors
00244 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
00245 for ss=1:length(t)
00246     % has to be one larger because of differences as well.
00247     if(minframes+1<size(NewTargetTest{ss},1))
00248         nonzero_cols{ss} = find(sum(tt{ss},1));
00249         % Distances of each corresponding point in 3D.
00250         errors{ss} = sqrt(mean((tt{ss}-t{ss}).^2,2));
00251         nerrors{ss} = sqrt(mean((tt{ss}(:,nonzero_cols{ss})-t{ss}(:,nonzero_cols{ss})).^2,2));
00252         fprintf('Avg Position RMS error over sequence %d: %f\n',ss,mean(nerrors{ss}(minstartframes+1:end-endframes)));
00253         gooderrors{ss} = errors{ss}(minstartframes+1:end-endframes,:);
00254         ngooderrors{ss} = nerrors{ss}(minstartframes+1:end-endframes,:);
00255     end
00256 end
00257 fprintf('POSITION ERROR: Sum of mean NON ZERO COLS , Mean per frame RMS Error: %f (USE THIS)\n',...
00258     mean(cell2mat(ngooderrors(~cellfun(@isempty,ngooderrors))')));
00259 % output
00260 unalignederror = mean(cell2mat(ngooderrors(~cellfun(@isempty,ngooderrors))'));
00261 fprintf('\n');
00262 
00263 %%%%
00264 % Aligned errors
00265 %%%%
00266 if(~exist('USING_STOCKS','var') || USING_STOCKS==0)
00267     fprintf('Aligning the two sequences (tt and t)\n');
00268     tempparams.aspect = [1 1 1]; % Don't scale the aspect ratio between the faces.
00269     tempparams.USE_DELTA_ORIGIN = 0;
00270     %     if(strcmp(space_align,'Affine'))
00271     %         fprintf('Using the affined data already.\n');
00272     %         tt2 = tt;
00273     %         t2 = t;
00274     %     else
00275     %         [tt2,t2] = globalspace_align(tt,t,tempparams);
00276     %     end
00277     
00278     [tt2,t2] = delta_velocity(tt,t);
00279     
00280     for ss=1:length(t)
00281         if(minframes+1<size(NewTargetTest{ss},1))
00282             % Distances of each corresponding point in 3D.
00283             errors2{ss} = sqrt(mean((tt2{ss}-t2{ss}).^2,2));
00284             nerrors2{ss} = sqrt(mean((tt2{ss}(:,nonzero_cols{ss})-t2{ss}(:,nonzero_cols{ss})).^2,2));
00285             %             fprintf('Avg Velocity RMS error over sequence in aligned space %d: %f\n',ss,mean(nerrors2{ss}(minstartframes+1:end-endframes)));
00286             gooderrors2{ss} = errors2{ss}(minstartframes+1:end-endframes);
00287             ngooderrors2{ss} = nerrors2{ss}(minstartframes+1:end-endframes,:);
00288         end
00289     end
00290     %     fprintf('VELOCITY ERROR: Sum of mean NON ZERO COLS , Mean per frame RMS Error: %f (USE THIS)\n',...
00291     %         mean(cell2mat(ngooderrors2(~cellfun(@isempty,ngooderrors2))')));
00292     % output
00293     alignederror = mean(cell2mat(ngooderrors2(~cellfun(@isempty,ngooderrors2))'));
00294     %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
00295 end
00296 fprintf('\n');
00297 
00298 
00299 
00300 
00301 
00302 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
00303 % Get the velocities of the whole dataset (treu data)
00304 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
00305 fprintf('Computing weighted metric.\n');
00306 % [svel,tvel] = delta_velocity(cat(2,OrigSource,OrigSourceTest),cat(2,OrigTarget,OrigTargetTest));
00307 % [svel,tvel] = delta_velocity(cat(2,OrigSource,OrigSourceTest),OrigTargetTest);
00308 % Get all data in matrix.
00309 % svel = cell2mat(svel');
00310 % tvel = cell2mat(tvel');
00311 
00312 
00313 
00314 for ss=1:length(t)
00315     if(minframes+1<size(NewTargetTest{ss},1))
00316         tvel = diff(OrigTargetTest{ss});
00317         
00318         dimvel = size(tvel,2)/3;
00319         % Get the per frame vecloties.
00320         veldims = sqrt(tvel(:,1:dimvel).^2 + tvel(:,dimvel+1:2*dimvel,:).^2 + tvel(:,2*dimvel+1:3*dimvel,:).^2);
00321         
00322         % velmeans = mean(veldims,1);
00323         velmeans = veldims;
00324         velweights = repmat(velmeans,[1 3]);
00325         % velweights = velweights(:)';
00326         % velweights = velweights.^2;
00327         velweights = bsxfun(@rdivide,velweights,sum(velweights,2)); % Normalize to sum to 1.
00328         %         velweights = ones(size(velweights))/length(velweights);
00329         velweights = [ones(1,size(velweights,2))./size(velweights,2);velweights];
00330         
00331         
00332         % Distances of each corresponding point in 3D.
00333         errors3{ss} = sqrt(sum(bsxfun(@times,velweights,(tt{ss}-t{ss}).^2),2));
00334         nerrors3{ss} = sqrt(sum(bsxfun(@times,velweights(nonzero_cols{ss}),(tt{ss}(:,nonzero_cols{ss})-t{ss}(:,nonzero_cols{ss})).^2),2));
00335         %         fprintf('Avg Position RMS Velocity weighted error over sequence in aligned space %d: %f\n',ss,mean(nerrors3{ss}(minstartframes+1:end-endframes)));
00336         gooderrors3{ss} = errors3{ss}(minstartframes+1:end-endframes);
00337         ngooderrors3{ss} = nerrors3{ss}(minstartframes+1:end-endframes,:);
00338     end
00339 end
00340 % fprintf('POSITION ERROR (vel weighted): Sum of mean NON ZERO COLS , Mean per frame RMS Error: %f (USE THIS)\n',...
00341 %             mean(cell2mat(ngooderrors3(~cellfun(@isempty,ngooderrors3))')));
00342 velweightederror = mean(cell2mat(ngooderrors3(~cellfun(@isempty,ngooderrors3))'));
00343 fprintf('Weighted Velocity Error %f\n',velweightederror);
00344 
00345 
00346 %
00347 % for ss=1:length(t)
00348 %     if(minframes+1<size(NewTargetTest{ss},1))
00349 %         % Distances of each corresponding point in 3D.
00350 %         %         neweightrs = velweights(1
00351 %         dim1 = size(t{ss},2)/3;
00352 %         x = t{ss}(:,1:dim1);
00353 %         y = t{ss}(:,dim1+1:2*dim1);
00354 %         z = t{ss}(:,2*dim1+1:end);
00355 %
00356 %         dim1 = size(tt{ss},2)/3;
00357 %         xt = tt{ss}(:,1:dim1);
00358 %         yt = tt{ss}(:,dim1+1:2*dim1);
00359 %         zt = tt{ss}(:,2*dim1+1:end);
00360 %
00361 %
00362 %         d = sqrt((x-xt).^2 + (y-yt).^2 + (z-zt).^2); % Total distance of each marker in each frame.
00363 %
00364 %         d = bsxfun(@times,d,velweights(1:length(velweights)/3)); % Weight these distances
00365 %
00366 %         errors4{ss} = sum(d,2); % Sum the weighted distances of all 90 of them. (leaves one number per frame).
00367 %
00368 %         %         errors4{ss} = sqrt(sum(bsxfun(@times,velweights,sqrt(reshape((tt{ss}-t{ss}).^2),[size(t{ss},1) size(t{ss},2)/3 3]),2));
00369 %         %         nerrors4{ss} = sqrt(sum(bsxfun(@times,...
00370 %         %             velweights(nonzero_cols{ss}),...
00371 %         %             sqrt(reshape((tt{ss}(:,nonzero_cols{ss})-t{ss}(:,nonzero_cols{ss})).^2,[size(t{ss},1) size(t{ss},2)/3 3])),2));
00372 %         fprintf('Avg Position RMS Velocity weighted error over sequence in aligned space %d: %f\n',ss,mean(errors4{ss}(minstartframes+1:end-endframes)));
00373 %         gooderrors4{ss} = errors4{ss}(minstartframes+1:end-endframes);
00374 %         ngooderrors4{ss} = errors4{ss}(minstartframes+1:end-endframes,:);
00375 %     end
00376 % end
00377 % fprintf('POSITION ERROR (vel weighted): Sum of mean NON ZERO COLS , Mean per frame RMS Error: %f (USE THIS)\n',...
00378 %     mean(cell2mat(ngooderrors4(~cellfun(@isempty,ngooderrors4))')));
00379 % velweightederror = mean(cell2mat(ngooderrors4(~cellfun(@isempt/y,ngooderrors4))'));
00380 
00381 
00382 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
00383 
00384 
00385 
00386 
 All Classes Files Functions Variables Typedefs Friends Defines