%%SCRIPT_CompareAppearanceAndContextPed
%
%intent:  Using the extracted Context and Appearance data, compare to see
%  performance difference.  Also compare true semantic vs estimated semantic.

load SobelAppearanceTrainData6_pedestrian;%-->X,y,ExtractedLocations...
AppearanceX = X;
Appearancey = y;
load TruSemXPed%-->TruSemX
TrueContextX = TruSemX;
[TrueContextX,Ntru] = outnormalizeDataX(TrueContextX);
TrueContexty = y;
load LinSVMSemXPed%-->LinSVMSemX
LinSVMContextX = LinSVMSemX;
[LinSVMContextX,Nsvm] = outnormalizeDataX(LinSVMSemX);
LinSVMContexty = y;

clear X y;

TTS = MakeTTS(length(Appearancey),floor(.25 * length(Appearancey)));
save('TTS_ped_25.mat','TTS');

%ModelAppearance = CLSosusvm(AppearanceX(:,TTS.Train),Appearancey(TTS.Train));
%ModelLinSVMContext = CLSosusvm(LinSVMContextX(:,TTS.Train),LinSVMContexty(TTS.Train));
%ModelTrueContext = CLSosusvm(TrueContextX(:,TTS.Train),TrueContexty(TTS.Train));
%[App.yempl,App.yemp] = CLSosusvmC(AppearanceX(:,TTS.Test),ModelAppearance); 
%[SVM.yempl,SVM.yemp] = CLSosusvmC(LinSVMContextX(:,TTS.Test),ModelLinSVMContext); 
%[Tru.yempl,Tru.yemp] = CLSosusvmC(TrueContextX(:,TTS.Test),ModelTrueContext); 
ModelAppearance = CLSgentleBoost(AppearanceX(:,TTS.Train),Appearancey(TTS.Train));
ModelLinSVMContext = CLSgentleBoost(LinSVMContextX(:,TTS.Train),LinSVMContexty(TTS.Train));
ModelTrueContext = CLSgentleBoost(TrueContextX(:,TTS.Train),TrueContexty(TTS.Train));
[App.yempl,App.yemp] = CLSgentleBoostC(AppearanceX(:,TTS.Test),ModelAppearance); 
[SVM.yempl,SVM.yemp] = CLSgentleBoostC(LinSVMContextX(:,TTS.Test),ModelLinSVMContext); 
[Tru.yempl,Tru.yemp] = CLSgentleBoostC(TrueContextX(:,TTS.Test),ModelTrueContext); 

App.EER = length(find(App.yempl ~= Appearancey(TTS.Test)))/length(TTS.Test);
SVM.EER = length(find(SVM.yempl ~= LinSVMContexty(TTS.Test)))/length(TTS.Test);
Tru.EER = length(find(Tru.yempl ~= TrueContexty(TTS.Test)))/length(TTS.Test);
App.stats = ROCetc2(App.yemp, Appearancey(TTS.Test));
SVM.stats = ROCetc2(SVM.yemp, LinSVMContexty(TTS.Test));
Tru.stats = ROCetc2(Tru.yemp, TrueContexty(TTS.Test));
figure
ShowROCPlus(App.stats,'b-');
hold on
ShowROCPlus(SVM.stats,'r-');
ShowROCPlus(Tru.stats,'k-');
if(length(unique(App.yempl)) < 3)
  Csc.yemp = App.yemp;
else
  Csc.yemp = App.yempl;
end
if(length(unique(SVM.yempl)) < 3)
  Csc.yemp(find(SVM.yemp < median(SVM.yemp))) = -inf;
else
  Csc.yemp(find(SVM.yempl < median(SVM.yempl))) = -inf;
end
Csc.stats = ROCetc2(Csc.yemp, TrueContexty(TTS.Test));
ShowROCPlus(Csc.stats,'b:');
save nightsavePed;


%-------------------
%Experiment: Feature Subsets
d.bRemoveSky = 0;
d.bRemoveColTex = 1;
d.bRemoveBTRS = 1;
d.bRemovePosition = 0;
d.bRemoveNearby = 1;
FSlistA = FeatureSelection_BuildFrom800(d);
d.bRemoveBTRS = 0;
d.bRemovePosition = 0; %%%1
FSlistB = FeatureSelection_BuildFrom800(d);
d.bRemoveBTRS = 1;
d.bRemoveColTex = 0;
FSlistC = FeatureSelection_BuildFrom800(d);
d.bRemoveBTRS = 0;
FSlistD = FeatureSelection_BuildFrom800(d);

