【阿旭机器学习实战】【15】人脸自动补全(多目标回归),并比较5种不同模型的预测效果

本文涉及的产品
模型训练 PAI-DLC,5000CU*H 3个月
模型在线服务 PAI-EAS,A10/V100等 500元 1个月
交互式建模 PAI-DSW,每月250计算时 3个月
简介: 【阿旭机器学习实战】【15】人脸自动补全(多目标回归),并比较5种不同模型的预测效果

机器学习实战—人脸自动补全(多目标预测)


目标


通过上半部分的人脸图案来预测下边部分人脸,进行人脸补全。


实质是一个多目标预测问题,对每一个目标点都会进行模型建模,然后通过相应模型对各个点进行预测


数据集


采用Olivetti人脸数据集包含400张灰度的64*64像素的人脸图像,每个图像被展平为大小为4096的一维向量,40个不同的人拍照十次。


from sklearn.neighbors import KNeighborsRegressor
from sklearn.linear_model import LinearRegression,Ridge,Lasso
from sklearn.ensemble import ExtraTreesRegressor
from sklearn import datasets
• 1
faces = datasets.fetch_olivetti_faces()
• 1
faces
{'data': array([[0.30991736, 0.3677686 , 0.41735536, ..., 0.15289256, 0.16115703,
         0.1570248 ],
        [0.45454547, 0.47107437, 0.5123967 , ..., 0.15289256, 0.15289256,
         0.15289256],
        [0.3181818 , 0.40082645, 0.49173555, ..., 0.14049587, 0.14876033,
         0.15289256],
        ...,
        [0.5       , 0.53305787, 0.607438  , ..., 0.17768595, 0.14876033,
         0.19008264],
        [0.21487603, 0.21900827, 0.21900827, ..., 0.57438016, 0.59090906,
         0.60330576],
        [0.5165289 , 0.46280992, 0.28099173, ..., 0.35950413, 0.3553719 ,
         0.38429752]], dtype=float32),
 'images': array([[[0.30991736, 0.3677686 , 0.41735536, ..., 0.37190083,
          0.3305785 , 0.30578512],
         [0.3429752 , 0.40495867, 0.43801653, ..., 0.37190083,
          0.338843  , 0.3140496 ],
         [0.3429752 , 0.41735536, 0.45041323, ..., 0.38016528,
          0.338843  , 0.29752067],
         ...,
         [0.21487603, 0.20661157, 0.2231405 , ..., 0.15289256,
          0.16528925, 0.17355372],
         [0.20247933, 0.2107438 , 0.2107438 , ..., 0.14876033,
          0.16115703, 0.16528925],
         [0.20247933, 0.20661157, 0.20247933, ..., 0.15289256,
          0.16115703, 0.1570248 ]],
        [[0.45454547, 0.47107437, 0.5123967 , ..., 0.19008264,
          0.18595041, 0.18595041],
         [0.446281  , 0.48347107, 0.5206612 , ..., 0.21487603,
          0.2107438 , 0.2107438 ],
         [0.49586776, 0.5165289 , 0.53305787, ..., 0.20247933,
          0.20661157, 0.20661157],
         ...,
         [0.77272725, 0.78099173, 0.7933884 , ..., 0.1446281 ,
          0.1446281 , 0.1446281 ],
         [0.77272725, 0.7768595 , 0.7892562 , ..., 0.13636364,
          0.13636364, 0.13636364],
         [0.7644628 , 0.7892562 , 0.78099173, ..., 0.15289256,
          0.15289256, 0.15289256]],
        [[0.3181818 , 0.40082645, 0.49173555, ..., 0.40082645,
          0.3553719 , 0.30991736],
         [0.30991736, 0.3966942 , 0.47933885, ..., 0.40495867,
          0.37603307, 0.30165288],
         [0.26859504, 0.34710744, 0.45454547, ..., 0.3966942 ,
          0.37190083, 0.30991736],
         ...,
         [0.1322314 , 0.09917355, 0.08264463, ..., 0.13636364,
          0.14876033, 0.15289256],
         [0.11570248, 0.09504132, 0.0785124 , ..., 0.1446281 ,
          0.1446281 , 0.1570248 ],
         [0.11157025, 0.09090909, 0.0785124 , ..., 0.14049587,
          0.14876033, 0.15289256]],
        ...,
        [[0.5       , 0.53305787, 0.607438  , ..., 0.28512397,
          0.23966943, 0.21487603],
         [0.49173555, 0.5413223 , 0.60330576, ..., 0.29752067,
          0.20247933, 0.20661157],
         [0.46694216, 0.55785125, 0.6198347 , ..., 0.29752067,
          0.17768595, 0.18595041],
         ...,
         [0.03305785, 0.46280992, 0.5289256 , ..., 0.17355372,
          0.17355372, 0.1694215 ],
         [0.1570248 , 0.5247934 , 0.53305787, ..., 0.16528925,
          0.1570248 , 0.18595041],
         [0.45454547, 0.5206612 , 0.53305787, ..., 0.17768595,
          0.14876033, 0.19008264]],
        [[0.21487603, 0.21900827, 0.21900827, ..., 0.71487606,
          0.71487606, 0.6942149 ],
         [0.20247933, 0.20661157, 0.20661157, ..., 0.7107438 ,
          0.7066116 , 0.6942149 ],
         [0.2107438 , 0.20661157, 0.20661157, ..., 0.6859504 ,
          0.69008267, 0.6942149 ],
         ...,
         [0.2644628 , 0.25619835, 0.2603306 , ..., 0.5413223 ,
          0.57438016, 0.59090906],
         [0.26859504, 0.2644628 , 0.26859504, ..., 0.56198347,
          0.58264464, 0.59504133],
         [0.27272728, 0.26859504, 0.27272728, ..., 0.57438016,
          0.59090906, 0.60330576]],
        [[0.5165289 , 0.46280992, 0.28099173, ..., 0.5785124 ,
          0.5413223 , 0.60330576],
         [0.5165289 , 0.45041323, 0.29338843, ..., 0.58264464,
          0.553719  , 0.5785124 ],
         [0.5165289 , 0.44214877, 0.29338843, ..., 0.59917355,
          0.5785124 , 0.54545456],
         ...,
         [0.39256197, 0.41322315, 0.38842976, ..., 0.33471075,
          0.37190083, 0.3966942 ],
         [0.39256197, 0.38429752, 0.40495867, ..., 0.3305785 ,
          0.35950413, 0.37603307],
         [0.3677686 , 0.40495867, 0.3966942 , ..., 0.35950413,
          0.3553719 , 0.38429752]]], dtype=float32),
 'target': array([ 0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  1,  1,  1,  1,  1,  1,  1,
         1,  1,  1,  2,  2,  2,  2,  2,  2,  2,  2,  2,  2,  3,  3,  3,  3,
         3,  3,  3,  3,  3,  3,  4,  4,  4,  4,  4,  4,  4,  4,  4,  4,  5,
         5,  5,  5,  5,  5,  5,  5,  5,  5,  6,  6,  6,  6,  6,  6,  6,  6,
         6,  6,  7,  7,  7,  7,  7,  7,  7,  7,  7,  7,  8,  8,  8,  8,  8,
         8,  8,  8,  8,  8,  9,  9,  9,  9,  9,  9,  9,  9,  9,  9, 10, 10,
        10, 10, 10, 10, 10, 10, 10, 10, 11, 11, 11, 11, 11, 11, 11, 11, 11,
        11, 12, 12, 12, 12, 12, 12, 12, 12, 12, 12, 13, 13, 13, 13, 13, 13,
        13, 13, 13, 13, 14, 14, 14, 14, 14, 14, 14, 14, 14, 14, 15, 15, 15,
        15, 15, 15, 15, 15, 15, 15, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16,
        17, 17, 17, 17, 17, 17, 17, 17, 17, 17, 18, 18, 18, 18, 18, 18, 18,
        18, 18, 18, 19, 19, 19, 19, 19, 19, 19, 19, 19, 19, 20, 20, 20, 20,
        20, 20, 20, 20, 20, 20, 21, 21, 21, 21, 21, 21, 21, 21, 21, 21, 22,
        22, 22, 22, 22, 22, 22, 22, 22, 22, 23, 23, 23, 23, 23, 23, 23, 23,
        23, 23, 24, 24, 24, 24, 24, 24, 24, 24, 24, 24, 25, 25, 25, 25, 25,
        25, 25, 25, 25, 25, 26, 26, 26, 26, 26, 26, 26, 26, 26, 26, 27, 27,
        27, 27, 27, 27, 27, 27, 27, 27, 28, 28, 28, 28, 28, 28, 28, 28, 28,
        28, 29, 29, 29, 29, 29, 29, 29, 29, 29, 29, 30, 30, 30, 30, 30, 30,
        30, 30, 30, 30, 31, 31, 31, 31, 31, 31, 31, 31, 31, 31, 32, 32, 32,
        32, 32, 32, 32, 32, 32, 32, 33, 33, 33, 33, 33, 33, 33, 33, 33, 33,
        34, 34, 34, 34, 34, 34, 34, 34, 34, 34, 35, 35, 35, 35, 35, 35, 35,
        35, 35, 35, 36, 36, 36, 36, 36, 36, 36, 36, 36, 36, 37, 37, 37, 37,
        37, 37, 37, 37, 37, 37, 38, 38, 38, 38, 38, 38, 38, 38, 38, 38, 39,
        39, 39, 39, 39, 39, 39, 39, 39, 39]),
 'DESCR': 'Modified Olivetti faces dataset.\n\nThe original database was available from\n\n    http://www.cl.cam.ac.uk/research/dtg/attarchive/facedatabase.html\n\nThe version retrieved here comes in MATLAB format from the personal\nweb page of Sam Roweis:\n\n    http://www.cs.nyu.edu/~roweis/\n\nThere are ten different images of each of 40 distinct subjects. For some\nsubjects, the images were taken at different times, varying the lighting,\nfacial expressions (open / closed eyes, smiling / not smiling) and facial\ndetails (glasses / no glasses). All the images were taken against a dark\nhomogeneous background with the subjects in an upright, frontal position (with\ntolerance for some side movement).\n\nThe original dataset consisted of 92 x 112, while the Roweis version\nconsists of 64x64 images.\n'}

