资源简介
一次课程实验作业,用人脸数据集进行降维处理显示降维处理后的图像
代码片段和文件信息
import operator
import numpy as np
import os
from PIL import Image
class PCAtest():
def __init__(self d):#d表示所降维度
self.d = d
def train(self X):
X = np.array(X dtype=“float64“)
X = self.normalize(X)
cov_matrix_t = X.T.dot(X)
eigen_values eigen_vectors = np.linalg.eig(cov_matrix_t)
eigen_values = eigen_values.tolist()
eigen_vectors = X.dot(eigen_vectors)
eigen_vectors = eigen_vectors.T.tolist()
eigen_zip = [(value.real vector) for value vector in zip(eigen_values eigen_vectors)]
print(“len of real eigen values“ len(eigen_zip))
orderd_eigens = sorted(eigen_zip key=operator.itemgetter(0) reverse=True)
self.max_eigen_vectors = [orderd_eigens[i][1] for i in range(self.d)]
self.W = np.real(np.array(self.max_eigen_vectors).T)
self.W /= np.sqrt(np.sum(self.W ** 2 axis=0 keepdims=True))
def normalize(self X):#标准化
self.mean_vector = 1.0 / X.shape[1] * np.sum(X axis=1 keepdims=True)
self.std = np.std(X axis=1 keepdims=True) + 10e-8
X = (X - self.mean_vector)
return X
def dim_reduction(self x):#降维处理
return self.W.T.dot(x)
def construct(self c):#数据构造
new = self.W.dot(c)
return (new)
def main():
imgName = [‘subject03.wink‘ ‘subject08.happy‘ ‘subject14.surprised‘]#所选图片
K = [20 100]#所降维数
for i in K:
for j in imgName:
d = i
path = “yalefaces“
X = []
n = 1
for file in os.listdir(path):
if not file.endswith(“.txt“):
img = Image.open(os.path.join(path file))
img = np.array(img).reshape(img.width * img.height)
X.append(img)
n += 1
X = np.array(X)
X = X.T
pca = PCAtest(d)
pca.train(X)
img = Image.open(os.path.join(path j))
w h = img.width img.height
img = np.array(img).reshape(w * h 1)
img = img - pca.mean_vector
features = pca.dim_reduction(img)
x_hat = pca.construct(features)
x_hat += pca.mean_vector
x_hat = x_hat.reshape(h w)
x_hat = np.array(x_hat dtype=“int32“)
new_img = Image.fromarray(x_hat).convert(‘L‘)
new_file = j + str(i) + ‘.png‘
new_img.save(os.path.join(‘yalefaces‘ new_file))
if __name__ == “__main__“:
main()
属性 大小 日期 时间 名称
----------- --------- ---------- ----- ----
文件 2656 2018-12-16 15:21 PCA_yale人脸数据集\test.py
文件 1172 2018-12-16 14:14 PCA_yale人脸数据集\yalefaces\Readme.txt
文件 35413 1997-04-06 16:06 PCA_yale人脸数据集\yalefaces\subject01.gif
文件 36294 1997-04-06 16:06 PCA_yale人脸数据集\yalefaces\subject01.glasses
文件 36294 1997-04-06 16:06 PCA_yale人脸数据集\yalefaces\subject01.glasses.gif
文件 36692 1997-04-06 16:06 PCA_yale人脸数据集\yalefaces\subject01.happy
文件 42921 1997-04-06 16:06 PCA_yale人脸数据集\yalefaces\subject01.leftlight
文件 34100 1997-04-06 16:06 PCA_yale人脸数据集\yalefaces\subject01.noglasses
文件 36129 1997-04-06 16:06 PCA_yale人脸数据集\yalefaces\subject01.normal
文件 71627 1997-04-06 16:06 PCA_yale人脸数据集\yalefaces\subject01.rightlight
文件 36370 1997-04-06 16:06 PCA_yale人脸数据集\yalefaces\subject01.sad
文件 36307 1997-04-06 16:06 PCA_yale人脸数据集\yalefaces\subject01.sleepy
文件 36647 1997-04-06 16:06 PCA_yale人脸数据集\yalefaces\subject01.surprised
文件 36381 1997-04-06 16:06 PCA_yale人脸数据集\yalefaces\subject01.wink
文件 31137 1997-04-06 16:06 PCA_yale人脸数据集\yalefaces\subject02.centerlight
文件 45239 1997-04-06 16:06 PCA_yale人脸数据集\yalefaces\subject02.glasses
文件 43765 1997-04-06 16:06 PCA_yale人脸数据集\yalefaces\subject02.happy
文件 38159 1997-04-06 16:06 PCA_yale人脸数据集\yalefaces\subject02.leftlight
文件 43681 1997-04-06 16:06 PCA_yale人脸数据集\yalefaces\subject02.noglasses
文件 43681 1997-04-06 16:06 PCA_yale人脸数据集\yalefaces\subject02.normal
文件 62924 1997-04-06 16:06 PCA_yale人脸数据集\yalefaces\subject02.rightlight
文件 43439 1997-04-06 16:06 PCA_yale人脸数据集\yalefaces\subject02.sad
文件 43518 1997-04-06 16:06 PCA_yale人脸数据集\yalefaces\subject02.sleepy
文件 45447 1997-04-06 16:06 PCA_yale人脸数据集\yalefaces\subject02.surprised
文件 43080 1997-04-06 16:06 PCA_yale人脸数据集\yalefaces\subject02.wink
文件 26833 1997-04-06 16:06 PCA_yale人脸数据集\yalefaces\subject03.centerlight
文件 27292 1997-04-06 16:06 PCA_yale人脸数据集\yalefaces\subject03.glasses
文件 28032 1997-04-06 16:06 PCA_yale人脸数据集\yalefaces\subject03.happy
文件 34932 1997-04-06 16:06 PCA_yale人脸数据集\yalefaces\subject03.leftlight
文件 27895 1997-04-06 16:07 PCA_yale人脸数据集\yalefaces\subject03.noglasses
............此处省略143个文件信息
相关资源
- PCAN-Explorer5基本使用方法new
- 基于PCA和BP神经网络的人脸识别
- Hands-On Machine Learning 英文原版
- 图像融合算法大全 image fusion
- Recurrent Neural Networks for Prediction(pdf)
- Yale人脸识别图片库bmp
- cip 报文 工控协议分析
- IPCamAdapter.msi程序
- 吴恩达新书 《Machine Learning Yearning》机
- 使用winpcap进行ip流量包分析程序
- 校园网计费系统的设计与实现
- Introduction to Algorithmic Marketing
- 吴恩达机器学习 缺失的 machine-learni
- 可直接实现的2DPCA算法代码
- KPCA人脸GUI.zip
- Introduction to Statistical Machine Learning B
- machine-learning-security.pdf
- Advances in Financial Machine learning
- Yale Face人脸表情数据库
- KPCA人脸识别程序
- 3D Morphable Model pca algorithm
- 基于PCA的人脸识别系统
- 基于PCA的人脸特征提取
- 机器学习与R语言,课件
- PCA-SVM人脸识别代码
- 基于opencv的双摄像头读取程序含Dire
- 计算机网络实验之ip数据包捕获程序
- TensorFlow Machine Learning Cookbook2017 高清完
- 表情识别源代码
- 机器学习_基于朴素贝叶斯算法_疾病预
评论
共有 条评论