Sklearn多种算法实现人脸补全的项目实践

 更新时间:2023年03月10日 09:04:35   作者:qq_30895747  
本文主要介绍了Sklearn多种算法实现人脸补全的项目实践,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面随着小编来一起学习学习吧

1 导入需要的类库

import matplotlib.pyplot as plt
 
from sklearn.linear_model import LinearRegression,Ridge,Lasso
from sklearn.tree import DecisionTreeRegressor
from sklearn.neighbors import KNeighborsRegressor
from sklearn import datasets
from sklearn.model_selection import train_test_split
from sklearn.ensemble import RandomForestRegressor
import numpy as np

2拉取数据集

faces=datasets.fetch_olivetti_faces()
images=faces.images
display(images.shape)
 
index=np.random.randint(0,400,size=1)[0]
 
img=images[index]
plt.figure(figsize=(3,3))
plt.imshow(img,cmap=plt.cm.gray)

3 处理图片数据(将人脸图片分为上下两部分)

index=np.random.randint(0,400,size=1)[0]
up_face=images[:,:32,:]
down_face=images[:,32:,:]
 
axes=plt.subplot(1,3,1)
axes.imshow(up_face[index],cmap=plt.cm.gray)
axes=plt.subplot(1,3,2)
axes.imshow(down_face[index],cmap=plt.cm.gray)
axes=plt.subplot(1,3,3)
axes.imshow(images[index],cmap=plt.cm.gray)

4 创建模型 

X=faces.data
 
x=X[:,:2048]
y=X[:,2048:]
 
estimators={}
 
estimators['linear']=LinearRegression()
estimators['ridge']=Ridge(alpha=0.1)
estimators['lasso']=Lasso(alpha=1)
estimators['knn']=KNeighborsRegressor(n_neighbors=5)
estimators['tree']=DecisionTreeRegressor()
estimators['forest']=RandomForestRegressor()

5 训练数据

x_train,x_test,y_train,y_test=train_test_split(x,y,test_size=0.2)
result={}
print
for key,model in estimators.items():
    print(key)
    model.fit(x_train,y_train)
    y_=model.predict(x_test)
    result[key]=y_

6展示测试结果