%XA = LinSVMContextX(find(FSlistA),:);
%XB = LinSVMContextX(find(FSlistB),:);
%XC = LinSVMContextX(find(FSlistC),:);
%XD = LinSVMContextX(find(FSlistD),:);

XA = TrueContextX(find(FSlistA),:);
XB = TrueContextX(find(FSlistB),:);
XC = TrueContextX(find(FSlistC),:);
XD = TrueContextX(find(FSlistD),:);

sParams.Nrounds = 100;
sParams.R = 0;
sParams.T = 0;

TTS.Train = TTS.Train(1:5000);
TTS.Train = TTS.Test(1:5000);


%ModelA = CLSosusvm(XA(:,TTS.Train),Appearancey(TTS.Train));
ModelF = CLSosusvm(XB(:,TTS.Train),Appearancey(TTS.Train));
ModelG = CLSosusvm(XC(:,TTS.Train),Appearancey(TTS.Train));
%ModelD = CLSosusvm(XD(:,TTS.Train),Appearancey(TTS.Train));
%[yemplA,yempA] = CLSosusvmC(XA(:,TTS.Test),ModelA); 
[yemplF,yempF] = CLSosusvmC(XB(:,TTS.Test),ModelF); 
[yemplG,yempG] = CLSosusvmC(XC(:,TTS.Test),ModelG); 
%[yemplD,yempD] = CLSosusvmC(XD(:,TTS.Test),ModelD); 
ModelA = CLSgentleBoost(XA(:,TTS.Train),Appearancey(TTS.Train),sParams);
ModelB = CLSgentleBoost(XB(:,TTS.Train),Appearancey(TTS.Train),sParams);
ModelC = CLSgentleBoost(XC(:,TTS.Train),Appearancey(TTS.Train),sParams);
ModelD = CLSgentleBoost(XD(:,TTS.Train),Appearancey(TTS.Train),sParams);
[yemplA,yempA] = CLSgentleBoostC(XA(:,TTS.Test),ModelA); 
[yemplB,yempB] = CLSgentleBoostC(XB(:,TTS.Test),ModelB); 
[yemplC,yempC] = CLSgentleBoostC(XC(:,TTS.Test),ModelC); 
[yemplD,yempD] = CLSgentleBoostC(XD(:,TTS.Test),ModelD); 



Astats = ROCetc2(yempA, Appearancey(TTS.Test));
Bstats = ROCetc2(yempB, Appearancey(TTS.Test));
Cstats = ROCetc2(yempC, Appearancey(TTS.Test));
Dstats = ROCetc2(yempD, Appearancey(TTS.Test));

Fstats = ROCetc2(yempF, Appearancey(TTS.Test));
Gstats = ROCetc2(yempG, Appearancey(TTS.Test));

figure
ShowROC(Astats,'k-');
hold on
ShowROC(Bstats,'b-');
ShowROC(Cstats,'r-');
%ShowROC(Dstats,'k-');
ShowROC(Fstats,'b:');
ShowROC(Gstats,'r:');

%-------------------
%Experiment: Near vs Far
clear d;
d.bRemoveSky = 0;
d.bRemovePosition = 1;
d.bRemoveNearby = 1;
FarMasks{1} = [1 0 0 0 0];
FarMasks{2} = [1 1 0 0 0];
FarMasks{3} = [1 1 1 0 0];
FarMasks{4} = [1 1 1 1 0];
FarMasks{5} = [0 1 1 1 1];
FarMasks{6} = [0 0 1 1 1];
FarMasks{7} = [0 0 0 1 1];
FarMasks{8} = [0 0 0 0 1];
%for i = 1:8
 for i = 5:8
  d.bRemoveNearbyMask = FarMasks{i}
  d.bRemoveBTRS = 0;
  d.bRemoveColTex = 1;
  FSlistFarB = FeatureSelection_BuildFrom800(d);
  d.bRemoveBTRS = 1;
  d.bRemoveColTex = 0;
  FSlistFarC = FeatureSelection_BuildFrom800(d);
  XFarSVMB = LinSVMContextX(find(FSlistFarB),:);
  XFarSVMC = LinSVMContextX(find(FSlistFarC),:);
  XFarTruB = TrueContextX(find(FSlistFarB),:);
