亚洲乱码中文字幕综合,中国熟女仑乱hd,亚洲精品乱拍国产一区二区三区,一本大道卡一卡二卡三乱码全集资源,又粗又黄又硬又爽的免费视频

Sklearn多種算法實(shí)現(xiàn)人臉補(bǔ)全的項(xiàng)目實(shí)踐

 更新時間:2023年03月10日 09:04:35   作者:qq_30895747  
本文主要介紹了Sklearn多種算法實(shí)現(xiàn)人臉補(bǔ)全的項(xiàng)目實(shí)踐,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧

1 導(dǎo)入需要的類庫

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拉取數(shù)據(jù)集

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 處理圖片數(shù)據(jù)(將人臉圖片分為上下兩部分)

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 創(chuàng)建模型 

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ùn)練數(shù)據(jù)

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展示測試結(jié)果

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')
    
    #繪制預(yù)測人臉
    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')
    
    #繪制預(yù)測人臉
    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)

到此這篇關(guān)于Sklearn多種算法實(shí)現(xiàn)人臉補(bǔ)全的項(xiàng)目實(shí)踐的文章就介紹到這了,更多相關(guān)Sklearn 人臉補(bǔ)全內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!

相關(guān)文章

最新評論