plt.figure(figsize=(40,40))
for i in range(0,10):
    #第一列,上半张人脸
    axes=plt.subplot(10,8,8*i+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('up-face')
    
    #第8列,整张人脸
    
    axes=plt.subplot(10,8,8*i+8)
    down_face=y_test[i].reshape(32,64)
    full_face=np.concatenate([up_face,down_face])
    axes.imshow(full_face,cmap=plt.cm.gray)
    axes.axis('off')
    
    if i==0:
        axes.set_title('full-face')
    
    #绘制预测人脸
    for j,key in enumerate(result):
        axes=plt.subplot(10,8,i*8+2+j)
        y_=result[key]
        predice_face=y_[i].reshape(32,64)
        pre_face=np.concatenate([up_face,predice_face])
        axes.imshow(pre_face,cmap=plt.cm.gray)
        axes.axis('off')
        if i==0:
            axes.set_title(key)

全部代码

import matplotlib.pyplot as plt
 
from sklearn.linear_model import LinearRegression,Ridge,Lasso
from sklearn.tree import DecisionTreeRegressor
from sklearn.neighbors import KNeighborsRegressor
from sklearn import datasets
from sklearn.model_selection import train_test_split
from sklearn.ensemble import RandomForestRegressor
import numpy as np
 
faces=datasets.fetch_olivetti_faces()
images=faces.images
display(images.shape)
 
index=np.random.randint(0,400,size=1)[0]
 
img=images[index]
plt.figure(figsize=(3,3))
plt.imshow(img,cmap=plt.cm.gray)
 
index=np.random.randint(0,400,size=1)[0]
up_face=images[:,:32,:]
down_face=images[:,32:,:]
 
axes=plt.subplot(1,3,1)
axes.imshow(up_face[index],cmap=plt.cm.gray)
axes=plt.subplot(1,3,2)
axes.imshow(down_face[index],cmap=plt.cm.gray)
axes=plt.subplot(1,3,3)
axes.imshow(images[index],cmap=plt.cm.gray)
 
X=faces.data
 
x=X[:,:2048]
y=X[:,2048:]
 
estimators={}
 
estimators['linear']=LinearRegression()
estimators['ridge']=Ridge(alpha=0.1)
estimators['lasso']=Lasso(alpha=1)
estimators['knn']=KNeighborsRegressor(n_neighbors=5)
estimators['tree']=DecisionTreeRegressor()
estimators['forest']=RandomForestRegressor()
x_train,x_test,y_train,y_test=train_test_split(x,y,test_size=0.2)
result={}
print
for key,model in estimators.items():
    print(key)
    model.fit(x_train,y_train)
    y_=model.predict(x_test)
    result[key]=y_
 
plt.figure(figsize=(40,40))
for i in range(0,10):
    #第一列,上半张人脸
    axes=plt.subplot(10,8,8*i+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('up-face')
    
    #第8列,整张人脸
    
    axes=plt.subplot(10,8,8*i+8)
    down_face=y_test[i].reshape(32,64)
    full_face=np.concatenate([up_face,down_face])
    axes.imshow(full_face,cmap=plt.cm.gray)
    axes.axis('off')
    
    if i==0:
        axes.set_title('full-face')
    
    #绘制预测人脸
    for j,key in enumerate(result):
        axes=plt.subplot(10,8,i*8+2+j)
        y_=result[key]
        predice_face=y_[i].reshape(32,64)
        pre_face=np.concatenate([up_face,predice_face])
        axes.imshow(pre_face,cmap=plt.cm.gray)
        axes.axis('off')
        if i==0:
            axes.set_title(key)

到此这篇关于Sklearn多种算法实现人脸补全的项目实践的文章就介绍到这了,更多相关Sklearn 人脸补全内容请搜索脚本之家以前的文章或继续浏览下面的相关文章希望大家以后多多支持脚本之家!

相关文章

  • pytorch 梯度NAN异常值的解决方案

    pytorch 梯度NAN异常值的解决方案

    这篇文章主要介绍了pytorch 梯度NAN异常值的解决方案,具有很好的参考价值,希望对大家有所帮助。如有错误或未考虑完全的地方,望不吝赐教
    2021-06-06
  • Python中RSA加解密与数字签名技术的使用

    Python中RSA加解密与数字签名技术的使用

    本文将详细介绍 RSA 数字签名的原理、实现步骤,以及如何通过 Python 的 rsa 库完成公钥私钥生成、数字签名和认证,具有很好的参考价值,希望对大家有所帮助,如有错误或未考虑完全的地方,望不吝赐教
    2025-03-03
  • Python 实现淘宝秒杀的示例代码

    Python 实现淘宝秒杀的示例代码

    本篇文章主要介绍了Python 实现淘宝秒杀的示例代码,小编觉得挺不错的,现在分享给大家,也给大家做个参考。一起跟随小编过来看看吧
    2018-01-01
  • django重新生成数据库中的某张表方法

    django重新生成数据库中的某张表方法

    今天小编就为大家分享一篇django重新生成数据库中的某张表方法,具有很好的参考价值,希望对大家有所帮助。一起跟随小编过来看看吧
    2019-08-08
  • jupyter notebook运行代码没反应且in[ ]没有*

    jupyter notebook运行代码没反应且in[ ]没有*

    本文主要介绍了jupyter notebook运行代码没反应且in[ ]没有*,文中通过示例代码介绍的非常详细,具有一定的参考价值,感兴趣的小伙伴们可以参考一下
    2022-03-03
  • python 已知平行四边形三个点,求第四个点的案例

    python 已知平行四边形三个点,求第四个点的案例

    这篇文章主要介绍了python 已知平行四边形三个点,求第四个点的案例,具有很好的参考价值,希望对大家有所帮助。一起跟随小编过来看看吧
    2020-04-04
  • Python无参装饰器的实现方案及优化

    Python无参装饰器的实现方案及优化

    装饰器(Decorators)是 Python 的一个重要部分,所谓装饰器就是闭包函数的一种应用场景,这篇文章主要给大家介绍了关于Python无参装饰器的相关资料,需要的朋友可以参考下
    2021-08-08
  • python爬虫 基于requests模块的get请求实现详解

    python爬虫 基于requests模块的get请求实现详解

    这篇文章主要介绍了python爬虫 基于requests模块的get请求实现详解,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友可以参考下
    2019-08-08
  • 基于Python Pygame实现的画饼图游戏

    基于Python Pygame实现的画饼图游戏

    这篇文章主要介绍了基于Pygame实现一个画饼图游戏,可以根据键盘上输入不同的数字,将圆分割成不同的几个部分,每部分用不同的颜色来实现。需要的朋友可以参考一下
    2021-12-12
  • Python程序慢的重要原因

    Python程序慢的重要原因

    在本篇内容里小编给大家整理的是一篇关于Python程序慢的重要原因分析内容,有兴趣的朋友们可以参考下。
    2020-09-09

最新评论