%  ModelSVMB = CLSosusvm(XFarSVMB(:,TTS.Train),Appearancey(TTS.Train));
%  ModelSVMC = CLSosusvm(XFarSVMC(:,TTS.Train),Appearancey(TTS.Train));
%  ModelTruB = CLSosusvm(XFarTruB(:,TTS.Train),Appearancey(TTS.Train));
%  [yemplSVMB,yempSVMB{i}] = CLSosusvmC(XFarSVMB(:,TTS.Test),ModelSVMB); 
%  [yemplSVMC,yempSVMC{i}] = CLSosusvmC(XFarSVMC(:,TTS.Test),ModelSVMC); 
%  [yemplTruB,yempTruB{i}] = CLSosusvmC(XFarTruB(:,TTS.Test),ModelTruB); 
  
  ModelSVMB = CLSgentleBoost(XFarSVMB(:,TTS.Train),Appearancey(TTS.Train));
  ModelSVMC = CLSgentleBoost(XFarSVMC(:,TTS.Train),Appearancey(TTS.Train));
  ModelTruB = CLSgentleBoost(XFarTruB(:,TTS.Train),Appearancey(TTS.Train));
  [yemplSVMB,yempSVMB{i}] = CLSgentleBoostC(XFarSVMB(:,TTS.Test),ModelSVMB); 
  [yemplSVMC,yempSVMC{i}] = CLSgentleBoostC(XFarSVMC(:,TTS.Test),ModelSVMC); 
  [yemplTruB,yempTruB{i}] = CLSgentleBoostC(XFarTruB(:,TTS.Test),ModelTruB); 

  SVMBstats{i} = ROCetc2(yempSVMB{i}, Appearancey(TTS.Test));
  SVMCstats{i} = ROCetc2(yempSVMC{i}, Appearancey(TTS.Test));
  TruBstats{i} = ROCetc2(yempTruB{i}, Appearancey(TTS.Test));
  
  %figure
  %ShowROCPlus(SVMBstats{i},'r-');
  %hold on
  %ShowROCPlus(SVMCstats{i},'b-');
  %ShowROCPlus(TruBstats{i},'k-');
end

%------------------
%Experiment: Context-Appearance Rejection Cascade
ptiles = (0:2:90) / 100.0;
clear AUCs_Cascade
if(length(unique(App.yempl)) < 3)
  Csc.yemp = App.yemp;
else
  Csc.yemp = App.yempl;
end
for i = 1:length(ptiles)
   rgb = hsv2rgb([(i+1)/(length(ptiles)+1),1,.85]);
   if(length(unique(SVM.yempl)) < 3)
     CascadeStats{i} = BuildDetectorCascade(SVM.yemp, Csc.yemp,TrueContexty(TTS.Test),ptiles(i));
   else
     CascadeStats{i} = BuildDetectorCascade(SVM.yempl, Csc.yemp,TrueContexty(TTS.Test),ptiles(i));
   end
   LegendNames{i+1} = sprintf('Context rejection threshold = %.2f',ptiles(i));
   hold on
   ShowROC(CascadeStats{i},'k-',rgb,1.3);
   AUCs_Cascade(i) = CascadeStats{i}.auc;
end
BestCascade = BestTPOfROCFamily(CascadeStats);

%---------------------
%Record a context classifier with no low level info
d.bRemoveSky = 0;
d.bRemoveColTex = 1;
d.bRemoveBTRS = 0;
d.bRemovePosition = 0;
d.bRemoveNearby = 0;
FSlist = FeatureSelection_BuildFrom800(d);
X = LinSVMContextX(find(FSlist),:);
Model = CLSgentleBoost(XFarSVMB(:,TTS.Train),Appearancey(TTS.Train));
cmd = sprintf('ModelLinSVMContext_%s_noColTex = Model;',objname);
eval(cmd);
cmd = sprintf('save(''ModelLinSVMContext_%s_noColTex'',''ModelLinSVMContext_%s_noColTex'');',objname,objname);
eval(cmd);

