PCA和Kmean在人脸重建olivetti人脸数据集上的比较

Olivetti 数据集

关于 Olivetti 数据集的简要信息:

  1. 拍摄时间和背景

    • 人脸图像拍摄时间:1992年4月到1994年4月之间。
    • 拍摄背景:所有人脸图像的背景均为黑色。
  2. 图像数量和人物

    • 数据集包含40个不同人物的图像。
    • 每个人有10张不同的图像。
    • 总共有400张人脸图像。
  3. 图像的多样性

    • 图像是在不同时间拍摄的。
    • 光照条件各不相同。
    • 面部表情和面部细节各异。
  4. 图像特征

    • 图像为灰度级。
    • 每张图像的大小为64x64像素。
    • 图像的像素值被缩放到[0, 1]区间。
  5. 人物编码

文末提供完整代码和下载好的数据集

加载数据集

import numpy as np
data=np.load("/content/drive/MyDrive/faces_dataset/olivetti_faces.npy")
target=np.load("/content/drive/MyDrive/faces_dataset/olivetti_faces_target.npy")

打印出数据集里面的信息

print("There are {} images in the dataset".format(len(data)))
print("There are {} unique targets in the dataset".format(len(np.unique(target))))
print("Size of each image is {}x{}".format(data.shape[1],data.shape[2]))
print("Pixel values were scaled to [0,1] interval. e.g:{}".format(data[0][0,:4]))

There are 400 images in the dataset
There are 40 unique targets in the dataset
Size of each image is 64x64
Pixel values were scaled to [0,1] interval. e.g:[0.30991736 0.3677686 0.41735536 0.44214877]

打印出里面的标签

print("unique target number:",np.unique(target))
#unique target number: [ 0  1  2  3  4  5  6  7  8  9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39]

根据标签展示出10个人脸和其对应的图片

def show_10_faces_of_n_subject(images, subject_ids):
    cols=10# each subject has 10 distinct face images
    rows=(len(subject_ids)*10)/cols #
    rows=int(rows)

    fig, axarr=plt.subplots(nrows=rows, ncols=cols, figsize=(18,9))
    #axarr=axarr.flatten()

    for i, subject_id in enumerate(subject_ids):
        for j in range(cols):
            image_index=subject_id*10 + j
            axarr[i,j].imshow(images[image_index], cmap="gray")
            axarr[i,j].set_xticks([])
            axarr[i,j].set_yticks([])
            axarr[i,j].set_title("face id:{}".format(subject_id))
#You can playaround subject_ids to see other people faces
show_10_faces_of_n_subject(images=data, subject_ids=[1,4, 25, 20, 39])

在这里插入图片描述
将图像重新调整以适应机器学习模型

#We reshape images for machine learnig  model
X=data.reshape((data.shape[0],data.shape[1]*data.shape[2]))
print("X shape:",X.shape) #X shape: (400, 4096)

分成训练集和车市集和验证集

from sklearn.model_selection import train_test_split

# 假设已经有特征数据 X 和标签数据 target

# 将数据划分为训练集和临时集(70%训练集,30%临时集)
X_train_temp, X_test_val, y_train_temp, y_test_val =
  • 28
    点赞
  • 12
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值