%SCRIPT_EzContextVersionTwoFigures
%
%intent: remember how I made the figures for this paper (do twice (cars and pedestrians))

load nightsave
objname = 'Car';
%load nightsavePed

%Figure: Context vs Apperance

FigRoot = '/cbcl/scratch01/bileschi/PrecomputedFeatures/StreetScenes/AppearanceDetector/Figures/ContextVsAppearance';
h = figure
ShowROC(App.stats,'b-',[0 0 1],4);
hold on
ShowROC(SVM.stats,'r-',[1 0 0 ],1);
ShowROC(Tru.stats,'r:',[1 0 0],1);
L = legend('Appearance','Emperical High-Level Context','True High-Level Context','Location','Best');
T = xlabel('False positive rate');
set(T,'FontSize',18);
T = ylabel('True positive rate');
set(T,'FontSize',18);
T = title(sprintf('%s detection ROC',objname));
set(T,'FontSize',20);
set(L,'FontSize',22);
saveas(h,fullfile(FigRoot,sprintf('AppVsCtx_%s.fig',objname)),'fig');
saveas(h,fullfile(FigRoot,sprintf('AppVsCtx_%s.epsc',objname)),'epsc');
saveas(h,fullfile(FigRoot,sprintf('AppVsCtx_%s.jpg',objname)),'jpg');

%Figure: Compare context features

FigRoot = '/cbcl/scratch01/bileschi/PrecomputedFeatures/StreetScenes/AppearanceDetector/Figures/RelativeContextFeatures';
h = figure
ShowROC(Astats,'k--',[0 0 0],3);
hold on
ShowROC(Bstats,'b--',[0 0 1],2);
ShowROC(Cstats,'r:',[1 0 0],1);
ShowROC(Dstats,'m-',[.8 0 .8],1);
plot([0 1],[0 1],'k-.');
L = legend('Global Position Features','Semantic Features','Color and Texture','Semantic + Color + Texture','Random Chance','Location','SouthEast');
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 context detection ROC with limited features',objname));
set(T,'FontSize',18);

saveas(h,fullfile(FigRoot,sprintf('CtxFeatures_%s.fig',objname)),'fig');
saveas(h,fullfile(FigRoot,sprintf('CtxFeatures_%s.epsc',objname)),'epsc');
saveas(h,fullfile(FigRoot,sprintf('CtxFeatures_%s.jpg',objname)),'jpg');

%Figure Near vs Far Features
%better ones in SCRIPT_FigureNearVsFarFeatures
FigRoot = '/cbcl/scratch01/bileschi/PrecomputedFeatures/StreetScenes/AppearanceDetector/Figures/RelativeContextDistance';
featdist = [3,5,10,15]
linestyle{1} = '-';
linestyle{2} = '--';
linestyle{3} = ':';
linestyle{4} = '-.';
h = figure
for i = 1:4
  ShowROC(SVMBstats{i},'-',[0 0 1], (5 - i));
  hold on
  ShowROC(SVMCstats{i},'-',[1 .8 .8],(5 - i));
  LegendNames{2*(i-1)+1} = sprintf('Semantic Features d > %d',featdist(i));
  LegendNames{2*(i-1)+2} = sprintf('Color and Texture d > %d',featdist(i));
end
legend(LegendNames,'Location','Best');
%legend('Semantic Features','Color and Texture','Location','Best');
xlabel('False positive rate');
ylabel('True positive rate');
title(sprintf('%s detection ROC: features d > threshold',objname));
saveas(h,fullfile(FigRoot,sprintf('FeatNearFar_%s.fig',objname)),'fig');
saveas(h,fullfile(FigRoot,sprintf('FeatNearFar_%s.epsc',objname)),'epsc');
saveas(h,fullfile(FigRoot,sprintf('FeatNearFar_%s.jpg',objname)),'jpg');
h = figure
for i = 5:8
  ShowROC(SVMBstats{i},'-',[0 0 1], (9 - i));
  hold on
  ShowROC(SVMCstats{i},'-',[1 .8 .8],(9 - i));
  LegendNames{2*(i-5)+1} = sprintf('Semantic Features d < %d',featdist(i-4));
  LegendNames{2*(i-5)+2} = sprintf('Color and Texture d < %d',featdist(i-4));
end
legend(LegendNames,'Location','Best');
%legend('Semantic Features','Color and Texture','Location','Best');
xlabel('False positive rate');
ylabel('True positive rate');
title(sprintf('%s detection ROC: features d < threshold',objname));




%Figure AUC vs Context Threshold

h = figure;
load('AUCs_ped');
load('AUCs_car');
ptiles = (5:5:95)/100;
plot(ptiles, AUCs_ped,'k-');
hold on
plot(ptiles, AUCs_car,'k:');
axis([0 .8 .9 1]);
xlabel('Context rejection threshold');
ylabel('Area under ROC curve');
title('Validation set context threshold tuning curve');
Legend('Pedestrian','Car','Location','Best');
saveas(h,fullfile(FigRoot,'AUC_vs_CtxThresh.fig'),'fig');
saveas(h,fullfile(FigRoot,'AUC_vs_CtxThresh.epsc'),'epsc');
saveas(h,fullfile(FigRoot,'AUC_vs_CtxThresh.jpg'),'jpg');

%Figure Context-Appearance Cascade
%h = figure;

%ShowROC(App.stats,'k-',[0 0 0],5);
%hold on
clear LegendNames;
LegendNames{1} = 'AppearanceDetector';

