r - R 中任何简单的 EigenFaces 分类代码

标签 r pca

我只是一个 R 编码新手,并且受到使用 PCA 和特征脸技术来分类图像的启发。然而,大多数示例似乎都是使用 Python 编写的,我更愿意继续使用 R 进行开发。

我已将剑桥灰度人脸图像加载到 400 个样本 x 10304 列 ImageData 中,每列代表折叠后的 112x92 灰度像素值。我可以使用 pixmapRGB OK 绘制每个图像。

我进行了 PCA 分析,并相信我已经提取了特征值,但是当我从 50 个特征脸重建我的第一张图像时,它仍然有很长的路要走,更像是一个粗糙的特征脸。

所以我认为我没有正确或正确地处理我的图像均值和缩放(我尝试过使用和不使用 colmeans 平均图像,以及不使用 Center =FALSE 的 prcomp。

所以我真的在寻找 R 中的一些端到端 EigenFaces 分类代码

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

生成的复合 Material 与原始第一个样本 Generated Composite vs Original 1st Sample

最佳答案

只是一点点进步。 基本上我决定忽略 prcomp 调用之前的计算平均值,而是使用 prcomp 来计算比例和中心:

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))

与各种 EigneFaces 教程和论文相比,仍然不是特别好。因此使用 25 个 EigenFaces Original vs reconstructed

Python sklearn EigenFaces 似乎比使用 R 好得多。因此,我将转向使用 Python 进行机器学习,因为它似乎得到了更好的社区支持。

关于r - R 中任何简单的 EigenFaces 分类代码,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40206606/

相关文章:

c++ - R 调用 C 代码比 C++ 函数调用 C 代码更快?

python - Rpy2:如何将字典列表转换为 R 数据框

r - 使用大于和小于列名中的符号连接 data.tables 时出现问题

python - 如何解释奇异值分解结果(Python 3)?

r - R 函数 Fitted() 和 Predict() 之间有区别吗?

python - Spark 中的 PCA 输出与 scikit-learn 不匹配

r - 从 R 中的主要载荷构建分数

matlab - 为什么 Scipy 和 MATLAB 的主成分值不一致?

python - PCA的主要成分

r - 从R中的向量中提取单词的总频率