Guest User

Untitled

a guest
Feb 23rd, 2018
98
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.91 KB | None | 0 0
  1. clear all; close all;
  2.  
  3. ground_truth_files = {'train_dataset/tool_video_01.txt', ...
  4. 'train_dataset/tool_video_02.txt' };
  5.  
  6. % load all the data
  7. allGT = [];
  8. allPred = [];
  9. for i = 1:length(ground_truth_files)
  10. ground_truth_file = ground_truth_files{i};
  11. pred_file = [ground_truth_file(1:end-4) '_pred.txt'];
  12.  
  13. [gt, toolNames] = ReadToolAnnotationFile(ground_truth_file);
  14. pred = ReadToolPredictionFile(pred_file);
  15.  
  16. if(size(gt, 1) ~= size(pred,1) || size(gt, 2) ~= size(pred,2))
  17. error(['ERROR:' ground_truth_file '\nGround truth and prediction have different sizes']);
  18. end
  19.  
  20. if(~isempty(find(gt(:,1) ~= pred(:,1))))
  21. error(['ERROR: ' ground_truth_file '\nThe frame index in ground truth and prediction is not equal']);
  22. end
  23.  
  24. allGT = [allGT; gt(:,2:end)];
  25. allPred = [allPred; pred(:,2:end)];
  26.  
  27. clear gt pred pred_file ground_truth_file;
  28. end
  29.  
  30. % compute average precision per tool
  31. ap = [];
  32. allPrec = [];
  33. allRec = [];
  34.  
  35. disp('========================================')
  36. disp('Average precision');
  37. disp('========================================')
  38. for iTool = 1:size(allGT,2)
  39. matScores = allPred(:,iTool);
  40. matGT = allGT(:,iTool);
  41.  
  42. % sanity check, making sure it is confidence values
  43. X = unique(matScores(:));
  44. if(length(X) == 2)
  45. disp('- WARNING: the computation of mAP requires confidence values');
  46. end
  47.  
  48. % NEW Script - less sensitive to confidence ranges
  49. maxScore = max(matScores);
  50. minScore = min(matScores);
  51. step = (double(maxScore)-double(minScore))/2000;
  52.  
  53. if(minScore == maxScore)
  54. error('no difference confidence values');
  55. end
  56.  
  57. prec = []; rec = [];
  58. for iScore = minScore:step:maxScore
  59. bufScore = matScores > iScore;
  60. tp = sum(double((bufScore == matGT) & (bufScore == 1)));
  61. fp = sum(double((bufScore ~= matGT) & (bufScore == 1)));
  62.  
  63. if(tp+fp ~= 0)
  64. rec(end+1) = tp/sum(matGT>0);
  65. prec(end+1) = tp/(tp+fp);
  66. end
  67. end
  68.  
  69. % % OLD Script
  70. % % compute precision/recall
  71. % [~,si]=sort(-matScores);
  72. % sortedMatScores = matScores(si);
  73. % % sortedMatScores = -sortedMatScores;
  74. % sortedMatGT = matGT(si);
  75. %
  76. % tp=sortedMatGT>0;
  77. % fp=sortedMatGT<=0;
  78. %
  79. % fp=cumsum(fp);
  80. % tp=cumsum(tp);
  81. % rec = tp/sum(sortedMatGT>0);
  82. % prec = tp./(fp+tp);
  83.  
  84. % compute average precision - finer way to compute AP
  85. ap(iTool)=0;
  86. for t=0:0.1:1
  87.  
  88. idx = [];
  89. threshold = 0.00001;
  90. while (isempty(idx))
  91. idx = find(abs(rec - t) < threshold);
  92. threshold = threshold * 2;
  93. end
  94. p = mean(prec(idx));
  95.  
  96. if isempty(p)
  97. p=0;
  98. end
  99. ap(iTool)=ap(iTool)+p/11;
  100. end
  101.  
  102. disp([toolNames{iTool} ': ' num2str(ap(iTool))]);
  103.  
  104. end
  105.  
  106. disp('----------------------------------------')
  107. disp(['All tools: ' num2str(mean(ap))])
  108. disp('----------------------------------------')
Add Comment
Please, Sign In to add comment