예제 #1
0
파일: operators.py 프로젝트: gsj987/facerec
  def __calculate_firstn_features(self, X, y, S, model1_limit):
    f1 = self.model1.compute(X, y)
    f2 = self.model2.compute(X,y)

    SX = np.asarray(S).reshape(1,-1)
    corr1 = []
    corr2 = []
    for feature in asColumnMatrix(f1):
      if abs(np.linalg.norm(feature))<1e-12:
        corr1.append(1)
      else:
        corr1.append(-abs(1-self.operator(feature, SX)))

    for feature in asColumnMatrix(f2):
      if abs(np.linalg.norm(feature))<1e-12:
        corr2.append(1)
      else:
        corr2.append(-abs(1-self.operator(feature, SX)))
   
    if model1_limit==None:
      corr1.extend(corr2)
      self.idx = np.argsort(corr1)
    else:
      idx1 = np.argsort(corr1)
      idx2 = np.argsort(corr2)
      if model1_limit>len(idx1):
        model1_limit = len(idx1)
      idx2 = idx2+len(idx1)
      p1 = idx1[:model1_limit]
      p2 = idx2[:self.nm-model1_limit]
      p3 = idx1[model1_limit:]
      p4 = idx2[len(p2):]
      self.idx = np.hstack((p1,p2))
      self.idx = np.hstack((self.idx,p3))
      self.idx = np.hstack((self.idx, p4))
예제 #2
0
파일: feature.py 프로젝트: gsj987/facerec
	def compute(self,X,y):
		# build the column matrix
		XC = asColumnMatrix(X)
		y = np.asarray(y)
		# set a valid number of components
		if self._num_components <= 0 or (self._num_components > XC.shape[1]-1):
			self._num_components = XC.shape[1]-1
		# center dataset
		self._mean = XC.mean(axis=1).reshape(-1,1)
		XC = XC - self._mean
		# perform an economy size decomposition (may still allocate too much memory for computation)
		self._eigenvectors, self._eigenvalues, variances = np.linalg.svd(XC, full_matrices=False)
		# sort eigenvectors by eigenvalues in descending order
		idx = np.argsort(-self._eigenvalues)
		self._eigenvalues, self._eigenvectors = self._eigenvalues[idx], self._eigenvectors[:,idx]
		# use only num_components
		self._eigenvectors = self._eigenvectors[0:,0:self._num_components].copy()
		self._eigenvalues = self._eigenvalues[0:self._num_components].copy()
		# finally turn singular values into eigenvalues 
		self._eigenvalues = np.power(self._eigenvalues,2) / XC.shape[1]
		# get the features from the given data
		features = []
		for x in X:
			xp = self.project(x.reshape(-1,1))
			features.append(xp)
		return features
예제 #3
0
파일: feature.py 프로젝트: gsj987/facerec
	def compute(self, X, y):
		# turn into numpy representation
		Xc = asColumnMatrix(X)
		y = np.asarray(y)
		# gather some statistics about the dataset
		n = len(y)
		c = len(np.unique(y))
		# define features to be extracted
		pca = PCA(num_components = (n-c))
		lda = LDA(num_components = self._num_components)
		# fisherfaces are a chained feature of PCA followed by LDA
		model = ChainOperator(pca,lda)
		# computing the chained model then calculates both decompositions
		model.compute(X,y)
		# store eigenvalues and number of components used
		self._eigenvalues = lda.eigenvalues
		self._num_components = lda.num_components
		# compute the new eigenspace as pca.eigenvectors*lda.eigenvectors
		self._eigenvectors = np.dot(pca.eigenvectors,lda.eigenvectors)
		# finally compute the features (these are the Fisherfaces)
		features = []
		for x in X:
			xp = self.project(x.reshape(-1,1))
			features.append(xp)
		return features
