Sklearn多種算法實現(xiàn)人臉補全的項目實踐
更新時間:2023年03月10日 09:04:35 作者:qq_30895747
本文主要介紹了Sklearn多種算法實現(xiàn)人臉補全的項目實踐,文中通過示例代碼介紹的非常詳細,對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友們下面隨著小編來一起學(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多種算法實現(xiàn)人臉補全的項目實踐的文章就介紹到這了,更多相關(guān)Sklearn 人臉補全內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
Python中RSA加解密與數(shù)字簽名技術(shù)的使用
本文將詳細介紹 RSA 數(shù)字簽名的原理、實現(xiàn)步驟,以及如何通過 Python 的 rsa 庫完成公鑰私鑰生成、數(shù)字簽名和認證,具有很好的參考價值,希望對大家有所幫助,如有錯誤或未考慮完全的地方,望不吝賜教2025-03-03
jupyter notebook運行代碼沒反應(yīng)且in[ ]沒有*
本文主要介紹了jupyter notebook運行代碼沒反應(yīng)且in[ ]沒有*,文中通過示例代碼介紹的非常詳細,具有一定的參考價值,感興趣的小伙伴們可以參考一下2022-03-03
Python無參裝飾器的實現(xiàn)方案及優(yōu)化
裝飾器(Decorators)是 Python 的一個重要部分,所謂裝飾器就是閉包函數(shù)的一種應(yīng)用場景,這篇文章主要給大家介紹了關(guān)于Python無參裝飾器的相關(guān)資料,需要的朋友可以參考下2021-08-08
python爬蟲 基于requests模塊的get請求實現(xiàn)詳解
這篇文章主要介紹了python爬蟲 基于requests模塊的get請求實現(xiàn)詳解,文中通過示例代碼介紹的非常詳細,對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友可以參考下2019-08-08

