【问题标题】:Any simple EigenFaces classification code in RR中任何简单的EigenFaces分类代码
【发布时间】:2017-03-05 12:48:18
【问题描述】:

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

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

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

所以我认为我没有正确或正确地处理我的图像均值和缩放(我尝试过使用和不使用 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)

特征面

生成的复合材料与原始的第一个样本

【问题讨论】:

    标签: r pca


    【解决方案1】:

    只是一点点进步。 基本上我决定在 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 进行机器学习,因为它似乎得到了更好的支持社区。

    【讨论】:

    • 您链接到的示例是否来自同一数据集?您是否尝试过绘制标准差?
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-07-04
    • 2015-01-11
    • 2011-06-05
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多