data = faces.data
target = faces.target
data.shape
(400, 4096)
• 1
faces.images.shape
• 1
(400, 64, 64)

import matplotlib.pyplot as plt
%matplotlib inline
• 1
• 2
# 打印一张人脸图片
plt.imshow(data[100].reshape((64,64)),cmap="gray")
• 1
• 2


9211e5872a2b42218d4fc6c559ff613b.png

数据切分


切分特征数据数据和标签数据,特征是上半边脸,标签是下半边脸


# 特征是上半边脸
faces_up = data[:,:2048]
# 需要预测的目标:标签是下半边脸
faces_down = data[:,2048:]
plt.figure(figsize=(2,2))
plt.imshow(faces_up[10].reshape((32,64)),cmap="gray")
• 1
• 2
<matplotlib.image.AxesImage at 0x25eca1c8828>

a8fa790840c54aca923b28b5143e7f91.png

plt.figure(figsize=(2,2))
plt.imshow(faces_down[10].reshape((32,64)),cmap="gray")

10da94a016c84d93bb5a463285be775c.png

划分数据集


# 数据切分
from sklearn.model_selection import train_test_split
x_train,x_test,y_train,y_test = train_test_split(faces_up,faces_down,test_size=0.02)
• 1
y_train[1]


array([0.5082645 , 0.5082645 , 0.5123967 , ..., 0.16115703, 0.17768595,
       0.1694215 ], dtype=float32)