예제 #4
0
 def compute(self, X, y):
     # build the column matrix
     XC = asColumnMatrix(X)
     y = np.asarray(y)
     # set a valid number of components
     if self._num_components <= 0 or (self._num_components >
                                      XC.shape[1] - 1):
         self._num_components = XC.shape[1] - 1
     # center dataset
     self._mean = XC.mean(axis=1).reshape(-1, 1)
     XC = XC - self._mean
     # perform an economy size decomposition (may still allocate too much memory for computation)
     self._eigenvectors, self._eigenvalues, variances = np.linalg.svd(
         XC, full_matrices=False)
     # sort eigenvectors by eigenvalues in descending order
     idx = np.argsort(-self._eigenvalues)
     self._eigenvalues, self._eigenvectors = self._eigenvalues[
         idx], self._eigenvectors[:, idx]
     # use only num_components
     self._eigenvectors = self._eigenvectors[0:,
                                             0:self._num_components].copy()
     self._eigenvalues = self._eigenvalues[0:self._num_components].copy()
     # finally turn singular values into eigenvalues
     self._eigenvalues = np.power(self._eigenvalues, 2) / XC.shape[1]
     # get the features from the given data
     features = []
     for x in X:
         xp = self.project(x.reshape(-1, 1))
         features.append(xp)
     return features
예제 #5
0
 def compute(self, X, y):
     # turn into numpy representation
     Xc = asColumnMatrix(X)
     y = np.asarray(y)
     # gather some statistics about the dataset
     n = len(y)
     c = len(np.unique(y))
     # define features to be extracted
     pca = PCA(num_components=(n - c))
     lda = LDA(num_components=self._num_components)
     # fisherfaces are a chained feature of PCA followed by LDA
     model = ChainOperator(pca, lda)
     # computing the chained model then calculates both decompositions
     model.compute(X, y)
     # store eigenvalues and number of components used
     self._eigenvalues = lda.eigenvalues
     self._num_components = lda.num_components
     # compute the new eigenspace as pca.eigenvectors*lda.eigenvectors
     self._eigenvectors = np.dot(pca.eigenvectors, lda.eigenvectors)
     # finally compute the features (these are the Fisherfaces)
     features = []
     for x in X:
         xp = self.project(x.reshape(-1, 1))
         features.append(xp)
     return features
예제 #6
0
파일: feature.py 프로젝트: LGDoor/facerec
 def compute(self, X, y):
     X = asColumnMatrix(X);
     y = np.asarray(y);
     n = len(y)
     c = len(np.unique(y))
     # set a valid number of components
     if self._num_components <= 0:
         self._num_components = c
     elif self._num_components > n:
         self._num_components = n
     # compute the weight matrix and Laplacian matrix
     S = spatial.distance.pdist(X.T, 'sqeuclidean');
     S = np.exp(-S/(self._weight*max(S)));
     S = spatial.distance.squareform(S);
     S[S < 0.0001] = 0;
     D = np.diag(S.sum(axis=0));
     L = D - S;        
     # calculate the eigenvectors
     eigenvalues, eigenvectors = linalg.eig(X.dot(L.dot(X.T)), X.dot(D.dot(X.T)));
     # sort eigenvectors by their eigenvalue in descending order
     idx = np.argsort(eigenvalues.real)
     eigenvalues, eigenvectors = eigenvalues[idx], eigenvectors[:,idx]
     # use only num_components
     self._eigenvectors = np.matrix(eigenvectors[:, 0:self._num_components].real, dtype=np.float32, copy=True)
     self._eigenvalues = np.array(eigenvalues[0:self._num_components].real, dtype=np.float32, copy=True)
     # get the features from the given data
     features = []
     X = self._eigenvectors.T.dot(X);
     for x in X.T:
         features.append(x.T);
     return features;
예제 #7
0
 def compute(self,X,y):
     Xp = []
     XC = asColumnMatrix(X)
     self._min = np.min(XC)
     self._max = np.max(XC)
     for xi in X:
         Xp.append(self.extract(xi))
     return Xp
예제 #8
0
 def compute(self,X,y):
     XC = asColumnMatrix(X)
     self._mean = XC.mean()
     self._std = XC.std()
     Xp = []
     for xi in X:
         Xp.append(self.extract(xi))
     return Xp
예제 #9
0
 def __init__(self, XSN):
     if XSN is None:
         raise ValueError("XSN cannot be None")
     # Reshape into a column matrix:
     XSN = asColumnMatrix(XSN)
     self.meanXSN = np.mean(XSN, axis=1)
     Sw = np.cov(XSN.T)
     w, v = np.linalg.eigh(Sw)
     idx = np.argsort(-w)
     w = w[idx]
     # Take the largest eigenvalue:
     maxeig = w[0]
     Sw = Sw + 0.1 * np.eye(Sw.shape[0]) * maxeig
     self.iSw = np.inv(Sw)
     self.sizeXSN = XSN.shape[1]
