2016-10-23 1 views
1

입니다. 저는 초보자 R 코더에 지나지 않으며, PCA와 eigenfaces 기법을 사용하여 이미지를 분류하는 데 영감을 받았습니다. 그러나 대부분의 예제는 Python으로 보입니다. 저는 R로 개발을 계속하고 싶습니다.모든 간단한 EigenFaces 분류 코드는 R

Cambridge 그레이 스케일 얼굴 이미지를 400 샘플 x 10304 열 ImageData에로드했습니다. 각 열은 112x92 그레이 스케일 픽셀 값을 나타냅니다. . pixmapRGB를 사용하여 각 이미지를 플롯 할 수 있습니다.

나는 PCA 분석을 수행하고, Eigenvalues를 추출했다고 믿지만, 50 개의 EigenFaces에서 첫 번째 이미지를 재구성하면, 거친 EigenFace와 같은 방법으로 아직 멀다.

(나는 센터없이 평균 이미지와 colmeans없이 시도하고 prcomp있다 = FALSE. 그래서

정말 찾고 올바르게 그래서 난 내 이미지 처리 수단입니다 생각하지 않는다 제대로 올바르게 확장, 또는 일부 끝이 R에 고유 얼굴을 classificational 코드를 종료하는

cmeans = colMeans(TrainImages) 
DisplayImage(cmeans, main = "Average Person") 
ProcTrainData = TrainImages # - cmeans 

# Now PCA Analysis - Adjusted Tolerance to 0.125 to return ~50 PCs 
PCAProcess = prcomp(ProcTrainData, center = TRUE, tol = 0.125) 

# Analyse PCA results Results 
par(mfrow = c(1, 2)) 
screeplot(PCAProcess) 
devs = PCAProcess$sdev^2/sum(PCAProcess$sdev^2) 
plot(1 - devs, main = "Percent Variance Explained", type = "l") 

EigenFaces = PCAProcess$rotation 

# Project Training Data into PCA Eignevalue space 
TrainPCAValues = ProcTrainData %*% EigenFaces 

# Plot first ten EigenFaces 
par(mfrow = c(2, 5)) 
par(oma = rep(2, 4), mar = c(0, 0, 3, 0)) 

for (i in 1:10) { 
    DisplayImage(EigenFaces[, i], main = paste0("EF ", i)) #PCs from sample data 
} 
# ======== Recover the first Image by the use of PCA attributes and Eigen 
# Images 
Composite[1:ImageSize] = 0 # PCAProcess$center; 
for (iv in 1:50) { 
    Composite = Composite + TrainPCAValues[1, iv] * EigenFaces[, iv] 
} 

DisplayImage(Composite) 
DisplayImage(TrainImages[1, ]) 
DisplayImage(PCAProcess$center) 

아이겐은 Eigen Faces

은 원래 1 차 샘플 대 복합 생성 얼굴

답변

0

약간의 진전이 있습니다. EigneFaces 자습서 및 논문 다양한에 비해 특히 좋지 않아 아직

enter code here# Adjusted Tolerance to 0.05 to return ~50 PCs 
PCAProcess = prcomp(TrainImages,center = TRUE,scale. = TRUE ,tol=0.05) 
# 
# Analyse PCA results Results 
summary(PCAProcess) 
par(mfrow=c(1, 2)) 
screeplot(PCAProcess) 
devs = PCAProcess$sdev^2/sum(PCAProcess$sdev^2) 
plot(1-devs, main='Percent Variance Explained', type='l') 
# 
# The PCA Process will have reduced the Original Image Dimension 96x96 = 9216 down to ~50 
# The Rotated Data into ~50 dimension is in PCAProcess$x arrays (
# The Eigen Rotatations of the original dimensionare captued in  PCAProcess$rotation 
# 
# Looks like we can get away with use of 25 PCs to get about 95% or varience 
EigenFaces = PCAProcess$rotation[,1:25]; 
# Plot first ten EigenFaces 
par(mfrow=c(2, 5)) 
par(oma = rep(2, 4), mar=c(0, 0, 3, 0)) 
for (i in 1:10){ 
    im <- matrix(data=rev(EigenFaces[,i]), nrow=96, ncol=96) 
image(1:96, 1:96, im, col=gray((0:255)/255)) 
} 
# 
# Training Reconstruction Matrix *just first 25 attributes in PCA space 
ReconstructTraining = PCAProcess$x[,1:25]%*%t(EigenFaces) 
# 
# Need to unscale and uncentre back using the prcomp computed scale and centre 
# 
if(PCAProcess$scale != FALSE){ 
ReconstructTraining <- scale(ReconstructTraining, center = FALSE ,scale=1/PCAProcess$scale) 
} 
if(all(PCAProcess$center != FALSE)){ 
    ReconstructTraining <- scale(ReconstructTraining, center = -1 * PCAProcess$center, scale=FALSE) 
} 
# ============================ 
#Recover the first Image by the use of PCA attributes and Eigen Images 
# 
par(mfrow=c(1, 2)) 
# Original Image 2 
im <- matrix(data=rev(im.train[2,]), nrow=96, ncol=96) 
image(1:96, 1:96, im, col=gray((0:255)/255)) 

RestoredImage <- matrix(data=rev(ReconstructTraining[2,]), nrow=96, ncol=96) 
image(1:96, 1:96, RestoredImage, col=gray((0:255)/255)) 

: 는 기본적으로 나는 규모와 중심을 계산하는 prcomp를 사용하는 대신 prcomp 호출하기 전에 평균을 계산 무시하고하기로 결정했습니다. 그래서 25 개 고유 얼굴의 사용과 Original vs reconstructed

파이썬 sklearn의 고유 얼굴은 지역 사회 더 나은 지원이 될 것으로 보인다 그래서 내가, 기계 학습 파이썬을 사용하여로 이동합니다 R.를 사용하는 것보다 더 많이 나타납니다.

+0

예를 들어 동일한 데이터 세트에서 연결 했습니까? 표준 편차를 플로팅 해 보았습니까? – broncoAbierto