开发者

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

目录
  • 1 导入需要的类库
  • 2拉取数据集
  • 3 处理图片数据(将人脸图片分为上下两部分)
  • 4 创建模型 
  • 5 训练数据
  • 6展示测试结果
  • 全部代码

1 导入需要的类库

import matplotlib.pyplot as plt
 
from sklearn.linear_model import LinearRegression,Ridge,javascriptLasso
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)

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

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=pltjs.subplot(1,3,3)
axes.imshow(images[index],cmap=plt.cm.gray)

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

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('o编程客栈ff')
        if i==0:
            axes.set_title(key)

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

全部代码

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 impor编程客栈t 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):
    #第一列,上半张人脸
   开发者_JAVA入门 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('offjs')
    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 人脸补全内容请搜索我们以前的文章或继续浏览下面的相关文章希望大家以后多多支持我们!

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新开发

开发排行榜