예제 #10
0
 def compute(self, X, y):
     XC = asColumnMatrix(X)
     y = np.asarray(y)
     #calculate dimensions
     d = XC.shape[0]
     c = len(np.unique(y))
     yset = set(y)
     if self._num_components <= 0:
         self._num_components = c - 1
     elif self._num_components > (c - 1):
         self._num_components = c - 1
     meanTotal = XC.mean(axis=1).reshape(-1, 1)
     #calculate the within and between scatter matrices"
     Sw = np.zeros((d, d), dtype=np.float32)
     Sb = np.zeros((d, d), dtype=np.float32)
     ysetlist = list(yset)
     for i in range(0, c):
         label = ysetlist[i]
         #grab entries from X for a given label
         Xi = XC[:, np.where(y == label)[0]]
         meanClass = np.mean(Xi, axis=1).reshape(-1, 1)
         Sw = Sw + np.dot((Xi - meanClass), (Xi - meanClass).T)
         Sb = Sb + Xi.shape[1] * np.dot((meanClass - meanTotal),
                                        (meanClass - meanTotal).T)
     #solve eigenvalue problem for a general matrix
     self._eigenvalues, self._eigenvectors = np.linalg.eig(
         np.linalg.inv(Sw) * Sb)
     # sort eigenvectors by their eigenvalue in descending order
     idx = np.argsort(-self._eigenvalues.real)
     self._eigenvalues, self._eigenvectors = self._eigenvalues[
         idx], self._eigenvectors[:, idx]
     # only store (c-1) non-zero eigenvalues
     self._eigenvalues = np.array(
         self._eigenvalues[0:self._num_components].real,
         dtype=np.float32,
         copy=True)
     self._eigenvectors = np.matrix(
         self._eigenvectors[0:, 0:self._num_components].real,
         dtype=np.float32,
         copy=True)
     # get the features from the given data
     features = []
     for x in X:
         xp = self.project(x.reshape(-1, 1))
         features.append(xp)
     return features
예제 #11
0
파일: feature.py 프로젝트: Zekom/facerec
 def compute(self, X, y):
     # build the column matrix
     XC = asColumnMatrix(X)
     y = np.asarray(y)
     # calculate dimensions
     d = XC.shape[0]
     c = len(np.unique(y))
     # set a valid number of components
     if self._num_components <= 0:
         self._num_components = c - 1
     elif self._num_components > (c - 1):
         self._num_components = c - 1
     # calculate total mean
     meanTotal = XC.mean(axis=1).reshape(-1, 1)
     # calculate the within and between scatter matrices
     Sw = np.zeros((d, d), dtype=np.float32)
     Sb = np.zeros((d, d), dtype=np.float32)
     for i in range(0, c):
         Xi = XC[:, np.where(y == i)[0]]
         meanClass = np.mean(Xi, axis=1).reshape(-1, 1)
         Sw = Sw + np.dot((Xi - meanClass), (Xi - meanClass).T)
         Sb = Sb + Xi.shape[1] * np.dot((meanClass - meanTotal), (meanClass - meanTotal).T)
     # solve eigenvalue problem for a general matrix
     self._eigenvalues, self._eigenvectors = np.linalg.eig(np.linalg.inv(Sw) * Sb)
     # sort eigenvectors by their eigenvalue in descending order
     idx = np.argsort(-self._eigenvalues.real)
     self._eigenvalues, self._eigenvectors = self._eigenvalues[idx], self._eigenvectors[:, idx]
     # only store (c-1) non-zero eigenvalues
     self._eigenvalues = np.array(self._eigenvalues[0 : self._num_components].real, dtype=np.float32, copy=True)
     self._eigenvectors = np.matrix(
         self._eigenvectors[0:, 0 : self._num_components].real, dtype=np.float32, copy=True
     )
     # get the features from the given data
     features = []
     for x in X:
         xp = self.project(x.reshape(-1, 1))
         features.append(xp)
     return features