Package pycv :: Package cs :: Package stats :: Package gaussian :: Module gaussian
[hide private]
[frames] | no frames]

Source Code for Module pycv.cs.stats.gaussian.gaussian

  1  # PyCV - A Computer Vision Package for Python Incorporating Fast Training of Face Detection 
  2   
  3  # Copyright 2007 Nanyang Technological University, Singapore. 
  4  # Authors: Minh-Tri Pham, Viet-Dung D. Hoang, and Tat-Jen Cham. 
  5   
  6  # This file is part of PyCV. 
  7   
  8  # PyCV is free software: you can redistribute it and/or modify 
  9  # it under the terms of the GNU General Public  
 10  # License as published by the Free Software Foundation, either version  
 11  # 3 of the License, or (at your option) any later version. 
 12   
 13  # PyCV is distributed in the hope that it will be useful, 
 14  # but WITHOUT ANY WARRANTY; without even the implied warranty of 
 15  # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the 
 16  # GNU General Public License for more details. 
 17   
 18  # You should have received a copy of the GNU General Public License 
 19  # along with this program.  If not, see <http://www.gnu.org/licenses/>. 
 20   
 21  # --------------------------------------------------------------------- 
 22  #!/usr/bin/env python 
 23   
 24   
 25  __all__ = ['find_classification_threshold','find_filtering_threshold', 
 26      'find_filtering_threshold2'] 
 27   
 28  from numpy import zeros 
 29   
 30  from pycv.cs.stats import Stats2e, stdnorm 
 31  from pycv.ext import gaussian_find_classification_threshold 
 32   
 33  #------------------------------------------------------------------------------- 
 34  # Subroutines dealing with class-conditional Gaussian ditributions. 
 35  #------------------------------------------------------------------------------- 
 36  # All these subroutines assume we have two random variables x+ and x- 
 37  # Gaussian-distributed. The statistics are stored in a Stats2e. 
 38  # A thresholded classifier is of this form: 
 39  #   if x >= b then return +1 
 40  #   else return -1 
 41  # A probabilistic classifier is of this form (MAP): 
 42  #   if p( x positive | x ) > p ( x negative | x) then return +1 
 43  #   else return -1 
 44  #------------------------------------------------------------------------------- 
 45   
46 -def find_classification_threshold(stats2e, thelambda = 1.0):
47 """Compute the estimated error. Here we penalize false negative by sqrt(thelambda) 48 and false positive by 1/sqrt(thelambda). 49 50 Input: 51 stats2e: per-element statistics of 2*N classes 52 thelambda: the value of 'lambda' 53 Output: 54 b: a numpy.array of shape (N,2) to hold the results where 55 b[i][0]: a threshold minimizing w.r.t. b 56 f(b) = (1/sqrt(thelambda) * w[i*2] * sf((b-mu[i*2])/sigma[i*2]) + 57 sqrt(thelambda) * w[i*2+1] * cdf((b-mu[i*2+1])/sigma[i*2+1])) 58 / (w[i*2]+w[i*2+1]) 59 where sf(.) and cdf(.) are w.r.t. the normalized gaussian distribution 60 b[i][1] = f(b[i][0]), the estimated error 61 Requirement: 62 mu[i*2] <= mu[i*2+1] for all i = 0..N-1 63 """ 64 tprint("Warning, this function is now obsolete. Please use sdGTSolve() instead") 65 N = stats2e.J / 2 66 A = stats2e.A 67 b = zeros([N,2]) 68 ld = float(thelambda) 69 gaussian_find_classification_threshold(N,A,b,ld) 70 return b
71
72 -def find_filtering_threshold(stats2e, minDR = 0.002):
73 """Use the thresholded classifier as above. If we wish detection rate >= minDR, what is the largest threshold? 74 75 Input: 76 stats2e: per-element statistics of 2*N classes 77 minDR: the value of 'minDR' 78 Output: 79 b: a numpy.array of shape (N,2) to hold the results where 80 b[i][0]: a threshold that 81 minimize FAR(b) = sf((b-mu[i*2])/sigma[i*2]) 82 w.r.t. b 83 subject to DR(b) = sf((b-mu[i*2+1])/sigma[i*2+1]) >= minDR 84 where sf(.) is w.r.t. the normalized gaussian distribution 85 b[i][1] = FAR(b[i][0]), the estimated false acceptance rate 86 Requirement: 87 mu[i*2] <= mu[i*2+1] for all i = 0..N-1 88 """ 89 tprint("Warning, this function is now obsolete. Please use sdGTSolve() instead") 90 b0 = stdnorm.isf(minDR) 91 N = stats2e.J / 2 92 A = stats2e.A.reshape(N,6) 93 b = zeros([N,2]) 94 b[:,0] = A[:,5]*b0 + A[:,4] 95 b[:,1] = stdnorm.sf((b[:,0]-A[:,1])/A[:,2]) 96 return b
97
98 -def find_filtering_threshold2(stats2e, maxFAR = 0.002):
99 """Use the thresholded classifier as above. If we wish false acceptance rate <= maxFAR, what is the smallest threshold? 100 101 Input: 102 stats2e: per-element statistics of 2*N classes 103 maxFAR: the value of 'maxFAR' 104 Output: 105 b: a numpy.array of shape (N,2) to hold the results where 106 b[i][0]: a threshold that 107 maximize DR(b) = sf((b-mu[i*2+1])/sigma[i*2+1]) 108 w.r.t. b 109 subject to FAR(b) = sf((b-mu[i*2])/sigma[i*2]) <= maxFAR 110 where sf(.) is w.r.t. the normalized gaussian distribution 111 b[i][1] = 1-DR(b[i][0]), the estimated false rejection rate 112 Requirement: 113 mu[i*2] <= mu[i*2+1] for all i = 0..N-1 114 """ 115 tprint("Warning, this function is now obsolete. Please use sdGTSolve() instead") 116 b0 = stdnorm.isf(maxFAR) 117 N = stats2e.J / 2 118 A = stats2e.A.reshape(N,6) 119 b = zeros([N,2]) 120 b[:,0] = A[:,2]*b0 + A[:,1] 121 b[:,1] = stdnorm.cdf((b[:,0]-A[:,4])/A[:,5]) 122 return b
123
124 -def main(): # seems ok for now
125 class Test: 126 pass 127 128 a = zeros([4,3]) 129 a[0,0] = 1 130 a[0,1] = 1 131 a[0,2] = 1 132 a[1,0] = 1 133 a[1,1] = 2 134 a[1,2] = 1 135 a[2,0] = 10 136 a[2,1] = 10 137 a[2,2] = 10 138 a[3,0] = 10 139 a[3,1] = 20 140 a[3,2] = 10 141 b = Test() 142 b.J = 4 143 b.A = a 144 return a, b, find_filtering_threshold2(b,0.01) 145 146 if __name__ == '__main__': 147 main() 148