%SCRIPT_BuildDetectionROC_Crops
%
%

clear all;
cd /cbcl/scratch01/bileschi/PrecomputedFeatures/StreetScenes/AppearanceDetector/
Root = '/cbcl/scratch01/bileschi/PrecomputedFeatures/StreetScenes/AppearanceDetector/';
BrianTempRoot = '/cbcl/scratch01/bileschi/Temp/';
objname{1} = 'car';
objname{2} = 'pedestrian';
objname{3} = 'bicycle';

sPARAMS.Nrounds = 250;
sPARAMS.R = 0;
sPARAMS.T = 0;
p = randperm(16384);
p = p(1:1536);
objs2do = 1:3;
for iObjname = objs2do
  on = objname{iObjname};
  grayXfull = [];
  toyotaXfull = [];
  brianXfull = [];
  yfull = [];
  d = dir(fullfile(Root,sprintf('AppearanceData_128x128_%s*.mat',on)));
  for iFl = 1:min(length(d),4);
    load(d(iFl).name);%-->grayX,toyotaX,y;
    grayXfull = [grayXfull,grayX(p,:)];
    toyotaXfull = [toyotaXfull,toyotaX];
    yfull = [yfull,y];
  end
  load(fullfile(BrianTempRoot,sprintf('temp_%s_BrianFeats.mat',on)));%-->X;
  brianXfull = X;, clear X;
  L = length(yfull);
  Test = 1:floor(L/3);
  Train = (floor(L/3)+1):L;
  grayXtest = grayXfull(:,Test);
  toyotaXtest = toyotaXfull(:,Test);
  brianXtest = brianXfull(:,Test);
  ytest{iObjname} = yfull(:,Test);
  grayXtrain = grayXfull(:,Train);
  toyotaXtrain = toyotaXfull(:,Train);
  brianXtrain = brianXfull(:,Train);
  ytrain = yfull(:,Train);
  Mgray = CLSgentleBoost(grayXtrain,ytrain',sPARAMS);
  Mtoyota = CLSgentleBoost(toyotaXtrain,ytrain',sPARAMS);
  Mbrian = CLSgentleBoost(brianXtrain,ytrain',sPARAMS);
  [yel_gray{iObjname},ye_gray{iObjname}] = CLSgentleBoostC(grayXtest,Mgray);
  [yel_toyota{iObjname},ye_toyota{iObjname}] = CLSgentleBoostC(toyotaXtest,Mtoyota);
  [yel_brian{iObjname},ye_brian{iObjname}] = CLSgentleBoostC(brianXtest,Mbrian);
  ROC_gray{iObjname} = ROCetc2(ye_gray{iObjname}',ytest{iObjname});
  ROC_toyota{iObjname} = ROCetc2(ye_toyota{iObjname}',ytest{iObjname});
  ROC_brian{iObjname} = ROCetc2(ye_brian{iObjname}',ytest{iObjname});

  for j = 1:5
     L2 = length(Train);
     nper = floor(L2/5);
     Train2{j} = Train(((j-1)*nper+1):((j)*nper));
     grayXtrain2{j} = grayXfull(:,Train2{j});
     toyotaXtrain2{j} = toyotaXfull(:,Train2{j});
     brianXtrain2{j} = brianXfull(:,Train2{j});
     ytrain2{j} = yfull(:,Train2{j});
     M2gray{j} = CLSgentleBoost( grayXtrain2{j},ytrain2{j}',sPARAMS);
     M2toyota{j} = CLSgentleBoost(toyotaXtrain2{j},ytrain2{j}',sPARAMS);
     M2brian{j} = CLSgentleBoost(brianXtrain2{j},ytrain2{j}',sPARAMS);
     [yel_gray2_p{iObjname}{j},ye_gray2_p{iObjname}{j}] = CLSgentleBoostC(grayXtest,M2gray{j});
     [yel_toyota2_p{iObjname}{j},ye_toyota2_p{iObjname}{j}] = CLSgentleBoostC(toyotaXtest,M2toyota{j});
     [yel_brian2_p{iObjname}{j},ye_brian2_p{iObjname}{j}] = CLSgentleBoostC(brianXtest,M2brian{j});
  end
  
  for j = 1:5
     ye_gray2{iObjname}(j,:) = ye_gray2_p{iObjname}{j};
     ye_toyota2{iObjname}(j,:) = ye_toyota2_p{iObjname}{j};
     ye_brian2{iObjname}(j,:) = ye_brian2_p{iObjname}{j};
  end
  ye_gray2{iObjname} = sum(ye_gray2{iObjname},1);
  ye_toyota2{iObjname} = sum(ye_toyota2{iObjname},1);
  ye_brian2{iObjname} = sum(ye_brian2{iObjname},1);
  ROC_gray2{iObjname} = ROCetc2(ye_gray2{iObjname}',ytest{iObjname});
  ROC_toyota2{iObjname} = ROCetc2(ye_toyota2{iObjname}',ytest{iObjname});
  ROC_brian2{iObjname} = ROCetc2(ye_brian2{iObjname}',ytest{iObjname});
    
end  

% save('C1_ObjectDetection.mat');
load('C1_ObjectDetection.mat');
load('ism_car_peds_bikes.mat');%-->ROC_ian

FigRoot = '/cbcl/scratch01/bileschi/TextureRec/Figures/RigidObjDet';
for iObjname = objs2do
 h = figure(iObjname+5);
 clf
 plot(ROC_toyota2{iObjname}.recall, ROC_toyota2{iObjname}.precision, 'r-','LineWidth',4,'Color',[0,.7,.7]);
 %plot(ROC_gray{iObjname}.recall, ROC_gray{iObjname}.precision, 'k--','LineWidth',2);
 hold on                                               
 plot(ROC_ian{iObjname}.recall, ROC_ian{iObjname}.precision, 'r-','LineWidth',1.5,'Color',[1 0 0]);
 plot(ROC_gray2{iObjname}.recall, ROC_gray2{iObjname}.precision, 'k--','LineWidth',4);
 %plot(ROC_toyota{iObjname}.recall, ROC_toyota{iObjname}.precision, 'r-','LineWidth',2,'Color',[0,.7,.7]);
 %plot(ROC_brian{iObjname}.recall, ROC_brian{iObjname}.precision, 'r-','LineWidth',2,'Color',[.7,.7,0]);
 plot(ROC_brian2{iObjname}.recall, ROC_brian2{iObjname}.precision, 'r-.','LineWidth',4,'Color',[.7,.7,0]);
 switch iObjname
   case 1
     loc = 'SouthWest';
   case 2
     loc = 'SouthWest';
   case 3
     loc = 'NorthEast';
 end
 L = legend('Standard Model (C1)','Part-Based System', 'Grayscale','Local Patch Correlation','Location',loc);
 set(L,'FontSize',18);
 T = xlabel('Recall');
 set(T,'FontSize',18);
 T = ylabel('Precision');
 set(T,'FontSize',18);
 T = title(sprintf('%s detection PR curve',objname{iObjname}));
 set(T,'FontSize',18);
 %saveas(h,fullfile(FigRoot,sprintf('RigidDet_PR_%s.fig',objname{iObjname})),'fig');
 %saveas(h,fullfile(FigRoot,sprintf('RigidDet_PR_%s.epsc',objname{iObjname})),'epsc');
 %saveas(h,fullfile(FigRoot,sprintf('RigidDet_PR_%s.jpg',objname{iObjname})),'jpg');
end


FigRoot = '/cbcl/scratch01/bileschi/TextureRec/Figures/RigidObjDet';

for iObjname = objs2do
 h = figure(iObjname+10 +5);
 clf
 hold on                                               
% ShowROC(ROC_toyota{iObjname}, 'r-',[0,.7,.7],2);
 ShowROC(ROC_toyota2{iObjname}, 'r-',[0,.7,.7],4);
 ShowROC(ROC_ian{iObjname}, 'r-',[1 0 0],1.5);
% ShowROC(ROC_gray{iObjname}, 'k--',[0 0 0],2);
 ShowROC(ROC_gray2{iObjname}, 'k--',[0 0 0],4);
% ShowROC(ROC_brian{iObjname}, 'r-.',[.7,.7,0],2);
 ShowROC(ROC_brian2{iObjname}, 'r-.',[.7,.7,0],4);
 switch iObjname
   case 1
     loc = 'SouthEast';
   case 2
     loc = 'SouthEast';
   case 3
     loc = 'SouthEast';
 end
 L = legend('Standard Model (C1)','Part-Based System', 'Grayscale','Local Patch Correlation','Location',loc);
 set(L,'FontSize',18);
 T = xlabel('False Positive Rate');
 set(T,'FontSize',18);
 T = ylabel('True Positive Rate');
 set(T,'FontSize',18);
 T = title(sprintf('%s detection ROC curve',objname{iObjname}));
 set(T,'FontSize',18);
 % saveas(h,fullfile(FigRoot,sprintf('RigidDet_ROC_%s.fig',objname{iObjname})),'fig');
 % saveas(h,fullfile(FigRoot,sprintf('RigidDet_ROC_%s.epsc',objname{iObjname})),'epsc');
 % saveas(h,fullfile(FigRoot,sprintf('RigidDet_ROC_%s.jpg',objname{iObjname})),'jpg');
 A(iObjname,1) = ROC_gray{iObjname}.auc;
 A(iObjname,2) = ROC_gray2{iObjname}.auc;
 A(iObjname,3) = ROC_toyota{iObjname}.auc;
 A(iObjname,4) = ROC_toyota2{iObjname}.auc;
 A(iObjname,5) = ROC_brian{iObjname}.auc;
 A(iObjname,6) = ROC_brian2{iObjname}.auc;
end



