Sklearn多種算法實(shí)現(xiàn)人臉補(bǔ)全的項(xiàng)目實(shí)踐
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)文章
對Python3中bytes和HexStr之間的轉(zhuǎn)換詳解
今天小編就為大家分享一篇對Python3中bytes和HexStr之間的轉(zhuǎn)換詳解,具有很好的參考價(jià)值,希望對大家有所幫助。一起跟隨小編過來看看吧2018-12-12python使用opencv resize圖像不進(jìn)行插值的操作
這篇文章主要介紹了python使用opencv resize圖像不進(jìn)行插值的操作,具有很好的參考價(jià)值,希望對大家有所幫助。一起跟隨小編過來看看吧2020-07-07python opencv如何實(shí)現(xiàn)圖片繪制
這篇文章主要介紹了python opencv如何實(shí)現(xiàn)圖片繪制,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友可以參考下2020-01-01利用PyQt中的QThread類實(shí)現(xiàn)多線程
本文主要給大家分享的是python實(shí)現(xiàn)多線程及線程間通信的簡單方法,非常的實(shí)用,有需要的小伙伴可以參考下2020-02-02python光學(xué)仿真相速度和群速度計(jì)算理解學(xué)習(xí)
從物理學(xué)的機(jī)制出發(fā),波動模型相對于光線模型,顯然更加接近光的本質(zhì);但是從物理學(xué)的發(fā)展來說,波動光學(xué)旨在解決幾何光學(xué)無法解決的問題,可謂光線模型的一種升級2021-10-10Python編程快速上手——Excel表格創(chuàng)建乘法表案例分析
這篇文章主要介紹了Python Excel表格創(chuàng)建乘法表,結(jié)合具體實(shí)例形式分析了Python接受cmd命令操作Excel文件創(chuàng)建乘法表相關(guān)實(shí)現(xiàn)技巧,需要的朋友可以參考下2020-02-02