%SCRIPT_BuildDetectionROC_Crops_AppearanceVSContext
%
%bileschi May 23 2005
%intent: context data has been extracted at all locations for which there was data in 
% AppearanceData_128x128_%s_f%.3d.mat.  We can directly compare in this situation the utility of these
% two modes of input.   Most of the data necessary for the full comparison is already saved in 
% 'C1_ObjectDetection.mat,' so we will keep this to prevent any re-work.

clear all;
load('C1_ObjectDetection.mat');

cd /cbcl/scratch01/bileschi/PrecomputedFeatures/StreetScenes/AppearanceDetector/
Root = '/cbcl/scratch01/bileschi/PrecomputedFeatures/StreetScenes/AppearanceDetector/';

objname{1} = 'car';
objname{2} = 'pedestrian';
objname{3} = 'bicycle';

sPARAMS.Nrounds = 50;
sPARAMS.R = 0;
sPARAMS.T = 0;

objs2do = 1:3;
for iObjname = objs2do
  on = objname{iObjname};
  ctxXfull = [];
  d_2 = dir(fullfile(Root,sprintf('ContextData_128x128_%s*.mat',on)));
  for iFl = 1:min(length(d),4);
    load(d_2(iFl).name);%-->ctxX,y;
    ctxXfull = [ctxXfull, ctxX];
  end
  Test = 1:floor(L/3);
  Train = (floor(L/3)+1):L;
  ctxXtest = ctxXfull(:,Test);
  ctxXtrain = ctxXfull(:,Train);
  Mctx = CLSgentleBoost(ctxXtrain,ytrain',sPARAMS);
  MctxOnC1 = CLSgentleBoost([ctxXtrain;toyotaXtrain],ytrain',sPARAMS);
  [yel_ctx{iObjname},ye_ctx{iObjname}] = CLSgentleBoostC(ctxXtest,Mctx);
  [yel_ctxOnC1{iObjname},ye_ctxOnC1{iObjname}] = CLSgentleBoostC([ctxXtest;toyotaXtest],MctxOnC1);
  ROC_ctx{iObjname} = ROCetc2(ye_ctx{iObjname}',ytest{iObjname});
  ROC_ctxOnC1{iObjname} = ROCetc2(ye_ctxOnC1{iObjname}',ytest{iObjname});

  for j = 1:5
     L2 = length(Train);
     nper = floor(L2/5);
     Train2{j} = Train(((j-1)*nper+1):((j)*nper));
     ctxXtrain2{j} = ctxXfull(:,Train2{j});
     M2ctx{j} = CLSgentleBoost(ctxXtrain2{j},ytrain2{j}',sPARAMS);
     M2ctxOnC1{j} = CLSgentleBoost([ctxXtrain2{j};toyotaXtrain2{j}],ytrain2{j}',sPARAMS);
     [yel_ctx2_p{iObjname}{j},ye_ctx2_p{iObjname}{j}] = CLSgentleBoostC(ctxXtest,M2ctx{j});
     [yel_ctxOnC12_p{iObjname}{j},ye_ctxOnC12_p{iObjname}{j}] = CLSgentleBoostC([ctxXtest;toyotaXtest],M2ctxOnC1{j});
  end
  for j = 1:5
     ye_ctx2{iObjname}(j,:) = ye_ctx2_p{iObjname}{j};
     ye_ctxOnC12{iObjname}(j,:) = ye_ctxOnC12_p{iObjname}{j};
  end
  ye_ctx2{iObjname} = sum(ye_ctx2{iObjname},1);
  ye_ctxOnC12{iObjname} = sum(ye_ctxOnC12{iObjname},1);
  ROC_ctx2{iObjname} = ROCetc2(ye_ctx2{iObjname}',ytest{iObjname});
  ROC_ctxOnC12{iObjname} = ROCetc2(ye_ctxOnC12{iObjname}',ytest{iObjname});
end  

save('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);
% ShowROC(ROC_ctx2{iObjname}, 'r-',[0,.5,0],2);
 ShowROC(ROC_ctx{iObjname}, 'r-',[0,.5,0],4);
% ShowROC(ROC_ctxOnC12{iObjname}, 'r-',[.5,0,0],2);
 ShowROC(ROC_ctxOnC1{iObjname}, 'r-',[.5,0,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','Context','C1 + Context','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



