【机器学习】六种算法在人脸补全中的应用比较(K紧邻,线性,决策树,岭回归,套索回归,ElasticNet)

需求:

根据人的上半边脸预测下半边脸,用各种算法取得的结果与原图比较

  • 思考:

    这是一个回归问题,不是分类问题(人脸数据不固定) 数据集一共包含40个人,每一个人10张照片,分布规律
    每一个人取出8张照片作为训练数据,2张照片作为测试数据 样本特征和样本标签如何拆分?上半边脸作为样本特征,下半边脸作为特征标签

————————————————

人脸图像补全的方法用途及研究

导包

import numpy as np
import matplotlib.pyplot as plt
%matplotlib inline
#构建方程
from sklearn.linear_model import LinearRegression,Ridge,Lasso
#不会构建方程
from sklearn.neighbors import KNeighborsRegressor
from sklearn.tree import DecisionTreeRegressor
from sklearn import datasets
from sklearn.model_selection import train_test_split

导入datasets中400位人脸数据数据

faces  = datasets.fetch_olivetti_faces()
X = faces.data
images = faces.images
y = faces.target

display(X.shape)
display(images.shape)
display(y.shape)

(400, 4096)

(400, 64, 64)

(400,)

plt.figure(figsize=(2,2))
index = np.random.randint(0,400,size =1)[0]
img = images[index]
plt.imshow(img,cmap = plt.cm.gist_gray)

在这里插入图片描述

将X(人脸数据)分成上半张人脸和下半张人脸

X_up = X[:,:2048]
X_down = X[:,2048:] 
 
index = np.random.randint(0,400,size =1)[0]

axes = plt.subplot(1,3,1)
up_face = X_up[index].reshape(32,64)
axes.imshow(up_face,cmap = plt.cm.gray)

axes = plt.subplot(1,3,2)
down_face = X_down[index].reshape(32,64)
axes.imshow(down_face,cmap = plt.cm.gray)

axes = plt.subplot(1,3,3)
face = X[index].reshape(64,64)
axes.imshow(face,cmap = plt.cm.gray)

在这里插入图片描述

X = X_up.copy()
y = X_down.copy()
display(X.shape,y.shape)

(400, 2048)
(400, 2048)

32*64

2048

X_train,X_test,y_train,y_test = train_test_split(X,y,test_size =30) 

estimators = {}
#线性回归
estimators['linear'] = LinearRegression()
estimators['ridge'] = Ridge(alpha=0.1)
estimators['knn'] = KNeighborsRegressor(n_neighbors=5)
estimators['lasso'] = Lasso(alpha=0.1)
estimators['ElasticNet'] = ElasticNet()

 
estimators['tree'] = DecisionTreeRegressor()#决策树费时间 2048个样本特征
#criterion = 'mse'  线性的是gini 和熵 都是越小越好

分别调用这六个每个算法

result = {}
for key,model in estimators.items():
    model.fit(X_train,y_train)
    y_ = model.predict(X_test)#预测的是下班长人脸
    result[key] = y_

结果可视化

plt.figure(figsize=(8*2,2*10,))

for i in range(0,10):
    #绘制第一列,上班张人脸
    axes = plt.subplot(10,8,i*8+1)
    up_face = X_test[i].reshape(32,64)
    axes.imshow(up_face,cmap= plt.cm.gray)
    #取消刻度
    axes.axis('off')
    #设置标题(只在第一列显示)
    if i == 0:
        axes.set_title('upface')
    
    #第七列绘制整张人脸
    axes = plt.subplot(10,8,i*8+8)
    down_face = y_test[i].reshape(32,64)
    #上下脸拼接
    true_face = np.concatenate([up_face,down_face])
    axes.imshow(true_face,cmap= plt.cm.gray) 
    axes.axis('off')
    if i == 0:
        axes.set_title('trueface')
        
    
    #绘制第二列到第六列 ,算法预测的数据result,
    #字典 key 算法value 预测人脸
    #用enumerate 循环增加了个j
    for j , key in enumerate(result): #j,0,1,2,3,4
        axes = plt.subplot(10,8,i*8+2+j)
        y_ = result[key]
        pre_downface = y_[i].reshape(32,64)
        pre_face = np.concatenate([up_face,pre_downface])
        axes.imshow(pre_face,cmap = plt.cm.gray)
        axes.axis('off')
        if i == 0:
            axes.set_title(key)

在这里插入图片描述

发布了354 篇原创文章 · 获赞 163 · 访问量 8万+

猜你喜欢

转载自blog.csdn.net/qq_41856814/article/details/103299271