axis([0 1 0 1]);
ShowROC(App.stats,'k-',[0 0 0],5);
xlabel('False positive rate');
ylabel('True positive rate');
title('ROC of Context-Appearance Cascade');
legend(LegendNames,'Location','Best');
saveas(h,fullfile(FigRoot,sprintf('Cascade_Perf_%s.fig',objname)),'fig');
saveas(h,fullfile(FigRoot,sprintf('Cascade_Perf_%s.jpg',objname)),'jpg');
saveas(h,fullfile(FigRoot,sprintf('Cascade_Perf_%s.epsc',objname)),'epsc');
axis([0 .075 0 1]);
xlabel('False positive rate');
ylabel('True positive rate');
title('ROC Context-Appearance Cascade');
legend(LegendNames,'Location','Best');
saveas(h,fullfile(FigRoot,sprintf('Cascade_Perf_Zoom_%s.fig',objname)),'fig');
saveas(h,fullfile(FigRoot,sprintf('Cascade_Perf_Zoom_%s.jpg',objname)),'jpg');
saveas(h,fullfile(FigRoot,sprintf('Cascade_Perf_Zoom_%s.epsc',objname)),'epsc');


%------------------
%Figure: Area under curve for different cascade first thresholds
FigRoot = '/cbcl/scratch01/bileschi/PrecomputedFeatures/StreetScenes/AppearanceDetector/Figures/ValidationTuning';
load AUCs_Cascade_Car
load AUCs_Cascade_Pedestrian
load AUCs_Cascade_Bicycle
xvals = (0:2:90) / 100.0;
h = figure;
plot(xvals,AUCs_Cascade_Car,'k:','LineWidth',3);
hold on
plot(xvals,AUCs_Cascade_Pedestrian,'b-','LineWidth',3);
plot(xvals,AUCs_Cascade_Bicycle,'r--','LineWidth',3);
[M,Mi] = max(AUCs_Cascade_Car);
Z = AUCs_Cascade_Car(1);
plot([0 1],[ Z Z],'k:','Color',[.5 .5 .5]) 
plot(xvals(Mi),M,'ko','MarkerSize',12);
plot(xvals(Mi),M,'k+','MarkerSize',12);
L = legend('Car','Pedestrian','Bicycle','Performance without context','Location','NorthEast');
set(L,'FontSize',18);

[M,Mi] = max(AUCs_Cascade_Pedestrian);
Z = AUCs_Cascade_Pedestrian(1);
plot([0 1],[ Z Z],'b:','Color',[.5 .5 .5]) 
plot(xvals(Mi),M,'bo','MarkerSize',12);
plot(xvals(Mi),M,'b+','MarkerSize',12);
[M,Mi] = max(AUCs_Cascade_Bicycle);
Z = AUCs_Cascade_Bicycle(1);
plot([0 1],[ Z Z],'r:','Color',[.5 .5 .5]) 
plot(xvals(Mi),M,'ro','MarkerSize',12);
plot(xvals(Mi),M,'r+','MarkerSize',12);
T = xlabel('Context Rejection Threshold');
set(T,'FontSize',18);
T = ylabel('Area under ROC');
set(T,'FontSize',18);
T = title('Tuning context threshold for classifier cascade');
set(T,'FontSize',18);
axis([0 .8 .9 1]);
saveas(h,fullfile(FigRoot,'ValidationTuning.fig'),'fig');
saveas(h,fullfile(FigRoot,'ValidationTuning.jpg'),'jpg');
saveas(h,fullfile(FigRoot,'ValidationTuning.epsc'),'epsc');

%------------------
%Figure: Area under curve for different cascade first thresholds
FigRoot = '/cbcl/scratch01/bileschi/PrecomputedFeatures/StreetScenes/AppearanceDetector/Figures/BestCascade';
h = figure
ShowROC(App.stats,'b-',[0 0 1],4);
hold on
ShowROC(BestCascade,'r--',[1 0 0],3);
L = legend('Appearance','Context-Appearance Cascade','Location','Best');
set(L,'FontSize',22);
T = xlabel('False positive rate');
set(T,'FontSize',18);
T = ylabel('True positive rate');
set(T,'FontSize',18);
T = title(sprintf('%s detection ROC',objname));
set(T,'FontSize',20);
saveas(h,fullfile(FigRoot,sprintf('CascadeImprovement_%s.fig',objname)),'fig');
saveas(h,fullfile(FigRoot,sprintf('CascadeImprovement_%s.epsc',objname)),'epsc');
saveas(h,fullfile(FigRoot,sprintf('CascadeImprovement_%s.jpg',objname)),'jpg');


%-------------------
%:Figure:  Distribution of App and Context
FigRoot = '/cbcl/scratch01/bileschi/PrecomputedFeatures/StreetScenes/AppearanceDetector/Figures/Distribution';
h = figure;
yz = y(TTS.Test);
pl = plot(App.yemp(find(yz == -1)),SVM.yemp(find(yz == -1))+1,'o');
set(pl,'Color',[1 .7 .7]);
set(pl,'MarkerSize',3);
hold on
pl = plot(App.yemp(find(yz == 1)),SVM.yemp(find(yz == 1))+1,'+');
set(pl,'Color',[0 0 .3]);
set(pl,'MarkerSize',5);
T = xlabel('Appearance Score');
set(T,'FontSize',18);
T = ylabel('Context Score');
set(T,'FontSize',18);
T = title('Car data distribution');
set(T,'FontSize',18);
plot([0 0],[-0.08,+0.06],'k--');
plot([-20 15],[-.01 -.01],'k-');
axis([-15 10 -0.06 0.04])
saveas(h,fullfile(FigRoot,'CarDistribution.fig'),'fig');
saveas(h,fullfile(FigRoot,'CarDistribution.epsc'),'epsc');
saveas(h,fullfile(FigRoot,'CarDistribution.jpg'),'jpg');