建立不同的回归模型并训练


此处分别用KNN回归模型,线性回归,岭回归,lasso回归,极端随机森林回归这几种不同的模型来进行建模


estimators = {
    "knn":KNeighborsRegressor(),
    "linear":LinearRegression(),
    "ridge":Ridge(),
    "lasso":Lasso(),
    "extra":ExtraTreesRegressor()  #极端随机森林回归
}
# 定义一个字典,用于保存每个算法预测结果
faces_pre = dict()
for key,estimator in estimators.items():
    # 对算法进行模型训练
    estimator.fit(x_train,y_train)
    # 预测
    y_ = estimator.predict(x_test)
    # 把预测的结果保存
    faces_pre[key] = y_
    # 得分
    score = estimator.score(x_test, y_test)
    print(key, score)
knn 0.4880642098170732
linear 0.18894319531680143
ridge 0.5157197923145055
lasso -0.2100687498661858
extra 0.35087195680524175
faces_pre
{'knn': array([[0.4471074 , 0.41652894, 0.42066115, ..., 0.54793394, 0.5355372 ,
         0.546281  ],
        [0.34876034, 0.34214878, 0.346281  , ..., 0.42727274, 0.42809922,
         0.43057853],
        [0.5355372 , 0.546281  , 0.58016527, ..., 0.56611574, 0.56280994,
         0.5644628 ],
        ...,
        [0.64793384, 0.67685956, 0.7049587 , ..., 0.41487604, 0.3586777 ,
         0.36776862],
        [0.3942149 , 0.41322312, 0.43553716, ..., 0.45785123, 0.43471074,
         0.39173552],
        [0.47520667, 0.47024792, 0.51404965, ..., 0.631405  , 0.6256199 ,
         0.59173554]], dtype=float32),
 'linear': array([[0.42212042, 0.35969752, 0.39748642, ..., 0.63096315, 0.5628751 ,
         0.5159277 ],
        [0.4241521 , 0.26758337, 0.16570012, ..., 0.09656662, 0.13010818,
         0.19814485],
        [0.62213266, 0.441006  , 0.48480797, ..., 0.5819658 , 0.69699645,
         0.44033697],
        ...,
        [0.71544605, 0.6732123 , 0.7088314 , ..., 0.37067276, 0.39097485,
         0.45659465],
        [0.2940399 , 0.3306437 , 0.32395566, ..., 0.19252078, 0.21714431,
         0.24263924],
        [0.4138433 , 0.47978985, 0.5166639 , ..., 0.5562554 , 0.4086836 ,
         0.42044348]], dtype=float32),
 'ridge': array([[0.4290133 , 0.37331253, 0.4017402 , ..., 0.5793132 , 0.53899723,
         0.4968022 ],
        [0.3253019 , 0.2301054 , 0.17614344, ..., 0.33642793, 0.3497425 ,
         0.3560007 ],
        [0.5519007 , 0.46847916, 0.5257808 , ..., 0.6301012 , 0.69831306,
         0.5881569 ],
        ...,
        [0.6989316 , 0.6826698 , 0.7077453 , ..., 0.29566136, 0.32281214,
         0.3521443 ],
        [0.31752783, 0.33159164, 0.33879474, ..., 0.24723864, 0.23903543,
         0.23862499],
        [0.39791593, 0.4184358 , 0.52279156, ..., 0.58981174, 0.50477254,
         0.5145724 ]], dtype=float32),
 'lasso': array([[0.5130819 , 0.5360938 , 0.56652683, ..., 0.31880376, 0.31096098,
         0.307535  ],
        [0.5130819 , 0.5360938 , 0.56652683, ..., 0.31880376, 0.31096098,
         0.307535  ],
        [0.5130819 , 0.5360938 , 0.56652683, ..., 0.31880376, 0.31096098,
         0.307535  ],
        ...,
        [0.5130819 , 0.5360938 , 0.56652683, ..., 0.31880376, 0.31096098,
         0.307535  ],
        [0.5130819 , 0.5360938 , 0.56652683, ..., 0.31880376, 0.31096098,
         0.307535  ],
        [0.5130819 , 0.5360938 , 0.56652683, ..., 0.31880376, 0.31096098,
         0.307535  ]], dtype=float32),
 'extra': array([[0.42644627, 0.39462809, 0.40661157, ..., 0.5409091 , 0.53388429,
         0.53966941],
        [0.30619835, 0.33347108, 0.35661157, ..., 0.43057852, 0.42066116,
         0.40909091],
        [0.43842976, 0.47768595, 0.58347108, ..., 0.45867768, 0.40041323,
         0.39380165],
        ...,
        [0.64049588, 0.65702479, 0.6731405 , ..., 0.36157025, 0.37272727,
         0.38429752],
        [0.3161157 , 0.3144628 , 0.37066115, ..., 0.41239669, 0.40206612,
         0.37685951],
        [0.43471075, 0.47272727, 0.51818182, ..., 0.54090908, 0.503719  ,
         0.50041322]])}
faces_pre["knn"]
array([[0.4471074 , 0.41652894, 0.42066115, ..., 0.54793394, 0.5355372 ,
        0.546281  ],
       [0.34876034, 0.34214878, 0.346281  , ..., 0.42727274, 0.42809922,
        0.43057853],
       [0.5355372 , 0.546281  , 0.58016527, ..., 0.56611574, 0.56280994,
        0.5644628 ],
       ...,
       [0.64793384, 0.67685956, 0.7049587 , ..., 0.41487604, 0.3586777 ,
        0.36776862],
       [0.3942149 , 0.41322312, 0.43553716, ..., 0.45785123, 0.43471074,
        0.39173552],
       [0.47520667, 0.47024792, 0.51404965, ..., 0.631405  , 0.6256199 ,
        0.59173554]], dtype=float32)


不同模型预测的人脸结果与实际的对比


import numpy as np
plt.figure(figsize=(6*3,8*3))
for i in range(8):
    axes = plt.subplot(8,6,i*6+1)
    axes.axis("off")
    face_up = x_test[i]
    face_down = y_test[i]
    face = np.concatenate([face_up,face_down])
    axes.imshow(face.reshape((64,64)),cmap="gray")
    if i==0:
        axes.set_title("True")
    # 把机器学习预测出来的下半边脸和上半边脸拼接
    for j,key in enumerate(faces_pre):
        axes = plt.subplot(8,6,i*6+2+j)
        axes.axis("off")
        if i==0:
            axes.set_title(key)
        face_up = x_test[i]
        y_pre = faces_pre[key]
        face_down_pre = y_pre[i]
        face =np.concatenate([face_up,face_down_pre])
        axes.imshow(face.reshape((64,64)),cmap="gray")

2abf7f312ef94f1283fceed0d172bc4b.png

通过对比发现,上述案例中通过KNN预测的结果的脸型要好一些只是有明显的分界线,需要进一步处理,线性回归与岭回归预测的结果没有明显分界线,但是实际预测效果没有那么好;lasso回归,极端随机森林预测出的人脸结果不理想。


相关文章
|
19天前
|
机器学习/深度学习 人工智能 算法
探索机器学习中的线性回归模型
本文深入探讨了机器学习中广泛使用的线性回归模型,从其基本概念和数学原理出发,逐步引导读者理解模型的构建、训练及评估过程。通过实例分析与代码演示,本文旨在为初学者提供一个清晰的学习路径,帮助他们在实践中更好地应用线性回归模型解决实际问题。
|
28天前
|
机器学习/深度学习 数据采集 监控
如何使用机器学习模型来自动化评估数据质量?
如何使用机器学习模型来自动化评估数据质量?
|
24天前
|
机器学习/深度学习 人工智能 算法
【手写数字识别】Python+深度学习+机器学习+人工智能+TensorFlow+算法模型
手写数字识别系统,使用Python作为主要开发语言,基于深度学习TensorFlow框架,搭建卷积神经网络算法。并通过对数据集进行训练,最后得到一个识别精度较高的模型。并基于Flask框架,开发网页端操作平台,实现用户上传一张图片识别其名称。
68 0
【手写数字识别】Python+深度学习+机器学习+人工智能+TensorFlow+算法模型
|
28天前
|
机器学习/深度学习 算法 PyTorch
用Python实现简单机器学习模型:以鸢尾花数据集为例
用Python实现简单机器学习模型:以鸢尾花数据集为例
74 1
|
1月前
|
机器学习/深度学习 TensorFlow API
机器学习实战:TensorFlow在图像识别中的应用探索
【10月更文挑战第28天】随着深度学习技术的发展,图像识别取得了显著进步。TensorFlow作为Google开源的机器学习框架,凭借其强大的功能和灵活的API,在图像识别任务中广泛应用。本文通过实战案例,探讨TensorFlow在图像识别中的优势与挑战,展示如何使用TensorFlow构建和训练卷积神经网络(CNN),并评估模型的性能。尽管面临学习曲线和资源消耗等挑战,TensorFlow仍展现出广阔的应用前景。
57 5
|
1月前
|
机器学习/深度学习 数据采集 Python
从零到一:手把手教你完成机器学习项目,从数据预处理到模型部署全攻略
【10月更文挑战第25天】本文通过一个预测房价的案例,详细介绍了从数据预处理到模型部署的完整机器学习项目流程。涵盖数据清洗、特征选择与工程、模型训练与调优、以及使用Flask进行模型部署的步骤,帮助读者掌握机器学习的最佳实践。
85 1
|
22天前
|
机器学习/深度学习 自然语言处理 语音技术
探索机器学习中的深度学习模型:原理与应用
探索机器学习中的深度学习模型:原理与应用
33 0
|
1月前
|
机器学习/深度学习 算法
探索机器学习模型的可解释性
【10月更文挑战第29天】在机器学习领域,一个关键议题是模型的可解释性。本文将通过简单易懂的语言和实例,探讨如何理解和评估机器学习模型的决策过程。我们将从基础概念入手,逐步深入到更复杂的技术手段,旨在为非专业人士提供一扇洞悉机器学习黑箱的窗口。
|
7月前
|
机器学习/深度学习 存储 搜索推荐
利用机器学习算法改善电商推荐系统的效率
电商行业日益竞争激烈,提升用户体验成为关键。本文将探讨如何利用机器学习算法优化电商推荐系统,通过分析用户行为数据和商品信息,实现个性化推荐,从而提高推荐效率和准确性。
248 14
|
7月前
|
机器学习/深度学习 算法 搜索推荐
Machine Learning机器学习之决策树算法 Decision Tree(附Python代码)
Machine Learning机器学习之决策树算法 Decision Tree(附Python代码)

热门文章

最新文章

相关产品

  • 人工智能平台 PAI