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

本文涉及的产品
模型在线服务 PAI-EAS,A10/V100等 500元 1个月
模型训练 PAI-DLC,100CU*H 3个月
交互式建模 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回归,极端随机森林预测出的人脸结果不理想。


相关文章
|
1月前
|
人工智能 JSON 算法
Qwen2.5-Coder 系列模型在 PAI-QuickStart 的训练、评测、压缩及部署实践
阿里云的人工智能平台 PAI,作为一站式、 AI Native 的大模型与 AIGC 工程平台,为开发者和企业客户提供了 Qwen2.5-Coder 系列模型的全链路最佳实践。本文以Qwen2.5-Coder-32B为例,详细介绍在 PAI-QuickStart 完成 Qwen2.5-Coder 的训练、评测和快速部署。
Qwen2.5-Coder 系列模型在 PAI-QuickStart 的训练、评测、压缩及部署实践
|
14天前
|
编解码 机器人 测试技术
技术实践 | 使用 PAI+LLaMA Factory 微调 Qwen2-VL 模型快速搭建专业领域知识问答机器人
Qwen2-VL是一款具备高级图像和视频理解能力的多模态模型,支持多种语言,适用于多模态应用开发。通过PAI和LLaMA Factory框架,用户可以轻松微调Qwen2-VL模型,快速构建文旅领域的知识问答机器人。本教程详细介绍了从模型部署、微调到对话测试的全过程,帮助开发者高效实现定制化多模态应用。
|
23天前
|
机器学习/深度学习 人工智能 算法
人工智能浪潮下的编程实践:构建你的第一个机器学习模型
在人工智能的巨浪中,每个人都有机会成为弄潮儿。本文将带你一探究竟,从零基础开始,用最易懂的语言和步骤,教你如何构建属于自己的第一个机器学习模型。不需要复杂的数学公式,也不必担心编程难题,只需跟随我们的步伐,一起探索这个充满魔力的AI世界。
42 12
|
1月前
|
机器学习/深度学习 Python
机器学习中评估模型性能的重要工具——混淆矩阵和ROC曲线。混淆矩阵通过真正例、假正例等指标展示模型预测情况
本文介绍了机器学习中评估模型性能的重要工具——混淆矩阵和ROC曲线。混淆矩阵通过真正例、假正例等指标展示模型预测情况,而ROC曲线则通过假正率和真正率评估二分类模型性能。文章还提供了Python中的具体实现示例,展示了如何计算和使用这两种工具来评估模型。
53 8
|
1月前
|
机器学习/深度学习 Python
机器学习中模型选择和优化的关键技术——交叉验证与网格搜索
本文深入探讨了机器学习中模型选择和优化的关键技术——交叉验证与网格搜索。介绍了K折交叉验证、留一交叉验证等方法,以及网格搜索的原理和步骤,展示了如何结合两者在Python中实现模型参数的优化,并强调了使用时需注意的计算成本、过拟合风险等问题。
53 6
|
1月前
|
机器学习/深度学习 数据采集 算法
从零到一:构建高效机器学习模型的旅程####
在探索技术深度与广度的征途中,我深刻体会到技术创新既在于理论的飞跃,更在于实践的积累。本文将通过一个具体案例,分享我在构建高效机器学习模型过程中的实战经验,包括数据预处理、特征工程、模型选择与优化等关键环节,旨在为读者提供一个从零开始构建并优化机器学习模型的实用指南。 ####
|
1月前
|
机器学习/深度学习 人工智能 算法
探索机器学习中的线性回归模型
本文深入探讨了机器学习中广泛使用的线性回归模型,从其基本概念和数学原理出发,逐步引导读者理解模型的构建、训练及评估过程。通过实例分析与代码演示,本文旨在为初学者提供一个清晰的学习路径,帮助他们在实践中更好地应用线性回归模型解决实际问题。
|
1月前
|
机器学习/深度学习 人工智能 TensorFlow
基于TensorFlow的深度学习模型训练与优化实战
基于TensorFlow的深度学习模型训练与优化实战
93 0
|
7月前
|
机器学习/深度学习 存储 搜索推荐
利用机器学习算法改善电商推荐系统的效率
电商行业日益竞争激烈,提升用户体验成为关键。本文将探讨如何利用机器学习算法优化电商推荐系统,通过分析用户行为数据和商品信息,实现个性化推荐,从而提高推荐效率和准确性。
255 14
|
7月前
|
机器学习/深度学习 算法 搜索推荐
Machine Learning机器学习之决策树算法 Decision Tree(附Python代码)
Machine Learning机器学习之决策树算法 Decision Tree(附Python代码)

热门文章

最新文章

相关产品

  • 人工智能平台 PAI