Advertisement
Guest User

Untitled

a guest
Jun 25th, 2017
63
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 7.54 KB | None | 0 0
  1. function [ error, falseNeg, falsePos ] = UseBoostedFeatures
  2.  
  3. Faces = ReadBmps('G:\Download\project2\Face16');
  4. faceSize = size(Faces, 1);
  5.  
  6. Nonfaces = ReadBmps('G:\Download\project2\Nonface16');
  7. nonFaceSize = size(Nonfaces, 1);
  8.  
  9. Results = zeros(200, 3);
  10.  
  11. %test start here
  12.  
  13. for w=2:200
  14.  
  15. resultFromAdaboost = zeros(8, w); %change this for how many boosted youre testing
  16. resultFromAdaboost = dlmread(strcat('selectedFeaturesBoosted', num2str(w), '.txt'));
  17.  
  18. halfSumTotalWeight = 0;
  19.  
  20. for y=1:size(resultFromAdaboost, 2);
  21. beta = resultFromAdaboost(8, y) / (1 - resultFromAdaboost(8, y));
  22. alpha = log10(1/beta);
  23. halfSumTotalWeight = halfSumTotalWeight + alpha;
  24. end
  25.  
  26. halfSumTotalWeight = .5 * halfSumTotalWeight;
  27.  
  28. falseNeg = 0;
  29. falsePos = 0;
  30. totalError = 0;
  31.  
  32. for faceImage=1:faceSize
  33. accumulation = 0;
  34. for l=1:size(resultFromAdaboost, 2);
  35. classification = 0;
  36. i = resultFromAdaboost(1, l);
  37. j = resultFromAdaboost(2, l);
  38. rows = resultFromAdaboost(3, l);
  39. columns = resultFromAdaboost(4, l);
  40. threshold = resultFromAdaboost(5, l);
  41. type = resultFromAdaboost(7, l);
  42.  
  43. if(type == 1)
  44. upperLeft = IntegralSum(reshape(Faces(faceImage,:,:), 16, 16), i, j, i+(rows/2)-1, j+(columns/2)-1);
  45. upperRight = IntegralSum(reshape(Faces(faceImage,:,:), 16, 16), i, j+(columns/2), i+(rows/2)-1, j+(columns)-1);
  46. lowerRight = IntegralSum(reshape(Faces(faceImage,:,:), 16, 16), i+(rows/2), j+(columns/2), i+(rows)-1, j+(columns)-1);
  47. lowerLeft = IntegralSum(reshape(Faces(faceImage,:,:), 16, 16), i+(rows/2), j, i+(rows)-1, j+(columns/2)-1);
  48. Result = upperRight + lowerLeft - upperLeft - lowerRight;
  49.  
  50. if Result >= threshold %correct
  51. classification = 1;
  52. else
  53. classification = 0;
  54. end
  55. elseif(type == 2)
  56. top = IntegralSum(reshape(Faces(faceImage,:,:), 16, 16), i, j, i+(rows/2)-1, j+(columns)-1);
  57. bottom = IntegralSum(reshape(Faces(faceImage,:,:), 16, 16), i+(rows/2), j, i+(rows)-1, j+(columns)-1);
  58. Result = top - bottom;
  59.  
  60. if Result >= threshold %correct
  61. classification = 1;
  62. else
  63. classification = 0;
  64. end
  65. elseif(type == 3)
  66. left = IntegralSum(reshape(Faces(faceImage,:,:), 16, 16), i, j, i+(rows)-1, j+(columns/2)-1);
  67. right = IntegralSum(reshape(Faces(faceImage,:,:), 16, 16), i, j+(columns/2), i+(rows)-1, j+(columns)-1);
  68. Result = right - left;
  69.  
  70. if Result >= threshold %correct
  71. classification = 1;
  72. else
  73. classification = 0;
  74. end
  75. elseif(type == 4)
  76. leftThird = IntegralSum(reshape(Faces(faceImage,:,:), 16, 16), i, j, i+(rows)-1, j+(columns/3)-1);
  77. middleThird = IntegralSum(reshape(Faces(faceImage,:,:), 16, 16), i, j+(columns/3), i+(rows)-1, j+(2*columns/3)-1);
  78. rightThird = IntegralSum(reshape(Faces(faceImage,:,:), 16, 16), i, j+(2*columns/3), i+(rows)-1, j+(columns)-1);
  79. Result = middleThird - leftThird - rightThird;
  80.  
  81. if Result >= threshold %correct
  82. classification = 1;
  83. else
  84. classification = 0;
  85. end
  86. end
  87.  
  88. if(classification == 1)
  89. beta = resultFromAdaboost(8, l) / (1 - resultFromAdaboost(8, l));
  90. alpha = log10(1/beta);
  91. accumulation = accumulation + alpha;
  92. end
  93.  
  94. end
  95.  
  96. if accumulation >= halfSumTotalWeight %correct classification
  97. else
  98. falseNeg = falseNeg + 1;
  99. totalError = totalError + 1;
  100. end
  101.  
  102. end
  103.  
  104. for nonFaceImage=1:nonFaceSize
  105. accumulation = 0;
  106. for l=1:size(resultFromAdaboost, 2);
  107. classification = 0;
  108. i = resultFromAdaboost(1, l);
  109. j = resultFromAdaboost(2, l);
  110. rows = resultFromAdaboost(3, l);
  111. columns = resultFromAdaboost(4, l);
  112. threshold = resultFromAdaboost(5, l);
  113. type = resultFromAdaboost(7, l);
  114.  
  115. if(type == 1)
  116. upperLeft = IntegralSum(reshape(Nonfaces(nonFaceImage,:,:), 16, 16), i, j, i+(rows/2)-1, j+(columns/2)-1);
  117. upperRight = IntegralSum(reshape(Nonfaces(nonFaceImage,:,:), 16, 16), i, j+(columns/2), i+(rows/2)-1, j+(columns)-1);
  118. lowerRight = IntegralSum(reshape(Nonfaces(nonFaceImage,:,:), 16, 16), i+(rows/2), j+(columns/2), i+(rows)-1, j+(columns)-1);
  119. lowerLeft = IntegralSum(reshape(Nonfaces(nonFaceImage,:,:), 16, 16), i+(rows/2), j, i+(rows)-1, j+(columns/2)-1);
  120. Result = upperRight + lowerLeft - upperLeft - lowerRight;
  121.  
  122. if Result >= threshold %incorrect
  123. classification = 1;
  124. else
  125. classification = 0;
  126. end
  127. elseif(type == 2)
  128. top = IntegralSum(reshape(Nonfaces(nonFaceImage,:,:), 16, 16), i, j, i+(rows/2)-1, j+(columns)-1);
  129. bottom = IntegralSum(reshape(Nonfaces(nonFaceImage,:,:), 16, 16), i+(rows/2), j, i+(rows)-1, j+(columns)-1);
  130. Result = top - bottom;
  131.  
  132. if Result >= threshold %incorrect
  133. classification = 1;
  134. else
  135. classification = 0;
  136. end
  137. elseif(type == 3)
  138. left = IntegralSum(reshape(Nonfaces(nonFaceImage,:,:), 16, 16), i, j, i+(rows)-1, j+(columns/2)-1);
  139. right = IntegralSum(reshape(Nonfaces(nonFaceImage,:,:), 16, 16), i, j+(columns/2), i+(rows)-1, j+(columns)-1);
  140. Result = right - left;
  141.  
  142. if Result >= threshold %incorrect for non face
  143. classification = 1;
  144. else
  145. classification = 0;
  146. end
  147. elseif(type == 4)
  148. leftThird = IntegralSum(reshape(Nonfaces(nonFaceImage,:,:), 16, 16), i, j, i+(rows)-1, j+(columns/3)-1);
  149. middleThird = IntegralSum(reshape(Nonfaces(nonFaceImage,:,:), 16, 16), i, j+(columns/3), i+(rows)-1, j+(2*columns/3)-1);
  150. rightThird = IntegralSum(reshape(Nonfaces(nonFaceImage,:,:), 16, 16), i, j+(2*columns/3), i+(rows)-1, j+(columns)-1);
  151. Result = middleThird - leftThird - rightThird;
  152.  
  153. if Result >= threshold %incorrect
  154. classification = 1;
  155. else
  156. classification = 0;
  157. end
  158. end
  159.  
  160. if(classification == 1)
  161. beta = resultFromAdaboost(8, l) / (1 - resultFromAdaboost(8, l));
  162. alpha = log10(1/beta);
  163. accumulation = accumulation + alpha;
  164. end
  165.  
  166. end
  167.  
  168. if accumulation >= halfSumTotalWeight
  169. falsePos = falsePos + 1;
  170. totalError = totalError + 1;
  171. else
  172.  
  173. end
  174.  
  175. end
  176.  
  177. error = totalError / 15175;
  178.  
  179. sprintf('%f %f %f', error, falseNeg, falsePos)
  180. Results(w, 1) = error;
  181. Results(w, 2) = falseNeg;
  182. Results(w, 3) = falsePos;
  183.  
  184. end
  185.  
  186. dlmwrite('trainingErrors.txt', Results);
  187.  
  188. end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement